github.com/luckypickle/go-ethereum-vet@v1.14.2/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/luckypickle/go-ethereum-vet/common" 28 "github.com/luckypickle/go-ethereum-vet/core/vm" 29 "github.com/luckypickle/go-ethereum-vet/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 func runTrace(tracer *Tracer) (json.RawMessage, error) { 47 env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) 48 49 contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000) 50 contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} 51 52 _, err := env.Interpreter().Run(contract, []byte{}) 53 if err != nil { 54 return nil, err 55 } 56 return tracer.GetResult() 57 } 58 59 func TestTracing(t *testing.T) { 60 tracer, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}") 61 if err != nil { 62 t.Fatal(err) 63 } 64 65 ret, err := runTrace(tracer) 66 if err != nil { 67 t.Fatal(err) 68 } 69 if !bytes.Equal(ret, []byte("3")) { 70 t.Errorf("Expected return value to be 3, got %s", string(ret)) 71 } 72 } 73 74 func TestStack(t *testing.T) { 75 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}") 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 ret, err := runTrace(tracer) 81 if err != nil { 82 t.Fatal(err) 83 } 84 if !bytes.Equal(ret, []byte("[0,1,2]")) { 85 t.Errorf("Expected return value to be [0,1,2], got %s", string(ret)) 86 } 87 } 88 89 func TestOpcodes(t *testing.T) { 90 tracer, err := New("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}") 91 if err != nil { 92 t.Fatal(err) 93 } 94 95 ret, err := runTrace(tracer) 96 if err != nil { 97 t.Fatal(err) 98 } 99 if !bytes.Equal(ret, []byte("[\"PUSH1\",\"PUSH1\",\"STOP\"]")) { 100 t.Errorf("Expected return value to be [\"PUSH1\",\"PUSH1\",\"STOP\"], got %s", string(ret)) 101 } 102 } 103 104 func TestHalt(t *testing.T) { 105 t.Skip("duktape doesn't support abortion") 106 107 timeout := errors.New("stahp") 108 tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}") 109 if err != nil { 110 t.Fatal(err) 111 } 112 113 go func() { 114 time.Sleep(1 * time.Second) 115 tracer.Stop(timeout) 116 }() 117 118 if _, err = runTrace(tracer); err.Error() != "stahp in server-side tracer function 'step'" { 119 t.Errorf("Expected timeout error, got %v", err) 120 } 121 } 122 123 func TestHaltBetweenSteps(t *testing.T) { 124 tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}") 125 if err != nil { 126 t.Fatal(err) 127 } 128 129 env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) 130 contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0) 131 132 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) 133 timeout := errors.New("stahp") 134 tracer.Stop(timeout) 135 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) 136 137 if _, err := tracer.GetResult(); err.Error() != timeout.Error() { 138 t.Errorf("Expected timeout error, got %v", err) 139 } 140 }