eth/tracers: fix issues with benchmarks #30667 (#1500)

This commit is contained in:
Daniel Liu 2025-09-17 08:18:51 +08:00 committed by GitHub
parent ea187b53bd
commit 35e5143fe7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 37 additions and 20 deletions

View file

@ -28,7 +28,6 @@ import (
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/hexutil"
"github.com/XinFinOrg/XDPoSChain/common/math"
"github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/core/rawdb"
"github.com/XinFinOrg/XDPoSChain/core/types"
@ -40,14 +39,6 @@ import (
"github.com/XinFinOrg/XDPoSChain/tests"
)
type callContext struct {
Number math.HexOrDecimal64 `json:"number"`
Difficulty *math.HexOrDecimal256 `json:"difficulty"`
Time math.HexOrDecimal64 `json:"timestamp"`
GasLimit math.HexOrDecimal64 `json:"gasLimit"`
Miner common.Address `json:"miner"`
}
// callLog is the result of LOG opCode
type callLog struct {
Address common.Address `json:"address"`
@ -217,7 +208,7 @@ func BenchmarkTracers(b *testing.B) {
func benchTracer(tracerName string, test *callTracerTest, b *testing.B) {
// Configure a blockchain with the given prestate
tx := new(types.Transaction)
if err := rlp.DecodeBytes(common.FromHex(test.Input), tx); err != nil {
if err := tx.UnmarshalBinary(common.FromHex(test.Input)); err != nil {
b.Fatalf("failed to parse testcase input: %v", err)
}
signer := types.MakeSigner(test.Genesis.Config, new(big.Int).SetUint64(uint64(test.Context.Number)))
@ -226,15 +217,7 @@ func benchTracer(tracerName string, test *callTracerTest, b *testing.B) {
Origin: origin,
GasPrice: tx.GasPrice(),
}
context := vm.BlockContext{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
Coinbase: test.Context.Miner,
BlockNumber: new(big.Int).SetUint64(uint64(test.Context.Number)),
Time: uint64(test.Context.Time),
Difficulty: (*big.Int)(test.Context.Difficulty),
GasLimit: uint64(test.Context.GasLimit),
}
context := test.Context.toBlockContext(test.Genesis)
msg, err := core.TransactionToMessage(tx, signer, nil, nil, context.BaseFee)
if err != nil {
b.Fatalf("failed to prepare transaction for tracing: %v", err)

View file

@ -1,9 +1,15 @@
package tracetest
import (
"math/big"
"strings"
"unicode"
"github.com/XinFinOrg/XDPoSChain/common"
"github.com/XinFinOrg/XDPoSChain/common/math"
"github.com/XinFinOrg/XDPoSChain/core"
"github.com/XinFinOrg/XDPoSChain/core/vm"
// Force-load native and js packages, to trigger registration
_ "github.com/XinFinOrg/XDPoSChain/eth/tracers/js"
_ "github.com/XinFinOrg/XDPoSChain/eth/tracers/native"
@ -17,3 +23,29 @@ func camel(str string) string {
}
return strings.Join(pieces, "")
}
type callContext struct {
Number math.HexOrDecimal64 `json:"number"`
Difficulty *math.HexOrDecimal256 `json:"difficulty"`
Time math.HexOrDecimal64 `json:"timestamp"`
GasLimit math.HexOrDecimal64 `json:"gasLimit"`
Miner common.Address `json:"miner"`
BaseFee *math.HexOrDecimal256 `json:"baseFeePerGas"`
}
func (c *callContext) toBlockContext(genesis *core.Genesis) vm.BlockContext {
context := vm.BlockContext{
CanTransfer: core.CanTransfer,
Transfer: core.Transfer,
Coinbase: c.Miner,
BlockNumber: new(big.Int).SetUint64(uint64(c.Number)),
Time: uint64(c.Time),
Difficulty: (*big.Int)(c.Difficulty),
GasLimit: uint64(c.GasLimit),
}
if genesis.Config.IsLondon(context.BlockNumber) {
context.BaseFee = (*big.Int)(c.BaseFee)
}
return context
}

View file

@ -98,11 +98,13 @@ func BenchmarkTransactionTrace(b *testing.B) {
for i := 0; i < b.N; i++ {
snap := state.Snapshot()
tracer.OnTxStart(evm.GetVMContext(), tx, msg.From)
st := core.NewStateTransition(evm, msg, new(core.GasPool).AddGas(tx.Gas()))
_, err = st.TransitionDb(common.Address{})
res, err := st.TransitionDb(common.Address{})
if err != nil {
b.Fatal(err)
}
tracer.OnTxEnd(&types.Receipt{GasUsed: res.UsedGas}, nil)
state.RevertToSnapshot(snap)
if have, want := len(tracer.StructLogs()), 244752; have != want {
b.Fatalf("trace wrong, want %d steps, have %d", want, have)