github.com/dis-happy/go-ethereum@v1.8.21-0.20190106040728-7bd69dac7276/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 "bytes" 21 "encoding/json" 22 "errors" 23 "math/big" 24 "testing" 25 "time" 26 27 "github.com/dis-happy/go-ethereum/common" 28 "github.com/dis-happy/go-ethereum/core/state" 29 "github.com/dis-happy/go-ethereum/core/vm" 30 "github.com/dis-happy/go-ethereum/params" 31 ) 32 33 type account struct{} 34 35 func (account) SubBalance(amount *big.Int) {} 36 func (account) AddBalance(amount *big.Int) {} 37 func (account) SetAddress(common.Address) {} 38 func (account) Value() *big.Int { return nil } 39 func (account) SetBalance(*big.Int) {} 40 func (account) SetNonce(uint64) {} 41 func (account) Balance() *big.Int { return nil } 42 func (account) Address() common.Address { return common.Address{} } 43 func (account) ReturnGas(*big.Int) {} 44 func (account) SetCode(common.Hash, []byte) {} 45 func (account) ForEachStorage(cb func(key, value common.Hash) bool) {} 46 47 type dummyStatedb struct { 48 state.StateDB 49 } 50 51 func (*dummyStatedb) GetRefund() uint64 { return 1337 } 52 53 func runTrace(tracer *Tracer) (json.RawMessage, error) { 54 env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) 55 56 contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000) 57 contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} 58 59 _, err := env.Interpreter().Run(contract, []byte{}, false) 60 if err != nil { 61 return nil, err 62 } 63 return tracer.GetResult() 64 } 65 66 func TestTracing(t *testing.T) { 67 tracer, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}") 68 if err != nil { 69 t.Fatal(err) 70 } 71 72 ret, err := runTrace(tracer) 73 if err != nil { 74 t.Fatal(err) 75 } 76 if !bytes.Equal(ret, []byte("3")) { 77 t.Errorf("Expected return value to be 3, got %s", string(ret)) 78 } 79 } 80 81 func TestStack(t *testing.T) { 82 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}") 83 if err != nil { 84 t.Fatal(err) 85 } 86 87 ret, err := runTrace(tracer) 88 if err != nil { 89 t.Fatal(err) 90 } 91 if !bytes.Equal(ret, []byte("[0,1,2]")) { 92 t.Errorf("Expected return value to be [0,1,2], got %s", string(ret)) 93 } 94 } 95 96 func TestOpcodes(t *testing.T) { 97 tracer, err := New("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}") 98 if err != nil { 99 t.Fatal(err) 100 } 101 102 ret, err := runTrace(tracer) 103 if err != nil { 104 t.Fatal(err) 105 } 106 if !bytes.Equal(ret, []byte("[\"PUSH1\",\"PUSH1\",\"STOP\"]")) { 107 t.Errorf("Expected return value to be [\"PUSH1\",\"PUSH1\",\"STOP\"], got %s", string(ret)) 108 } 109 } 110 111 func TestHalt(t *testing.T) { 112 t.Skip("duktape doesn't support abortion") 113 114 timeout := errors.New("stahp") 115 tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}") 116 if err != nil { 117 t.Fatal(err) 118 } 119 120 go func() { 121 time.Sleep(1 * time.Second) 122 tracer.Stop(timeout) 123 }() 124 125 if _, err = runTrace(tracer); err.Error() != "stahp in server-side tracer function 'step'" { 126 t.Errorf("Expected timeout error, got %v", err) 127 } 128 } 129 130 func TestHaltBetweenSteps(t *testing.T) { 131 tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}") 132 if err != nil { 133 t.Fatal(err) 134 } 135 136 env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) 137 contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0) 138 139 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) 140 timeout := errors.New("stahp") 141 tracer.Stop(timeout) 142 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) 143 144 if _, err := tracer.GetResult(); err.Error() != timeout.Error() { 145 t.Errorf("Expected timeout error, got %v", err) 146 } 147 }