diff --git a/core/rawdb/database.go b/core/rawdb/database.go index 7fb78773bf..a03dbafb1f 100644 --- a/core/rawdb/database.go +++ b/core/rawdb/database.go @@ -645,7 +645,6 @@ func SafeDeleteRange(db ethdb.KeyValueStore, start, end []byte, hashScheme bool, buff = crypto.NewKeccakState() startTime = time.Now() ) - defer crypto.ReturnKeccakState(buff) batch := db.NewBatch() it := db.NewIterator(nil, start) diff --git a/core/state/state_object.go b/core/state/state_object.go index 76139766d9..a6979bd361 100644 --- a/core/state/state_object.go +++ b/core/state/state_object.go @@ -386,7 +386,6 @@ func (s *stateObject) commitStorage(op *accountUpdate) { return blob } ) - defer crypto.ReturnKeccakState(buf) for key, val := range s.pendingStorage { // Skip the noop storage changes, it might be possible the value // of tracked slot is same in originStorage and pendingStorage diff --git a/core/state/statedb.go b/core/state/statedb.go index 511292820f..9378cae7de 100644 --- a/core/state/statedb.go +++ b/core/state/statedb.go @@ -1067,7 +1067,6 @@ func (s *StateDB) handleDestruction(noStorageWiping bool) (map[common.Hash]*acco buf = crypto.NewKeccakState() deletes = make(map[common.Hash]*accountDelete) ) - defer crypto.ReturnKeccakState(buf) for addr, prevObj := range s.stateObjectsDestruct { prev := prevObj.origin diff --git a/core/types/bloom9.go b/core/types/bloom9.go index b8704d8fff..2c9d9763af 100644 --- a/core/types/bloom9.go +++ b/core/types/bloom9.go @@ -141,7 +141,6 @@ func Bloom9(data []byte) []byte { // 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) { sha := crypto.NewKeccakState() - defer crypto.ReturnKeccakState(sha) sha.Reset() sha.Write(data) sha.Read(hashbuf) diff --git a/core/types/hashing.go b/core/types/hashing.go index 540c5111fb..2e6fb2bf31 100644 --- a/core/types/hashing.go +++ b/core/types/hashing.go @@ -51,7 +51,6 @@ func getPooledBuffer(size uint64) ([]byte, *bytes.Buffer, error) { // rlpHash encodes x and hashes the encoded bytes. func rlpHash(x interface{}) (h common.Hash) { sha := crypto.NewKeccakState() - defer crypto.ReturnKeccakState(sha) sha.Reset() rlp.Encode(sha, x) sha.Read(h[:]) @@ -62,7 +61,6 @@ func rlpHash(x interface{}) (h common.Hash) { // It's used for typed transactions. func prefixedRlpHash(prefix byte, x interface{}) (h common.Hash) { sha := crypto.NewKeccakState() - defer crypto.ReturnKeccakState(sha) sha.Reset() sha.Write([]byte{prefix}) rlp.Encode(sha, x) diff --git a/crypto/crypto.go b/crypto/crypto.go index cd0af3f3e5..09596c05ce 100644 --- a/crypto/crypto.go +++ b/crypto/crypto.go @@ -71,9 +71,7 @@ type KeccakState interface { // NewKeccakState creates a new KeccakState func NewKeccakState() KeccakState { - state := hasherPool.Get().(KeccakState) - state.Reset() - return state + return sha3.NewLegacyKeccak256().(KeccakState) } 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 func HashData(kh KeccakState, data []byte) (h common.Hash) { 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. 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) - ReturnKeccakState(d) + 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[:]) - ReturnKeccakState(d) + hasherPool.Put(d) return h } diff --git a/eth/handler.go b/eth/handler.go index 44bac477cb..8283d7d02f 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -488,7 +488,6 @@ func (h *handler) BroadcastTransactions(txs types.Transactions) { hasher = crypto.NewKeccakState() hash = make([]byte, 32) ) - defer crypto.ReturnKeccakState(hasher) for _, tx := range txs { var maybeDirect bool switch { diff --git a/eth/protocols/snap/sync.go b/eth/protocols/snap/sync.go index e1b5ed79b8..9e079f540f 100644 --- a/eth/protocols/snap/sync.go +++ b/eth/protocols/snap/sync.go @@ -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 // that the serving node is missing hasher := crypto.NewKeccakState() - defer crypto.ReturnKeccakState(hasher) hash := make([]byte, 32) 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)) fills uint64 ) - defer crypto.ReturnKeccakState(hasher) for i, j := 0, 0; i < len(trienodes); i++ { // Find the next hash that we've been served, leaving misses with nils hasher.Reset() diff --git a/triedb/pathdb/execute.go b/triedb/pathdb/execute.go index f8cb484744..db1e679277 100644 --- a/triedb/pathdb/execute.go +++ b/triedb/pathdb/execute.go @@ -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 // 'slim-rlp' format bytes. h := crypto.NewKeccakState() - defer crypto.ReturnKeccakState(h) addrHash := crypto.HashData(h, addr.Bytes()) 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 { // The account must be existent in post-state, load the account. h := crypto.NewKeccakState() - defer crypto.ReturnKeccakState(h) addrHash := crypto.HashData(h, addr.Bytes()) blob, err := ctx.accountTrie.Get(addrHash.Bytes()) diff --git a/triedb/pathdb/history.go b/triedb/pathdb/history.go index 9eb1a08fc7..aed0296da5 100644 --- a/triedb/pathdb/history.go +++ b/triedb/pathdb/history.go @@ -282,7 +282,6 @@ func (h *history) stateSet() (map[common.Hash][]byte, map[common.Hash]map[common accounts = make(map[common.Hash][]byte) storages = make(map[common.Hash]map[common.Hash][]byte) ) - defer crypto.ReturnKeccakState(buff) for addr, blob := range h.accounts { addrHash := crypto.HashData(buff, addr.Bytes()) accounts[addrHash] = blob