tracer_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. package tracers
  17. import (
  18. "encoding/json"
  19. "errors"
  20. "math/big"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/state"
  25. "github.com/ethereum/go-ethereum/core/vm"
  26. "github.com/ethereum/go-ethereum/params"
  27. )
  28. type account struct{}
  29. func (account) SubBalance(amount *big.Int) {}
  30. func (account) AddBalance(amount *big.Int) {}
  31. func (account) SetAddress(common.Address) {}
  32. func (account) Value() *big.Int { return nil }
  33. func (account) SetBalance(*big.Int) {}
  34. func (account) SetNonce(uint64) {}
  35. func (account) Balance() *big.Int { return nil }
  36. func (account) Address() common.Address { return common.Address{} }
  37. func (account) ReturnGas(*big.Int) {}
  38. func (account) SetCode(common.Hash, []byte) {}
  39. func (account) ForEachStorage(cb func(key, value common.Hash) bool) {}
  40. type dummyStatedb struct {
  41. state.StateDB
  42. }
  43. func (*dummyStatedb) GetRefund() uint64 { return 1337 }
  44. type vmContext struct {
  45. blockCtx vm.BlockContext
  46. txCtx vm.TxContext
  47. }
  48. func testCtx() *vmContext {
  49. return &vmContext{blockCtx: vm.BlockContext{BlockNumber: big.NewInt(1)}, txCtx: vm.TxContext{GasPrice: big.NewInt(100000)}}
  50. }
  51. func runTrace(tracer *Tracer, vmctx *vmContext) (json.RawMessage, error) {
  52. env := vm.NewEVM(vmctx.blockCtx, vmctx.txCtx, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
  53. var (
  54. startGas uint64 = 10000
  55. value = big.NewInt(0)
  56. )
  57. contract := vm.NewContract(account{}, account{}, value, startGas)
  58. contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
  59. tracer.CaptureStart(contract.Caller(), contract.Address(), false, []byte{}, startGas, value)
  60. ret, err := env.Interpreter().Run(contract, []byte{}, false)
  61. tracer.CaptureEnd(ret, startGas-contract.Gas, 1, err)
  62. if err != nil {
  63. return nil, err
  64. }
  65. return tracer.GetResult()
  66. }
  67. func TestTracer(t *testing.T) {
  68. execTracer := func(code string) []byte {
  69. t.Helper()
  70. ctx := &vmContext{blockCtx: vm.BlockContext{BlockNumber: big.NewInt(1)}, txCtx: vm.TxContext{GasPrice: big.NewInt(100000)}}
  71. tracer, err := New(code, ctx.txCtx)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. ret, err := runTrace(tracer, ctx)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. return ret
  80. }
  81. for i, tt := range []struct {
  82. code string
  83. want string
  84. }{
  85. { // tests that we don't panic on bad arguments to memory access
  86. code: "{depths: [], step: function(log) { this.depths.push(log.memory.slice(-1,-2)); }, fault: function() {}, result: function() { return this.depths; }}",
  87. want: `[{},{},{}]`,
  88. }, { // tests that we don't panic on bad arguments to stack peeks
  89. code: "{depths: [], step: function(log) { this.depths.push(log.stack.peek(-1)); }, fault: function() {}, result: function() { return this.depths; }}",
  90. want: `["0","0","0"]`,
  91. }, { // tests that we don't panic on bad arguments to memory getUint
  92. code: "{ depths: [], step: function(log, db) { this.depths.push(log.memory.getUint(-64));}, fault: function() {}, result: function() { return this.depths; }}",
  93. want: `["0","0","0"]`,
  94. }, { // tests some general counting
  95. code: "{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}",
  96. want: `3`,
  97. }, { // tests that depth is reported correctly
  98. code: "{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}",
  99. want: `[0,1,2]`,
  100. }, { // tests to-string of opcodes
  101. code: "{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}",
  102. want: `["PUSH1","PUSH1","STOP"]`,
  103. }, { // tests intrinsic gas
  104. code: "{depths: [], step: function() {}, fault: function() {}, result: function(ctx) { return ctx.gasPrice+'.'+ctx.gasUsed+'.'+ctx.intrinsicGas; }}",
  105. want: `"100000.6.21000"`,
  106. },
  107. } {
  108. if have := execTracer(tt.code); tt.want != string(have) {
  109. t.Errorf("testcase %d: expected return value to be %s got %s\n\tcode: %v", i, tt.want, string(have), tt.code)
  110. }
  111. }
  112. }
  113. func TestHalt(t *testing.T) {
  114. t.Skip("duktape doesn't support abortion")
  115. timeout := errors.New("stahp")
  116. vmctx := testCtx()
  117. tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}", vmctx.txCtx)
  118. if err != nil {
  119. t.Fatal(err)
  120. }
  121. go func() {
  122. time.Sleep(1 * time.Second)
  123. tracer.Stop(timeout)
  124. }()
  125. if _, err = runTrace(tracer, vmctx); err.Error() != "stahp in server-side tracer function 'step'" {
  126. t.Errorf("Expected timeout error, got %v", err)
  127. }
  128. }
  129. func TestHaltBetweenSteps(t *testing.T) {
  130. vmctx := testCtx()
  131. tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}", vmctx.txCtx)
  132. if err != nil {
  133. t.Fatal(err)
  134. }
  135. env := vm.NewEVM(vm.BlockContext{BlockNumber: big.NewInt(1)}, vm.TxContext{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
  136. contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0)
  137. tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, nil, nil, contract, 0, nil)
  138. timeout := errors.New("stahp")
  139. tracer.Stop(timeout)
  140. tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, nil, nil, contract, 0, nil)
  141. if _, err := tracer.GetResult(); err.Error() != timeout.Error() {
  142. t.Errorf("Expected timeout error, got %v", err)
  143. }
  144. }