triedb: reuse the global hash buffer

This commit is contained in:
N 2025-05-16 16:49:43 +08:00
parent f1448c2913
commit 0f528fcb70
2 changed files with 6 additions and 11 deletions

View file

@ -86,9 +86,7 @@ func apply(db database.NodeDatabase, prevRoot common.Hash, postRoot common.Hash,
func updateAccount(ctx *context, db database.NodeDatabase, addr common.Address) error {
// The account was present in prev-state, decode it from the
// 'slim-rlp' format bytes.
h := crypto.NewKeccakState()
addrHash := crypto.HashData(h, addr.Bytes())
addrHash := crypto.Keccak256Hash(addr.Bytes())
prev, err := types.FullAccount(ctx.accounts[addr])
if err != nil {
return err
@ -113,7 +111,7 @@ func updateAccount(ctx *context, db database.NodeDatabase, addr common.Address)
for key, val := range ctx.storages[addr] {
tkey := key
if ctx.rawStorageKey {
tkey = crypto.HashData(h, key.Bytes())
tkey = crypto.Keccak256Hash(key.Bytes())
}
var err error
if len(val) == 0 {
@ -149,9 +147,7 @@ func updateAccount(ctx *context, db database.NodeDatabase, addr common.Address)
// account and storage is wiped out correctly.
func deleteAccount(ctx *context, db database.NodeDatabase, addr common.Address) error {
// The account must be existent in post-state, load the account.
h := crypto.NewKeccakState()
addrHash := crypto.HashData(h, addr.Bytes())
addrHash := crypto.Keccak256Hash(addr.Bytes())
blob, err := ctx.accountTrie.Get(addrHash.Bytes())
if err != nil {
return err
@ -173,7 +169,7 @@ func deleteAccount(ctx *context, db database.NodeDatabase, addr common.Address)
}
tkey := key
if ctx.rawStorageKey {
tkey = crypto.HashData(h, key.Bytes())
tkey = crypto.Keccak256Hash(key.Bytes())
}
if err := st.Delete(tkey.Bytes()); err != nil {
return err

View file

@ -278,12 +278,11 @@ func newHistory(root common.Hash, parent common.Hash, block uint64, accounts map
// and the hash of the storage slot key.
func (h *history) stateSet() (map[common.Hash][]byte, map[common.Hash]map[common.Hash][]byte) {
var (
buff = crypto.NewKeccakState()
accounts = make(map[common.Hash][]byte)
storages = make(map[common.Hash]map[common.Hash][]byte)
)
for addr, blob := range h.accounts {
addrHash := crypto.HashData(buff, addr.Bytes())
addrHash := crypto.Keccak256Hash(addr.Bytes())
accounts[addrHash] = blob
storage, exist := h.storages[addr]
@ -295,7 +294,7 @@ func (h *history) stateSet() (map[common.Hash][]byte, map[common.Hash]map[common
} else {
subset := make(map[common.Hash][]byte)
for key, slot := range storage {
subset[crypto.HashData(buff, key.Bytes())] = slot
subset[crypto.Keccak256Hash(key.Bytes())] = slot
}
storages[addrHash] = subset
}