github.com/codysnider/go-ethereum@v1.10.18-0.20220420071915-14f4ae99222a/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/common/hexutil" 25 "github.com/ethereum/go-ethereum/core" 26 "github.com/ethereum/go-ethereum/core/rawdb" 27 "github.com/ethereum/go-ethereum/core/types" 28 "github.com/ethereum/go-ethereum/core/vm" 29 "github.com/ethereum/go-ethereum/crypto" 30 "github.com/ethereum/go-ethereum/eth/tracers/logger" 31 "github.com/ethereum/go-ethereum/params" 32 "github.com/ethereum/go-ethereum/tests" 33 ) 34 35 // callTrace is the result of a callTracer run. 36 type callTrace struct { 37 Type string `json:"type"` 38 From common.Address `json:"from"` 39 To common.Address `json:"to"` 40 Input hexutil.Bytes `json:"input"` 41 Output hexutil.Bytes `json:"output"` 42 Gas *hexutil.Uint64 `json:"gas,omitempty"` 43 GasUsed *hexutil.Uint64 `json:"gasUsed,omitempty"` 44 Value *hexutil.Big `json:"value,omitempty"` 45 Error string `json:"error,omitempty"` 46 Calls []callTrace `json:"calls,omitempty"` 47 } 48 49 func BenchmarkTransactionTrace(b *testing.B) { 50 key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291") 51 from := crypto.PubkeyToAddress(key.PublicKey) 52 gas := uint64(1000000) // 1M gas 53 to := common.HexToAddress("0x00000000000000000000000000000000deadbeef") 54 signer := types.LatestSignerForChainID(big.NewInt(1337)) 55 tx, err := types.SignNewTx(key, signer, 56 &types.LegacyTx{ 57 Nonce: 1, 58 GasPrice: big.NewInt(500), 59 Gas: gas, 60 To: &to, 61 }) 62 if err != nil { 63 b.Fatal(err) 64 } 65 txContext := vm.TxContext{ 66 Origin: from, 67 GasPrice: tx.GasPrice(), 68 } 69 context := vm.BlockContext{ 70 CanTransfer: core.CanTransfer, 71 Transfer: core.Transfer, 72 Coinbase: common.Address{}, 73 BlockNumber: new(big.Int).SetUint64(uint64(5)), 74 Time: new(big.Int).SetUint64(uint64(5)), 75 Difficulty: big.NewInt(0xffffffff), 76 GasLimit: gas, 77 BaseFee: big.NewInt(8), 78 } 79 alloc := core.GenesisAlloc{} 80 // The code pushes 'deadbeef' into memory, then the other params, and calls CREATE2, then returns 81 // the address 82 loop := []byte{ 83 byte(vm.JUMPDEST), // [ count ] 84 byte(vm.PUSH1), 0, // jumpdestination 85 byte(vm.JUMP), 86 } 87 alloc[common.HexToAddress("0x00000000000000000000000000000000deadbeef")] = core.GenesisAccount{ 88 Nonce: 1, 89 Code: loop, 90 Balance: big.NewInt(1), 91 } 92 alloc[from] = core.GenesisAccount{ 93 Nonce: 1, 94 Code: []byte{}, 95 Balance: big.NewInt(500000000000000), 96 } 97 _, statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false) 98 // Create the tracer, the EVM environment and run it 99 tracer := logger.NewStructLogger(&logger.Config{ 100 Debug: false, 101 //DisableStorage: true, 102 //EnableMemory: false, 103 //EnableReturnData: false, 104 }) 105 evm := vm.NewEVM(context, txContext, statedb, params.AllEthashProtocolChanges, vm.Config{Debug: true, Tracer: tracer}) 106 msg, err := tx.AsMessage(signer, nil) 107 if err != nil { 108 b.Fatalf("failed to prepare transaction for tracing: %v", err) 109 } 110 b.ResetTimer() 111 b.ReportAllocs() 112 113 for i := 0; i < b.N; i++ { 114 snap := statedb.Snapshot() 115 st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas())) 116 _, err = st.TransitionDb() 117 if err != nil { 118 b.Fatal(err) 119 } 120 statedb.RevertToSnapshot(snap) 121 if have, want := len(tracer.StructLogs()), 244752; have != want { 122 b.Fatalf("trace wrong, want %d steps, have %d", want, have) 123 } 124 tracer.Reset() 125 } 126 }