tracer_test.go 6.9 KB

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