Skip to content

Commit 4bb5c6c

Browse files
holimankaralabe
andauthored
eth/protocols/snap: speed up hash checks (#22023)
* eth/protocols/snap: speed up hash checks * eth/protocols/snap: nit fix Co-authored-by: Péter Szilágyi <peterke@gmail.com>
1 parent 38310f9 commit 4bb5c6c

2 files changed

Lines changed: 107 additions & 6 deletions

File tree

‎eth/protocols/snap/sync.go‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2112,14 +2112,15 @@ func (s *Syncer) onByteCodes(peer *Peer, id uint64, bytecodes [][]byte) error {
21122112

21132113
// Cross reference the requested bytecodes with the response to find gaps
21142114
// that the serving node is missing
2115-
hasher := sha3.NewLegacyKeccak256()
2115+
hasher := sha3.NewLegacyKeccak256().(crypto.KeccakState)
2116+
hash := make([]byte, 32)
21162117

21172118
codes := make([][]byte, len(req.hashes))
21182119
for i, j := 0, 0; i < len(bytecodes); i++ {
21192120
// Find the next hash that we've been served, leaving misses with nils
21202121
hasher.Reset()
21212122
hasher.Write(bytecodes[i])
2122-
hash := hasher.Sum(nil)
2123+
hasher.Read(hash)
21232124

21242125
for j < len(req.hashes) && !bytes.Equal(hash, req.hashes[j][:]) {
21252126
j++
@@ -2350,14 +2351,15 @@ func (s *Syncer) OnTrieNodes(peer *Peer, id uint64, trienodes [][]byte) error {
23502351

23512352
// Cross reference the requested trienodes with the response to find gaps
23522353
// that the serving node is missing
2353-
hasher := sha3.NewLegacyKeccak256()
2354+
hasher := sha3.NewLegacyKeccak256().(crypto.KeccakState)
2355+
hash := make([]byte, 32)
23542356

23552357
nodes := make([][]byte, len(req.hashes))
23562358
for i, j := 0, 0; i < len(trienodes); i++ {
23572359
// Find the next hash that we've been served, leaving misses with nils
23582360
hasher.Reset()
23592361
hasher.Write(trienodes[i])
2360-
hash := hasher.Sum(nil)
2362+
hasher.Read(hash)
23612363

23622364
for j < len(req.hashes) && !bytes.Equal(hash, req.hashes[j][:]) {
23632365
j++
@@ -2437,14 +2439,15 @@ func (s *Syncer) onHealByteCodes(peer *Peer, id uint64, bytecodes [][]byte) erro
24372439

24382440
// Cross reference the requested bytecodes with the response to find gaps
24392441
// that the serving node is missing
2440-
hasher := sha3.NewLegacyKeccak256()
2442+
hasher := sha3.NewLegacyKeccak256().(crypto.KeccakState)
2443+
hash := make([]byte, 32)
24412444

24422445
codes := make([][]byte, len(req.hashes))
24432446
for i, j := 0, 0; i < len(bytecodes); i++ {
24442447
// Find the next hash that we've been served, leaving misses with nils
24452448
hasher.Reset()
24462449
hasher.Write(bytecodes[i])
2447-
hash := hasher.Sum(nil)
2450+
hasher.Read(hash)
24482451

24492452
for j < len(req.hashes) && !bytes.Equal(hash, req.hashes[j][:]) {
24502453
j++

‎eth/protocols/snap/sync_test.go‎

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// Copyright 2020 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package snap
18+
19+
import (
20+
"crypto/rand"
21+
"fmt"
22+
"testing"
23+
24+
"github.com/ethereum/go-ethereum/crypto"
25+
"golang.org/x/crypto/sha3"
26+
)
27+
28+
func TestHashing(t *testing.T) {
29+
var bytecodes = make([][]byte, 10)
30+
for i := 0; i < len(bytecodes); i++ {
31+
buf := make([]byte, 100)
32+
rand.Read(buf)
33+
bytecodes[i] = buf
34+
}
35+
var want, got string
36+
var old = func() {
37+
hasher := sha3.NewLegacyKeccak256()
38+
for i := 0; i < len(bytecodes); i++ {
39+
hasher.Reset()
40+
hasher.Write(bytecodes[i])
41+
hash := hasher.Sum(nil)
42+
got = fmt.Sprintf("%v\n%v", got, hash)
43+
}
44+
}
45+
var new = func() {
46+
hasher := sha3.NewLegacyKeccak256().(crypto.KeccakState)
47+
var hash = make([]byte, 32)
48+
for i := 0; i < len(bytecodes); i++ {
49+
hasher.Reset()
50+
hasher.Write(bytecodes[i])
51+
hasher.Read(hash)
52+
want = fmt.Sprintf("%v\n%v", want, hash)
53+
}
54+
}
55+
old()
56+
new()
57+
if want != got {
58+
t.Errorf("want\n%v\ngot\n%v\n", want, got)
59+
}
60+
}
61+
62+
func BenchmarkHashing(b *testing.B) {
63+
var bytecodes = make([][]byte, 10000)
64+
for i := 0; i < len(bytecodes); i++ {
65+
buf := make([]byte, 100)
66+
rand.Read(buf)
67+
bytecodes[i] = buf
68+
}
69+
var old = func() {
70+
hasher := sha3.NewLegacyKeccak256()
71+
for i := 0; i < len(bytecodes); i++ {
72+
hasher.Reset()
73+
hasher.Write(bytecodes[i])
74+
hasher.Sum(nil)
75+
}
76+
}
77+
var new = func() {
78+
hasher := sha3.NewLegacyKeccak256().(crypto.KeccakState)
79+
var hash = make([]byte, 32)
80+
for i := 0; i < len(bytecodes); i++ {
81+
hasher.Reset()
82+
hasher.Write(bytecodes[i])
83+
hasher.Read(hash)
84+
}
85+
}
86+
b.Run("old", func(b *testing.B) {
87+
b.ReportAllocs()
88+
for i := 0; i < b.N; i++ {
89+
old()
90+
}
91+
})
92+
b.Run("new", func(b *testing.B) {
93+
b.ReportAllocs()
94+
for i := 0; i < b.N; i++ {
95+
new()
96+
}
97+
})
98+
}

0 commit comments

Comments
 (0)