github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/eth/tracers/tracer_test.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2017 Go Ethereum作者 10 //此文件是Go以太坊库的一部分。 11 // 12 //Go-Ethereum库是免费软件:您可以重新分发它和/或修改 13 //根据GNU发布的较低通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊图书馆的发行目的是希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU较低的通用公共许可证,了解更多详细信息。 21 // 22 //你应该收到一份GNU较低级别的公共许可证副本 23 //以及Go以太坊图书馆。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package tracers 26 27 import ( 28 "bytes" 29 "encoding/json" 30 "errors" 31 "math/big" 32 "testing" 33 "time" 34 35 "github.com/ethereum/go-ethereum/common" 36 "github.com/ethereum/go-ethereum/core/vm" 37 "github.com/ethereum/go-ethereum/params" 38 ) 39 40 type account struct{} 41 42 func (account) SubBalance(amount *big.Int) {} 43 func (account) AddBalance(amount *big.Int) {} 44 func (account) SetAddress(common.Address) {} 45 func (account) Value() *big.Int { return nil } 46 func (account) SetBalance(*big.Int) {} 47 func (account) SetNonce(uint64) {} 48 func (account) Balance() *big.Int { return nil } 49 func (account) Address() common.Address { return common.Address{} } 50 func (account) ReturnGas(*big.Int) {} 51 func (account) SetCode(common.Hash, []byte) {} 52 func (account) ForEachStorage(cb func(key, value common.Hash) bool) {} 53 54 func runTrace(tracer *Tracer) (json.RawMessage, error) { 55 env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) 56 57 contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000) 58 contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0} 59 60 _, err := env.Interpreter().Run(contract, []byte{}) 61 if err != nil { 62 return nil, err 63 } 64 return tracer.GetResult() 65 } 66 67 func TestTracing(t *testing.T) { 68 tracer, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}") 69 if err != nil { 70 t.Fatal(err) 71 } 72 73 ret, err := runTrace(tracer) 74 if err != nil { 75 t.Fatal(err) 76 } 77 if !bytes.Equal(ret, []byte("3")) { 78 t.Errorf("Expected return value to be 3, got %s", string(ret)) 79 } 80 } 81 82 func TestStack(t *testing.T) { 83 tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}") 84 if err != nil { 85 t.Fatal(err) 86 } 87 88 ret, err := runTrace(tracer) 89 if err != nil { 90 t.Fatal(err) 91 } 92 if !bytes.Equal(ret, []byte("[0,1,2]")) { 93 t.Errorf("Expected return value to be [0,1,2], got %s", string(ret)) 94 } 95 } 96 97 func TestOpcodes(t *testing.T) { 98 tracer, err := New("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}") 99 if err != nil { 100 t.Fatal(err) 101 } 102 103 ret, err := runTrace(tracer) 104 if err != nil { 105 t.Fatal(err) 106 } 107 if !bytes.Equal(ret, []byte("[\"PUSH1\",\"PUSH1\",\"STOP\"]")) { 108 t.Errorf("Expected return value to be [\"PUSH1\",\"PUSH1\",\"STOP\"], got %s", string(ret)) 109 } 110 } 111 112 func TestHalt(t *testing.T) { 113 t.Skip("duktape doesn't support abortion") 114 115 timeout := errors.New("stahp") 116 tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}") 117 if err != nil { 118 t.Fatal(err) 119 } 120 121 go func() { 122 time.Sleep(1 * time.Second) 123 tracer.Stop(timeout) 124 }() 125 126 if _, err = runTrace(tracer); err.Error() != "stahp in server-side tracer function 'step'" { 127 t.Errorf("Expected timeout error, got %v", err) 128 } 129 } 130 131 func TestHaltBetweenSteps(t *testing.T) { 132 tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}") 133 if err != nil { 134 t.Fatal(err) 135 } 136 137 env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer}) 138 contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0) 139 140 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) 141 timeout := errors.New("stahp") 142 tracer.Stop(timeout) 143 tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil) 144 145 if _, err := tracer.GetResult(); err.Error() != timeout.Error() { 146 t.Errorf("Expected timeout error, got %v", err) 147 } 148 }