@@ -21,6 +21,7 @@ import (
21
21
22
22
"github.com/CortexFoundation/CortexTheseus/common"
23
23
"github.com/CortexFoundation/CortexTheseus/core/types"
24
+ "github.com/CortexFoundation/CortexTheseus/crypto"
24
25
"github.com/CortexFoundation/CortexTheseus/log"
25
26
"github.com/CortexFoundation/CortexTheseus/rlp"
26
27
)
@@ -53,8 +54,7 @@ func NewSecure(stateRoot common.Hash, owner common.Hash, root common.Hash, db *D
53
54
type StateTrie struct {
54
55
trie Trie
55
56
preimages * preimageStore
56
- hashKeyBuf [common .HashLength ]byte
57
- secKeyCache map [string ][]byte
57
+ secKeyCache map [common.Hash ][]byte
58
58
secKeyCacheOwner * StateTrie // Pointer to self, replace the key cache on mismatch
59
59
}
60
60
@@ -107,7 +107,7 @@ func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
107
107
// If the specified account is not in the trie, nil will be returned.
108
108
// If a trie node is not found in the database, a MissingNodeError is returned.
109
109
func (t * StateTrie ) GetAccount (address common.Address ) (* types.StateAccount , error ) {
110
- res , err := t .TryGet (t . hashKey (address .Bytes ()))
110
+ res , err := t .TryGet (crypto . Keccak256 (address .Bytes ()))
111
111
if res == nil || err != nil {
112
112
return nil , err
113
113
}
@@ -120,7 +120,7 @@ func (t *StateTrie) GetAccount(address common.Address) (*types.StateAccount, err
120
120
// The value bytes must not be modified by the caller.
121
121
// If a node was not found in the database, a MissingNodeError is returned.
122
122
func (t * StateTrie ) TryGet (key []byte ) ([]byte , error ) {
123
- return t .trie .TryGet (t . hashKey (key ))
123
+ return t .trie .TryGet (crypto . Keccak256 (key ))
124
124
}
125
125
126
126
// TryGetNode attempts to retrieve a trie node by compact-encoded path. It is not
@@ -138,28 +138,28 @@ func (t *StateTrie) TryGetNode(path []byte) ([]byte, int, error) {
138
138
//
139
139
// If a node is not found in the database, a MissingNodeError is returned.
140
140
func (t * StateTrie ) UpdateStorage (_ common.Address , key , value []byte ) error {
141
- hk := t . hashKey (key )
141
+ hk := crypto . Keccak256 (key )
142
142
v , _ := rlp .EncodeToBytes (value )
143
143
err := t .trie .TryUpdate (hk , v )
144
144
if err != nil {
145
145
return err
146
146
}
147
- t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
147
+ t .getSecKeyCache ()[common . Hash (hk )] = common .CopyBytes (key )
148
148
return nil
149
149
}
150
150
151
151
// TryUpdateAccount account will abstract the write of an account to the
152
152
// secure trie.
153
153
func (t * StateTrie ) TryUpdateAccount (key []byte , acc * types.StateAccount ) error {
154
- hk := t . hashKey (key )
154
+ hk := crypto . Keccak256 (key )
155
155
data , err := rlp .EncodeToBytes (acc )
156
156
if err != nil {
157
157
return err
158
158
}
159
159
if err := t .trie .TryUpdate (hk , data ); err != nil {
160
160
return err
161
161
}
162
- t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
162
+ t .getSecKeyCache ()[common . Hash (hk )] = common .CopyBytes (key )
163
163
return nil
164
164
}
165
165
@@ -188,12 +188,12 @@ func (t *StateTrie) Update(key, value []byte) {
188
188
//
189
189
// If a node was not found in the database, a MissingNodeError is returned.
190
190
func (t * StateTrie ) TryUpdate (key , value []byte ) error {
191
- hk := t . hashKey (key )
191
+ hk := crypto . Keccak256 (key )
192
192
err := t .trie .TryUpdate (hk , value )
193
193
if err != nil {
194
194
return err
195
195
}
196
- t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
196
+ t .getSecKeyCache ()[common . Hash (hk )] = common .CopyBytes (key )
197
197
return nil
198
198
}
199
199
@@ -207,22 +207,22 @@ func (t *StateTrie) Delete(key []byte) {
207
207
// TryDelete removes any existing value for key from the trie.
208
208
// If a node was not found in the database, a MissingNodeError is returned.
209
209
func (t * StateTrie ) TryDelete (key []byte ) error {
210
- hk := t . hashKey (key )
211
- delete (t .getSecKeyCache (), string (hk ))
210
+ hk := crypto . Keccak256 (key )
211
+ delete (t .getSecKeyCache (), common . Hash (hk ))
212
212
return t .trie .TryDelete (hk )
213
213
}
214
214
215
215
// TryDeleteAccount abstracts an account deletion from the trie.
216
216
func (t * StateTrie ) TryDeleteAccount (key []byte ) error {
217
- hk := t . hashKey (key )
218
- delete (t .getSecKeyCache (), string (hk ))
217
+ hk := crypto . Keccak256 (key )
218
+ delete (t .getSecKeyCache (), common . Hash (hk ))
219
219
return t .trie .TryDelete (hk )
220
220
}
221
221
222
222
// GetKey returns the sha3 preimage of a hashed key that was
223
223
// previously used to store a value.
224
224
func (t * StateTrie ) GetKey (shaKey []byte ) []byte {
225
- if key , ok := t .getSecKeyCache ()[string (shaKey )]; ok {
225
+ if key , ok := t .getSecKeyCache ()[common . Hash (shaKey )]; ok {
226
226
return key
227
227
}
228
228
if t .preimages == nil {
@@ -240,13 +240,9 @@ func (t *StateTrie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
240
240
// Write all the pre-images to the actual disk database
241
241
if len (t .getSecKeyCache ()) > 0 {
242
242
if t .preimages != nil {
243
- preimages := make (map [common.Hash ][]byte , len (t .secKeyCache ))
244
- for hk , key := range t .secKeyCache {
245
- preimages [common .BytesToHash ([]byte (hk ))] = key
246
- }
247
- t .preimages .insertPreimage (preimages )
243
+ t .preimages .insertPreimage (t .secKeyCache )
248
244
}
249
- t .secKeyCache = make (map [string ][]byte )
245
+ t .secKeyCache = make (map [common. Hash ][]byte )
250
246
}
251
247
// Commit the trie to its intermediate node database
252
248
return t .trie .Commit (onleaf )
@@ -279,25 +275,13 @@ func (t *StateTrie) MustNodeIterator(start []byte) NodeIterator {
279
275
return t .trie .MustNodeIterator (start )
280
276
}
281
277
282
- // hashKey returns the hash of key as an ephemeral buffer.
283
- // The caller must not hold onto the return value because it will become
284
- // invalid on the next call to hashKey or secKey.
285
- func (t * StateTrie ) hashKey (key []byte ) []byte {
286
- h := newHasher (false )
287
- h .sha .Reset ()
288
- h .sha .Write (key )
289
- h .sha .Read (t .hashKeyBuf [:])
290
- returnHasherToPool (h )
291
- return t .hashKeyBuf [:]
292
- }
293
-
294
278
// getSecKeyCache returns the current secure key cache, creating a new one if
295
279
// ownership changed (i.e. the current secure trie is a copy of another owning
296
280
// the actual cache).
297
- func (t * StateTrie ) getSecKeyCache () map [string ][]byte {
281
+ func (t * StateTrie ) getSecKeyCache () map [common. Hash ][]byte {
298
282
if t != t .secKeyCacheOwner {
299
283
t .secKeyCacheOwner = t
300
- t .secKeyCache = make (map [string ][]byte )
284
+ t .secKeyCache = make (map [common. Hash ][]byte )
301
285
}
302
286
return t .secKeyCache
303
287
}
0 commit comments