Skip to content

Commit 2cc8dbc

Browse files
committed
cmd/workload, eth/tracers/native: introduce state proof tests
1 parent 479b803 commit 2cc8dbc

File tree

8 files changed

+488
-27
lines changed

8 files changed

+488
-27
lines changed

cmd/workload/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ the following commands (in this directory) against a synced mainnet node:
3434
> go run . filtergen --queries queries/filter_queries_mainnet.json http://host:8545
3535
> go run . historygen --history-tests queries/history_mainnet.json http://host:8545
3636
> go run . tracegen --trace-tests queries/trace_mainnet.json --trace-start 4000000 --trace-end 4000100 http://host:8545
37+
> go run . proofgen --proof-tests queries/proof_mainnet.json --proof-states 3000 http://host:8545
3738
```

cmd/workload/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func init() {
4848
historyGenerateCommand,
4949
filterGenerateCommand,
5050
traceGenerateCommand,
51+
proofGenerateCommand,
5152
filterPerfCommand,
5253
}
5354
}

cmd/workload/prooftest.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// Copyright 2025 The go-ethereum Authors
2+
// This file is part of go-ethereum.
3+
//
4+
// go-ethereum is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU 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+
// go-ethereum 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 General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU General Public License
15+
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package main
18+
19+
import (
20+
"context"
21+
"encoding/json"
22+
"fmt"
23+
"math/big"
24+
"os"
25+
"strings"
26+
27+
"github.com/ethereum/go-ethereum/common"
28+
"github.com/ethereum/go-ethereum/crypto"
29+
"github.com/ethereum/go-ethereum/internal/utesting"
30+
"github.com/urfave/cli/v2"
31+
)
32+
33+
// proofTest is the content of a state-proof test.
34+
type proofTest struct {
35+
BlockNumbers []uint64 `json:"blockNumbers"`
36+
Addresses [][]common.Address `json:"addresses"`
37+
StorageKeys [][][]string `json:"storageKeys"`
38+
Results [][]common.Hash `json:"results"`
39+
}
40+
41+
type proofTestSuite struct {
42+
cfg testConfig
43+
tests proofTest
44+
invalidDir string
45+
}
46+
47+
func newProofTestSuite(cfg testConfig, ctx *cli.Context) *proofTestSuite {
48+
s := &proofTestSuite{
49+
cfg: cfg,
50+
invalidDir: ctx.String(proofTestInvalidOutputFlag.Name),
51+
}
52+
if err := s.loadTests(); err != nil {
53+
exit(err)
54+
}
55+
return s
56+
}
57+
58+
func (s *proofTestSuite) loadTests() error {
59+
file, err := s.cfg.fsys.Open(s.cfg.proofTestFile)
60+
if err != nil {
61+
// If not found in embedded FS, try to load it from disk
62+
if !os.IsNotExist(err) {
63+
return err
64+
}
65+
file, err = os.OpenFile(s.cfg.proofTestFile, os.O_RDONLY, 0666)
66+
if err != nil {
67+
return fmt.Errorf("can't open proofTestFile: %v", err)
68+
}
69+
}
70+
defer file.Close()
71+
if err := json.NewDecoder(file).Decode(&s.tests); err != nil {
72+
return fmt.Errorf("invalid JSON in %s: %v", s.cfg.proofTestFile, err)
73+
}
74+
if len(s.tests.BlockNumbers) == 0 {
75+
return fmt.Errorf("proofTestFile %s has no test data", s.cfg.proofTestFile)
76+
}
77+
return nil
78+
}
79+
80+
func (s *proofTestSuite) allTests() []workloadTest {
81+
return []workloadTest{
82+
newArchiveWorkloadTest("Proof/GetProof", s.getProof),
83+
}
84+
}
85+
86+
func (s *proofTestSuite) getProof(t *utesting.T) {
87+
ctx := context.Background()
88+
for i, blockNumber := range s.tests.BlockNumbers {
89+
for j := 0; j < len(s.tests.Addresses[i]); j++ {
90+
res, err := s.cfg.client.Geth.GetProof(ctx, s.tests.Addresses[i][j], s.tests.StorageKeys[i][j], big.NewInt(int64(blockNumber)))
91+
if err != nil {
92+
t.Errorf("State proving fails, blockNumber: %d, address: %x, keys: %v, err: %v\n", blockNumber, s.tests.Addresses[i][j], strings.Join(s.tests.StorageKeys[i][j], " "), err)
93+
continue
94+
}
95+
blob, err := json.Marshal(res)
96+
if err != nil {
97+
t.Fatalf("State proving fails: error %v", err)
98+
continue
99+
}
100+
if crypto.Keccak256Hash(blob) != s.tests.Results[i][j] {
101+
t.Errorf("State proof mismatch, %d, number: %d, address: %x, keys: %v: invalid result", i, blockNumber, s.tests.Addresses[i][j], strings.Join(s.tests.StorageKeys[i][j], " "))
102+
}
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)