From 0f528fcb7088fc177ca9ffa8b43f4c4e777c252f Mon Sep 17 00:00:00 2001 From: N Date: Fri, 16 May 2025 16:49:43 +0800 Subject: [PATCH] triedb: reuse the global hash buffer --- triedb/pathdb/execute.go | 12 ++++-------- triedb/pathdb/history.go | 5 ++--- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/triedb/pathdb/execute.go b/triedb/pathdb/execute.go index db1e679277..aa4bd8b44b 100644 --- a/triedb/pathdb/execute.go +++ b/triedb/pathdb/execute.go @@ -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 diff --git a/triedb/pathdb/history.go b/triedb/pathdb/history.go index aed0296da5..63c9152bf7 100644 --- a/triedb/pathdb/history.go +++ b/triedb/pathdb/history.go @@ -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 }