tracer_test.go 6.7 KB

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