github.com/ethereum/go-ethereum@v1.16.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/ethereum/go-ethereum/common"
    24  	"github.com/ethereum/go-ethereum/core"
    25  	"github.com/ethereum/go-ethereum/core/rawdb"
    26  	"github.com/ethereum/go-ethereum/core/types"
    27  	"github.com/ethereum/go-ethereum/core/vm"
    28  	"github.com/ethereum/go-ethereum/crypto"
    29  	"github.com/ethereum/go-ethereum/eth/tracers/logger"
    30  	"github.com/ethereum/go-ethereum/params"
    31  	"github.com/ethereum/go-ethereum/tests"
    32  )
    33  
    34  func BenchmarkTransactionTraceV2(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  	context := vm.BlockContext{
    51  		CanTransfer: core.CanTransfer,
    52  		Transfer:    core.Transfer,
    53  		Coinbase:    common.Address{},
    54  		BlockNumber: new(big.Int).SetUint64(uint64(5)),
    55  		Time:        5,
    56  		Difficulty:  big.NewInt(0xffffffff),
    57  		GasLimit:    gas,
    58  		BaseFee:     big.NewInt(8),
    59  	}
    60  	alloc := types.GenesisAlloc{}
    61  	// The code pushes 'deadbeef' into memory, then the other params, and calls CREATE2, then returns
    62  	// the address
    63  	loop := []byte{
    64  		byte(vm.JUMPDEST), //  [ count ]
    65  		byte(vm.PUSH1), 0, // jumpdestination
    66  		byte(vm.JUMP),
    67  	}
    68  	alloc[common.HexToAddress("0x00000000000000000000000000000000deadbeef")] = types.Account{
    69  		Nonce:   1,
    70  		Code:    loop,
    71  		Balance: big.NewInt(1),
    72  	}
    73  	alloc[from] = types.Account{
    74  		Nonce:   1,
    75  		Code:    []byte{},
    76  		Balance: big.NewInt(500000000000000),
    77  	}
    78  	state := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false, rawdb.HashScheme)
    79  	defer state.Close()
    80  
    81  	evm := vm.NewEVM(context, state.StateDB, params.AllEthashProtocolChanges, vm.Config{})
    82  
    83  	msg, err := core.TransactionToMessage(tx, signer, context.BaseFee)
    84  	if err != nil {
    85  		b.Fatalf("failed to prepare transaction for tracing: %v", err)
    86  	}
    87  	b.ResetTimer()
    88  	b.ReportAllocs()
    89  
    90  	for i := 0; i < b.N; i++ {
    91  		tracer := logger.NewStructLogger(&logger.Config{}).Hooks()
    92  		tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
    93  		evm.Config.Tracer = tracer
    94  
    95  		snap := state.StateDB.Snapshot()
    96  		_, err := core.ApplyMessage(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
    97  		if err != nil {
    98  			b.Fatal(err)
    99  		}
   100  		state.StateDB.RevertToSnapshot(snap)
   101  	}
   102  }