cmd: use stateless keccak

This commit is contained in:
Piotr Mikołajczyk 2025-11-18 11:16:08 +01:00
parent 4c753498b2
commit 5c631f6206
No known key found for this signature in database
GPG key ID: 2E4C2AAD5E71D22D
4 changed files with 7 additions and 26 deletions

View file

@ -900,16 +900,12 @@ func (s *Suite) snapGetByteCodes(t *utesting.T, tc *byteCodesTest) error {
// that the serving node is missing // that the serving node is missing
var ( var (
bytecodes = res.Codes bytecodes = res.Codes
hasher = crypto.NewKeccakState()
hash = make([]byte, 32)
codes = make([][]byte, len(req.Hashes)) codes = make([][]byte, len(req.Hashes))
) )
for i, j := 0, 0; i < len(bytecodes); i++ { for i, j := 0, 0; i < len(bytecodes); i++ {
// Find the next hash that we've been served, leaving misses with nils // Find the next hash that we've been served, leaving misses with nils
hasher.Reset() hash := crypto.Keccak256(bytecodes[i])
hasher.Write(bytecodes[i])
hasher.Read(hash)
for j < len(req.Hashes) && !bytes.Equal(hash, req.Hashes[j][:]) { for j < len(req.Hashes) && !bytes.Equal(hash, req.Hashes[j][:]) {
j++ j++
@ -959,16 +955,12 @@ func (s *Suite) snapGetTrieNodes(t *utesting.T, tc *trieNodesTest) error {
// Cross reference the requested trienodes with the response to find gaps // Cross reference the requested trienodes with the response to find gaps
// that the serving node is missing // that the serving node is missing
hasher := crypto.NewKeccakState()
hash := make([]byte, 32)
trienodes := res.Nodes trienodes := res.Nodes
if got, want := len(trienodes), len(tc.expHashes); got != want { if got, want := len(trienodes), len(tc.expHashes); got != want {
return fmt.Errorf("wrong trienode count, got %d, want %d", got, want) return fmt.Errorf("wrong trienode count, got %d, want %d", got, want)
} }
for i, trienode := range trienodes { for i, trienode := range trienodes {
hasher.Reset() hash := crypto.Keccak256(trienode[:])
hasher.Write(trienode)
hasher.Read(hash)
if got, want := hash, tc.expHashes[i]; !bytes.Equal(got, want[:]) { if got, want := hash, tc.expHashes[i]; !bytes.Equal(got, want[:]) {
t.Logf(" hash %d wrong, got %#x, want %#x\n", i, got, want) t.Logf(" hash %d wrong, got %#x, want %#x\n", i, got, want)
err = fmt.Errorf("hash %d wrong, got %#x, want %#x", i, got, want) err = fmt.Errorf("hash %d wrong, got %#x, want %#x", i, got, want)

View file

@ -354,8 +354,6 @@ func checkStateContent(ctx *cli.Context) error {
defer db.Close() defer db.Close()
var ( var (
it = rawdb.NewKeyLengthIterator(db.NewIterator(prefix, start), 32) it = rawdb.NewKeyLengthIterator(db.NewIterator(prefix, start), 32)
hasher = crypto.NewKeccakState()
got = make([]byte, 32)
errs int errs int
count int count int
startTime = time.Now() startTime = time.Now()
@ -365,9 +363,7 @@ func checkStateContent(ctx *cli.Context) error {
count++ count++
k := it.Key() k := it.Key()
v := it.Value() v := it.Value()
hasher.Reset() got := crypto.Keccak256(v)
hasher.Write(v)
hasher.Read(got)
if !bytes.Equal(k, got) { if !bytes.Equal(k, got) {
errs++ errs++
fmt.Printf("Error at %#x\n", k) fmt.Printf("Error at %#x\n", k)

View file

@ -430,8 +430,6 @@ func traverseRawState(ctx *cli.Context) error {
codes int codes int
lastReport time.Time lastReport time.Time
start = time.Now() start = time.Now()
hasher = crypto.NewKeccakState()
got = make([]byte, 32)
) )
accIter, err := t.NodeIterator(nil) accIter, err := t.NodeIterator(nil)
if err != nil { if err != nil {
@ -455,9 +453,7 @@ func traverseRawState(ctx *cli.Context) error {
log.Error("Missing trie node(account)", "hash", node) log.Error("Missing trie node(account)", "hash", node)
return errors.New("missing account") return errors.New("missing account")
} }
hasher.Reset() got := crypto.Keccak256(blob)
hasher.Write(blob)
hasher.Read(got)
if !bytes.Equal(got, node.Bytes()) { if !bytes.Equal(got, node.Bytes()) {
log.Error("Invalid trie node(account)", "hash", node.Hex(), "value", blob) log.Error("Invalid trie node(account)", "hash", node.Hex(), "value", blob)
return errors.New("invalid account node") return errors.New("invalid account node")
@ -496,9 +492,7 @@ func traverseRawState(ctx *cli.Context) error {
log.Error("Missing trie node(storage)", "hash", node) log.Error("Missing trie node(storage)", "hash", node)
return errors.New("missing storage") return errors.New("missing storage")
} }
hasher.Reset() got := crypto.Keccak256(blob)
hasher.Write(blob)
hasher.Read(got)
if !bytes.Equal(got, node.Bytes()) { if !bytes.Equal(got, node.Bytes()) {
log.Error("Invalid trie node(storage)", "hash", node.Hex(), "value", blob) log.Error("Invalid trie node(storage)", "hash", node.Hex(), "value", blob)
return errors.New("invalid storage node") return errors.New("invalid storage node")

View file

@ -132,9 +132,8 @@ func generateHistoryTests(clictx *cli.Context) error {
} }
func calcReceiptsHash(rcpt []*types.Receipt) common.Hash { func calcReceiptsHash(rcpt []*types.Receipt) common.Hash {
h := crypto.NewKeccakState() encoded, _ := rlp.EncodeToBytes(rcpt)
rlp.Encode(h, rcpt) return crypto.Keccak256Hash(encoded)
return common.Hash(h.Sum(nil))
} }
func writeJSON(fileName string, value any) { func writeJSON(fileName string, value any) {