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