|
| 1 | +// Copyright 2021 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/rlp" |
| 34 | + "github.com/ethereum/go-ethereum/tests" |
| 35 | +) |
| 36 | + |
| 37 | +// prestateTrace is the result of a prestateTrace run. |
| 38 | +type prestateTrace = map[common.Address]*account |
| 39 | +type account struct { |
| 40 | + Balance string `json:"balance,omitempty"` |
| 41 | + Nonce uint64 `json:"nonce,omitempty"` |
| 42 | + Code string `json:"code,omitempty"` |
| 43 | + Storage map[common.Hash]common.Hash `json:"storage,omitempty"` |
| 44 | +} |
| 45 | +type prePostStateTrace struct { |
| 46 | + Pre prestateTrace `json:"pre"` |
| 47 | + Post prestateTrace `json:"post"` |
| 48 | +} |
| 49 | + |
| 50 | +// prestateTraceTest defines a single test to check the stateDiff tracer against. |
| 51 | +type prestateTraceTest struct { |
| 52 | + Genesis *core.Genesis `json:"genesis"` |
| 53 | + Context *callContext `json:"context"` |
| 54 | + Input string `json:"input"` |
| 55 | + TracerConfig json.RawMessage `json:"tracerConfig"` |
| 56 | + Result interface{} `json:"result"` |
| 57 | +} |
| 58 | + |
| 59 | +func TestPrestateTracer(t *testing.T) { |
| 60 | + testPrestateDiffTracer("prestateTracer", "prestate_tracer", t, func() interface{} { return new(prestateTrace) }) |
| 61 | +} |
| 62 | + |
| 63 | +func TestPrestateWithDiffModeTracer(t *testing.T) { |
| 64 | + testPrestateDiffTracer("prestateTracer", "prestate_tracer_with_diff_mode", t, func() interface{} { return new(prePostStateTrace) }) |
| 65 | +} |
| 66 | + |
| 67 | +func testPrestateDiffTracer(tracerName string, dirPath string, t *testing.T, typeBuilder func() interface{}) { |
| 68 | + files, err := os.ReadDir(filepath.Join("testdata", dirPath)) |
| 69 | + if err != nil { |
| 70 | + t.Fatalf("failed to retrieve tracer test suite: %v", err) |
| 71 | + } |
| 72 | + for _, file := range files { |
| 73 | + if !strings.HasSuffix(file.Name(), ".json") { |
| 74 | + continue |
| 75 | + } |
| 76 | + file := file // capture range variable |
| 77 | + t.Run(camel(strings.TrimSuffix(file.Name(), ".json")), func(t *testing.T) { |
| 78 | + t.Parallel() |
| 79 | + |
| 80 | + var ( |
| 81 | + test = new(prestateTraceTest) |
| 82 | + tx = new(types.Transaction) |
| 83 | + ) |
| 84 | + // Call tracer test found, read if from disk |
| 85 | + if blob, err := os.ReadFile(filepath.Join("testdata", dirPath, file.Name())); err != nil { |
| 86 | + t.Fatalf("failed to read testcase: %v", err) |
| 87 | + } else if err := json.Unmarshal(blob, test); err != nil { |
| 88 | + t.Fatalf("failed to parse testcase: %v", err) |
| 89 | + } |
| 90 | + if err := rlp.DecodeBytes(common.FromHex(test.Input), tx); err != nil { |
| 91 | + t.Fatalf("failed to parse testcase input: %v", err) |
| 92 | + } |
| 93 | + // Configure a blockchain with the given prestate |
| 94 | + var ( |
| 95 | + signer = types.MakeSigner(test.Genesis.Config, new(big.Int).SetUint64(uint64(test.Context.Number))) |
| 96 | + origin, _ = signer.Sender(tx) |
| 97 | + txContext = vm.TxContext{ |
| 98 | + Origin: origin, |
| 99 | + GasPrice: tx.GasPrice(), |
| 100 | + } |
| 101 | + context = vm.BlockContext{ |
| 102 | + CanTransfer: core.CanTransfer, |
| 103 | + Transfer: core.Transfer, |
| 104 | + Coinbase: test.Context.Miner, |
| 105 | + BlockNumber: new(big.Int).SetUint64(uint64(test.Context.Number)), |
| 106 | + Time: new(big.Int).SetUint64(uint64(test.Context.Time)), |
| 107 | + Difficulty: (*big.Int)(test.Context.Difficulty), |
| 108 | + GasLimit: uint64(test.Context.GasLimit), |
| 109 | + } |
| 110 | + _, statedb = tests.MakePreState(rawdb.NewMemoryDatabase(), test.Genesis.Alloc, false) |
| 111 | + ) |
| 112 | + tracer, err := tracers.New(tracerName, new(tracers.Context), test.TracerConfig) |
| 113 | + if err != nil { |
| 114 | + t.Fatalf("failed to create call tracer: %v", err) |
| 115 | + } |
| 116 | + evm := vm.NewEVM(context, txContext, statedb, test.Genesis.Config, vm.Config{Debug: true, Tracer: tracer}) |
| 117 | + msg, err := tx.AsMessage(signer, nil) |
| 118 | + if err != nil { |
| 119 | + t.Fatalf("failed to prepare transaction for tracing: %v", err) |
| 120 | + } |
| 121 | + st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas())) |
| 122 | + if _, err = st.TransitionDb(); err != nil { |
| 123 | + t.Fatalf("failed to execute transaction: %v", err) |
| 124 | + } |
| 125 | + // Retrieve the trace result and compare against the etalon |
| 126 | + res, err := tracer.GetResult() |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("failed to retrieve trace result: %v", err) |
| 129 | + } |
| 130 | + ret := typeBuilder() |
| 131 | + if err := json.Unmarshal(res, ret); err != nil { |
| 132 | + t.Fatalf("failed to unmarshal trace result: %v", err) |
| 133 | + } |
| 134 | + |
| 135 | + if !jsonEqual(ret, test.Result, typeBuilder(), typeBuilder()) { |
| 136 | + // uncomment this for easier debugging |
| 137 | + // have, _ := json.MarshalIndent(ret, "", " ") |
| 138 | + // want, _ := json.MarshalIndent(test.Result, "", " ") |
| 139 | + // t.Fatalf("trace mismatch: \nhave %+v\nwant %+v", string(have), string(want)) |
| 140 | + t.Fatalf("trace mismatch: \nhave %+v\nwant %+v", ret, test.Result) |
| 141 | + } |
| 142 | + }) |
| 143 | + } |
| 144 | +} |
0 commit comments