github.com/coltonfike/e2c@v21.1.0+incompatible/eth/tracers/tracer_test.go (about)

     1  // Copyright 2017 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  
    17  package tracers
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"errors"
    23  	"math/big"
    24  	"testing"
    25  	"time"
    26  
    27  	"github.com/ethereum/go-ethereum/common"
    28  	"github.com/ethereum/go-ethereum/core/state"
    29  	"github.com/ethereum/go-ethereum/core/vm"
    30  	"github.com/ethereum/go-ethereum/params"
    31  )
    32  
    33  type account struct{}
    34  
    35  func (account) SubBalance(amount *big.Int)                          {}
    36  func (account) AddBalance(amount *big.Int)                          {}
    37  func (account) SetAddress(common.Address)                           {}
    38  func (account) Value() *big.Int                                     { return nil }
    39  func (account) SetBalance(*big.Int)                                 {}
    40  func (account) SetNonce(uint64)                                     {}
    41  func (account) Balance() *big.Int                                   { return nil }
    42  func (account) Address() common.Address                             { return common.Address{} }
    43  func (account) ReturnGas(*big.Int)                                  {}
    44  func (account) SetCode(common.Hash, []byte)                         {}
    45  func (account) ForEachStorage(cb func(key, value common.Hash) bool) {}
    46  
    47  type dummyStatedb struct {
    48  	state.StateDB
    49  }
    50  
    51  func (*dummyStatedb) GetRefund() uint64 { return 1337 }
    52  
    53  func runTrace(tracer *Tracer) (json.RawMessage, error) {
    54  	db := &dummyStatedb{}
    55  	env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, db, db, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
    56  
    57  	contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000)
    58  	contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
    59  
    60  	_, err := env.Interpreter().Run(contract, []byte{}, false)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  	return tracer.GetResult()
    65  }
    66  
    67  func TestTracing(t *testing.T) {
    68  	tracer, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}")
    69  	if err != nil {
    70  		t.Fatal(err)
    71  	}
    72  
    73  	ret, err := runTrace(tracer)
    74  	if err != nil {
    75  		t.Fatal(err)
    76  	}
    77  	if !bytes.Equal(ret, []byte("3")) {
    78  		t.Errorf("Expected return value to be 3, got %s", string(ret))
    79  	}
    80  }
    81  
    82  func TestStack(t *testing.T) {
    83  	tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}")
    84  	if err != nil {
    85  		t.Fatal(err)
    86  	}
    87  
    88  	ret, err := runTrace(tracer)
    89  	if err != nil {
    90  		t.Fatal(err)
    91  	}
    92  	if !bytes.Equal(ret, []byte("[0,1,2]")) {
    93  		t.Errorf("Expected return value to be [0,1,2], got %s", string(ret))
    94  	}
    95  }
    96  
    97  func TestOpcodes(t *testing.T) {
    98  	tracer, err := New("{opcodes: [], step: function(log) { this.opcodes.push(log.op.toString()); }, fault: function() {}, result: function() { return this.opcodes; }}")
    99  	if err != nil {
   100  		t.Fatal(err)
   101  	}
   102  
   103  	ret, err := runTrace(tracer)
   104  	if err != nil {
   105  		t.Fatal(err)
   106  	}
   107  	if !bytes.Equal(ret, []byte("[\"PUSH1\",\"PUSH1\",\"STOP\"]")) {
   108  		t.Errorf("Expected return value to be [\"PUSH1\",\"PUSH1\",\"STOP\"], got %s", string(ret))
   109  	}
   110  }
   111  
   112  func TestHalt(t *testing.T) {
   113  	t.Skip("duktape doesn't support abortion")
   114  
   115  	timeout := errors.New("stahp")
   116  	tracer, err := New("{step: function() { while(1); }, result: function() { return null; }}")
   117  	if err != nil {
   118  		t.Fatal(err)
   119  	}
   120  
   121  	go func() {
   122  		time.Sleep(1 * time.Second)
   123  		tracer.Stop(timeout)
   124  	}()
   125  
   126  	if _, err = runTrace(tracer); err.Error() != "stahp    in server-side tracer function 'step'" {
   127  		t.Errorf("Expected timeout error, got %v", err)
   128  	}
   129  }
   130  
   131  func TestHaltBetweenSteps(t *testing.T) {
   132  	tracer, err := New("{step: function() {}, fault: function() {}, result: function() { return null; }}")
   133  	if err != nil {
   134  		t.Fatal(err)
   135  	}
   136  	db := &dummyStatedb{}
   137  	env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, db, db, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
   138  	contract := vm.NewContract(&account{}, &account{}, big.NewInt(0), 0)
   139  
   140  	tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil)
   141  	timeout := errors.New("stahp")
   142  	tracer.Stop(timeout)
   143  	tracer.CaptureState(env, 0, 0, 0, 0, nil, nil, contract, 0, nil)
   144  
   145  	if _, err := tracer.GetResult(); err.Error() != timeout.Error() {
   146  		t.Errorf("Expected timeout error, got %v", err)
   147  	}
   148  }