github.com/ethereum/go-ethereum@v1.16.1/eth/tracers/internal/tracetest/prestate_test.go (about) 1 // Copyright 2022 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 tracetest 18 19 import ( 20 "encoding/json" 21 "math/big" 22 "os" 23 "path/filepath" 24 "strings" 25 "testing" 26 27 "github.com/ethereum/go-ethereum/common" 28 "github.com/ethereum/go-ethereum/core" 29 "github.com/ethereum/go-ethereum/core/rawdb" 30 "github.com/ethereum/go-ethereum/core/types" 31 "github.com/ethereum/go-ethereum/core/vm" 32 "github.com/ethereum/go-ethereum/eth/tracers" 33 "github.com/ethereum/go-ethereum/tests" 34 ) 35 36 // prestateTrace is the result of a prestateTrace run. 37 type prestateTrace = map[common.Address]*account 38 39 type account struct { 40 Balance string `json:"balance"` 41 Code string `json:"code"` 42 Nonce uint64 `json:"nonce"` 43 Storage map[common.Hash]common.Hash `json:"storage"` 44 } 45 46 // prestateTracerTest defines a single test to check the stateDiff tracer against. 47 type prestateTracerTest struct { 48 tracerTestEnv 49 Result interface{} `json:"result"` 50 } 51 52 func TestPrestateTracerLegacy(t *testing.T) { 53 testPrestateTracer("prestateTracerLegacy", "prestate_tracer_legacy", t) 54 } 55 56 func TestPrestateTracer(t *testing.T) { 57 testPrestateTracer("prestateTracer", "prestate_tracer", t) 58 } 59 60 func TestPrestateWithDiffModeTracer(t *testing.T) { 61 testPrestateTracer("prestateTracer", "prestate_tracer_with_diff_mode", t) 62 } 63 64 func testPrestateTracer(tracerName string, dirPath string, t *testing.T) { 65 files, err := os.ReadDir(filepath.Join("testdata", dirPath)) 66 if err != nil { 67 t.Fatalf("failed to retrieve tracer test suite: %v", err) 68 } 69 for _, file := range files { 70 if !strings.HasSuffix(file.Name(), ".json") { 71 continue 72 } 73 t.Run(camel(strings.TrimSuffix(file.Name(), ".json")), func(t *testing.T) { 74 t.Parallel() 75 76 var ( 77 test = new(prestateTracerTest) 78 tx = new(types.Transaction) 79 ) 80 // Call tracer test found, read if from disk 81 if blob, err := os.ReadFile(filepath.Join("testdata", dirPath, file.Name())); err != nil { 82 t.Fatalf("failed to read testcase: %v", err) 83 } else if err := json.Unmarshal(blob, test); err != nil { 84 t.Fatalf("failed to parse testcase: %v", err) 85 } 86 if err := tx.UnmarshalBinary(common.FromHex(test.Input)); err != nil { 87 t.Fatalf("failed to parse testcase input: %v", err) 88 } 89 // Configure a blockchain with the given prestate 90 var ( 91 signer = types.MakeSigner(test.Genesis.Config, new(big.Int).SetUint64(uint64(test.Context.Number)), uint64(test.Context.Time)) 92 context = test.Context.toBlockContext(test.Genesis) 93 state = tests.MakePreState(rawdb.NewMemoryDatabase(), test.Genesis.Alloc, false, rawdb.HashScheme) 94 ) 95 defer state.Close() 96 97 tracer, err := tracers.DefaultDirectory.New(tracerName, new(tracers.Context), test.TracerConfig, test.Genesis.Config) 98 if err != nil { 99 t.Fatalf("failed to create call tracer: %v", err) 100 } 101 102 msg, err := core.TransactionToMessage(tx, signer, context.BaseFee) 103 if err != nil { 104 t.Fatalf("failed to prepare transaction for tracing: %v", err) 105 } 106 evm := vm.NewEVM(context, state.StateDB, test.Genesis.Config, vm.Config{Tracer: tracer.Hooks}) 107 tracer.OnTxStart(evm.GetVMContext(), tx, msg.From) 108 vmRet, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(tx.Gas())) 109 if err != nil { 110 t.Fatalf("failed to execute transaction: %v", err) 111 } 112 tracer.OnTxEnd(&types.Receipt{GasUsed: vmRet.UsedGas}, nil) 113 // Retrieve the trace result and compare against the expected 114 res, err := tracer.GetResult() 115 if err != nil { 116 t.Fatalf("failed to retrieve trace result: %v", err) 117 } 118 // The legacy javascript calltracer marshals json in js, which 119 // is not deterministic (as opposed to the golang json encoder). 120 if strings.HasSuffix(dirPath, "_legacy") { 121 // This is a tweak to make it deterministic. Can be removed when 122 // we remove the legacy tracer. 123 var x prestateTrace 124 json.Unmarshal(res, &x) 125 res, _ = json.Marshal(x) 126 } 127 want, err := json.Marshal(test.Result) 128 if err != nil { 129 t.Fatalf("failed to marshal test: %v", err) 130 } 131 if string(want) != string(res) { 132 t.Fatalf("trace mismatch\n have: %v\n want: %v\n", string(res), string(want)) 133 } 134 }) 135 } 136 }