From 63c481259cfc37b7329b0e61db1774007743f2d3 Mon Sep 17 00:00:00 2001 From: maskpp Date: Mon, 17 Jun 2024 22:11:02 +0800 Subject: [PATCH] reuse Keccak256Hash and Keccak256 --- core/rawdb/accessors_trie.go | 4 ++-- core/state/state_object.go | 6 +----- core/state/statedb.go | 4 ++-- crypto/crypto.go | 20 +++++++++++--------- crypto/crypto_test.go | 2 +- trie/sync.go | 2 +- trie/triestate/state.go | 4 ++-- triedb/pathdb/disklayer.go | 4 ++-- 8 files changed, 22 insertions(+), 24 deletions(-) diff --git a/core/rawdb/accessors_trie.go b/core/rawdb/accessors_trie.go index ff6e9b15bb..7e94bd169d 100644 --- a/core/rawdb/accessors_trie.go +++ b/core/rawdb/accessors_trie.go @@ -150,7 +150,7 @@ func HasTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash c if len(blob) == 0 { return false } - return crypto.HashData(blob) == hash // exists but not match + return crypto.Keccak256Hash(blob) == hash // exists but not match default: panic(fmt.Sprintf("Unknown scheme %v", scheme)) } @@ -172,7 +172,7 @@ func ReadTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash if len(blob) == 0 { return nil } - if crypto.HashData(blob) != hash { + if crypto.Keccak256Hash(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 525e7bf99c..99292b8c0d 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -408,8 +408,6 @@ func (s *stateObject) updateRoot() { // fulfills the storage diffs into the given accountUpdate struct. func (s *stateObject) commitStorage(op *accountUpdate) { var ( - hash common.Hash - hasher = crypto.NewKeccakState() encode = func(val common.Hash) []byte { if val == (common.Hash{}) { return nil @@ -426,9 +424,7 @@ func (s *stateObject) commitStorage(op *accountUpdate) { if val == s.originStorage[key] { continue } - hasher.Reset() - hasher.Write(key[:]) - hasher.Read(hash[:]) + hash := crypto.Keccak256Hash(key[:]) if op.storages == nil { op.storages = make(map[common.Hash][]byte) } diff --git a/core/state/statedb.go b/core/state/statedb.go index ef3ae57bb1..e99f936eb5 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(addr.Bytes())) + acc, err := s.snap.Account(crypto.Keccak256Hash(addr.Bytes())) s.SnapshotAccountReads += time.Since(start) if err == nil { @@ -1069,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(addr.Bytes()) + addrHash := crypto.Keccak256Hash(addr.Bytes()) op := &accountDelete{ address: addr, origin: types.SlimAccountRLP(*prev), diff --git a/crypto/crypto.go b/crypto/crypto.go index 04dbedbfa4..813bb5a257 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -74,39 +74,41 @@ 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(data []byte) (h common.Hash) { - kh := hasherPool.Get().(KeccakState) +func HashData(kh KeccakState, data []byte) (h common.Hash) { kh.Reset() kh.Write(data) kh.Read(h[:]) - hasherPool.Put(kh) return h } +var hasherPool = sync.Pool{ + New: func() interface{} { return NewKeccakState() }, +} + // Keccak256 calculates and returns the Keccak256 hash of the input data. func Keccak256(data ...[]byte) []byte { b := make([]byte, 32) - d := NewKeccakState() + d := hasherPool.Get().(KeccakState) + d.Reset() for _, b := range data { d.Write(b) } d.Read(b) + hasherPool.Put(d) return b } // Keccak256Hash calculates and returns the Keccak256 hash of the input data, // converting it to an internal Hash data structure. func Keccak256Hash(data ...[]byte) (h common.Hash) { - d := NewKeccakState() + d := hasherPool.Get().(KeccakState) + d.Reset() for _, b := range data { d.Write(b) } d.Read(h[:]) + hasherPool.Put(d) return h } diff --git a/crypto/crypto_test.go b/crypto/crypto_test.go index 5318ed27e9..df75441072 100644 --- a/crypto/crypto_test.go +++ b/crypto/crypto_test.go @@ -44,7 +44,7 @@ func TestKeccak256Hash(t *testing.T) { func TestKeccak256Hasher(t *testing.T) { msg := []byte("abc") exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45") - checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := HashData(in); return h[:] }, msg, exp) + checkhash(t, "Sha3-256-array", func(in []byte) []byte { h := Keccak256Hash(in); return h[:] }, msg, exp) } func TestToECDSAErrors(t *testing.T) { diff --git a/trie/sync.go b/trie/sync.go index 633c665827..8d0ce6901c 100644 --- a/trie/sync.go +++ b/trie/sync.go @@ -729,7 +729,7 @@ func (s *Sync) hasNode(owner common.Hash, path []byte, hash common.Hash) (exists } else { blob = rawdb.ReadStorageTrieNode(s.database, owner, path) } - exists = hash == crypto.HashData(blob) + exists = hash == crypto.Keccak256Hash(blob) inconsistent = !exists && len(blob) != 0 return exists, inconsistent } diff --git a/trie/triestate/state.go b/trie/triestate/state.go index a16f4ab27d..7a1e869b8d 100644 --- a/trie/triestate/state.go +++ b/trie/triestate/state.go @@ -138,7 +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 { - addrHash := crypto.HashData(addr.Bytes()) + addrHash := crypto.Keccak256Hash(addr.Bytes()) prev, err := types.FullAccount(ctx.accounts[addr]) if err != nil { return err @@ -195,7 +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. - addrHash := crypto.HashData(addr.Bytes()) + addrHash := crypto.Keccak256Hash(addr.Bytes()) blob, err := ctx.accountTrie.Get(addrHash.Bytes()) if err != nil { return err diff --git a/triedb/pathdb/disklayer.go b/triedb/pathdb/disklayer.go index e917bb85fe..6e150e8c47 100644 --- a/triedb/pathdb/disklayer.go +++ b/triedb/pathdb/disklayer.go @@ -120,7 +120,7 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co if blob := dl.cleans.Get(nil, key); len(blob) > 0 { cleanHitMeter.Mark(1) cleanReadMeter.Mark(int64(len(blob))) - return blob, crypto.HashData(blob), &nodeLoc{loc: locCleanCache, depth: depth}, nil + return blob, crypto.Keccak256Hash(blob), &nodeLoc{loc: locCleanCache, depth: depth}, nil } cleanMissMeter.Mark(1) } @@ -136,7 +136,7 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co cleanWriteMeter.Mark(int64(len(blob))) } - return blob, crypto.HashData(blob), &nodeLoc{loc: locDiskLayer, depth: depth}, nil + return blob, crypto.Keccak256Hash(blob), &nodeLoc{loc: locDiskLayer, depth: depth}, nil } // update implements the layer interface, returning a new diff layer on top