From 277aa5b31a160667cf9f83b409867dfc9fb58c21 Mon Sep 17 00:00:00 2001 From: maskpp Date: Mon, 17 Jun 2024 20:33:16 +0800 Subject: [PATCH] unify the usage of hash pool --- core/rawdb/accessors_trie.go | 28 ++-------------------------- core/state/state_object.go | 7 +++++-- core/state/statedb.go | 5 ++--- crypto/crypto.go | 9 ++++++++- crypto/crypto_test.go | 3 +-- trie/sync.go | 24 +----------------------- trie/triestate/state.go | 32 ++------------------------------ triedb/pathdb/disklayer.go | 27 ++------------------------- 8 files changed, 23 insertions(+), 112 deletions(-) diff --git a/core/rawdb/accessors_trie.go b/core/rawdb/accessors_trie.go index 44eb715d04..ff6e9b15bb 100644 --- a/core/rawdb/accessors_trie.go +++ b/core/rawdb/accessors_trie.go @@ -18,7 +18,6 @@ package rawdb import ( "fmt" - "sync" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" @@ -45,25 +44,6 @@ const HashScheme = "hash" // on extra state diffs to survive deep reorg. const PathScheme = "path" -// hasher is used to compute the sha256 hash of the provided data. -type hasher struct{ sha crypto.KeccakState } - -var hasherPool = sync.Pool{ - New: func() interface{} { return &hasher{sha: crypto.NewKeccakState()} }, -} - -func newHasher() *hasher { - return hasherPool.Get().(*hasher) -} - -func (h *hasher) hash(data []byte) common.Hash { - return crypto.HashData(h.sha, data) -} - -func (h *hasher) release() { - hasherPool.Put(h) -} - // ReadAccountTrieNode retrieves the account trie node with the specified node path. func ReadAccountTrieNode(db ethdb.KeyValueReader, path []byte) []byte { data, _ := db.Get(accountTrieNodeKey(path)) @@ -170,9 +150,7 @@ func HasTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash c if len(blob) == 0 { return false } - h := newHasher() - defer h.release() - return h.hash(blob) == hash // exists but not match + return crypto.HashData(blob) == hash // exists but not match default: panic(fmt.Sprintf("Unknown scheme %v", scheme)) } @@ -194,9 +172,7 @@ func ReadTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash if len(blob) == 0 { return nil } - h := newHasher() - defer h.release() - if h.hash(blob) != hash { + if crypto.HashData(blob) != hash { return nil // exists but not match } return blob diff --git a/core/state/state_object.go b/core/state/state_object.go index 5c1dab53dc..525e7bf99c 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -408,7 +408,8 @@ func (s *stateObject) updateRoot() { // fulfills the storage diffs into the given accountUpdate struct. func (s *stateObject) commitStorage(op *accountUpdate) { var ( - buf = crypto.NewKeccakState() + hash common.Hash + hasher = crypto.NewKeccakState() encode = func(val common.Hash) []byte { if val == (common.Hash{}) { return nil @@ -425,7 +426,9 @@ func (s *stateObject) commitStorage(op *accountUpdate) { if val == s.originStorage[key] { continue } - hash := crypto.HashData(buf, key[:]) + hasher.Reset() + hasher.Write(key[:]) + hasher.Read(hash[:]) if op.storages == nil { op.storages = make(map[common.Hash][]byte) } diff --git a/core/state/statedb.go b/core/state/statedb.go index 4f84d93d63..ef3ae57bb1 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -580,7 +580,7 @@ func (s *StateDB) getStateObject(addr common.Address) *stateObject { var data *types.StateAccount if s.snap != nil { start := time.Now() - acc, err := s.snap.Account(crypto.HashData(s.hasher, addr.Bytes())) + acc, err := s.snap.Account(crypto.HashData(addr.Bytes())) s.SnapshotAccountReads += time.Since(start) if err == nil { @@ -1057,7 +1057,6 @@ func (s *StateDB) deleteStorage(addr common.Address, addrHash common.Hash, root func (s *StateDB) handleDestruction() (map[common.Hash]*accountDelete, []*trienode.NodeSet, error) { var ( nodes []*trienode.NodeSet - buf = crypto.NewKeccakState() deletes = make(map[common.Hash]*accountDelete) ) for addr, prev := range s.stateObjectsDestruct { @@ -1070,7 +1069,7 @@ func (s *StateDB) handleDestruction() (map[common.Hash]*accountDelete, []*trieno continue } // The account was existent, it can be either case (c) or (d). - addrHash := crypto.HashData(buf, addr.Bytes()) + addrHash := crypto.HashData(addr.Bytes()) op := &accountDelete{ address: addr, origin: types.SlimAccountRLP(*prev), diff --git a/crypto/crypto.go b/crypto/crypto.go index 7f7171f730..04dbedbfa4 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -28,6 +28,7 @@ import ( "io" "math/big" "os" + "sync" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common/math" @@ -73,11 +74,17 @@ func NewKeccakState() KeccakState { return sha3.NewLegacyKeccak256().(KeccakState) } +var hasherPool = sync.Pool{ + New: func() interface{} { return NewKeccakState() }, +} + // HashData hashes the provided data using the KeccakState and returns a 32 byte hash -func HashData(kh KeccakState, data []byte) (h common.Hash) { +func HashData(data []byte) (h common.Hash) { + kh := hasherPool.Get().(KeccakState) kh.Reset() kh.Write(data) kh.Read(h[:]) + hasherPool.Put(kh) return h } diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index da123cf980..5318ed27e9 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -44,8 +44,7 @@ func TestKeccak256Hash(t *testing.T) { func TestKeccak256Hasher(t *testing.T) { msg := []byte("abc") exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45") - hasher := NewKeccakState() - checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := HashData(hasher, in); return h[:] }, msg, exp) + checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := HashData(in); return h[:] }, msg, exp) } func TestToECDSAErrors(t *testing.T) { diff --git a/trie/sync.go b/trie/sync.go index 3b7caae5b1..633c665827 100644 --- a/trie/sync.go +++ b/trie/sync.go @@ -729,9 +729,7 @@ func (s *Sync) hasNode(owner common.Hash, path []byte, hash common.Hash) (exists } else { blob = rawdb.ReadStorageTrieNode(s.database, owner, path) } - h := newBlobHasher() - defer h.release() - exists = hash == h.hash(blob) + exists = hash == crypto.HashData(blob) inconsistent = !exists && len(blob) != 0 return exists, inconsistent } @@ -746,23 +744,3 @@ func ResolvePath(path []byte) (common.Hash, []byte) { } return owner, path } - -// blobHasher is used to compute the sha256 hash of the provided data. -type blobHasher struct{ state crypto.KeccakState } - -// blobHasherPool is the pool for reusing pre-allocated hash state. -var blobHasherPool = sync.Pool{ - New: func() interface{} { return &blobHasher{state: crypto.NewKeccakState()} }, -} - -func newBlobHasher() *blobHasher { - return blobHasherPool.Get().(*blobHasher) -} - -func (h *blobHasher) hash(data []byte) common.Hash { - return crypto.HashData(h.state, data) -} - -func (h *blobHasher) release() { - blobHasherPool.Put(h) -} diff --git a/trie/triestate/state.go b/trie/triestate/state.go index 7508da5d60..a16f4ab27d 100644 --- a/trie/triestate/state.go +++ b/trie/triestate/state.go @@ -19,7 +19,6 @@ package triestate import ( "errors" "fmt" - "sync" "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/core/types" @@ -139,12 +138,7 @@ func Apply(prevRoot common.Hash, postRoot common.Hash, accounts map[common.Addre // existent in post-state. Apply the reverse diff and verify if the storage // root matches the one in prev-state account. func updateAccount(ctx *context, loader TrieLoader, addr common.Address) error { - // The account was present in prev-state, decode it from the - // 'slim-rlp' format bytes. - h := newHasher() - defer h.release() - - addrHash := h.hash(addr.Bytes()) + addrHash := crypto.HashData(addr.Bytes()) prev, err := types.FullAccount(ctx.accounts[addr]) if err != nil { return err @@ -201,10 +195,7 @@ func updateAccount(ctx *context, loader TrieLoader, addr common.Address) error { // account and storage is wiped out correctly. func deleteAccount(ctx *context, loader TrieLoader, addr common.Address) error { // The account must be existent in post-state, load the account. - h := newHasher() - defer h.release() - - addrHash := h.hash(addr.Bytes()) + addrHash := crypto.HashData(addr.Bytes()) blob, err := ctx.accountTrie.Get(addrHash.Bytes()) if err != nil { return err @@ -242,22 +233,3 @@ func deleteAccount(ctx *context, loader TrieLoader, addr common.Address) error { // Delete the post-state account from the main trie. return ctx.accountTrie.Delete(addrHash.Bytes()) } - -// hasher is used to compute the sha256 hash of the provided data. -type hasher struct{ sha crypto.KeccakState } - -var hasherPool = sync.Pool{ - New: func() interface{} { return &hasher{sha: crypto.NewKeccakState()} }, -} - -func newHasher() *hasher { - return hasherPool.Get().(*hasher) -} - -func (h *hasher) hash(data []byte) common.Hash { - return crypto.HashData(h.sha, data) -} - -func (h *hasher) release() { - hasherPool.Put(h) -} diff --git a/triedb/pathdb/disklayer.go b/triedb/pathdb/disklayer.go index 964ad2ef77..e917bb85fe 100644 --- a/triedb/pathdb/disklayer.go +++ b/triedb/pathdb/disklayer.go @@ -115,16 +115,12 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co } dirtyMissMeter.Mark(1) - // Try to retrieve the trie node from the clean memory cache - h := newHasher() - defer h.release() - key := cacheKey(owner, path) if dl.cleans != nil { if blob := dl.cleans.Get(nil, key); len(blob) > 0 { cleanHitMeter.Mark(1) cleanReadMeter.Mark(int64(len(blob))) - return blob, h.hash(blob), &nodeLoc{loc: locCleanCache, depth: depth}, nil + return blob, crypto.HashData(blob), &nodeLoc{loc: locCleanCache, depth: depth}, nil } cleanMissMeter.Mark(1) } @@ -140,7 +136,7 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co cleanWriteMeter.Mark(int64(len(blob))) } - return blob, h.hash(blob), &nodeLoc{loc: locDiskLayer, depth: depth}, nil + return blob, crypto.HashData(blob), &nodeLoc{loc: locDiskLayer, depth: depth}, nil } // update implements the layer interface, returning a new diff layer on top @@ -295,22 +291,3 @@ func (dl *diskLayer) resetCache() { dl.cleans.Reset() } } - -// hasher is used to compute the sha256 hash of the provided data. -type hasher struct{ sha crypto.KeccakState } - -var hasherPool = sync.Pool{ - New: func() interface{} { return &hasher{sha: crypto.NewKeccakState()} }, -} - -func newHasher() *hasher { - return hasherPool.Get().(*hasher) -} - -func (h *hasher) hash(data []byte) common.Hash { - return crypto.HashData(h.sha, data) -} - -func (h *hasher) release() { - hasherPool.Put(h) -}