use common.Hash as the key in secKeyCache map

This commit is contained in:
maskpp 2025-05-08 20:34:49 +08:00
parent 10519768a2
commit e99dd3990f

View file

@ -64,7 +64,7 @@ type StateTrie struct {
db database.NodeDatabase db database.NodeDatabase
preimages preimageStore preimages preimageStore
hashKeyBuf [common.HashLength]byte hashKeyBuf [common.HashLength]byte
secKeyCache map[string][]byte secKeyCache map[common.Hash][]byte
secKeyCacheOwner *StateTrie // Pointer to self, replace the key cache on mismatch secKeyCacheOwner *StateTrie // Pointer to self, replace the key cache on mismatch
} }
@ -159,7 +159,7 @@ func (t *StateTrie) GetNode(path []byte) ([]byte, int, error) {
func (t *StateTrie) MustUpdate(key, value []byte) { func (t *StateTrie) MustUpdate(key, value []byte) {
hk := t.hashKey(key) hk := t.hashKey(key)
t.trie.MustUpdate(hk, value) t.trie.MustUpdate(hk, value)
t.getSecKeyCache()[string(hk)] = common.CopyBytes(key) t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
} }
// UpdateStorage associates key with value in the trie. Subsequent calls to // UpdateStorage associates key with value in the trie. Subsequent calls to
@ -177,7 +177,7 @@ func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
if err != nil { if err != nil {
return err return err
} }
t.getSecKeyCache()[string(hk)] = common.CopyBytes(key) t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
return nil return nil
} }
@ -191,7 +191,7 @@ func (t *StateTrie) UpdateAccount(address common.Address, acc *types.StateAccoun
if err := t.trie.Update(hk, data); err != nil { if err := t.trie.Update(hk, data); err != nil {
return err return err
} }
t.getSecKeyCache()[string(hk)] = address.Bytes() t.getSecKeyCache()[common.Hash(hk)] = address.Bytes()
return nil return nil
} }
@ -203,7 +203,7 @@ func (t *StateTrie) UpdateContractCode(_ common.Address, _ common.Hash, _ []byte
// will omit any encountered error but just print out an error message. // will omit any encountered error but just print out an error message.
func (t *StateTrie) MustDelete(key []byte) { func (t *StateTrie) MustDelete(key []byte) {
hk := t.hashKey(key) hk := t.hashKey(key)
delete(t.getSecKeyCache(), string(hk)) delete(t.getSecKeyCache(), common.Hash(hk))
t.trie.MustDelete(hk) t.trie.MustDelete(hk)
} }
@ -212,21 +212,21 @@ func (t *StateTrie) MustDelete(key []byte) {
// If a node is not found in the database, a MissingNodeError is returned. // If a node is not found in the database, a MissingNodeError is returned.
func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error { func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error {
hk := t.hashKey(key) hk := t.hashKey(key)
delete(t.getSecKeyCache(), string(hk)) delete(t.getSecKeyCache(), common.Hash(hk))
return t.trie.Delete(hk) return t.trie.Delete(hk)
} }
// DeleteAccount abstracts an account deletion from the trie. // DeleteAccount abstracts an account deletion from the trie.
func (t *StateTrie) DeleteAccount(address common.Address) error { func (t *StateTrie) DeleteAccount(address common.Address) error {
hk := t.hashKey(address.Bytes()) hk := t.hashKey(address.Bytes())
delete(t.getSecKeyCache(), string(hk)) delete(t.getSecKeyCache(), common.Hash(hk))
return t.trie.Delete(hk) return t.trie.Delete(hk)
} }
// GetKey returns the sha3 preimage of a hashed key that was // GetKey returns the sha3 preimage of a hashed key that was
// previously used to store a value. // previously used to store a value.
func (t *StateTrie) GetKey(shaKey []byte) []byte { func (t *StateTrie) GetKey(shaKey []byte) []byte {
if key, ok := t.getSecKeyCache()[string(shaKey)]; ok { if key, ok := t.getSecKeyCache()[common.BytesToHash(shaKey)]; ok {
return key return key
} }
if t.preimages == nil { if t.preimages == nil {
@ -251,13 +251,9 @@ func (t *StateTrie) Commit(collectLeaf bool) (common.Hash, *trienode.NodeSet) {
// Write all the pre-images to the actual disk database // Write all the pre-images to the actual disk database
if len(t.getSecKeyCache()) > 0 { if len(t.getSecKeyCache()) > 0 {
if t.preimages != nil { if t.preimages != nil {
preimages := make(map[common.Hash][]byte, len(t.secKeyCache)) t.preimages.InsertPreimage(t.secKeyCache)
for hk, key := range t.secKeyCache {
preimages[common.BytesToHash([]byte(hk))] = key
} }
t.preimages.InsertPreimage(preimages) t.secKeyCache = make(map[common.Hash][]byte)
}
t.secKeyCache = make(map[string][]byte)
} }
// Commit the trie and return its modified nodeset. // Commit the trie and return its modified nodeset.
return t.trie.Commit(collectLeaf) return t.trie.Commit(collectLeaf)
@ -306,10 +302,10 @@ func (t *StateTrie) hashKey(key []byte) []byte {
// getSecKeyCache returns the current secure key cache, creating a new one if // getSecKeyCache returns the current secure key cache, creating a new one if
// ownership changed (i.e. the current secure trie is a copy of another owning // ownership changed (i.e. the current secure trie is a copy of another owning
// the actual cache). // the actual cache).
func (t *StateTrie) getSecKeyCache() map[string][]byte { func (t *StateTrie) getSecKeyCache() map[common.Hash][]byte {
if t != t.secKeyCacheOwner { if t != t.secKeyCacheOwner {
t.secKeyCacheOwner = t t.secKeyCacheOwner = t
t.secKeyCache = make(map[string][]byte) t.secKeyCache = make(map[common.Hash][]byte)
} }
return t.secKeyCache return t.secKeyCache
} }