tracer_test.go 6.0 KB

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