From 1b0bf9608f81298066f0ce01d952f731058252df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Miko=C5=82ajczyk?= Date: Tue, 18 Nov 2025 11:23:28 +0100 Subject: [PATCH] eth: use stateless keccak --- eth/protocols/snap/sync.go | 25 +++++-------------------- eth/protocols/snap/sync_test.go | 12 ++---------- 2 files changed, 7 insertions(+), 30 deletions(-) diff --git a/eth/protocols/snap/sync.go b/eth/protocols/snap/sync.go index cf4e494645..0c919f8886 100644 --- a/eth/protocols/snap/sync.go +++ b/eth/protocols/snap/sync.go @@ -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++ } diff --git a/eth/protocols/snap/sync_test.go b/eth/protocols/snap/sync_test.go index 713b358ff8..175bade62d 100644 --- a/eth/protocols/snap/sync_test.go +++ b/eth/protocols/snap/sync_test.go @@ -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) {