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