Skip to content

Commit d36e9fa

Browse files
authored
feat(core): create global hasher pool ethereum#31769 (#2314)
Replace pooled and embedded Keccak state with stateless crypto helpers. This simplifies trie node hashing and EVM keccak handling while keeping focused package tests green.
1 parent 14a9b63 commit d36e9fa

3 files changed

Lines changed: 9 additions & 37 deletions

File tree

core/rawdb/accessors_trie.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ package rawdb
1818

1919
import (
2020
"fmt"
21-
"sync"
2221

2322
"github.com/XinFinOrg/XDPoSChain/common"
2423
"github.com/XinFinOrg/XDPoSChain/crypto"
@@ -45,25 +44,6 @@ const HashScheme = "hashScheme"
4544
// on extra state diffs to survive deep reorg.
4645
const PathScheme = "pathScheme"
4746

48-
// hasher is used to compute the sha256 hash of the provided data.
49-
type hasher struct{ sha crypto.KeccakState }
50-
51-
var hasherPool = sync.Pool{
52-
New: func() interface{} { return &hasher{sha: crypto.NewKeccakState()} },
53-
}
54-
55-
func newHasher() *hasher {
56-
return hasherPool.Get().(*hasher)
57-
}
58-
59-
func (h *hasher) hash(data []byte) common.Hash {
60-
return crypto.HashData(h.sha, data)
61-
}
62-
63-
func (h *hasher) release() {
64-
hasherPool.Put(h)
65-
}
66-
6747
// ReadAccountTrieNode retrieves the account trie node with the specified node path.
6848
func ReadAccountTrieNode(db ethdb.KeyValueReader, path []byte) []byte {
6949
data, _ := db.Get(accountTrieNodeKey(path))
@@ -170,9 +150,7 @@ func HasTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash c
170150
if len(blob) == 0 {
171151
return false
172152
}
173-
h := newHasher()
174-
defer h.release()
175-
return h.hash(blob) == hash // exists but not match
153+
return crypto.Keccak256Hash(blob) == hash // exist and match
176154
default:
177155
panic(fmt.Sprintf("Unknown scheme %v", scheme))
178156
}
@@ -194,10 +172,8 @@ func ReadTrieNode(db ethdb.KeyValueReader, owner common.Hash, path []byte, hash
194172
if len(blob) == 0 {
195173
return nil
196174
}
197-
h := newHasher()
198-
defer h.release()
199-
if h.hash(blob) != hash {
200-
return nil // exists but not match
175+
if crypto.Keccak256Hash(blob) != hash {
176+
return nil // exist but not match
201177
}
202178
return blob
203179
default:

core/vm/evm.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,6 @@ type EVM struct {
124124
// jumpDests stores results of JUMPDEST analysis.
125125
jumpDests JumpDestCache
126126

127-
hasher crypto.KeccakState // Keccak256 hasher instance shared across opcodes
128-
hasherBuf common.Hash // Keccak256 hasher result array shared across opcodes
129-
130127
readOnly bool // Whether to throw on stateful modifications
131128
returnData []byte // Last CALL's return data for subsequent reuse
132129
}
@@ -144,7 +141,6 @@ func NewEVM(blockCtx BlockContext, statedb StateDB, tradingStateDB *tradingstate
144141
chainConfig: chainConfig,
145142
chainRules: chainConfig.Rules(blockCtx.BlockNumber),
146143
jumpDests: newMapJumpDests(),
147-
hasher: crypto.NewKeccakState(),
148144
}
149145
evm.precompiles = activePrecompiledContracts(evm.chainRules)
150146

@@ -566,7 +562,8 @@ func (evm *EVM) Create(caller common.Address, code []byte, gas uint64, value *ui
566562
// The different between Create2 with Create is Create2 uses keccak256(0xff ++ msg.sender ++ salt ++ keccak256(init_code))[12:]
567563
// instead of the usual sender-and-nonce-hash as the address where the contract is initialized at.
568564
func (evm *EVM) Create2(caller common.Address, code []byte, gas uint64, endowment *uint256.Int, salt *uint256.Int) (ret []byte, contractAddr common.Address, leftOverGas uint64, err error) {
569-
contractAddr = crypto.CreateAddress2(caller, salt.Bytes32(), crypto.Keccak256(code))
565+
inithash := crypto.Keccak256Hash(code)
566+
contractAddr = crypto.CreateAddress2(caller, salt.Bytes32(), inithash[:])
570567
return evm.create(caller, code, gas, endowment, contractAddr, CREATE2)
571568
}
572569

core/vm/instructions.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/XinFinOrg/XDPoSChain/common"
2323
"github.com/XinFinOrg/XDPoSChain/core/tracing"
2424
"github.com/XinFinOrg/XDPoSChain/core/types"
25+
"github.com/XinFinOrg/XDPoSChain/crypto"
2526
"github.com/XinFinOrg/XDPoSChain/params"
2627
"github.com/holiman/uint256"
2728
)
@@ -233,14 +234,12 @@ func opKeccak256(pc *uint64, evm *EVM, scope *ScopeContext) ([]byte, error) {
233234
offset, size := scope.Stack.pop(), scope.Stack.peek()
234235
data := scope.Memory.GetPtr(offset.Uint64(), size.Uint64())
235236

236-
evm.hasher.Reset()
237-
evm.hasher.Write(data)
238-
evm.hasher.Read(evm.hasherBuf[:])
237+
hash := crypto.Keccak256Hash(data)
239238

240239
if evm.Config.EnablePreimageRecording {
241-
evm.StateDB.AddPreimage(evm.hasherBuf, data)
240+
evm.StateDB.AddPreimage(hash, data)
242241
}
243-
size.SetBytes(evm.hasherBuf[:])
242+
size.SetBytes(hash[:])
244243
return nil, nil
245244
}
246245

0 commit comments

Comments
 (0)