tracer_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. func (*dummyStatedb) GetBalance(addr common.Address) *big.Int { return new(big.Int) }
  45. type vmContext struct {
  46. blockCtx vm.BlockContext
  47. txCtx vm.TxContext
  48. }
  49. func testCtx() *vmContext {
  50. return &vmContext{blockCtx: vm.BlockContext{BlockNumber: big.NewInt(1)}, txCtx: vm.TxContext{GasPrice: big.NewInt(100000)}}
  51. }
  52. func runTrace(tracer *Tracer, vmctx *vmContext) (json.RawMessage, error) {
  53. env := vm.NewEVM(vmctx.blockCtx, vmctx.txCtx, &dummyStatedb{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
  54. var (
  55. startGas uint64 = 10000
  56. value = big.NewInt(0)
  57. )
  58. contract := vm.NewContract(account{}, account{}, value, startGas)
  59. contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
  60. tracer.CaptureStart(env, contract.Caller(), contract.Address(), false, []byte{}, startGas, value)
  61. ret, err := env.Interpreter().Run(contract, []byte{}, false)
  62. tracer.CaptureEnd(ret, startGas-contract.Gas, 1, err)
  63. if err != nil {
  64. return nil, err
  65. }
  66. return tracer.GetResult()
  67. }
  68. func TestTracer(t *testing.T) {
  69. execTracer := func(code string) []byte {
  70. t.Helper()
  71. ctx := &vmContext{blockCtx: vm.BlockContext{BlockNumber: big.NewInt(1)}, txCtx: vm.TxContext{GasPrice: big.NewInt(100000)}}
  72. tracer, err := New(code, ctx.txCtx)
  73. if err != nil {
  74. t.Fatal(err)
  75. }
  76. ret, err := runTrace(tracer, ctx)
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. return ret
  81. }
  82. for i, tt := range []struct {
  83. code string
  84. want string
  85. }{
  86. { // tests that we don't panic on bad arguments to memory access
  87. code: "{depths: [], step: function(log) { this.depths.push(log.memory.slice(-1,-2)); }, fault: function() {}, result: function() { return this.depths; }}",
  88. want: `[{},{},{}]`,
  89. }, { // tests that we don't panic on bad arguments to stack peeks
  90. code: "{depths: [], step: function(log) { this.depths.push(log.stack.peek(-1)); }, fault: function() {}, result: function() { return this.depths; }}",
  91. want: `["0","0","0"]`,
  92. }, { // tests that we don't panic on bad arguments to memory getUint
  93. code: "{ depths: [], step: function(log, db) { this.depths.push(log.memory.getUint(-64));}, fault: function() {}, result: function() { return this.depths; }}",
  94. want: `["0","0","0"]`,
  95. }, { // tests some general counting
  96. code: "{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}",
  97. want: `3`,
  98. }, { // tests that depth is reported correctly
  99. code: "{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}",
  100. want: `[0,1,2]`,
  101. }, { // tests to-string of opcodes
  102. code: "{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}",
  103. want: `["PUSH1","PUSH1","STOP"]`,
  104. }, { // tests intrinsic gas
  105. code: "{depths: [], step: function() {}, fault: function() {}, result: function(ctx) { return ctx.gasPrice+'.'+ctx.gasUsed+'.'+ctx.intrinsicGas; }}",
  106. want: `"100000.6.21000"`,
  107. },
  108. } {
  109. if have := execTracer(tt.code); tt.want != string(have) {
  110. t.Errorf("testcase %d: expected return value to be %s got %s\n\tcode: %v", i, tt.want, string(have), tt.code)
  111. }
  112. }
  113. }
  114. func TestHalt(t *testing.T) {
  115. t.Skip("duktape doesn't support abortion")
  116. timeout := errors.New("stahp")
  117. vmctx := testCtx()
  118. tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}", vmctx.txCtx)
  119. if err != nil {
  120. t.Fatal(err)
  121. }
  122. go func() {
  123. time.Sleep(1 * time.Second)
  124. tracer.Stop(timeout)
  125. }()
  126. if _, err = runTrace(tracer, vmctx); err.Error() != "stahp in server-side tracer function 'step'" {
  127. t.Errorf("Expected timeout error, got %v", err)
  128. }
  129. }
  130. func TestHaltBetweenSteps(t *testing.T) {
  131. vmctx := testCtx()
  132. tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}", vmctx.txCtx)
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. env := vm.NewEVM(vm.BlockContext{BlockNumber: big.NewInt(1)}, vm.TxContext{}, &dummyStatedb{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
  137. scope := &vm.ScopeContext{
  138. Contract: vm.NewContract(&account{}, &account{}, big.NewInt(0), 0),
  139. }
  140. tracer.CaptureState(env, 0, 0, 0, 0, scope, nil, 0, nil)
  141. timeout := errors.New("stahp")
  142. tracer.Stop(timeout)
  143. tracer.CaptureState(env, 0, 0, 0, 0, scope, nil, 0, nil)
  144. if _, err := tracer.GetResult(); err.Error() != timeout.Error() {
  145. t.Errorf("Expected timeout error, got %v", err)
  146. }
  147. }
  148. // TestNoStepExec tests a regular value transfer (no exec), and accessing the statedb
  149. // in 'result'
  150. func TestNoStepExec(t *testing.T) {
  151. runEmptyTrace := func(tracer *Tracer, vmctx *vmContext) (json.RawMessage, error) {
  152. env := vm.NewEVM(vmctx.blockCtx, vmctx.txCtx, &dummyStatedb{}, &dummyStatedb{}, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
  153. startGas := uint64(10000)
  154. contract := vm.NewContract(account{}, account{}, big.NewInt(0), startGas)
  155. tracer.CaptureStart(env, contract.Caller(), contract.Address(), false, []byte{}, startGas, big.NewInt(0))
  156. tracer.CaptureEnd(nil, startGas-contract.Gas, 1, nil)
  157. return tracer.GetResult()
  158. }
  159. execTracer := func(code string) []byte {
  160. t.Helper()
  161. ctx := &vmContext{blockCtx: vm.BlockContext{BlockNumber: big.NewInt(1)}, txCtx: vm.TxContext{GasPrice: big.NewInt(100000)}}
  162. tracer, err := New(code, ctx.txCtx)
  163. if err != nil {
  164. t.Fatal(err)
  165. }
  166. ret, err := runEmptyTrace(tracer, ctx)
  167. if err != nil {
  168. t.Fatal(err)
  169. }
  170. return ret
  171. }
  172. for i, tt := range []struct {
  173. code string
  174. want string
  175. }{
  176. { // tests that we don't panic on accessing the db methods
  177. code: "{depths: [], step: function() {}, fault: function() {}, result: function(ctx, db){ return db.getBalance(ctx.to)} }",
  178. want: `"0"`,
  179. },
  180. } {
  181. if have := execTracer(tt.code); tt.want != string(have) {
  182. t.Errorf("testcase %d: expected return value to be %s got %s\n\tcode: %v", i, tt.want, string(have), tt.code)
  183. }
  184. }
  185. }