github.com/calmw/ethereum@v0.1.1/eth/tracers/tracers_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  	"math/big"
    21  	"testing"
    22  
    23  	"github.com/calmw/ethereum/common"
    24  	"github.com/calmw/ethereum/core"
    25  	"github.com/calmw/ethereum/core/rawdb"
    26  	"github.com/calmw/ethereum/core/types"
    27  	"github.com/calmw/ethereum/core/vm"
    28  	"github.com/calmw/ethereum/crypto"
    29  	"github.com/calmw/ethereum/eth/tracers/logger"
    30  	"github.com/calmw/ethereum/params"
    31  	"github.com/calmw/ethereum/tests"
    32  )
    33  
    34  func BenchmarkTransactionTrace(b *testing.B) {
    35  	key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    36  	from := crypto.PubkeyToAddress(key.PublicKey)
    37  	gas := uint64(1000000) // 1M gas
    38  	to := common.HexToAddress("0x00000000000000000000000000000000deadbeef")
    39  	signer := types.LatestSignerForChainID(big.NewInt(1337))
    40  	tx, err := types.SignNewTx(key, signer,
    41  		&types.LegacyTx{
    42  			Nonce:    1,
    43  			GasPrice: big.NewInt(500),
    44  			Gas:      gas,
    45  			To:       &to,
    46  		})
    47  	if err != nil {
    48  		b.Fatal(err)
    49  	}
    50  	txContext := vm.TxContext{
    51  		Origin:   from,
    52  		GasPrice: tx.GasPrice(),
    53  	}
    54  	context := vm.BlockContext{
    55  		CanTransfer: core.CanTransfer,
    56  		Transfer:    core.Transfer,
    57  		Coinbase:    common.Address{},
    58  		BlockNumber: new(big.Int).SetUint64(uint64(5)),
    59  		Time:        5,
    60  		Difficulty:  big.NewInt(0xffffffff),
    61  		GasLimit:    gas,
    62  		BaseFee:     big.NewInt(8),
    63  	}
    64  	alloc := core.GenesisAlloc{}
    65  	// The code pushes 'deadbeef' into memory, then the other params, and calls CREATE2, then returns
    66  	// the address
    67  	loop := []byte{
    68  		byte(vm.JUMPDEST), //  [ count ]
    69  		byte(vm.PUSH1), 0, // jumpdestination
    70  		byte(vm.JUMP),
    71  	}
    72  	alloc[common.HexToAddress("0x00000000000000000000000000000000deadbeef")] = core.GenesisAccount{
    73  		Nonce:   1,
    74  		Code:    loop,
    75  		Balance: big.NewInt(1),
    76  	}
    77  	alloc[from] = core.GenesisAccount{
    78  		Nonce:   1,
    79  		Code:    []byte{},
    80  		Balance: big.NewInt(500000000000000),
    81  	}
    82  	_, statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false)
    83  	// Create the tracer, the EVM environment and run it
    84  	tracer := logger.NewStructLogger(&logger.Config{
    85  		Debug: false,
    86  		//DisableStorage: true,
    87  		//EnableMemory: false,
    88  		//EnableReturnData: false,
    89  	})
    90  	evm := vm.NewEVM(context, txContext, statedb, params.AllEthashProtocolChanges, vm.Config{Tracer: tracer})
    91  	msg, err := core.TransactionToMessage(tx, signer, nil)
    92  	if err != nil {
    93  		b.Fatalf("failed to prepare transaction for tracing: %v", err)
    94  	}
    95  	b.ResetTimer()
    96  	b.ReportAllocs()
    97  
    98  	for i := 0; i < b.N; i++ {
    99  		snap := statedb.Snapshot()
   100  		st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
   101  		_, err = st.TransitionDb()
   102  		if err != nil {
   103  			b.Fatal(err)
   104  		}
   105  		statedb.RevertToSnapshot(snap)
   106  		if have, want := len(tracer.StructLogs()), 244752; have != want {
   107  			b.Fatalf("trace wrong, want %d steps, have %d", want, have)
   108  		}
   109  		tracer.Reset()
   110  	}
   111  }
   112  
   113  func TestMemCopying(t *testing.T) {
   114  	for i, tc := range []struct {
   115  		memsize  int64
   116  		offset   int64
   117  		size     int64
   118  		wantErr  string
   119  		wantSize int
   120  	}{
   121  		{0, 0, 100, "", 100},    // Should pad up to 100
   122  		{0, 100, 0, "", 0},      // No need to pad (0 size)
   123  		{100, 50, 100, "", 100}, // Should pad 100-150
   124  		{100, 50, 5, "", 5},     // Wanted range fully within memory
   125  		{100, -50, 0, "offset or size must not be negative", 0},                        // Errror
   126  		{0, 1, 1024*1024 + 1, "reached limit for padding memory slice: 1048578", 0},    // Errror
   127  		{10, 0, 1024*1024 + 100, "reached limit for padding memory slice: 1048666", 0}, // Errror
   128  
   129  	} {
   130  		mem := vm.NewMemory()
   131  		mem.Resize(uint64(tc.memsize))
   132  		cpy, err := GetMemoryCopyPadded(mem, tc.offset, tc.size)
   133  		if want := tc.wantErr; want != "" {
   134  			if err == nil {
   135  				t.Fatalf("test %d: want '%v' have no error", i, want)
   136  			}
   137  			if have := err.Error(); want != have {
   138  				t.Fatalf("test %d: want '%v' have '%v'", i, want, have)
   139  			}
   140  			continue
   141  		}
   142  		if err != nil {
   143  			t.Fatalf("test %d: unexpected error: %v", i, err)
   144  		}
   145  		if want, have := tc.wantSize, len(cpy); have != want {
   146  			t.Fatalf("test %d: want %v have %v", i, want, have)
   147  		}
   148  	}
   149  }