github.com/MetalBlockchain/subnet-evm@v0.4.9/eth/tracers/tracers_test.go (about)

     1  // (c) 2019-2020, Ava Labs, Inc.
     2  //
     3  // This file is a derived work, based on the go-ethereum library whose original
     4  // notices appear below.
     5  //
     6  // It is distributed under a license compatible with the licensing terms of the
     7  // original code from which it is derived.
     8  //
     9  // Much love to the original authors for their work.
    10  // **********
    11  // Copyright 2017 The go-ethereum Authors
    12  // This file is part of the go-ethereum library.
    13  //
    14  // The go-ethereum library is free software: you can redistribute it and/or modify
    15  // it under the terms of the GNU Lesser General Public License as published by
    16  // the Free Software Foundation, either version 3 of the License, or
    17  // (at your option) any later version.
    18  //
    19  // The go-ethereum library is distributed in the hope that it will be useful,
    20  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    21  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    22  // GNU Lesser General Public License for more details.
    23  //
    24  // You should have received a copy of the GNU Lesser General Public License
    25  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    26  
    27  package tracers
    28  
    29  import (
    30  	"math/big"
    31  	"testing"
    32  
    33  	"github.com/MetalBlockchain/subnet-evm/core"
    34  	"github.com/MetalBlockchain/subnet-evm/core/rawdb"
    35  	"github.com/MetalBlockchain/subnet-evm/core/types"
    36  	"github.com/MetalBlockchain/subnet-evm/core/vm"
    37  	"github.com/MetalBlockchain/subnet-evm/eth/tracers/logger"
    38  	"github.com/MetalBlockchain/subnet-evm/params"
    39  	"github.com/MetalBlockchain/subnet-evm/tests"
    40  	"github.com/ethereum/go-ethereum/common"
    41  	"github.com/ethereum/go-ethereum/crypto"
    42  )
    43  
    44  func BenchmarkTransactionTrace(b *testing.B) {
    45  	key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
    46  	from := crypto.PubkeyToAddress(key.PublicKey)
    47  	gas := uint64(1000000) // 1M gas
    48  	to := common.HexToAddress("0x00000000000000000000000000000000deadbeef")
    49  	signer := types.LatestSignerForChainID(big.NewInt(1337))
    50  	tx, err := types.SignNewTx(key, signer,
    51  		&types.LegacyTx{
    52  			Nonce:    1,
    53  			GasPrice: big.NewInt(500),
    54  			Gas:      gas,
    55  			To:       &to,
    56  		})
    57  	if err != nil {
    58  		b.Fatal(err)
    59  	}
    60  	txContext := vm.TxContext{
    61  		Origin:   from,
    62  		GasPrice: tx.GasPrice(),
    63  	}
    64  	context := vm.BlockContext{
    65  		CanTransfer: core.CanTransfer,
    66  		Transfer:    core.Transfer,
    67  		Coinbase:    common.Address{},
    68  		BlockNumber: new(big.Int).SetUint64(uint64(5)),
    69  		Time:        new(big.Int).SetUint64(uint64(5)),
    70  		Difficulty:  big.NewInt(0xffffffff),
    71  		GasLimit:    gas,
    72  		BaseFee:     big.NewInt(8),
    73  	}
    74  	alloc := core.GenesisAlloc{}
    75  	// The code pushes 'deadbeef' into memory, then the other params, and calls CREATE2, then returns
    76  	// the address
    77  	loop := []byte{
    78  		byte(vm.JUMPDEST), //  [ count ]
    79  		byte(vm.PUSH1), 0, // jumpdestination
    80  		byte(vm.JUMP),
    81  	}
    82  	alloc[common.HexToAddress("0x00000000000000000000000000000000deadbeef")] = core.GenesisAccount{
    83  		Nonce:   1,
    84  		Code:    loop,
    85  		Balance: big.NewInt(1),
    86  	}
    87  	alloc[from] = core.GenesisAccount{
    88  		Nonce:   1,
    89  		Code:    []byte{},
    90  		Balance: big.NewInt(500000000000000),
    91  	}
    92  	_, statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false)
    93  	// Create the tracer, the EVM environment and run it
    94  	tracer := logger.NewStructLogger(&logger.Config{
    95  		Debug: false,
    96  		//DisableStorage: true,
    97  		//EnableMemory: false,
    98  		//EnableReturnData: false,
    99  	})
   100  	evm := vm.NewEVM(context, txContext, statedb, params.TestChainConfig, vm.Config{Debug: true, Tracer: tracer})
   101  	msg, err := tx.AsMessage(signer, nil)
   102  	if err != nil {
   103  		b.Fatalf("failed to prepare transaction for tracing: %v", err)
   104  	}
   105  	b.ResetTimer()
   106  	b.ReportAllocs()
   107  
   108  	for i := 0; i < b.N; i++ {
   109  		snap := statedb.Snapshot()
   110  		st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
   111  		_, err = st.TransitionDb()
   112  		if err != nil {
   113  			b.Fatal(err)
   114  		}
   115  		statedb.RevertToSnapshot(snap)
   116  		if have, want := len(tracer.StructLogs()), 244752; have != want {
   117  			b.Fatalf("trace wrong, want %d steps, have %d", want, have)
   118  		}
   119  		tracer.Reset()
   120  	}
   121  }