tracer_test.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2016 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 ethapi
  17. import (
  18. "errors"
  19. "math/big"
  20. "reflect"
  21. "testing"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. "github.com/ethereum/go-ethereum/params"
  26. )
  27. type account struct{}
  28. func (account) SubBalance(amount *big.Int) {}
  29. func (account) AddBalance(amount *big.Int) {}
  30. func (account) SetAddress(common.Address) {}
  31. func (account) Value() *big.Int { return nil }
  32. func (account) SetBalance(*big.Int) {}
  33. func (account) SetNonce(uint64) {}
  34. func (account) Balance() *big.Int { return nil }
  35. func (account) Address() common.Address { return common.Address{} }
  36. func (account) ReturnGas(*big.Int) {}
  37. func (account) SetCode(common.Hash, []byte) {}
  38. func (account) ForEachStorage(cb func(key, value common.Hash) bool) {}
  39. func runTrace(tracer *JavascriptTracer) (interface{}, error) {
  40. env := vm.NewEVM(vm.Context{}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
  41. contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000)
  42. contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
  43. _, err := env.Interpreter().Run(contract, []byte{})
  44. if err != nil {
  45. return nil, err
  46. }
  47. return tracer.GetResult()
  48. }
  49. func TestTracing(t *testing.T) {
  50. tracer, err := NewJavascriptTracer("{count: 0, step: function() { this.count += 1; }, result: function() { return this.count; }}")
  51. if err != nil {
  52. t.Fatal(err)
  53. }
  54. ret, err := runTrace(tracer)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. value, ok := ret.(float64)
  59. if !ok {
  60. t.Errorf("Expected return value to be float64, was %T", ret)
  61. }
  62. if value != 3 {
  63. t.Errorf("Expected return value to be 3, got %v", value)
  64. }
  65. }
  66. func TestStack(t *testing.T) {
  67. tracer, err := NewJavascriptTracer("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, result: function() { return this.depths; }}")
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. ret, err := runTrace(tracer)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. expected := []int{0, 1, 2}
  76. if !reflect.DeepEqual(ret, expected) {
  77. t.Errorf("Expected return value to be %#v, got %#v", expected, ret)
  78. }
  79. }
  80. func TestOpcodes(t *testing.T) {
  81. tracer, err := NewJavascriptTracer("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, result: function() { return this.opcodes; }}")
  82. if err != nil {
  83. t.Fatal(err)
  84. }
  85. ret, err := runTrace(tracer)
  86. if err != nil {
  87. t.Fatal(err)
  88. }
  89. expected := []string{"PUSH1", "PUSH1", "STOP"}
  90. if !reflect.DeepEqual(ret, expected) {
  91. t.Errorf("Expected return value to be %#v, got %#v", expected, ret)
  92. }
  93. }
  94. func TestHalt(t *testing.T) {
  95. timeout := errors.New("stahp")
  96. tracer, err := NewJavascriptTracer("{step: function() { while(1); }, result: function() { return null; }}")
  97. if err != nil {
  98. t.Fatal(err)
  99. }
  100. go func() {
  101. time.Sleep(1 * time.Second)
  102. tracer.Stop(timeout)
  103. }()
  104. if _, err = runTrace(tracer); err.Error() != "stahp in server-side tracer function 'step'" {
  105. t.Errorf("Expected timeout error, got %v", err)
  106. }
  107. }
  108. func TestHaltBetweenSteps(t *testing.T) {
  109. tracer, err := NewJavascriptTracer("{step: function() {}, result: function() { return null; }}")
  110. if err != nil {
  111. t.Fatal(err)
  112. }
  113. env := vm.NewEVM(vm.Context{}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
  114. contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0)
  115. tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil)
  116. timeout := errors.New("stahp")
  117. tracer.Stop(timeout)
  118. tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil)
  119. if _, err := tracer.GetResult(); err.Error() != "stahp in server-side tracer function 'step'" {
  120. t.Errorf("Expected timeout error, got %v", err)
  121. }
  122. }