github.com/Elemental-core/elementalcore@v0.0.0-20191206075037-63891242267a/internal/ethapi/tracer_test.go (about) 1 // Copyright 2016 The elementalcore Authors 2 // This file is part of the elementalcore library. 3 // 4 // The elementalcore 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 elementalcore 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 elementalcore library. If not, see <http://www.gnu.org/licenses/>. 16 17 package ethapi 18 19 import ( 20 "errors" 21 "math/big" 22 "reflect" 23 "testing" 24 "time" 25 26 "github.com/Elemental-core/elementalcore/common" 27 "github.com/Elemental-core/elementalcore/core/vm" 28 "github.com/Elemental-core/elementalcore/params" 29 ) 30 31 type account struct{} 32 33 func (account) SubBalance(amount *big.Int) {} 34 func (account) AddBalance(amount *big.Int) {} 35 func (account) SetAddress(common.Address) {} 36 func (account) Value() *big.Int { return nil } 37 func (account) SetBalance(*big.Int) {} 38 func (account) SetNonce(uint64) {} 39 func (account) Balance() *big.Int { return nil } 40 func (account) Address() common.Address { return common.Address{} } 41 func (account) ReturnGas(*big.Int) {} 42 func (account) SetCode(common.Hash, []byte) {} 43 func (account) ForEachStorage(cb func(key, value common.Hash) bool) {} 44 45 func runTrace(tracer *JavascriptTracer) (interface{}, error) { 46 env := vm.NewEVM(vm.Context{}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) 47 48 contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000) 49 contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} 50 51 _, err := env.Interpreter().Run(0, contract, []byte{}) 52 if err != nil { 53 return nil, err 54 } 55 56 return tracer.GetResult() 57 } 58 59 func TestTracing(t *testing.T) { 60 tracer, err := NewJavascriptTracer("{count: 0, step: function() { this.count += 1; }, 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 70 value, ok := ret.(float64) 71 if !ok { 72 t.Errorf("Expected return value to be float64, was %T", ret) 73 } 74 if value != 3 { 75 t.Errorf("Expected return value to be 3, got %v", value) 76 } 77 } 78 79 func TestStack(t *testing.T) { 80 tracer, err := NewJavascriptTracer("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, result: function() { return this.depths; }}") 81 if err != nil { 82 t.Fatal(err) 83 } 84 85 ret, err := runTrace(tracer) 86 if err != nil { 87 t.Fatal(err) 88 } 89 90 expected := []int{0, 1, 2} 91 if !reflect.DeepEqual(ret, expected) { 92 t.Errorf("Expected return value to be %#v, got %#v", expected, ret) 93 } 94 } 95 96 func TestOpcodes(t *testing.T) { 97 tracer, err := NewJavascriptTracer("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, 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 107 expected := []string{"PUSH1", "PUSH1", "STOP"} 108 if !reflect.DeepEqual(ret, expected) { 109 t.Errorf("Expected return value to be %#v, got %#v", expected, ret) 110 } 111 } 112 113 func TestHalt(t *testing.T) { 114 timeout := errors.New("stahp") 115 tracer, err := NewJavascriptTracer("{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 := NewJavascriptTracer("{step: function() {}, result: function() { return null; }}") 132 if err != nil { 133 t.Fatal(err) 134 } 135 136 env := vm.NewEVM(vm.Context{}, nil, 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() != "stahp in server-side tracer function 'step'" { 145 t.Errorf("Expected timeout error, got %v", err) 146 } 147 }