tracers_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. package tracers
  17. import (
  18. "math/big"
  19. "testing"
  20. "github.com/ethereum/go-ethereum/common"
  21. "github.com/ethereum/go-ethereum/core"
  22. "github.com/ethereum/go-ethereum/core/rawdb"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/core/vm"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. "github.com/ethereum/go-ethereum/eth/tracers/logger"
  27. "github.com/ethereum/go-ethereum/params"
  28. "github.com/ethereum/go-ethereum/tests"
  29. )
  30. func BenchmarkTransactionTrace(b *testing.B) {
  31. key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  32. from := crypto.PubkeyToAddress(key.PublicKey)
  33. gas := uint64(1000000) // 1M gas
  34. to := common.HexToAddress("0x00000000000000000000000000000000deadbeef")
  35. signer := types.LatestSignerForChainID(big.NewInt(1337))
  36. tx, err := types.SignNewTx(key, signer,
  37. &types.LegacyTx{
  38. Nonce: 1,
  39. GasPrice: big.NewInt(500),
  40. Gas: gas,
  41. To: &to,
  42. })
  43. if err != nil {
  44. b.Fatal(err)
  45. }
  46. txContext := vm.TxContext{
  47. Origin: from,
  48. GasPrice: tx.GasPrice(),
  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: new(big.Int).SetUint64(uint64(5)),
  56. Difficulty: big.NewInt(0xffffffff),
  57. GasLimit: gas,
  58. BaseFee: big.NewInt(8),
  59. }
  60. alloc := core.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")] = core.GenesisAccount{
  69. Nonce: 1,
  70. Code: loop,
  71. Balance: big.NewInt(1),
  72. }
  73. alloc[from] = core.GenesisAccount{
  74. Nonce: 1,
  75. Code: []byte{},
  76. Balance: big.NewInt(500000000000000),
  77. }
  78. _, statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false)
  79. // Create the tracer, the EVM environment and run it
  80. tracer := logger.NewStructLogger(&logger.Config{
  81. Debug: false,
  82. //DisableStorage: true,
  83. //EnableMemory: false,
  84. //EnableReturnData: false,
  85. })
  86. evm := vm.NewEVM(context, txContext, statedb, params.AllEthashProtocolChanges, vm.Config{Debug: true, Tracer: tracer})
  87. msg, err := tx.AsMessage(signer, nil)
  88. if err != nil {
  89. b.Fatalf("failed to prepare transaction for tracing: %v", err)
  90. }
  91. b.ResetTimer()
  92. b.ReportAllocs()
  93. for i := 0; i < b.N; i++ {
  94. snap := statedb.Snapshot()
  95. st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
  96. _, err = st.TransitionDb()
  97. if err != nil {
  98. b.Fatal(err)
  99. }
  100. statedb.RevertToSnapshot(snap)
  101. if have, want := len(tracer.StructLogs()), 244752; have != want {
  102. b.Fatalf("trace wrong, want %d steps, have %d", want, have)
  103. }
  104. tracer.Reset()
  105. }
  106. }