crypto: do not use hasher pool for NewKeccakState

This commit is contained in:
MariusVanDerWijden 2025-05-08 11:53:10 +02:00 committed by Gary Rong
parent 80ccc0e322
commit c84f999c9e
10 changed files with 7 additions and 23 deletions

View file

@ -645,7 +645,6 @@ func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashScheme bool,
buff = crypto.NewKeccakState() buff = crypto.NewKeccakState()
startTime = time.Now() startTime = time.Now()
) )
defer crypto.ReturnKeccakState(buff)
batch := db.NewBatch() batch := db.NewBatch()
it := db.NewIterator(nil, start) it := db.NewIterator(nil, start)

View file

@ -386,7 +386,6 @@ func (s *stateObject) commitStorage(op *accountUpdate) {
return blob return blob
} }
) )
defer crypto.ReturnKeccakState(buf)
for key, val := range s.pendingStorage { for key, val := range s.pendingStorage {
// Skip the noop storage changes, it might be possible the value // Skip the noop storage changes, it might be possible the value
// of tracked slot is same in originStorage and pendingStorage // of tracked slot is same in originStorage and pendingStorage

View file

@ -1067,7 +1067,6 @@ func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*acco
buf = crypto.NewKeccakState() buf = crypto.NewKeccakState()
deletes = make(map[common.Hash]*accountDelete) deletes = make(map[common.Hash]*accountDelete)
) )
defer crypto.ReturnKeccakState(buf)
for addr, prevObj := range s.stateObjectsDestruct { for addr, prevObj := range s.stateObjectsDestruct {
prev := prevObj.origin prev := prevObj.origin

View file

@ -141,7 +141,6 @@ func Bloom9(data []byte) []byte {
// bloomValues returns the bytes (index-value pairs) to set for the given data // bloomValues returns the bytes (index-value pairs) to set for the given data
func bloomValues(data []byte, hashbuf []byte) (uint, byte, uint, byte, uint, byte) { func bloomValues(data []byte, hashbuf []byte) (uint, byte, uint, byte, uint, byte) {
sha := crypto.NewKeccakState() sha := crypto.NewKeccakState()
defer crypto.ReturnKeccakState(sha)
sha.Reset() sha.Reset()
sha.Write(data) sha.Write(data)
sha.Read(hashbuf) sha.Read(hashbuf)

View file

@ -51,7 +51,6 @@ func getPooledBuffer(size uint64) ([]byte, *bytes.Buffer, error) {
// rlpHash encodes x and hashes the encoded bytes. // rlpHash encodes x and hashes the encoded bytes.
func rlpHash(x interface{}) (h common.Hash) { func rlpHash(x interface{}) (h common.Hash) {
sha := crypto.NewKeccakState() sha := crypto.NewKeccakState()
defer crypto.ReturnKeccakState(sha)
sha.Reset() sha.Reset()
rlp.Encode(sha, x) rlp.Encode(sha, x)
sha.Read(h[:]) sha.Read(h[:])
@ -62,7 +61,6 @@ func rlpHash(x interface{}) (h common.Hash) {
// It's used for typed transactions. // It's used for typed transactions.
func prefixedRlpHash(prefix byte, x interface{}) (h common.Hash) { func prefixedRlpHash(prefix byte, x interface{}) (h common.Hash) {
sha := crypto.NewKeccakState() sha := crypto.NewKeccakState()
defer crypto.ReturnKeccakState(sha)
sha.Reset() sha.Reset()
sha.Write([]byte{prefix}) sha.Write([]byte{prefix})
rlp.Encode(sha, x) rlp.Encode(sha, x)

View file

@ -71,9 +71,7 @@ type KeccakState interface {
// NewKeccakState creates a new KeccakState // NewKeccakState creates a new KeccakState
func NewKeccakState() KeccakState { func NewKeccakState() KeccakState {
state := hasherPool.Get().(KeccakState) return sha3.NewLegacyKeccak256().(KeccakState)
state.Reset()
return state
} }
var hasherPool = sync.Pool{ var hasherPool = sync.Pool{
@ -82,10 +80,6 @@ var hasherPool = sync.Pool{
}, },
} }
func ReturnKeccakState(state KeccakState) {
hasherPool.Put(state)
}
// 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(kh KeccakState, data []byte) (h common.Hash) {
kh.Reset() kh.Reset()
@ -97,24 +91,26 @@ func HashData(kh KeccakState, data []byte) (h common.Hash) {
// Keccak256 calculates and returns the Keccak256 hash of the input data. // Keccak256 calculates and returns the Keccak256 hash of the input data.
func Keccak256(data ...[]byte) []byte { func Keccak256(data ...[]byte) []byte {
b := make([]byte, 32) b := make([]byte, 32)
d := NewKeccakState() d := hasherPool.Get().(KeccakState)
d.Reset()
for _, b := range data { for _, b := range data {
d.Write(b) d.Write(b)
} }
d.Read(b) d.Read(b)
ReturnKeccakState(d) hasherPool.Put(d)
return b return b
} }
// Keccak256Hash calculates and returns the Keccak256 hash of the input data, // Keccak256Hash calculates and returns the Keccak256 hash of the input data,
// converting it to an internal Hash data structure. // converting it to an internal Hash data structure.
func Keccak256Hash(data ...[]byte) (h common.Hash) { func Keccak256Hash(data ...[]byte) (h common.Hash) {
d := NewKeccakState() d := hasherPool.Get().(KeccakState)
d.Reset()
for _, b := range data { for _, b := range data {
d.Write(b) d.Write(b)
} }
d.Read(h[:]) d.Read(h[:])
ReturnKeccakState(d) hasherPool.Put(d)
return h return h
} }

View file

@ -488,7 +488,6 @@ func (h *handler) BroadcastTransactions(txs types.Transactions) {
hasher = crypto.NewKeccakState() hasher = crypto.NewKeccakState()
hash = make([]byte, 32) hash = make([]byte, 32)
) )
defer crypto.ReturnKeccakState(hasher)
for _, tx := range txs { for _, tx := range txs {
var maybeDirect bool var maybeDirect bool
switch { switch {

View file

@ -2655,7 +2655,6 @@ func (s *Syncer) onByteCodes(peer SyncPeer, id uint64, bytecodes [][]byte) error
// Cross reference the requested bytecodes with the response to find gaps // Cross reference the requested bytecodes with the response to find gaps
// that the serving node is missing // that the serving node is missing
hasher := crypto.NewKeccakState() hasher := crypto.NewKeccakState()
defer crypto.ReturnKeccakState(hasher)
hash := make([]byte, 32) hash := make([]byte, 32)
codes := make([][]byte, len(req.hashes)) codes := make([][]byte, len(req.hashes))
@ -2908,7 +2907,6 @@ func (s *Syncer) OnTrieNodes(peer SyncPeer, id uint64, trienodes [][]byte) error
nodes = make([][]byte, len(req.hashes)) nodes = make([][]byte, len(req.hashes))
fills uint64 fills uint64
) )
defer crypto.ReturnKeccakState(hasher)
for i, j := 0, 0; i < len(trienodes); i++ { for i, j := 0, 0; i < len(trienodes); i++ {
// Find the next hash that we've been served, leaving misses with nils // Find the next hash that we've been served, leaving misses with nils
hasher.Reset() hasher.Reset()

View file

@ -87,7 +87,6 @@ func updateAccount(ctx *context, db database.NodeDatabase, addr common.Address)
// The account was present in prev-state, decode it from the // The account was present in prev-state, decode it from the
// 'slim-rlp' format bytes. // 'slim-rlp' format bytes.
h := crypto.NewKeccakState() h := crypto.NewKeccakState()
defer crypto.ReturnKeccakState(h)
addrHash := crypto.HashData(h, addr.Bytes()) addrHash := crypto.HashData(h, addr.Bytes())
prev, err := types.FullAccount(ctx.accounts[addr]) prev, err := types.FullAccount(ctx.accounts[addr])
@ -151,7 +150,6 @@ func updateAccount(ctx *context, db database.NodeDatabase, addr common.Address)
func deleteAccount(ctx *context, db database.NodeDatabase, addr common.Address) error { func deleteAccount(ctx *context, db database.NodeDatabase, 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 := crypto.NewKeccakState() h := crypto.NewKeccakState()
defer crypto.ReturnKeccakState(h)
addrHash := crypto.HashData(h, addr.Bytes()) addrHash := crypto.HashData(h, addr.Bytes())
blob, err := ctx.accountTrie.Get(addrHash.Bytes()) blob, err := ctx.accountTrie.Get(addrHash.Bytes())

View file

@ -282,7 +282,6 @@ func (h *history) stateSet() (map[common.Hash][]byte, map[common.Hash]map[common
accounts = make(map[common.Hash][]byte) accounts = make(map[common.Hash][]byte)
storages = make(map[common.Hash]map[common.Hash][]byte) storages = make(map[common.Hash]map[common.Hash][]byte)
) )
defer crypto.ReturnKeccakState(buff)
for addr, blob := range h.accounts { for addr, blob := range h.accounts {
addrHash := crypto.HashData(buff, addr.Bytes()) addrHash := crypto.HashData(buff, addr.Bytes())
accounts[addrHash] = blob accounts[addrHash] = blob