github.com/beyonderyue/gochain@v2.2.26+incompatible/eth/tracers/tracers_test.go (about) 1 // Copyright 2017 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 tracers 18 19 import ( 20 "context" 21 "encoding/json" 22 "io/ioutil" 23 "math/big" 24 "path/filepath" 25 "reflect" 26 "strings" 27 "testing" 28 29 "github.com/gochain-io/gochain/common" 30 "github.com/gochain-io/gochain/common/hexutil" 31 "github.com/gochain-io/gochain/common/math" 32 "github.com/gochain-io/gochain/core" 33 "github.com/gochain-io/gochain/core/types" 34 "github.com/gochain-io/gochain/core/vm" 35 "github.com/gochain-io/gochain/ethdb" 36 "github.com/gochain-io/gochain/rlp" 37 "github.com/gochain-io/gochain/tests" 38 ) 39 40 // To generate a new callTracer test, copy paste the makeTest method below into 41 // a GoChain console and call it with a transaction hash you which to export. 42 43 /* 44 // makeTest generates a callTracer test by running a prestate reassembled and a 45 // call trace run, assembling all the gathered information into a test case. 46 var makeTest = function(tx, rewind) { 47 // Generate the genesis block from the block, transaction and prestate data 48 var block = eth.getBlock(eth.getTransaction(tx).blockHash); 49 var genesis = eth.getBlock(block.parentHash); 50 51 delete genesis.gasUsed; 52 delete genesis.logsBloom; 53 delete genesis.parentHash; 54 delete genesis.receiptsRoot; 55 delete genesis.sha3Uncles; 56 delete genesis.size; 57 delete genesis.transactions; 58 delete genesis.transactionsRoot; 59 delete genesis.uncles; 60 61 genesis.gasLimit = genesis.gasLimit.toString(); 62 genesis.number = genesis.number.toString(); 63 genesis.timestamp = genesis.timestamp.toString(); 64 65 genesis.alloc = debug.traceTransaction(tx, {tracer: "prestateTracer", rewind: rewind}); 66 for (var key in genesis.alloc) { 67 genesis.alloc[key].nonce = genesis.alloc[key].nonce.toString(); 68 } 69 genesis.config = admin.nodeInfo.protocols.eth.config; 70 71 // Generate the call trace and produce the test input 72 var result = debug.traceTransaction(tx, {tracer: "callTracer", rewind: rewind}); 73 delete result.time; 74 75 console.log(JSON.stringify({ 76 genesis: genesis, 77 context: { 78 number: block.number.toString(), 79 difficulty: block.difficulty, 80 timestamp: block.timestamp.toString(), 81 gasLimit: block.gasLimit.toString(), 82 miner: block.miner, 83 }, 84 input: eth.getRawTransaction(tx), 85 result: result, 86 }, null, 2)); 87 } 88 */ 89 90 // callTrace is the result of a callTracer run. 91 type callTrace struct { 92 Type string `json:"type"` 93 From common.Address `json:"from"` 94 To common.Address `json:"to"` 95 Input hexutil.Bytes `json:"input"` 96 Output hexutil.Bytes `json:"output"` 97 Gas *hexutil.Uint64 `json:"gas,omitempty"` 98 GasUsed *hexutil.Uint64 `json:"gasUsed,omitempty"` 99 Value *hexutil.Big `json:"value,omitempty"` 100 Error string `json:"error,omitempty"` 101 Calls []callTrace `json:"calls,omitempty"` 102 } 103 104 type callContext struct { 105 Number math.HexOrDecimal64 `json:"number"` 106 Difficulty *math.HexOrDecimal256 `json:"difficulty"` 107 Time math.HexOrDecimal64 `json:"timestamp"` 108 GasLimit math.HexOrDecimal64 `json:"gasLimit"` 109 Miner common.Address `json:"miner"` 110 } 111 112 // callTracerTest defines a single test to check the call tracer against. 113 type callTracerTest struct { 114 Genesis *core.Genesis `json:"genesis"` 115 Context *callContext `json:"context"` 116 Input string `json:"input"` 117 Result *callTrace `json:"result"` 118 } 119 120 // Iterates over all the input-output datasets in the tracer test harness and 121 // runs the JavaScript tracers against them. 122 func TestCallTracer(t *testing.T) { 123 ctx := context.Background() 124 files, err := ioutil.ReadDir("testdata") 125 if err != nil { 126 t.Fatalf("failed to retrieve tracer test suite: %v", err) 127 } 128 for _, file := range files { 129 if !strings.HasPrefix(file.Name(), "call_tracer_") { 130 continue 131 } 132 file := file // capture range variable 133 t.Run(camel(strings.TrimSuffix(strings.TrimPrefix(file.Name(), "call_tracer_"), ".json")), func(t *testing.T) { 134 t.Parallel() 135 136 // Call tracer test found, read if from disk 137 blob, err := ioutil.ReadFile(filepath.Join("testdata", file.Name())) 138 if err != nil { 139 t.Fatalf("failed to read testcase: %v", err) 140 } 141 test := new(callTracerTest) 142 if err := json.Unmarshal(blob, test); err != nil { 143 t.Fatalf("failed to parse testcase: %v", err) 144 } 145 // Configure a blockchain with the given prestate 146 tx := new(types.Transaction) 147 if err := rlp.DecodeBytes(common.FromHex(test.Input), tx); err != nil { 148 t.Fatalf("failed to parse testcase input: %v", err) 149 } 150 signer := types.MakeSigner(test.Genesis.Config, new(big.Int).SetUint64(uint64(test.Context.Number))) 151 origin, _ := signer.Sender(tx) 152 153 context := vm.Context{ 154 CanTransfer: core.CanTransfer, 155 Transfer: core.Transfer, 156 Origin: origin, 157 Coinbase: test.Context.Miner, 158 BlockNumber: new(big.Int).SetUint64(uint64(test.Context.Number)), 159 Time: new(big.Int).SetUint64(uint64(test.Context.Time)), 160 Difficulty: (*big.Int)(test.Context.Difficulty), 161 GasLimit: uint64(test.Context.GasLimit), 162 GasPrice: tx.GasPrice(), 163 } 164 db := ethdb.NewMemDatabase() 165 statedb := tests.MakePreState(db, test.Genesis.Alloc) 166 167 // Create the tracer, the EVM environment and run it 168 tracer, err := New("callTracer") 169 if err != nil { 170 t.Fatalf("failed to create call tracer: %v", err) 171 } 172 evm := vm.NewEVM(context, statedb, test.Genesis.Config, vm.Config{Debug: true, Tracer: tracer}) 173 174 msg, err := tx.AsMessage(ctx, signer) 175 if err != nil { 176 t.Fatalf("failed to prepare transaction for tracing: %v", err) 177 } 178 st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas())) 179 if _, _, _, err = st.TransitionDb(); err != nil { 180 t.Fatalf("failed to execute transaction: %v", err) 181 } 182 // Retrieve the trace result and compare against the etalon 183 res, err := tracer.GetResult() 184 if err != nil { 185 t.Fatalf("failed to retrieve trace result: %v", err) 186 } 187 ret := new(callTrace) 188 if err := json.Unmarshal(res, ret); err != nil { 189 t.Fatalf("failed to unmarshal trace result: %v", err) 190 } 191 if !reflect.DeepEqual(ret, test.Result) { 192 t.Fatalf("trace mismatch: have %+v, want %+v", ret, test.Result) 193 } 194 }) 195 } 196 }