github.com/juliankolbe/go-ethereum@v1.9.992/eth/tracers/tracer_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 "encoding/json" 21 "errors" 22 "math/big" 23 "testing" 24 "time" 25 26 "github.com/juliankolbe/go-ethereum/common" 27 "github.com/juliankolbe/go-ethereum/core/state" 28 "github.com/juliankolbe/go-ethereum/core/vm" 29 "github.com/juliankolbe/go-ethereum/params" 30 ) 31 32 type account struct{} 33 34 func (account) SubBalance(amount *big.Int) {} 35 func (account) AddBalance(amount *big.Int) {} 36 func (account) SetAddress(common.Address) {} 37 func (account) Value() *big.Int { return nil } 38 func (account) SetBalance(*big.Int) {} 39 func (account) SetNonce(uint64) {} 40 func (account) Balance() *big.Int { return nil } 41 func (account) Address() common.Address { return common.Address{} } 42 func (account) ReturnGas(*big.Int) {} 43 func (account) SetCode(common.Hash, []byte) {} 44 func (account) ForEachStorage(cb func(key, value common.Hash) bool) {} 45 46 type dummyStatedb struct { 47 state.StateDB 48 } 49 50 func (*dummyStatedb) GetRefund() uint64 { return 1337 } 51 52 type vmContext struct { 53 blockCtx vm.BlockContext 54 txCtx vm.TxContext 55 } 56 57 func testCtx() *vmContext { 58 return &vmContext{blockCtx: vm.BlockContext{BlockNumber: big.NewInt(1)}, txCtx: vm.TxContext{GasPrice: big.NewInt(100000)}} 59 } 60 61 func runTrace(tracer *Tracer, vmctx *vmContext) (json.RawMessage, error) { 62 env := vm.NewEVM(vmctx.blockCtx, vmctx.txCtx, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) 63 var ( 64 startGas uint64 = 10000 65 value = big.NewInt(0) 66 ) 67 contract := vm.NewContract(account{}, account{}, value, startGas) 68 contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} 69 70 tracer.CaptureStart(contract.Caller(), contract.Address(), false, []byte{}, startGas, value) 71 ret, err := env.Interpreter().Run(contract, []byte{}, false) 72 tracer.CaptureEnd(ret, startGas-contract.Gas, 1, err) 73 if err != nil { 74 return nil, err 75 } 76 return tracer.GetResult() 77 } 78 79 func TestTracer(t *testing.T) { 80 execTracer := func(code string) []byte { 81 t.Helper() 82 ctx := &vmContext{blockCtx: vm.BlockContext{BlockNumber: big.NewInt(1)}, txCtx: vm.TxContext{GasPrice: big.NewInt(100000)}} 83 tracer, err := New(code, ctx.txCtx) 84 if err != nil { 85 t.Fatal(err) 86 } 87 ret, err := runTrace(tracer, ctx) 88 if err != nil { 89 t.Fatal(err) 90 } 91 return ret 92 } 93 for i, tt := range []struct { 94 code string 95 want string 96 }{ 97 { // tests that we don't panic on bad arguments to memory access 98 code: "{depths: [], step: function(log) { this.depths.push(log.memory.slice(-1,-2)); }, fault: function() {}, result: function() { return this.depths; }}", 99 want: `[{},{},{}]`, 100 }, { // tests that we don't panic on bad arguments to stack peeks 101 code: "{depths: [], step: function(log) { this.depths.push(log.stack.peek(-1)); }, fault: function() {}, result: function() { return this.depths; }}", 102 want: `["0","0","0"]`, 103 }, { // tests that we don't panic on bad arguments to memory getUint 104 code: "{ depths: [], step: function(log, db) { this.depths.push(log.memory.getUint(-64));}, fault: function() {}, result: function() { return this.depths; }}", 105 want: `["0","0","0"]`, 106 }, { // tests some general counting 107 code: "{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}", 108 want: `3`, 109 }, { // tests that depth is reported correctly 110 code: "{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}", 111 want: `[0,1,2]`, 112 }, { // tests to-string of opcodes 113 code: "{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}", 114 want: `["PUSH1","PUSH1","STOP"]`, 115 }, { // tests intrinsic gas 116 code: "{depths: [], step: function() {}, fault: function() {}, result: function(ctx) { return ctx.gasPrice+'.'+ctx.gasUsed+'.'+ctx.intrinsicGas; }}", 117 want: `"100000.6.21000"`, 118 }, 119 } { 120 if have := execTracer(tt.code); tt.want != string(have) { 121 t.Errorf("testcase %d: expected return value to be %s got %s\n\tcode: %v", i, tt.want, string(have), tt.code) 122 } 123 } 124 } 125 126 func TestHalt(t *testing.T) { 127 t.Skip("duktape doesn't support abortion") 128 129 timeout := errors.New("stahp") 130 vmctx := testCtx() 131 tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}", vmctx.txCtx) 132 if err != nil { 133 t.Fatal(err) 134 } 135 136 go func() { 137 time.Sleep(1 * time.Second) 138 tracer.Stop(timeout) 139 }() 140 141 if _, err = runTrace(tracer, vmctx); err.Error() != "stahp in server-side tracer function 'step'" { 142 t.Errorf("Expected timeout error, got %v", err) 143 } 144 } 145 146 func TestHaltBetweenSteps(t *testing.T) { 147 vmctx := testCtx() 148 tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}", vmctx.txCtx) 149 if err != nil { 150 t.Fatal(err) 151 } 152 env := vm.NewEVM(vm.BlockContext{BlockNumber: big.NewInt(1)}, vm.TxContext{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) 153 contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0) 154 155 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, nil, nil, contract, 0, nil) 156 timeout := errors.New("stahp") 157 tracer.Stop(timeout) 158 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, nil, nil, contract, 0, nil) 159 160 if _, err := tracer.GetResult(); err.Error() != timeout.Error() { 161 t.Errorf("Expected timeout error, got %v", err) 162 } 163 }