tracer_test.go 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/crypto"
  26. )
  27. type ruleSet struct{}
  28. func (self *ruleSet) IsHomestead(*big.Int) bool { return true }
  29. type Env struct {
  30. gasLimit *big.Int
  31. depth int
  32. evm *vm.EVM
  33. }
  34. func NewEnv(config *vm.Config) *Env {
  35. env := &Env{gasLimit: big.NewInt(10000), depth: 0}
  36. env.evm = vm.New(env, *config)
  37. return env
  38. }
  39. func (self *Env) RuleSet() vm.RuleSet { return &ruleSet{} }
  40. func (self *Env) Vm() vm.Vm { return self.evm }
  41. func (self *Env) Origin() common.Address { return common.Address{} }
  42. func (self *Env) BlockNumber() *big.Int { return big.NewInt(0) }
  43. //func (self *Env) PrevHash() []byte { return self.parent }
  44. func (self *Env) Coinbase() common.Address { return common.Address{} }
  45. func (self *Env) SnapshotDatabase() int { return 0 }
  46. func (self *Env) RevertToSnapshot(int) {}
  47. func (self *Env) Time() *big.Int { return big.NewInt(time.Now().Unix()) }
  48. func (self *Env) Difficulty() *big.Int { return big.NewInt(0) }
  49. func (self *Env) Db() vm.Database { return nil }
  50. func (self *Env) GasLimit() *big.Int { return self.gasLimit }
  51. func (self *Env) VmType() vm.Type { return vm.StdVmTy }
  52. func (self *Env) GetHash(n uint64) common.Hash {
  53. return common.BytesToHash(crypto.Keccak256([]byte(big.NewInt(int64(n)).String())))
  54. }
  55. func (self *Env) AddLog(log *vm.Log) {
  56. }
  57. func (self *Env) Depth() int { return self.depth }
  58. func (self *Env) SetDepth(i int) { self.depth = i }
  59. func (self *Env) CanTransfer(from common.Address, balance *big.Int) bool {
  60. return true
  61. }
  62. func (self *Env) Transfer(from, to vm.Account, amount *big.Int) {}
  63. func (self *Env) Call(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  64. return nil, nil
  65. }
  66. func (self *Env) CallCode(caller vm.ContractRef, addr common.Address, data []byte, gas, price, value *big.Int) ([]byte, error) {
  67. return nil, nil
  68. }
  69. func (self *Env) Create(caller vm.ContractRef, data []byte, gas, price, value *big.Int) ([]byte, common.Address, error) {
  70. return nil, common.Address{}, nil
  71. }
  72. func (self *Env) DelegateCall(me vm.ContractRef, addr common.Address, data []byte, gas, price *big.Int) ([]byte, error) {
  73. return nil, nil
  74. }
  75. type account struct{}
  76. func (account) SubBalance(amount *big.Int) {}
  77. func (account) AddBalance(amount *big.Int) {}
  78. func (account) SetAddress(common.Address) {}
  79. func (account) Value() *big.Int { return nil }
  80. func (account) SetBalance(*big.Int) {}
  81. func (account) SetNonce(uint64) {}
  82. func (account) Balance() *big.Int { return nil }
  83. func (account) Address() common.Address { return common.Address{} }
  84. func (account) ReturnGas(*big.Int, *big.Int) {}
  85. func (account) SetCode(common.Hash, []byte) {}
  86. func (account) ForEachStorage(cb func(key, value common.Hash) bool) {}
  87. func runTrace(tracer *JavascriptTracer) (interface{}, error) {
  88. env := NewEnv(&vm.Config{Debug: true, Tracer: tracer})
  89. contract := vm.NewContract(account{}, account{}, big.NewInt(0), env.GasLimit(), big.NewInt(1))
  90. contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
  91. _, err := env.Vm().Run(contract, []byte{})
  92. if err != nil {
  93. return nil, err
  94. }
  95. return tracer.GetResult()
  96. }
  97. func TestTracing(t *testing.T) {
  98. tracer, err := NewJavascriptTracer("{count: 0, step: function() { this.count += 1; }, result: function() { return this.count; }}")
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. ret, err := runTrace(tracer)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. value, ok := ret.(float64)
  107. if !ok {
  108. t.Errorf("Expected return value to be float64, was %T", ret)
  109. }
  110. if value != 3 {
  111. t.Errorf("Expected return value to be 3, got %v", value)
  112. }
  113. }
  114. func TestStack(t *testing.T) {
  115. tracer, err := NewJavascriptTracer("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, result: function() { return this.depths; }}")
  116. if err != nil {
  117. t.Fatal(err)
  118. }
  119. ret, err := runTrace(tracer)
  120. if err != nil {
  121. t.Fatal(err)
  122. }
  123. expected := []int{0, 1, 2}
  124. if !reflect.DeepEqual(ret, expected) {
  125. t.Errorf("Expected return value to be %#v, got %#v", expected, ret)
  126. }
  127. }
  128. func TestOpcodes(t *testing.T) {
  129. tracer, err := NewJavascriptTracer("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, result: function() { return this.opcodes; }}")
  130. if err != nil {
  131. t.Fatal(err)
  132. }
  133. ret, err := runTrace(tracer)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. expected := []string{"PUSH1", "PUSH1", "STOP"}
  138. if !reflect.DeepEqual(ret, expected) {
  139. t.Errorf("Expected return value to be %#v, got %#v", expected, ret)
  140. }
  141. }
  142. func TestHalt(t *testing.T) {
  143. timeout := errors.New("stahp")
  144. tracer, err := NewJavascriptTracer("{step: function() { while(1); }, result: function() { return null; }}")
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. go func() {
  149. time.Sleep(1 * time.Second)
  150. tracer.Stop(timeout)
  151. }()
  152. if _, err = runTrace(tracer); err.Error() != "stahp in server-side tracer function 'step'" {
  153. t.Errorf("Expected timeout error, got %v", err)
  154. }
  155. }
  156. func TestHaltBetweenSteps(t *testing.T) {
  157. tracer, err := NewJavascriptTracer("{step: function() {}, result: function() { return null; }}")
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. env := NewEnv(&vm.Config{Debug: true, Tracer: tracer})
  162. contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), big.NewInt(0), big.NewInt(0))
  163. tracer.CaptureState(env, 0, 0, big.NewInt(0), big.NewInt(0), nil, nil, contract, 0, nil)
  164. timeout := errors.New("stahp")
  165. tracer.Stop(timeout)
  166. tracer.CaptureState(env, 0, 0, big.NewInt(0), big.NewInt(0), nil, nil, contract, 0, nil)
  167. if _, err := tracer.GetResult(); err.Error() != "stahp in server-side tracer function 'step'" {
  168. t.Errorf("Expected timeout error, got %v", err)
  169. }
  170. }