|
| 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