github.com/klaytn/klaytn@v1.12.1/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 type vmContext struct { 60 blockCtx vm.BlockContext 61 txCtx vm.TxContext 62 } 63 64 func testCtx() *vmContext { 65 return &vmContext{blockCtx: vm.BlockContext{BlockNumber: big.NewInt(1)}, txCtx: vm.TxContext{GasPrice: big.NewInt(100000)}} 66 } 67 68 func runTrace(tracer *Tracer) (json.RawMessage, error) { 69 env := vm.NewEVM(vm.BlockContext{BlockNumber: big.NewInt(1)}, vm.TxContext{}, &dummyStatedb{}, params.TestChainConfig, &vm.Config{Debug: true, Tracer: tracer}) 70 71 contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000) 72 contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} 73 74 _, err := env.Interpreter().Run(contract, []byte{}) 75 if err != nil { 76 return nil, err 77 } 78 return tracer.GetResult() 79 } 80 81 // TestRegressionPanicSlice tests that we don't panic on bad arguments to memory access 82 func TestRegressionPanicSlice(t *testing.T) { 83 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.memory.slice(-1,-2)); }, fault: function() {}, result: function() { return this.depths; }}", new(Context), true) 84 if err != nil { 85 t.Fatal(err) 86 } 87 if _, err = runTrace(tracer); err != nil { 88 t.Fatal(err) 89 } 90 } 91 92 // TestRegressionPanicSlice tests that we don't panic on bad arguments to stack peeks 93 func TestRegressionPanicPeek(t *testing.T) { 94 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.peek(-1)); }, fault: function() {}, result: function() { return this.depths; }}", new(Context), true) 95 if err != nil { 96 t.Fatal(err) 97 } 98 if _, err = runTrace(tracer); err != nil { 99 t.Fatal(err) 100 } 101 } 102 103 // TestRegressionPanicSlice tests that we don't panic on bad arguments to memory getUint 104 func TestRegressionPanicGetUint(t *testing.T) { 105 tracer, err := New("{ depths: [], step: function(log, db) { this.depths.push(log.memory.getUint(-64));}, fault: function() {}, result: function() { return this.depths; }}", new(Context), true) 106 if err != nil { 107 t.Fatal(err) 108 } 109 if _, err = runTrace(tracer); err != nil { 110 t.Fatal(err) 111 } 112 } 113 114 // TestTracingDeepObject tests if it returns an expected error when the json object has too many recursive children 115 func TestTracingDeepObject(t *testing.T) { 116 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; }}", new(Context), true) 117 if err != nil { 118 t.Fatal(err) 119 } 120 121 _, err = runTrace(tracer) 122 expectedErr := `RangeError: json encode recursion limit in server-side tracer function 'result'` 123 if !strings.Contains(err.Error(), expectedErr) { 124 t.Errorf("Expected return error to be %s, got %v", expectedErr, err) 125 } 126 } 127 128 func TestTracing(t *testing.T) { 129 tracer, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}", new(Context), true) 130 if err != nil { 131 t.Fatal(err) 132 } 133 134 ret, err := runTrace(tracer) 135 if err != nil { 136 t.Fatal(err) 137 } 138 if !bytes.Equal(ret, []byte("3")) { 139 t.Errorf("Expected return value to be 3, got %s", string(ret)) 140 } 141 } 142 143 func TestUnsafeTracingDisabled(t *testing.T) { 144 _, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}", new(Context), false) 145 if err == nil || err.Error() != "Only predefined tracers are supported" { 146 t.Fatal("Must disable JS code based tracers if unsafe") 147 } 148 } 149 150 func TestStack(t *testing.T) { 151 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}", new(Context), true) 152 if err != nil { 153 t.Fatal(err) 154 } 155 156 ret, err := runTrace(tracer) 157 if err != nil { 158 t.Fatal(err) 159 } 160 if !bytes.Equal(ret, []byte("[0,1,2]")) { 161 t.Errorf("Expected return value to be [0,1,2], got %s", string(ret)) 162 } 163 } 164 165 func TestOpcodes(t *testing.T) { 166 tracer, err := New("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}", new(Context), true) 167 if err != nil { 168 t.Fatal(err) 169 } 170 171 ret, err := runTrace(tracer) 172 if err != nil { 173 t.Fatal(err) 174 } 175 if !bytes.Equal(ret, []byte("[\"PUSH1\",\"PUSH1\",\"STOP\"]")) { 176 t.Errorf("Expected return value to be [\"PUSH1\",\"PUSH1\",\"STOP\"], got %s", string(ret)) 177 } 178 } 179 180 func TestHalt(t *testing.T) { 181 t.Skip("duktape doesn't support abortion") 182 183 timeout := errors.New("stahp") 184 tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}", new(Context), true) 185 if err != nil { 186 t.Fatal(err) 187 } 188 189 go func() { 190 time.Sleep(1 * time.Second) 191 tracer.Stop(timeout) 192 }() 193 194 if _, err = runTrace(tracer); err.Error() != "stahp in server-side tracer function 'step'" { 195 t.Errorf("Expected timeout error, got %v", err) 196 } 197 } 198 199 func TestHaltBetweenSteps(t *testing.T) { 200 tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}", new(Context), true) 201 if err != nil { 202 t.Fatal(err) 203 } 204 205 env := vm.NewEVM(vm.BlockContext{BlockNumber: big.NewInt(1)}, vm.TxContext{}, &dummyStatedb{}, params.TestChainConfig, &vm.Config{Debug: true, Tracer: tracer}) 206 contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0) 207 208 tracer.CaptureState(env, 0, 0, 0, 0, &vm.ScopeContext{Contract: contract}, 0, nil) 209 timeout := errors.New("stahp") 210 tracer.Stop(timeout) 211 tracer.CaptureState(env, 0, 0, 0, 0, &vm.ScopeContext{Contract: contract}, 0, nil) 212 213 if _, err := tracer.GetResult(); err.Error() != timeout.Error() { 214 t.Errorf("Expected timeout error, got %v", err) 215 } 216 } 217 218 func TestEnterExit(t *testing.T) { 219 // test that either both or none of enter() and exit() are defined 220 if _, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}}", new(Context), true); err == nil { 221 t.Fatal("tracer creation should've failed without exit() definition") 222 } 223 if _, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }, enter: function() {}, exit: function() {}}", new(Context), true); err != nil { 224 t.Fatal(err) 225 } 226 227 // test that the enter and exit method are correctly invoked and the values passed 228 tracer, err := New("{enters: 0, exits: 0, enterGas: 0, gasUsed: 0, step: function() {}, fault: function() {}, result: function() { return {enters: this.enters, exits: this.exits, enterGas: this.enterGas, gasUsed: this.gasUsed} }, enter: function(frame) { this.enters++; this.enterGas = frame.getGas(); }, exit: function(res) { this.exits++; this.gasUsed = res.getGasUsed(); }}", new(Context), true) 229 if err != nil { 230 t.Fatal(err) 231 } 232 233 scope := &vm.ScopeContext{ 234 Contract: vm.NewContract(&account{}, &account{}, big.NewInt(0), 0), 235 } 236 237 tracer.CaptureEnter(vm.CALL, scope.Contract.Caller(), scope.Contract.Address(), []byte{}, 1000, new(big.Int)) 238 tracer.CaptureExit([]byte{}, 400, nil) 239 240 have, err := tracer.GetResult() 241 if err != nil { 242 t.Fatal(err) 243 } 244 want := `{"enters":1,"exits":1,"enterGas":1000,"gasUsed":400}` 245 if string(have) != want { 246 t.Errorf("Number of invocations of enter() and exit() is wrong. Have %s, want %s\n", have, want) 247 } 248 }