@@ -21,6 +21,7 @@ import (
2121
2222 "github.com/CortexFoundation/CortexTheseus/common"
2323 "github.com/CortexFoundation/CortexTheseus/core/types"
24+ "github.com/CortexFoundation/CortexTheseus/crypto"
2425 "github.com/CortexFoundation/CortexTheseus/log"
2526 "github.com/CortexFoundation/CortexTheseus/rlp"
2627)
@@ -53,8 +54,7 @@ func NewSecure(stateRoot common.Hash, owner common.Hash, root common.Hash, db *D
5354type StateTrie struct {
5455 trie Trie
5556 preimages * preimageStore
56- hashKeyBuf [common .HashLength ]byte
57- secKeyCache map [string ][]byte
57+ secKeyCache map [common.Hash ][]byte
5858 secKeyCacheOwner * StateTrie // Pointer to self, replace the key cache on mismatch
5959}
6060
@@ -107,7 +107,7 @@ func (t *StateTrie) GetStorage(_ common.Address, key []byte) ([]byte, error) {
107107// If the specified account is not in the trie, nil will be returned.
108108// If a trie node is not found in the database, a MissingNodeError is returned.
109109func (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 ()))
111111 if res == nil || err != nil {
112112 return nil , err
113113 }
@@ -120,7 +120,7 @@ func (t *StateTrie) GetAccount(address common.Address) (*types.StateAccount, err
120120// The value bytes must not be modified by the caller.
121121// If a node was not found in the database, a MissingNodeError is returned.
122122func (t * StateTrie ) TryGet (key []byte ) ([]byte , error ) {
123- return t .trie .TryGet (t . hashKey (key ))
123+ return t .trie .TryGet (crypto . Keccak256 (key ))
124124}
125125
126126// 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) {
138138//
139139// If a node is not found in the database, a MissingNodeError is returned.
140140func (t * StateTrie ) UpdateStorage (_ common.Address , key , value []byte ) error {
141- hk := t . hashKey (key )
141+ hk := crypto . Keccak256 (key )
142142 v , _ := rlp .EncodeToBytes (value )
143143 err := t .trie .TryUpdate (hk , v )
144144 if err != nil {
145145 return err
146146 }
147- t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
147+ t .getSecKeyCache ()[common . Hash (hk )] = common .CopyBytes (key )
148148 return nil
149149}
150150
151151// TryUpdateAccount account will abstract the write of an account to the
152152// secure trie.
153153func (t * StateTrie ) TryUpdateAccount (key []byte , acc * types.StateAccount ) error {
154- hk := t . hashKey (key )
154+ hk := crypto . Keccak256 (key )
155155 data , err := rlp .EncodeToBytes (acc )
156156 if err != nil {
157157 return err
158158 }
159159 if err := t .trie .TryUpdate (hk , data ); err != nil {
160160 return err
161161 }
162- t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
162+ t .getSecKeyCache ()[common . Hash (hk )] = common .CopyBytes (key )
163163 return nil
164164}
165165
@@ -188,12 +188,12 @@ func (t *StateTrie) Update(key, value []byte) {
188188//
189189// If a node was not found in the database, a MissingNodeError is returned.
190190func (t * StateTrie ) TryUpdate (key , value []byte ) error {
191- hk := t . hashKey (key )
191+ hk := crypto . Keccak256 (key )
192192 err := t .trie .TryUpdate (hk , value )
193193 if err != nil {
194194 return err
195195 }
196- t .getSecKeyCache ()[string (hk )] = common .CopyBytes (key )
196+ t .getSecKeyCache ()[common . Hash (hk )] = common .CopyBytes (key )
197197 return nil
198198}
199199
@@ -207,22 +207,22 @@ func (t *StateTrie) Delete(key []byte) {
207207// TryDelete removes any existing value for key from the trie.
208208// If a node was not found in the database, a MissingNodeError is returned.
209209func (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 ))
212212 return t .trie .TryDelete (hk )
213213}
214214
215215// TryDeleteAccount abstracts an account deletion from the trie.
216216func (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 ))
219219 return t .trie .TryDelete (hk )
220220}
221221
222222// GetKey returns the sha3 preimage of a hashed key that was
223223// previously used to store a value.
224224func (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 {
226226 return key
227227 }
228228 if t .preimages == nil {
@@ -240,13 +240,9 @@ func (t *StateTrie) Commit(onleaf LeafCallback) (root common.Hash, err error) {
240240 // Write all the pre-images to the actual disk database
241241 if len (t .getSecKeyCache ()) > 0 {
242242 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 )
248244 }
249- t .secKeyCache = make (map [string ][]byte )
245+ t .secKeyCache = make (map [common. Hash ][]byte )
250246 }
251247 // Commit the trie to its intermediate node database
252248 return t .trie .Commit (onleaf )
@@ -279,25 +275,13 @@ func (t *StateTrie) MustNodeIterator(start []byte) NodeIterator {
279275 return t .trie .MustNodeIterator (start )
280276}
281277
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-
294278// getSecKeyCache returns the current secure key cache, creating a new one if
295279// ownership changed (i.e. the current secure trie is a copy of another owning
296280// the actual cache).
297- func (t * StateTrie ) getSecKeyCache () map [string ][]byte {
281+ func (t * StateTrie ) getSecKeyCache () map [common. Hash ][]byte {
298282 if t != t .secKeyCacheOwner {
299283 t .secKeyCacheOwner = t
300- t .secKeyCache = make (map [string ][]byte )
284+ t .secKeyCache = make (map [common. Hash ][]byte )
301285 }
302286 return t .secKeyCache
303287}
0 commit comments