trie: no need to store preimage if not enabled

Signed-off-by: jsvisa <delweng@gmail.com>
This commit is contained in:
jsvisa 2025-06-11 21:30:15 +08:00
parent 9e0611b660
commit 959f00d922

View file

@ -159,8 +159,10 @@ func (t *StateTrie) GetNode(path []byte) ([]byte, int, error) {
func (t *StateTrie) MustUpdate(key, value []byte) { func (t *StateTrie) MustUpdate(key, value []byte) {
hk := crypto.Keccak256(key) hk := crypto.Keccak256(key)
t.trie.MustUpdate(hk, value) t.trie.MustUpdate(hk, value)
if t.preimages != nil {
t.getSecKeyCache()[common.Hash(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
// Get will return value. If value has length zero, any existing value // Get will return value. If value has length zero, any existing value
@ -177,7 +179,9 @@ func (t *StateTrie) UpdateStorage(_ common.Address, key, value []byte) error {
if err != nil { if err != nil {
return err return err
} }
if t.preimages != nil {
t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key) t.getSecKeyCache()[common.Hash(hk)] = common.CopyBytes(key)
}
return nil return nil
} }
@ -191,7 +195,9 @@ 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
} }
if t.preimages != nil {
t.getSecKeyCache()[common.Hash(hk)] = address.Bytes() t.getSecKeyCache()[common.Hash(hk)] = address.Bytes()
}
return nil return nil
} }
@ -203,7 +209,9 @@ 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 := crypto.Keccak256(key) hk := crypto.Keccak256(key)
if t.preimages != nil {
delete(t.getSecKeyCache(), common.Hash(hk)) delete(t.getSecKeyCache(), common.Hash(hk))
}
t.trie.MustDelete(hk) t.trie.MustDelete(hk)
} }
@ -212,14 +220,18 @@ 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 := crypto.Keccak256(key) hk := crypto.Keccak256(key)
if t.preimages != nil {
delete(t.getSecKeyCache(), common.Hash(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 := crypto.Keccak256(address.Bytes()) hk := crypto.Keccak256(address.Bytes())
if t.preimages != nil {
delete(t.getSecKeyCache(), common.Hash(hk)) delete(t.getSecKeyCache(), common.Hash(hk))
}
return t.trie.Delete(hk) return t.trie.Delete(hk)
} }