tracer_test.go 6.3 KB

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