diff --git a/trie/secure_trie.go b/trie/secure_trie.go index ac20f961e5..0424ecb6e5 100644 --- a/trie/secure_trie.go +++ b/trie/secure_trie.go @@ -64,11 +64,10 @@ func NewSecure(stateRoot common.Hash, owner common.Hash, root common.Hash, db da // // StateTrie is not safe for concurrent use. type StateTrie struct { - trie Trie - db database.NodeDatabase - preimages preimageStore - secKeyCache map[common.Hash][]byte - secKeyCacheOwner *StateTrie // Pointer to self, replace the key cache on mismatch + trie Trie + db database.NodeDatabase + preimages preimageStore + secKeyCache map[common.Hash][]byte } // NewStateTrie creates a trie with an existing root node from a backing database. @@ -84,7 +83,11 @@ func NewStateTrie(id *ID, db database.NodeDatabase) (*StateTrie, error) { if err != nil { return nil, err } - tr := &StateTrie{trie: *trie, db: db} + tr := &StateTrie{ + trie: *trie, + db: db, + secKeyCache: make(map[common.Hash][]byte), + } // link the preimage store if it's supported if preimages, ok := db.(preimageStore); ok && preimages.PreimageEnabled() { @@ -162,7 +165,7 @@ func (t *StateTrie) MustUpdate(key, value []byte) { hk := crypto.Keccak256(key) t.trie.MustUpdate(hk, value) if t.preimages != nil { - t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key) + t.secKeyCache[common.Hash(hk)] = common.CopyBytes(key) } } @@ -182,7 +185,7 @@ func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error { return err } if t.preimages != nil { - t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key) + t.secKeyCache[common.Hash(hk)] = common.CopyBytes(key) } return nil } @@ -198,7 +201,7 @@ func (t *StateTrie) UpdateAccount(address common.Address, acc *types.StateAccoun return err } if t.preimages != nil { - t.getSecKeyCache()[common.Hash(hk)] = address.Bytes() + t.secKeyCache[common.Hash(hk)] = address.Bytes() } return nil } @@ -212,7 +215,7 @@ func (t *StateTrie) UpdateContractCode(_ common.Address, _ common.Hash, _ []byte func (t *StateTrie) MustDelete(key []byte) { hk := crypto.Keccak256(key) if t.preimages != nil { - delete(t.getSecKeyCache(), common.Hash(hk)) + delete(t.secKeyCache, common.Hash(hk)) } t.trie.MustDelete(hk) } @@ -223,7 +226,7 @@ func (t *StateTrie) MustDelete(key []byte) { func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error { hk := crypto.Keccak256(key) if t.preimages != nil { - delete(t.getSecKeyCache(), common.Hash(hk)) + delete(t.secKeyCache, common.Hash(hk)) } return t.trie.Delete(hk) } @@ -232,7 +235,7 @@ func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error { func (t *StateTrie) DeleteAccount(address common.Address) error { hk := crypto.Keccak256(address.Bytes()) if t.preimages != nil { - delete(t.getSecKeyCache(), common.Hash(hk)) + delete(t.secKeyCache, common.Hash(hk)) } return t.trie.Delete(hk) } @@ -243,7 +246,7 @@ func (t *StateTrie) GetKey(shaKey []byte) []byte { if t.preimages == nil { return nil } - if key, ok := t.getSecKeyCache()[common.BytesToHash(shaKey)]; ok { + if key, ok := t.secKeyCache[common.BytesToHash(shaKey)]; ok { return key } return t.preimages.Preimage(common.BytesToHash(shaKey)) @@ -263,11 +266,11 @@ func (t *StateTrie) Witness() map[string]struct{} { // be created with new root and updated trie database for following usage func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) { // Write all the pre-images to the actual disk database - if len(t.getSecKeyCache()) > 0 { + if len(t.secKeyCache) > 0 { if t.preimages != nil { t.preimages.InsertPreimage(t.secKeyCache) } - t.secKeyCache = make(map[common.Hash][]byte) + clear(t.secKeyCache) } // Commit the trie and return its modified nodeset. return t.trie.Commit(collectLeaf) @@ -284,7 +287,7 @@ func (t *StateTrie) Copy() *StateTrie { return &StateTrie{ trie: *t.trie.Copy(), db: t.db, - secKeyCache: t.secKeyCache, + secKeyCache: make(map[common.Hash][]byte), preimages: t.preimages, } } @@ -301,17 +304,6 @@ func (t *StateTrie) MustNodeIterator(start []byte) NodeIterator { return t.trie.MustNodeIterator(start) } -// getSecKeyCache returns the current secure key cache, creating a new one if -// ownership changed (i.e. the current secure trie is a copy of another owning -// the actual cache). -func (t *StateTrie) getSecKeyCache() map[common.Hash][]byte { - if t != t.secKeyCacheOwner { - t.secKeyCacheOwner = t - t.secKeyCache = make(map[common.Hash][]byte) - } - return t.secKeyCache -} - func (t *StateTrie) IsVerkle() bool { return false }