github.com/aquanetwork/aquachain@v1.7.8/aqua/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  // +build gccgo cgo
    18  
    19  package tracers
    20  
    21  import (
    22  	"bytes"
    23  	"encoding/json"
    24  	"errors"
    25  	"math/big"
    26  	"testing"
    27  	"time"
    28  
    29  	"gitlab.com/aquachain/aquachain/common"
    30  	"gitlab.com/aquachain/aquachain/core/vm"
    31  	"gitlab.com/aquachain/aquachain/params"
    32  )
    33  
    34  type account struct{}
    35  
    36  func (account) SubBalance(amount *big.Int)                          {}
    37  func (account) AddBalance(amount *big.Int)                          {}
    38  func (account) SetAddress(common.Address)                           {}
    39  func (account) Value() *big.Int                                     { return nil }
    40  func (account) SetBalance(*big.Int)                                 {}
    41  func (account) SetNonce(uint64)                                     {}
    42  func (account) Balance() *big.Int                                   { return nil }
    43  func (account) Address() common.Address                             { return common.Address{} }
    44  func (account) ReturnGas(*big.Int)                                  {}
    45  func (account) SetCode(common.Hash, []byte)                         {}
    46  func (account) ForEachStorage(cb func(key, value common.Hash) bool) {}
    47  
    48  func runTrace(tracer *Tracer) (json.RawMessage, error) {
    49  	env := vm.NewEVM(vm.Context{BlockNumber: big.NewInt(1)}, nil, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
    50  
    51  	contract := vm.NewContract(account{}, account{}, big.NewInt(0), 10000)
    52  	contract.Code = []byte{byte(vm.PUSH1), 0x1, byte(vm.PUSH1), 0x1, 0x0}
    53  
    54  	_, err := env.Interpreter().Run(contract, []byte{})
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  	return tracer.GetResult()
    59  }
    60  
    61  func TestTracing(t *testing.T) {
    62  	tracer, err := New("{count: 0, step: function() { this.count += 1; }, fault: function() {}, result: function() { return this.count; }}")
    63  	if err != nil {
    64  		t.Fatal(err)
    65  	}
    66  
    67  	ret, err := runTrace(tracer)
    68  	if err != nil {
    69  		t.Fatal(err)
    70  	}
    71  	if !bytes.Equal(ret, []byte("3")) {
    72  		t.Errorf("Expected return value to be 3, got %s", string(ret))
    73  	}
    74  }
    75  
    76  func TestStack(t *testing.T) {
    77  	tracer, err := New("{depths: [], step: function(log) { this.depths.push(log.stack.length()); }, fault: function() {}, result: function() { return this.depths; }}")
    78  	if err != nil {
    79  		t.Fatal(err)
    80  	}
    81  
    82  	ret, err := runTrace(tracer)
    83  	if err != nil {
    84  		t.Fatal(err)
    85  	}
    86  	if !bytes.Equal(ret, []byte("[0,1,2]")) {
    87  		t.Errorf("Expected return value to be [0,1,2], got %s", string(ret))
    88  	}
    89  }
    90  
    91  func TestOpcodes(t *testing.T) {
    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  }