core: reuse the global hash buffer

This commit is contained in:
N 2025-05-16 16:49:23 +08:00
parent 57e985ecab
commit f1448c2913
4 changed files with 6 additions and 11 deletions

View file

@ -642,7 +642,6 @@ func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashScheme bool,
var ( var (
count, deleted, skipped int count, deleted, skipped int
buff = crypto.NewKeccakState()
startTime = time.Now() startTime = time.Now()
) )
@ -655,7 +654,7 @@ func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashScheme bool,
for it.Next() && bytes.Compare(end, it.Key()) > 0 { for it.Next() && bytes.Compare(end, it.Key()) > 0 {
// Prevent deletion for trie nodes in hash mode // Prevent deletion for trie nodes in hash mode
if len(it.Key()) != 32 || crypto.HashData(buff, it.Value()) != common.BytesToHash(it.Key()) { if len(it.Key()) != 32 || crypto.Keccak256Hash(it.Value()) != common.BytesToHash(it.Key()) {
if err := batch.Delete(it.Key()); err != nil { if err := batch.Delete(it.Key()); err != nil {
return err return err
} }

View file

@ -204,9 +204,8 @@ func (r *flatReader) Storage(addr common.Address, key common.Hash) (common.Hash,
// //
// trieReader is safe for concurrent read. // trieReader is safe for concurrent read.
type trieReader struct { type trieReader struct {
root common.Hash // State root which uniquely represent a state root common.Hash // State root which uniquely represent a state
db *triedb.Database // Database for loading trie db *triedb.Database // Database for loading trie
buff crypto.KeccakState // Buffer for keccak256 hashing
// Main trie, resolved in constructor. Note either the Merkle-Patricia-tree // Main trie, resolved in constructor. Note either the Merkle-Patricia-tree
// or Verkle-tree is not safe for concurrent read. // or Verkle-tree is not safe for concurrent read.
@ -235,7 +234,6 @@ func newTrieReader(root common.Hash, db *triedb.Database, cache *utils.PointCach
return &trieReader{ return &trieReader{
root: root, root: root,
db: db, db: db,
buff: crypto.NewKeccakState(),
mainTrie: tr, mainTrie: tr,
subRoots: make(map[common.Address]common.Hash), subRoots: make(map[common.Address]common.Hash),
subTries: make(map[common.Address]Trie), subTries: make(map[common.Address]Trie),
@ -298,7 +296,7 @@ func (r *trieReader) Storage(addr common.Address, key common.Hash) (common.Hash,
root = r.subRoots[addr] root = r.subRoots[addr]
} }
var err error var err error
tr, err = trie.NewStateTrie(trie.StorageTrieID(r.root, crypto.HashData(r.buff, addr.Bytes()), root), r.db) tr, err = trie.NewStateTrie(trie.StorageTrieID(r.root, crypto.Keccak256Hash(addr.Bytes()), root), r.db)
if err != nil { if err != nil {
return common.Hash{}, err return common.Hash{}, err
} }

View file

@ -377,7 +377,6 @@ func (s *stateObject) updateRoot() {
// fulfills the storage diffs into the given accountUpdate struct. // fulfills the storage diffs into the given accountUpdate struct.
func (s *stateObject) commitStorage(op *accountUpdate) { func (s *stateObject) commitStorage(op *accountUpdate) {
var ( var (
buf = crypto.NewKeccakState()
encode = func(val common.Hash) []byte { encode = func(val common.Hash) []byte {
if val == (common.Hash{}) { if val == (common.Hash{}) {
return nil return nil
@ -394,7 +393,7 @@ func (s *stateObject) commitStorage(op *accountUpdate) {
if val == s.originStorage[key] { if val == s.originStorage[key] {
continue continue
} }
hash := crypto.HashData(buf, key[:]) hash := crypto.Keccak256Hash(key[:])
if op.storages == nil { if op.storages == nil {
op.storages = make(map[common.Hash][]byte) op.storages = make(map[common.Hash][]byte)
} }

View file

@ -1064,7 +1064,6 @@ func (s *StateDB) deleteStorage(addr common.Address, addrHash common.Hash, root
func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*accountDelete, []*trienode.NodeSet, error) { func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*accountDelete, []*trienode.NodeSet, error) {
var ( var (
nodes []*trienode.NodeSet nodes []*trienode.NodeSet
buf = crypto.NewKeccakState()
deletes = make(map[common.Hash]*accountDelete) deletes = make(map[common.Hash]*accountDelete)
) )
for addr, prevObj := range s.stateObjectsDestruct { for addr, prevObj := range s.stateObjectsDestruct {
@ -1079,7 +1078,7 @@ func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*acco
continue continue
} }
// The account was existent, it can be either case (c) or (d). // The account was existent, it can be either case (c) or (d).
addrHash := crypto.HashData(buf, addr.Bytes()) addrHash := crypto.Keccak256Hash(addr.Bytes())
op := &accountDelete{ op := &accountDelete{
address: addr, address: addr,
origin: types.SlimAccountRLP(*prev), origin: types.SlimAccountRLP(*prev),