@@ -19,6 +19,7 @@ package trie
1919import (
2020 "github.com/ethereum/go-ethereum/common"
2121 "github.com/ethereum/go-ethereum/core/types"
22+ "github.com/ethereum/go-ethereum/crypto"
2223 "github.com/ethereum/go-ethereum/rlp"
2324 "github.com/ethereum/go-ethereum/trie/trienode"
2425 "github.com/ethereum/go-ethereum/triedb/database"
@@ -63,8 +64,7 @@ type StateTrie struct {
6364 trie Trie
6465 db database.NodeDatabase
6566 preimages preimageStore
66- hashKeyBuf [common .HashLength ]byte
67- secKeyCache map [string ][]byte
67+ secKeyCache map [common.Hash ][]byte
6868 secKeyCacheOwner * StateTrie // Pointer to self, replace the key cache on mismatch
6969}
7070
@@ -97,15 +97,15 @@ func NewStateTrie(id *ID, db database.NodeDatabase) (*StateTrie, error) {
9797// This function will omit any encountered error but just
9898// print out an error message.
9999func (t * StateTrie ) MustGet (key []byte ) []byte {
100- return t .trie .MustGet (t . hashKey (key ))
100+ return t .trie .MustGet (crypto . Keccak256 (key ))
101101}
102102
103103// GetStorage attempts to retrieve a storage slot with provided account address
104104// and slot key. The value bytes must not be modified by the caller.
105105// If the specified storage slot is not in the trie, nil will be returned.
106106// If a trie node is not found in the database, a MissingNodeError is returned.
107107func (t * StateTrie ) GetStorage (_ common.Address , key []byte ) ([]byte , error ) {
108- enc , err := t .trie .Get (t . hashKey (key ))
108+ enc , err := t .trie .Get (crypto . Keccak256 (key ))
109109 if err != nil || len (enc ) == 0 {
110110 return nil , err
111111 }
@@ -117,7 +117,7 @@ func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
117117// If the specified account is not in the trie, nil will be returned.
118118// If a trie node is not found in the database, a MissingNodeError is returned.
119119func (t * StateTrie ) GetAccount (address common.Address ) (* types.StateAccount , error ) {
120- res , err := t .trie .Get (t . hashKey (address .Bytes ()))
120+ res , err := t .trie .Get (crypto . Keccak256 (address .Bytes ()))
121121 if res == nil || err != nil {
122122 return nil , err
123123 }
@@ -157,9 +157,9 @@ func (t *StateTrie) GetNode(path []byte) ([]byte, int, error) {
157157// This function will omit any encountered error but just print out an
158158// error message.
159159func (t * StateTrie ) MustUpdate (key , value []byte ) {
160- hk := t . hashKey (key )
160+ hk := crypto . Keccak256 (key )
161161 t .trie .MustUpdate (hk , value )
162- t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
162+ t .getSecKeyCache ()[common . Hash (hk )] = common .CopyBytes (key )
163163}
164164
165165// UpdateStorage associates key with value in the trie. Subsequent calls to
@@ -171,27 +171,27 @@ func (t *StateTrie) MustUpdate(key, value []byte) {
171171//
172172// If a node is not found in the database, a MissingNodeError is returned.
173173func (t * StateTrie ) UpdateStorage (_ common.Address , key , value []byte ) error {
174- hk := t . hashKey (key )
174+ hk := crypto . Keccak256 (key )
175175 v , _ := rlp .EncodeToBytes (value )
176176 err := t .trie .Update (hk , v )
177177 if err != nil {
178178 return err
179179 }
180- t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
180+ t .getSecKeyCache ()[common . Hash (hk )] = common .CopyBytes (key )
181181 return nil
182182}
183183
184184// UpdateAccount will abstract the write of an account to the secure trie.
185185func (t * StateTrie ) UpdateAccount (address common.Address , acc * types.StateAccount , _ int ) error {
186- hk := t . hashKey (address .Bytes ())
186+ hk := crypto . Keccak256 (address .Bytes ())
187187 data , err := rlp .EncodeToBytes (acc )
188188 if err != nil {
189189 return err
190190 }
191191 if err := t .trie .Update (hk , data ); err != nil {
192192 return err
193193 }
194- t .getSecKeyCache ()[string (hk )] = address .Bytes ()
194+ t .getSecKeyCache ()[common . Hash (hk )] = address .Bytes ()
195195 return nil
196196}
197197
@@ -202,31 +202,31 @@ func (t *StateTrie) UpdateContractCode(_ common.Address, _ common.Hash, _ []byte
202202// MustDelete removes any existing value for key from the trie. This function
203203// will omit any encountered error but just print out an error message.
204204func (t * StateTrie ) MustDelete (key []byte ) {
205- hk := t . hashKey (key )
206- delete (t .getSecKeyCache (), string (hk ))
205+ hk := crypto . Keccak256 (key )
206+ delete (t .getSecKeyCache (), common . Hash (hk ))
207207 t .trie .MustDelete (hk )
208208}
209209
210210// DeleteStorage removes any existing storage slot from the trie.
211211// If the specified trie node is not in the trie, nothing will be changed.
212212// If a node is not found in the database, a MissingNodeError is returned.
213213func (t * StateTrie ) DeleteStorage (_ common.Address , key []byte ) error {
214- hk := t . hashKey (key )
215- delete (t .getSecKeyCache (), string (hk ))
214+ hk := crypto . Keccak256 (key )
215+ delete (t .getSecKeyCache (), common . Hash (hk ))
216216 return t .trie .Delete (hk )
217217}
218218
219219// DeleteAccount abstracts an account deletion from the trie.
220220func (t * StateTrie ) DeleteAccount (address common.Address ) error {
221- hk := t . hashKey (address .Bytes ())
222- delete (t .getSecKeyCache (), string (hk ))
221+ hk := crypto . Keccak256 (address .Bytes ())
222+ delete (t .getSecKeyCache (), common . Hash (hk ))
223223 return t .trie .Delete (hk )
224224}
225225
226226// GetKey returns the sha3 preimage of a hashed key that was
227227// previously used to store a value.
228228func (t * StateTrie ) GetKey (shaKey []byte ) []byte {
229- if key , ok := t .getSecKeyCache ()[string (shaKey )]; ok {
229+ if key , ok := t .getSecKeyCache ()[common . BytesToHash (shaKey )]; ok {
230230 return key
231231 }
232232 if t .preimages == nil {
@@ -251,13 +251,9 @@ func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
251251 // Write all the pre-images to the actual disk database
252252 if len (t .getSecKeyCache ()) > 0 {
253253 if t .preimages != nil {
254- preimages := make (map [common.Hash ][]byte , len (t .secKeyCache ))
255- for hk , key := range t .secKeyCache {
256- preimages [common .BytesToHash ([]byte (hk ))] = key
257- }
258- t .preimages .InsertPreimage (preimages )
254+ t .preimages .InsertPreimage (t .secKeyCache )
259255 }
260- t .secKeyCache = make (map [string ][]byte )
256+ t .secKeyCache = make (map [common. Hash ][]byte )
261257 }
262258 // Commit the trie and return its modified nodeset.
263259 return t .trie .Commit (collectLeaf )
@@ -291,25 +287,13 @@ func (t *StateTrie) MustNodeIterator(start []byte) NodeIterator {
291287 return t .trie .MustNodeIterator (start )
292288}
293289
294- // hashKey returns the hash of key as an ephemeral buffer.
295- // The caller must not hold onto the return value because it will become
296- // invalid on the next call to hashKey or secKey.
297- func (t * StateTrie ) hashKey (key []byte ) []byte {
298- h := newHasher (false )
299- h .sha .Reset ()
300- h .sha .Write (key )
301- h .sha .Read (t .hashKeyBuf [:])
302- returnHasherToPool (h )
303- return t .hashKeyBuf [:]
304- }
305-
306290// getSecKeyCache returns the current secure key cache, creating a new one if
307291// ownership changed (i.e. the current secure trie is a copy of another owning
308292// the actual cache).
309- func (t * StateTrie ) getSecKeyCache () map [string ][]byte {
293+ func (t * StateTrie ) getSecKeyCache () map [common. Hash ][]byte {
310294 if t != t .secKeyCacheOwner {
311295 t .secKeyCacheOwner = t
312- t .secKeyCache = make (map [string ][]byte )
296+ t .secKeyCache = make (map [common. Hash ][]byte )
313297 }
314298 return t .secKeyCache
315299}
0 commit comments