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
preimages preimageStore
hashKeyBuf [common.HashLength]byte
secKeyCache map[string][]byte
secKeyCache map[common.Hash][]byte
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) {
hk := t.hashKey(key)
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
@ -177,7 +177,7 @@ func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
if err != nil {
return err
}
t.getSecKeyCache()[string(hk)] = common.CopyBytes(key)
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
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 {
return err
}
t.getSecKeyCache()[string(hk)] = address.Bytes()
t.getSecKeyCache()[common.Hash(hk)] = address.Bytes()
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.
func (t *StateTrie) MustDelete(key []byte) {
hk := t.hashKey(key)
delete(t.getSecKeyCache(), string(hk))
delete(t.getSecKeyCache(), common.Hash(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.
func (t *StateTrie) DeleteStorage(_ common.Address, key []byte) error {
hk := t.hashKey(key)
delete(t.getSecKeyCache(), string(hk))
delete(t.getSecKeyCache(), common.Hash(hk))
return t.trie.Delete(hk)
}
// DeleteAccount abstracts an account deletion from the trie.
func (t *StateTrie) DeleteAccount(address common.Address) error {
hk := t.hashKey(address.Bytes())
delete(t.getSecKeyCache(), string(hk))
delete(t.getSecKeyCache(), common.Hash(hk))
return t.trie.Delete(hk)
}
// GetKey returns the sha3 preimage of a hashed key that was
// previously used to store a value.
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
}
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
if len(t.getSecKeyCache()) > 0 {
if t.preimages != nil {
preimages := make(map[common.Hash][]byte, len(t.secKeyCache))
for hk, key := range t.secKeyCache {
preimages[common.BytesToHash([]byte(hk))] = key
}
t.preimages.InsertPreimage(preimages)
t.preimages.InsertPreimage(t.secKeyCache)
}
t.secKeyCache = make(map[string][]byte)
t.secKeyCache = make(map[common.Hash][]byte)
}
// Commit the trie and return its modified nodeset.
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
// ownership changed (i.e. the current secure trie is a copy of another owning
// the actual cache).
func (t *StateTrie) getSecKeyCache() map[string][]byte {
func (t *StateTrie) getSecKeyCache() map[common.Hash][]byte {
if t != t.secKeyCacheOwner {
t.secKeyCacheOwner = t
t.secKeyCache = make(map[string][]byte)
t.secKeyCache = make(map[common.Hash][]byte)
}
return t.secKeyCache
}