@@ -19,6 +19,7 @@ package trie
19
19
import (
20
20
"github.com/ethereum/go-ethereum/common"
21
21
"github.com/ethereum/go-ethereum/core/types"
22
+ "github.com/ethereum/go-ethereum/crypto"
22
23
"github.com/ethereum/go-ethereum/rlp"
23
24
"github.com/ethereum/go-ethereum/trie/trienode"
24
25
"github.com/ethereum/go-ethereum/triedb/database"
@@ -63,8 +64,7 @@ type StateTrie struct {
63
64
trie Trie
64
65
db database.NodeDatabase
65
66
preimages preimageStore
66
- hashKeyBuf [common .HashLength ]byte
67
- secKeyCache map [string ][]byte
67
+ secKeyCache map [common.Hash ][]byte
68
68
secKeyCacheOwner * StateTrie // Pointer to self, replace the key cache on mismatch
69
69
}
70
70
@@ -97,15 +97,15 @@ func NewStateTrie(id *ID, db database.NodeDatabase) (*StateTrie, error) {
97
97
// This function will omit any encountered error but just
98
98
// print out an error message.
99
99
func (t * StateTrie ) MustGet (key []byte ) []byte {
100
- return t .trie .MustGet (t . hashKey (key ))
100
+ return t .trie .MustGet (crypto . Keccak256 (key ))
101
101
}
102
102
103
103
// GetStorage attempts to retrieve a storage slot with provided account address
104
104
// and slot key. The value bytes must not be modified by the caller.
105
105
// If the specified storage slot is not in the trie, nil will be returned.
106
106
// If a trie node is not found in the database, a MissingNodeError is returned.
107
107
func (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 ))
109
109
if err != nil || len (enc ) == 0 {
110
110
return nil , err
111
111
}
@@ -117,7 +117,7 @@ func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
117
117
// If the specified account is not in the trie, nil will be returned.
118
118
// If a trie node is not found in the database, a MissingNodeError is returned.
119
119
func (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 ()))
121
121
if res == nil || err != nil {
122
122
return nil , err
123
123
}
@@ -157,9 +157,9 @@ func (t *StateTrie) GetNode(path []byte) ([]byte, int, error) {
157
157
// This function will omit any encountered error but just print out an
158
158
// error message.
159
159
func (t * StateTrie ) MustUpdate (key , value []byte ) {
160
- hk := t . hashKey (key )
160
+ hk := crypto . Keccak256 (key )
161
161
t .trie .MustUpdate (hk , value )
162
- t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
162
+ t .getSecKeyCache ()[common . Hash (hk )] = common .CopyBytes (key )
163
163
}
164
164
165
165
// UpdateStorage associates key with value in the trie. Subsequent calls to
@@ -171,27 +171,27 @@ func (t *StateTrie) MustUpdate(key, value []byte) {
171
171
//
172
172
// If a node is not found in the database, a MissingNodeError is returned.
173
173
func (t * StateTrie ) UpdateStorage (_ common.Address , key , value []byte ) error {
174
- hk := t . hashKey (key )
174
+ hk := crypto . Keccak256 (key )
175
175
v , _ := rlp .EncodeToBytes (value )
176
176
err := t .trie .Update (hk , v )
177
177
if err != nil {
178
178
return err
179
179
}
180
- t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
180
+ t .getSecKeyCache ()[common . Hash (hk )] = common .CopyBytes (key )
181
181
return nil
182
182
}
183
183
184
184
// UpdateAccount will abstract the write of an account to the secure trie.
185
185
func (t * StateTrie ) UpdateAccount (address common.Address , acc * types.StateAccount , _ int ) error {
186
- hk := t . hashKey (address .Bytes ())
186
+ hk := crypto . Keccak256 (address .Bytes ())
187
187
data , err := rlp .EncodeToBytes (acc )
188
188
if err != nil {
189
189
return err
190
190
}
191
191
if err := t .trie .Update (hk , data ); err != nil {
192
192
return err
193
193
}
194
- t .getSecKeyCache ()[string (hk )] = address .Bytes ()
194
+ t .getSecKeyCache ()[common . Hash (hk )] = address .Bytes ()
195
195
return nil
196
196
}
197
197
@@ -202,31 +202,31 @@ func (t *StateTrie) UpdateContractCode(_ common.Address, _ common.Hash, _ []byte
202
202
// MustDelete removes any existing value for key from the trie. This function
203
203
// will omit any encountered error but just print out an error message.
204
204
func (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 ))
207
207
t .trie .MustDelete (hk )
208
208
}
209
209
210
210
// DeleteStorage removes any existing storage slot from the trie.
211
211
// If the specified trie node is not in the trie, nothing will be changed.
212
212
// If a node is not found in the database, a MissingNodeError is returned.
213
213
func (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 ))
216
216
return t .trie .Delete (hk )
217
217
}
218
218
219
219
// DeleteAccount abstracts an account deletion from the trie.
220
220
func (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 ))
223
223
return t .trie .Delete (hk )
224
224
}
225
225
226
226
// GetKey returns the sha3 preimage of a hashed key that was
227
227
// previously used to store a value.
228
228
func (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 {
230
230
return key
231
231
}
232
232
if t .preimages == nil {
@@ -251,13 +251,9 @@ func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
251
251
// Write all the pre-images to the actual disk database
252
252
if len (t .getSecKeyCache ()) > 0 {
253
253
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 )
259
255
}
260
- t .secKeyCache = make (map [string ][]byte )
256
+ t .secKeyCache = make (map [common. Hash ][]byte )
261
257
}
262
258
// Commit the trie and return its modified nodeset.
263
259
return t .trie .Commit (collectLeaf )
@@ -291,25 +287,13 @@ func (t *StateTrie) MustNodeIterator(start []byte) NodeIterator {
291
287
return t .trie .MustNodeIterator (start )
292
288
}
293
289
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
-
306
290
// getSecKeyCache returns the current secure key cache, creating a new one if
307
291
// ownership changed (i.e. the current secure trie is a copy of another owning
308
292
// the actual cache).
309
- func (t * StateTrie ) getSecKeyCache () map [string ][]byte {
293
+ func (t * StateTrie ) getSecKeyCache () map [common. Hash ][]byte {
310
294
if t != t .secKeyCacheOwner {
311
295
t .secKeyCacheOwner = t
312
- t .secKeyCache = make (map [string ][]byte )
296
+ t .secKeyCache = make (map [common. Hash ][]byte )
313
297
}
314
298
return t .secKeyCache
315
299
}
0 commit comments