@@ -52,10 +52,9 @@ func NewSecure(stateRoot common.Hash, owner common.Hash, root common.Hash, db *D
5252//
5353// StateTrie is not safe for concurrent use.
5454type StateTrie struct {
55- trie Trie
56- preimages * preimageStore
57- secKeyCache map [common.Hash ][]byte
58- secKeyCacheOwner * StateTrie // Pointer to self, replace the key cache on mismatch
55+ trie Trie
56+ preimages * preimageStore
57+ secKeyCache map [common.Hash ][]byte
5958}
6059
6160// NewSecure creates a trie with an existing root node from a backing database
@@ -78,7 +77,10 @@ func NewStateTrie(id *ID, db *Database) (*StateTrie, error) {
7877 return nil , err
7978 }
8079
81- tr := & StateTrie {trie : * trie }
80+ tr := & StateTrie {
81+ trie : * trie ,
82+ secKeyCache : make (map [common.Hash ][]byte ),
83+ }
8284 if db .PreimageEnabled () {
8385 tr .preimages = db .preimages
8486 }
@@ -150,7 +152,7 @@ func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
150152 return err
151153 }
152154 if t .preimages != nil {
153- t .getSecKeyCache () [common .Hash (hk )] = common .CopyBytes (key )
155+ t .secKeyCache [common .Hash (hk )] = common .CopyBytes (key )
154156 }
155157 return nil
156158}
@@ -167,7 +169,7 @@ func (t *StateTrie) TryUpdateAccount(key common.Address, acc *types.StateAccount
167169 return err
168170 }
169171 if t .preimages != nil {
170- t .getSecKeyCache () [common .Hash (hk )] = common .CopyBytes (key .Bytes ())
172+ t .secKeyCache [common .Hash (hk )] = common .CopyBytes (key .Bytes ())
171173 }
172174 return nil
173175}
@@ -203,7 +205,7 @@ func (t *StateTrie) TryUpdate(key, value []byte) error {
203205 return err
204206 }
205207 if t .preimages != nil {
206- t .getSecKeyCache () [common .Hash (hk )] = common .CopyBytes (key )
208+ t .secKeyCache [common .Hash (hk )] = common .CopyBytes (key )
207209 }
208210 return nil
209211}
@@ -220,7 +222,7 @@ func (t *StateTrie) Delete(key []byte) {
220222func (t * StateTrie ) TryDelete (key []byte ) error {
221223 hk := crypto .Keccak256 (key )
222224 if t .preimages != nil {
223- delete (t .getSecKeyCache () , common .Hash (hk ))
225+ delete (t .secKeyCache , common .Hash (hk ))
224226 }
225227 return t .trie .TryDelete (hk )
226228}
@@ -229,7 +231,7 @@ func (t *StateTrie) TryDelete(key []byte) error {
229231func (t * StateTrie ) TryDeleteAccount (key []byte ) error {
230232 hk := crypto .Keccak256 (key )
231233 if t .preimages != nil {
232- delete (t .getSecKeyCache () , common .Hash (hk ))
234+ delete (t .secKeyCache , common .Hash (hk ))
233235 }
234236 return t .trie .TryDelete (hk )
235237}
@@ -240,7 +242,7 @@ func (t *StateTrie) GetKey(shaKey []byte) []byte {
240242 if t .preimages == nil {
241243 return nil
242244 }
243- if key , ok := t .getSecKeyCache () [common .Hash (shaKey )]; ok {
245+ if key , ok := t .secKeyCache [common .Hash (shaKey )]; ok {
244246 return key
245247 }
246248 return t .preimages .preimage (common .BytesToHash (shaKey ))
@@ -253,11 +255,11 @@ func (t *StateTrie) GetKey(shaKey []byte) []byte {
253255// from the database.
254256func (t * StateTrie ) Commit (onleaf LeafCallback ) (root common.Hash , err error ) {
255257 // Write all the pre-images to the actual disk database
256- if len (t .getSecKeyCache () ) > 0 {
258+ if len (t .secKeyCache ) > 0 {
257259 if t .preimages != nil {
258260 t .preimages .insertPreimage (t .secKeyCache )
259261 }
260- t .secKeyCache = make ( map [common. Hash ][] byte )
262+ clear ( t .secKeyCache )
261263 }
262264 // Commit the trie to its intermediate node database
263265 return t .trie .Commit (onleaf )
@@ -274,7 +276,7 @@ func (t *StateTrie) Copy() *StateTrie {
274276 return & StateTrie {
275277 trie : * t .trie .Copy (),
276278 preimages : t .preimages ,
277- secKeyCache : t . secKeyCache ,
279+ secKeyCache : make ( map [common. Hash ][] byte ) ,
278280 }
279281}
280282
@@ -289,14 +291,3 @@ func (t *StateTrie) NodeIterator(start []byte) NodeIterator {
289291func (t * StateTrie ) MustNodeIterator (start []byte ) NodeIterator {
290292 return t .trie .MustNodeIterator (start )
291293}
292-
293- // getSecKeyCache returns the current secure key cache, creating a new one if
294- // ownership changed (i.e. the current secure trie is a copy of another owning
295- // the actual cache).
296- func (t * StateTrie ) getSecKeyCache () map [common.Hash ][]byte {
297- if t != t .secKeyCacheOwner {
298- t .secKeyCacheOwner = t
299- t .secKeyCache = make (map [common.Hash ][]byte )
300- }
301- return t .secKeyCache
302- }
0 commit comments