github.com/ethereum/go-ethereum@v1.16.1/cmd/workload/tracetest.go (about) 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 "os" 24 "path/filepath" 25 26 "github.com/ethereum/go-ethereum/common" 27 "github.com/ethereum/go-ethereum/crypto" 28 "github.com/ethereum/go-ethereum/eth/tracers" 29 "github.com/ethereum/go-ethereum/internal/utesting" 30 "github.com/ethereum/go-ethereum/log" 31 "github.com/urfave/cli/v2" 32 ) 33 34 // traceTest is the content of a history test. 35 type traceTest struct { 36 TxHashes []common.Hash `json:"txHashes"` 37 TraceConfigs []tracers.TraceConfig `json:"traceConfigs"` 38 ResultHashes []common.Hash `json:"resultHashes"` 39 } 40 41 type traceTestSuite struct { 42 cfg testConfig 43 tests traceTest 44 invalidDir string 45 } 46 47 func newTraceTestSuite(cfg testConfig, ctx *cli.Context) *traceTestSuite { 48 s := &traceTestSuite{ 49 cfg: cfg, 50 invalidDir: ctx.String(traceTestInvalidOutputFlag.Name), 51 } 52 if err := s.loadTests(); err != nil { 53 exit(err) 54 } 55 return s 56 } 57 58 func (s *traceTestSuite) loadTests() error { 59 file, err := s.cfg.fsys.Open(s.cfg.traceTestFile) 60 if err != nil { 61 return fmt.Errorf("can't open traceTestFile: %v", err) 62 } 63 defer file.Close() 64 65 if err := json.NewDecoder(file).Decode(&s.tests); err != nil { 66 return fmt.Errorf("invalid JSON in %s: %v", s.cfg.traceTestFile, err) 67 } 68 if len(s.tests.TxHashes) == 0 { 69 return fmt.Errorf("traceTestFile %s has no test data", s.cfg.traceTestFile) 70 } 71 return nil 72 } 73 74 func (s *traceTestSuite) allTests() []workloadTest { 75 return []workloadTest{ 76 newArchiveWorkloadTest("Trace/Transaction", s.traceTransaction), 77 } 78 } 79 80 // traceTransaction runs all transaction tracing tests 81 func (s *traceTestSuite) traceTransaction(t *utesting.T) { 82 ctx := context.Background() 83 84 for i, hash := range s.tests.TxHashes { 85 config := s.tests.TraceConfigs[i] 86 result, err := s.cfg.client.Geth.TraceTransaction(ctx, hash, &config) 87 if err != nil { 88 t.Fatalf("Transaction %d (hash %v): error %v", i, hash, err) 89 } 90 blob, err := json.Marshal(result) 91 if err != nil { 92 t.Fatalf("Transaction %d (hash %v): error %v", i, hash, err) 93 continue 94 } 95 if crypto.Keccak256Hash(blob) != s.tests.ResultHashes[i] { 96 t.Errorf("Transaction %d (hash %v): invalid result", i, hash) 97 98 writeInvalidTraceResult(s.invalidDir, hash, result) 99 } 100 } 101 } 102 103 func writeInvalidTraceResult(dir string, hash common.Hash, result any) { 104 if dir == "" { 105 return 106 } 107 err := os.MkdirAll(dir, os.ModePerm) 108 if err != nil { 109 log.Info("Failed to make output directory", "err", err) 110 return 111 } 112 name := filepath.Join(dir, "invalid"+"_"+hash.String()) 113 file, err := os.Create(name) 114 if err != nil { 115 exit(fmt.Errorf("error creating %s: %v", name, err)) 116 return 117 } 118 defer file.Close() 119 120 data, _ := json.MarshalIndent(result, "", " ") 121 _, err = file.Write(data) 122 if err != nil { 123 exit(fmt.Errorf("error writing %s: %v", name, err)) 124 return 125 } 126 }