unify the usage of hash pool

This commit is contained in:
maskpp 2024-06-17 20:33:16 +08:00
parent 115d154392
commit 277aa5b31a
8 changed files with 23 additions and 112 deletions

View file

@ -18,7 +18,6 @@ package rawdb
import ( import (
"fmt" "fmt"
"sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
@ -45,25 +44,6 @@ const HashScheme = "hash"
// on extra state diffs to survive deep reorg. // on extra state diffs to survive deep reorg.
const PathScheme = "path" 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. // ReadAccountTrieNode retrieves the account trie node with the specified node path.
func ReadAccountTrieNode(db ethdb.KeyValueReader, path []byte) []byte { func ReadAccountTrieNode(db ethdb.KeyValueReader, path []byte) []byte {
data, _ := db.Get(accountTrieNodeKey(path)) 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 { if len(blob) == 0 {
return false return false
} }
h := newHasher() return crypto.HashData(blob) == hash // exists but not match
defer h.release()
return h.hash(blob) == hash // exists but not match
default: default:
panic(fmt.Sprintf("Unknown scheme %v", scheme)) 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 { if len(blob) == 0 {
return nil return nil
} }
h := newHasher() if crypto.HashData(blob) != hash {
defer h.release()
if h.hash(blob) != hash {
return nil // exists but not match return nil // exists but not match
} }
return blob return blob

View file

@ -408,7 +408,8 @@ 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() hash common.Hash
hasher = 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
@ -425,7 +426,9 @@ func (s *stateObject) commitStorage(op *accountUpdate) {
if val == s.originStorage[key] { if val == s.originStorage[key] {
continue continue
} }
hash := crypto.HashData(buf, key[:]) hasher.Reset()
hasher.Write(key[:])
hasher.Read(hash[:])
if op.storages == nil { if op.storages == nil {
op.storages = make(map[common.Hash][]byte) op.storages = make(map[common.Hash][]byte)
} }

View file

@ -580,7 +580,7 @@ func (s *StateDB) getStateObject(addr common.Address) *stateObject {
var data *types.StateAccount var data *types.StateAccount
if s.snap != nil { if s.snap != nil {
start := time.Now() 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) s.SnapshotAccountReads += time.Since(start)
if err == nil { 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) { func (s *StateDB) handleDestruction() (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, prev := range s.stateObjectsDestruct { for addr, prev := range s.stateObjectsDestruct {
@ -1070,7 +1069,7 @@ func (s *StateDB) handleDestruction() (map[common.Hash]*accountDelete, []*trieno
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.HashData(addr.Bytes())
op := &accountDelete{ op := &accountDelete{
address: addr, address: addr,
origin: types.SlimAccountRLP(*prev), origin: types.SlimAccountRLP(*prev),

View file

@ -28,6 +28,7 @@ import (
"io" "io"
"math/big" "math/big"
"os" "os"
"sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/math" "github.com/ethereum/go-ethereum/common/math"
@ -73,11 +74,17 @@ func NewKeccakState() KeccakState {
return sha3.NewLegacyKeccak256().(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 // 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.Reset()
kh.Write(data) kh.Write(data)
kh.Read(h[:]) kh.Read(h[:])
hasherPool.Put(kh)
return h return h
} }

View file

@ -44,8 +44,7 @@ func TestKeccak256Hash(t *testing.T) {
func TestKeccak256Hasher(t *testing.T) { func TestKeccak256Hasher(t *testing.T) {
msg := []byte("abc") msg := []byte("abc")
exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45") exp, _ := hex.DecodeString("4e03657aea45a94fc7d47ba826c8d667c0d1e6e33a64a036ec44f58fa12d6c45")
hasher := NewKeccakState() 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 := HashData(hasher, in); return h[:] }, msg, exp)
} }
func TestToECDSAErrors(t *testing.T) { func TestToECDSAErrors(t *testing.T) {

View file

@ -729,9 +729,7 @@ func (s *Sync) hasNode(owner common.Hash, path []byte, hash common.Hash) (exists
} else { } else {
blob = rawdb.ReadStorageTrieNode(s.database, owner, path) blob = rawdb.ReadStorageTrieNode(s.database, owner, path)
} }
h := newBlobHasher() exists = hash == crypto.HashData(blob)
defer h.release()
exists = hash == h.hash(blob)
inconsistent = !exists && len(blob) != 0 inconsistent = !exists && len(blob) != 0
return exists, inconsistent return exists, inconsistent
} }
@ -746,23 +744,3 @@ func ResolvePath(path []byte) (common.Hash, []byte) {
} }
return owner, path 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)
}

View file

@ -19,7 +19,6 @@ package triestate
import ( import (
"errors" "errors"
"fmt" "fmt"
"sync"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "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 // existent in post-state. Apply the reverse diff and verify if the storage
// root matches the one in prev-state account. // root matches the one in prev-state account.
func updateAccount(ctx *context, loader TrieLoader, addr common.Address) error { func updateAccount(ctx *context, loader TrieLoader, addr common.Address) error {
// The account was present in prev-state, decode it from the addrHash := crypto.HashData(addr.Bytes())
// 'slim-rlp' format bytes.
h := newHasher()
defer h.release()
addrHash := h.hash(addr.Bytes())
prev, err := types.FullAccount(ctx.accounts[addr]) prev, err := types.FullAccount(ctx.accounts[addr])
if err != nil { if err != nil {
return err return err
@ -201,10 +195,7 @@ func updateAccount(ctx *context, loader TrieLoader, addr common.Address) error {
// account and storage is wiped out correctly. // account and storage is wiped out correctly.
func deleteAccount(ctx *context, loader TrieLoader, addr common.Address) error { func deleteAccount(ctx *context, loader TrieLoader, addr common.Address) error {
// The account must be existent in post-state, load the account. // The account must be existent in post-state, load the account.
h := newHasher() addrHash := crypto.HashData(addr.Bytes())
defer h.release()
addrHash := h.hash(addr.Bytes())
blob, err := ctx.accountTrie.Get(addrHash.Bytes()) blob, err := ctx.accountTrie.Get(addrHash.Bytes())
if err != nil { if err != nil {
return err 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. // Delete the post-state account from the main trie.
return ctx.accountTrie.Delete(addrHash.Bytes()) 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)
}

View file

@ -115,16 +115,12 @@ func (dl *diskLayer) node(owner common.Hash, path []byte, depth int) ([]byte, co
} }
dirtyMissMeter.Mark(1) dirtyMissMeter.Mark(1)
// Try to retrieve the trie node from the clean memory cache
h := newHasher()
defer h.release()
key := cacheKey(owner, path) key := cacheKey(owner, path)
if dl.cleans != nil { if dl.cleans != nil {
if blob := dl.cleans.Get(nil, key); len(blob) > 0 { if blob := dl.cleans.Get(nil, key); len(blob) > 0 {
cleanHitMeter.Mark(1) cleanHitMeter.Mark(1)
cleanReadMeter.Mark(int64(len(blob))) 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) 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))) 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 // update implements the layer interface, returning a new diff layer on top
@ -295,22 +291,3 @@ func (dl *diskLayer) resetCache() {
dl.cleans.Reset() 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)
}