github.com/carter-ya/go-ethereum@v0.0.0-20230628080049-d2309be3983b/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 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: new(big.Int).SetUint64(uint64(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{Debug: true, Tracer: tracer}) 91 msg, err := tx.AsMessage(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 }