tracer_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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/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. func runTrace(tracer *Tracer) (json.RawMessage, error) {
  41. env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
  42. contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000)
  43. contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
  44. _, err := env.Interpreter().Run(contract, []byte{})
  45. if err != nil {
  46. return nil, err
  47. }
  48. return tracer.GetResult()
  49. }
  50. func TestTracing(t *testing.T) {
  51. tracer, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}")
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. ret, err := runTrace(tracer)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. if !bytes.Equal(ret, []byte("3")) {
  60. t.Errorf("Expected return value to be 3, got %s", string(ret))
  61. }
  62. }
  63. func TestStack(t *testing.T) {
  64. tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}")
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. ret, err := runTrace(tracer)
  69. if err != nil {
  70. t.Fatal(err)
  71. }
  72. if !bytes.Equal(ret, []byte("[0,1,2]")) {
  73. t.Errorf("Expected return value to be [0,1,2], got %s", string(ret))
  74. }
  75. }
  76. func TestOpcodes(t *testing.T) {
  77. tracer, err := New("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}")
  78. if err != nil {
  79. t.Fatal(err)
  80. }
  81. ret, err := runTrace(tracer)
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. if !bytes.Equal(ret, []byte("[\"PUSH1\",\"PUSH1\",\"STOP\"]")) {
  86. t.Errorf("Expected return value to be [\"PUSH1\",\"PUSH1\",\"STOP\"], got %s", string(ret))
  87. }
  88. }
  89. func TestHalt(t *testing.T) {
  90. t.Skip("duktape doesn't support abortion")
  91. timeout := errors.New("stahp")
  92. tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}")
  93. if err != nil {
  94. t.Fatal(err)
  95. }
  96. go func() {
  97. time.Sleep(1 * time.Second)
  98. tracer.Stop(timeout)
  99. }()
  100. if _, err = runTrace(tracer); err.Error() != "stahp in server-side tracer function 'step'" {
  101. t.Errorf("Expected timeout error, got %v", err)
  102. }
  103. }
  104. func TestHaltBetweenSteps(t *testing.T) {
  105. tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}")
  106. if err != nil {
  107. t.Fatal(err)
  108. }
  109. env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
  110. contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0)
  111. tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil)
  112. timeout := errors.New("stahp")
  113. tracer.Stop(timeout)
  114. tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil)
  115. if _, err := tracer.GetResult(); err.Error() != timeout.Error() {
  116. t.Errorf("Expected timeout error, got %v", err)
  117. }
  118. }