github.com/klaytn/klaytn@v1.10.2/node/cn/tracers/tracer_test.go (about) 1 // Copyright 2018 The klaytn Authors 2 // Copyright 2017 The go-ethereum Authors 3 // This file is part of the go-ethereum library. 4 // 5 // The go-ethereum library is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Lesser General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // The go-ethereum library is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Lesser General Public License for more details. 14 // 15 // You should have received a copy of the GNU Lesser General Public License 16 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 17 // 18 // This file is derived from eth/tracers/tracer_test.go (2018/06/04). 19 // Modified and improved for the klaytn development. 20 21 package tracers 22 23 import ( 24 "bytes" 25 "encoding/json" 26 "errors" 27 "math/big" 28 "strings" 29 "testing" 30 "time" 31 32 "github.com/klaytn/klaytn/blockchain/state" 33 "github.com/klaytn/klaytn/blockchain/vm" 34 "github.com/klaytn/klaytn/common" 35 "github.com/klaytn/klaytn/params" 36 ) 37 38 type account struct{} 39 40 func (account) SubBalance(amount *big.Int) {} 41 func (account) AddBalance(amount *big.Int) {} 42 func (account) SetAddress(common.Address) {} 43 func (account) Value() *big.Int { return nil } 44 func (account) SetBalance(*big.Int) {} 45 func (account) SetNonce(uint64) {} 46 func (account) Balance() *big.Int { return nil } 47 func (account) Address() common.Address { return common.Address{} } 48 func (account) FeePayer() common.Address { return common.Address{} } 49 func (account) ReturnGas(*big.Int) {} 50 func (account) SetCode(common.Hash, []byte) {} 51 func (account) ForEachStorage(cb func(key, value common.Hash) bool) {} 52 53 type dummyStatedb struct { 54 state.StateDB 55 } 56 57 func (*dummyStatedb) GetRefund() uint64 { return 1337 } 58 59 func runTrace(tracer *Tracer) (json.RawMessage, error) { 60 env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, &dummyStatedb{}, params.TestChainConfig, &vm.Config{Debug: true, Tracer: tracer}) 61 62 contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000) 63 contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} 64 65 _, err := env.Interpreter().Run(contract, []byte{}) 66 if err != nil { 67 return nil, err 68 } 69 return tracer.GetResult() 70 } 71 72 // TestRegressionPanicSlice tests that we don't panic on bad arguments to memory access 73 func TestRegressionPanicSlice(t *testing.T) { 74 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.memory.slice(-1,-2)); }, fault: function() {}, result: function() { return this.depths; }}", true) 75 if err != nil { 76 t.Fatal(err) 77 } 78 if _, err = runTrace(tracer); err != nil { 79 t.Fatal(err) 80 } 81 } 82 83 // TestRegressionPanicSlice tests that we don't panic on bad arguments to stack peeks 84 func TestRegressionPanicPeek(t *testing.T) { 85 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.peek(-1)); }, fault: function() {}, result: function() { return this.depths; }}", true) 86 if err != nil { 87 t.Fatal(err) 88 } 89 if _, err = runTrace(tracer); err != nil { 90 t.Fatal(err) 91 } 92 } 93 94 // TestRegressionPanicSlice tests that we don't panic on bad arguments to memory getUint 95 func TestRegressionPanicGetUint(t *testing.T) { 96 tracer, err := New("{ depths: [], step: function(log, db) { this.depths.push(log.memory.getUint(-64));}, fault: function() {}, result: function() { return this.depths; }}", true) 97 if err != nil { 98 t.Fatal(err) 99 } 100 if _, err = runTrace(tracer); err != nil { 101 t.Fatal(err) 102 } 103 } 104 105 // TestTracingDeepObject tests if it returns an expected error when the json object has too many recursive children 106 func TestTracingDeepObject(t *testing.T) { 107 tracer, err := New("{step: function() {}, fault: function() {}, result: function() { var o={}; var x=o; for (var i=0; i<1000; i++){ o.foo={}; o=o.foo; } return x; }}", true) 108 if err != nil { 109 t.Fatal(err) 110 } 111 112 _, err = runTrace(tracer) 113 expectedErr := `RangeError: json encode recursion limit in server-side tracer function 'result'` 114 if !strings.Contains(err.Error(), expectedErr) { 115 t.Errorf("Expected return error to be %s, got %v", expectedErr, err) 116 } 117 } 118 119 func TestTracing(t *testing.T) { 120 tracer, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}", true) 121 if err != nil { 122 t.Fatal(err) 123 } 124 125 ret, err := runTrace(tracer) 126 if err != nil { 127 t.Fatal(err) 128 } 129 if !bytes.Equal(ret, []byte("3")) { 130 t.Errorf("Expected return value to be 3, got %s", string(ret)) 131 } 132 } 133 134 func TestUnsafeTracingDisabled(t *testing.T) { 135 _, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}", false) 136 if err == nil || err.Error() != "Only predefined tracers are supported" { 137 t.Fatal("Must disable JS code based tracers if unsafe") 138 } 139 } 140 141 func TestStack(t *testing.T) { 142 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}", true) 143 if err != nil { 144 t.Fatal(err) 145 } 146 147 ret, err := runTrace(tracer) 148 if err != nil { 149 t.Fatal(err) 150 } 151 if !bytes.Equal(ret, []byte("[0,1,2]")) { 152 t.Errorf("Expected return value to be [0,1,2], got %s", string(ret)) 153 } 154 } 155 156 func TestOpcodes(t *testing.T) { 157 tracer, err := New("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}", true) 158 if err != nil { 159 t.Fatal(err) 160 } 161 162 ret, err := runTrace(tracer) 163 if err != nil { 164 t.Fatal(err) 165 } 166 if !bytes.Equal(ret, []byte("[\"PUSH1\",\"PUSH1\",\"STOP\"]")) { 167 t.Errorf("Expected return value to be [\"PUSH1\",\"PUSH1\",\"STOP\"], got %s", string(ret)) 168 } 169 } 170 171 func TestHalt(t *testing.T) { 172 t.Skip("duktape doesn't support abortion") 173 174 timeout := errors.New("stahp") 175 tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}", true) 176 if err != nil { 177 t.Fatal(err) 178 } 179 180 go func() { 181 time.Sleep(1 * time.Second) 182 tracer.Stop(timeout) 183 }() 184 185 if _, err = runTrace(tracer); err.Error() != "stahp in server-side tracer function 'step'" { 186 t.Errorf("Expected timeout error, got %v", err) 187 } 188 } 189 190 func TestHaltBetweenSteps(t *testing.T) { 191 tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}", true) 192 if err != nil { 193 t.Fatal(err) 194 } 195 196 env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, &dummyStatedb{}, params.TestChainConfig, &vm.Config{Debug: true, Tracer: tracer}) 197 contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0) 198 199 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) 200 timeout := errors.New("stahp") 201 tracer.Stop(timeout) 202 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) 203 204 if _, err := tracer.GetResult(); err.Error() != timeout.Error() { 205 t.Errorf("Expected timeout error, got %v", err) 206 } 207 }