mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-04 23:32:55 +00:00
trie: no need to store preimage if not enabled (#32012)
As the preimage will only be stored if `t.preimages != nil`, so no need to save them into local cache if not enabled. This will reduce the memory wasted to copy the bytes --------- Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
parent
82c2c8191f
commit
999f09f8af
3 changed files with 34 additions and 11 deletions
|
|
@ -86,6 +86,10 @@ func (db *testDb) InsertPreimage(preimages map[common.Hash][]byte) {
|
||||||
rawdb.WritePreimages(db.disk, preimages)
|
rawdb.WritePreimages(db.disk, preimages)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (db *testDb) PreimageEnabled() bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
func (db *testDb) Scheme() string { return db.scheme }
|
func (db *testDb) Scheme() string { return db.scheme }
|
||||||
|
|
||||||
func (db *testDb) Update(root common.Hash, parent common.Hash, nodes *trienode.MergedNodeSet) error {
|
func (db *testDb) Update(root common.Hash, parent common.Hash, nodes *trienode.MergedNodeSet) error {
|
||||||
|
|
|
||||||
|
|
@ -33,6 +33,9 @@ type preimageStore interface {
|
||||||
|
|
||||||
// InsertPreimage commits a set of preimages along with their hashes.
|
// InsertPreimage commits a set of preimages along with their hashes.
|
||||||
InsertPreimage(preimages map[common.Hash][]byte)
|
InsertPreimage(preimages map[common.Hash][]byte)
|
||||||
|
|
||||||
|
// PreimageEnabled returns true if the preimage store is enabled.
|
||||||
|
PreimageEnabled() bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// SecureTrie is the old name of StateTrie.
|
// SecureTrie is the old name of StateTrie.
|
||||||
|
|
@ -84,8 +87,7 @@ func NewStateTrie(id *ID, db database.NodeDatabase) (*StateTrie, error) {
|
||||||
tr := &StateTrie{trie: *trie, db: db}
|
tr := &StateTrie{trie: *trie, db: db}
|
||||||
|
|
||||||
// link the preimage store if it's supported
|
// link the preimage store if it's supported
|
||||||
preimages, ok := db.(preimageStore)
|
if preimages, ok := db.(preimageStore); ok && preimages.PreimageEnabled() {
|
||||||
if ok {
|
|
||||||
tr.preimages = preimages
|
tr.preimages = preimages
|
||||||
}
|
}
|
||||||
return tr, nil
|
return tr, nil
|
||||||
|
|
@ -159,7 +161,9 @@ func (t *StateTrie) GetNode(path []byte) ([]byte, int, error) {
|
||||||
func (t *StateTrie) MustUpdate(key, value []byte) {
|
func (t *StateTrie) MustUpdate(key, value []byte) {
|
||||||
hk := crypto.Keccak256(key)
|
hk := crypto.Keccak256(key)
|
||||||
t.trie.MustUpdate(hk, value)
|
t.trie.MustUpdate(hk, value)
|
||||||
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
|
if t.preimages != nil {
|
||||||
|
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// UpdateStorage associates key with value in the trie. Subsequent calls to
|
// UpdateStorage associates key with value in the trie. Subsequent calls to
|
||||||
|
|
@ -177,7 +181,9 @@ func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
|
if t.preimages != nil {
|
||||||
|
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -191,7 +197,9 @@ func (t *StateTrie) UpdateAccount(address common.Address, acc *types.StateAccoun
|
||||||
if err := t.trie.Update(hk, data); err != nil {
|
if err := t.trie.Update(hk, data); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
t.getSecKeyCache()[common.Hash(hk)] = address.Bytes()
|
if t.preimages != nil {
|
||||||
|
t.getSecKeyCache()[common.Hash(hk)] = address.Bytes()
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -203,7 +211,9 @@ func (t *StateTrie) UpdateContractCode(_ common.Address, _ common.Hash, _ []byte
|
||||||
// will omit any encountered error but just print out an error message.
|
// will omit any encountered error but just print out an error message.
|
||||||
func (t *StateTrie) MustDelete(key []byte) {
|
func (t *StateTrie) MustDelete(key []byte) {
|
||||||
hk := crypto.Keccak256(key)
|
hk := crypto.Keccak256(key)
|
||||||
delete(t.getSecKeyCache(), common.Hash(hk))
|
if t.preimages != nil {
|
||||||
|
delete(t.getSecKeyCache(), common.Hash(hk))
|
||||||
|
}
|
||||||
t.trie.MustDelete(hk)
|
t.trie.MustDelete(hk)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -212,26 +222,30 @@ func (t *StateTrie) MustDelete(key []byte) {
|
||||||
// If a node is not found in the database, a MissingNodeError is returned.
|
// If a node is not found in the database, a MissingNodeError is returned.
|
||||||
func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error {
|
func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error {
|
||||||
hk := crypto.Keccak256(key)
|
hk := crypto.Keccak256(key)
|
||||||
delete(t.getSecKeyCache(), common.Hash(hk))
|
if t.preimages != nil {
|
||||||
|
delete(t.getSecKeyCache(), common.Hash(hk))
|
||||||
|
}
|
||||||
return t.trie.Delete(hk)
|
return t.trie.Delete(hk)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteAccount abstracts an account deletion from the trie.
|
// DeleteAccount abstracts an account deletion from the trie.
|
||||||
func (t *StateTrie) DeleteAccount(address common.Address) error {
|
func (t *StateTrie) DeleteAccount(address common.Address) error {
|
||||||
hk := crypto.Keccak256(address.Bytes())
|
hk := crypto.Keccak256(address.Bytes())
|
||||||
delete(t.getSecKeyCache(), common.Hash(hk))
|
if t.preimages != nil {
|
||||||
|
delete(t.getSecKeyCache(), common.Hash(hk))
|
||||||
|
}
|
||||||
return t.trie.Delete(hk)
|
return t.trie.Delete(hk)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetKey returns the sha3 preimage of a hashed key that was
|
// GetKey returns the sha3 preimage of a hashed key that was
|
||||||
// previously used to store a value.
|
// previously used to store a value.
|
||||||
func (t *StateTrie) GetKey(shaKey []byte) []byte {
|
func (t *StateTrie) GetKey(shaKey []byte) []byte {
|
||||||
if key, ok := t.getSecKeyCache()[common.BytesToHash(shaKey)]; ok {
|
|
||||||
return key
|
|
||||||
}
|
|
||||||
if t.preimages == nil {
|
if t.preimages == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
if key, ok := t.getSecKeyCache()[common.BytesToHash(shaKey)]; ok {
|
||||||
|
return key
|
||||||
|
}
|
||||||
return t.preimages.Preimage(common.BytesToHash(shaKey))
|
return t.preimages.Preimage(common.BytesToHash(shaKey))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -213,6 +213,11 @@ func (db *Database) InsertPreimage(preimages map[common.Hash][]byte) {
|
||||||
db.preimages.insertPreimage(preimages)
|
db.preimages.insertPreimage(preimages)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PreimageEnabled returns the indicator if the pre-image store is enabled.
|
||||||
|
func (db *Database) PreimageEnabled() bool {
|
||||||
|
return db.preimages != nil
|
||||||
|
}
|
||||||
|
|
||||||
// Cap iteratively flushes old but still referenced trie nodes until the total
|
// Cap iteratively flushes old but still referenced trie nodes until the total
|
||||||
// memory usage goes below the given threshold. The held pre-images accumulated
|
// memory usage goes below the given threshold. The held pre-images accumulated
|
||||||
// up to this point will be flushed in case the size exceeds the threshold.
|
// up to this point will be flushed in case the size exceeds the threshold.
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue