eth/tracers: improve tracing performance (#23016)

Improves the performance of debug.traceTransaction
This commit is contained in:
Martin Holst Swende
2021-07-01 09:15:04 +02:00
committed by GitHub
parent bbbeb7d8ba
commit f5f906dd0d
5 changed files with 105 additions and 34 deletions

View File

@ -300,3 +300,81 @@ func jsonEqual(x, y interface{}) bool {
}
return reflect.DeepEqual(xTrace, yTrace)
}
func BenchmarkTransactionTrace(b *testing.B) {
key, _ := crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
from := crypto.PubkeyToAddress(key.PublicKey)
gas := uint64(1000000) // 1M gas
to := common.HexToAddress("0x00000000000000000000000000000000deadbeef")
signer := types.LatestSignerForChainID(big.NewInt(1337))
tx, err := types.SignNewTx(key, signer,
&types.LegacyTx{
Nonce: 1,
GasPrice: big.NewInt(500),
Gas: gas,
To: &to,
})
if err != nil {
b.Fatal(err)
}
txContext := vm.TxContext{
Origin: from,
GasPrice: tx.GasPrice(),
}
context := vm.BlockContext{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
Coinbase: common.Address{},
BlockNumber: new(big.Int).SetUint64(uint64(5)),
Time: new(big.Int).SetUint64(uint64(5)),
Difficulty: big.NewInt(0xffffffff),
GasLimit: gas,
}
alloc := core.GenesisAlloc{}
// The code pushes 'deadbeef' into memory, then the other params, and calls CREATE2, then returns
// the address
loop := []byte{
byte(vm.JUMPDEST), // [ count ]
byte(vm.PUSH1), 0, // jumpdestination
byte(vm.JUMP),
}
alloc[common.HexToAddress("0x00000000000000000000000000000000deadbeef")] = core.GenesisAccount{
Nonce: 1,
Code: loop,
Balance: big.NewInt(1),
}
alloc[from] = core.GenesisAccount{
Nonce: 1,
Code: []byte{},
Balance: big.NewInt(500000000000000),
}
_, statedb := tests.MakePreState(rawdb.NewMemoryDatabase(), alloc, false)
// Create the tracer, the EVM environment and run it
tracer := vm.NewStructLogger(&vm.LogConfig{
Debug: false,
//DisableStorage: true,
//DisableMemory: true,
//DisableReturnData: true,
})
evm := vm.NewEVM(context, txContext, statedb, params.AllEthashProtocolChanges, vm.Config{Debug: true, Tracer: tracer})
msg, err := tx.AsMessage(signer, nil)
if err != nil {
b.Fatalf("failed to prepare transaction for tracing: %v", err)
}
b.ResetTimer()
b.ReportAllocs()
for i := 0; i < b.N; i++ {
snap := statedb.Snapshot()
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
_, err = st.TransitionDb()
if err != nil {
b.Fatal(err)
}
statedb.RevertToSnapshot(snap)
if have, want := len(tracer.StructLogs()), 244752; have != want {
b.Fatalf("trace wrong, want %d steps, have %d", want, have)
}
tracer.Reset()
}
}