eth: use stateless keccak

This commit is contained in:
Piotr Mikołajczyk 2025-11-18 11:23:28 +01:00
parent bc9e3faed1
commit 1b0bf9608f
No known key found for this signature in database
GPG key ID: 2E4C2AAD5E71D22D
2 changed files with 7 additions and 30 deletions

View file

@ -2672,15 +2672,10 @@ 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()
hash := make([]byte, 32)
codes := make([][]byte, len(req.hashes))
for i, j := 0, 0; i < len(bytecodes); i++ {
// Find the next hash that we've been served, leaving misses with nils
hasher.Reset()
hasher.Write(bytecodes[i])
hasher.Read(hash)
hash := crypto.Keccak256(bytecodes[i])
for j < len(req.hashes) && !bytes.Equal(hash, req.hashes[j][:]) {
j++
@ -2920,16 +2915,12 @@ func (s *Syncer) OnTrieNodes(peer SyncPeer, id uint64, trienodes [][]byte) error
// Cross reference the requested trienodes with the response to find gaps
// that the serving node is missing
var (
hasher = crypto.NewKeccakState()
hash = make([]byte, 32)
nodes = make([][]byte, len(req.hashes))
fills uint64
nodes = make([][]byte, len(req.hashes))
fills uint64
)
for i, j := 0, 0; i < len(trienodes); i++ {
// Find the next hash that we've been served, leaving misses with nils
hasher.Reset()
hasher.Write(trienodes[i])
hasher.Read(hash)
hash := crypto.Keccak256(trienodes[i])
for j < len(req.hashes) && !bytes.Equal(hash, req.hashes[j][:]) {
j++
@ -3026,16 +3017,10 @@ func (s *Syncer) onHealByteCodes(peer SyncPeer, id uint64, bytecodes [][]byte) e
// Cross reference the requested bytecodes with the response to find gaps
// that the serving node is missing
hasher := crypto.NewKeccakState()
hash := make([]byte, 32)
codes := make([][]byte, len(req.hashes))
for i, j := 0, 0; i < len(bytecodes); i++ {
// Find the next hash that we've been served, leaving misses with nils
hasher.Reset()
hasher.Write(bytecodes[i])
hasher.Read(hash)
hash := crypto.Keccak256(bytecodes[i])
for j < len(req.hashes) && !bytes.Equal(hash, req.hashes[j][:]) {
j++
}

View file

@ -64,12 +64,8 @@ func TestHashing(t *testing.T) {
}
}
var new = func() {
hasher := crypto.NewKeccakState()
var hash = make([]byte, 32)
for i := 0; i < len(bytecodes); i++ {
hasher.Reset()
hasher.Write(bytecodes[i])
hasher.Read(hash)
hash := crypto.Keccak256(bytecodes[i])
want = fmt.Sprintf("%v\n%v", want, hash)
}
}
@ -96,12 +92,8 @@ func BenchmarkHashing(b *testing.B) {
}
}
var new = func() {
hasher := crypto.NewKeccakState()
var hash = make([]byte, 32)
for i := 0; i < len(bytecodes); i++ {
hasher.Reset()
hasher.Write(bytecodes[i])
hasher.Read(hash)
crypto.Keccak256(bytecodes[i])
}
}
b.Run("old", func(b *testing.B) {