From 971554f8ee8730e06e78da1a2b2191c10ab84a4d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Mi=C5=82kowski?= Date: Fri, 29 Mar 2024 21:09:21 +0100 Subject: [PATCH] Submit only per-hash logs --- core/vm/interface.go | 1 + eth/filters/trace_api.go | 2 ++ eth/tracers/blocknative/blocknative.go | 6 ++++- eth/tracers/blocknative/tracer.go | 31 ++++++++++++++++++++------ 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/core/vm/interface.go b/core/vm/interface.go index 4aef4271a78e..a72d1688d041 100644 --- a/core/vm/interface.go +++ b/core/vm/interface.go @@ -82,6 +82,7 @@ type StateDB interface { // BlockNative additions Logs() []*types.Log + GetLogs(hash common.Hash, blockNumber uint64, blockHash common.Hash) []*types.Log IntermediateRoot(bool) common.Hash } diff --git a/eth/filters/trace_api.go b/eth/filters/trace_api.go index ff62c6cb6fe4..1b09eaa454fe 100644 --- a/eth/filters/trace_api.go +++ b/eth/filters/trace_api.go @@ -306,6 +306,7 @@ func traceTx(message *core.Message, txCtx *tracers.Context, vmctx vm.BlockContex func traceBlockTx(message *core.Message, txCtx *tracers.Context, vmctx vm.BlockContext, chainConfig *params.ChainConfig, statedb *state.StateDB, tracerOpts blocknative.TracerOpts) (*core.ExecutionResult, *blocknative.Trace, error) { tracerOpts.DisableBlockContext = false + tracerOpts.PerHashLogs = true tracer, err := blocknative.NewTracerWithOpts(tracerOpts) if err != nil { return nil, nil, err @@ -313,6 +314,7 @@ func traceBlockTx(message *core.Message, txCtx *tracers.Context, vmctx vm.BlockC vmenv := vm.NewEVM(vmctx, core.NewEVMTxContext(message), statedb, chainConfig, vm.Config{Tracer: tracer, NoBaseFee: false}) statedb.SetTxContext(txCtx.TxHash, txCtx.TxIndex) + tracer.SetTxContext(txCtx.TxHash, txCtx.TxIndex) result, err := core.ApplyMessage(vmenv, message, new(core.GasPool).AddGas(message.GasLimit)) if err != nil { diff --git a/eth/tracers/blocknative/blocknative.go b/eth/tracers/blocknative/blocknative.go index c67fe0ba8894..15467ba5e3f9 100644 --- a/eth/tracers/blocknative/blocknative.go +++ b/eth/tracers/blocknative/blocknative.go @@ -14,6 +14,7 @@ import ( // resulting Trace object directly. type Tracer interface { vm.EVMLogger + SetTxContext(thash common.Hash, ti int) GetTrace() (*Trace, error) GetResult() (json.RawMessage, error) Stop(err error) @@ -21,7 +22,10 @@ type Tracer interface { // TracerOpts configure the tracer to save or ignore various aspects of a transaction execution. type TracerOpts struct { - Logs bool `json:"logs"` + Logs bool `json:"logs"` + // Get per tx hash logs + PerHashLogs bool `json:"per_hash_logs"` + Decode bool `json:"decode"` BalanceChanges bool `json:"balanceChanges"` diff --git a/eth/tracers/blocknative/tracer.go b/eth/tracers/blocknative/tracer.go index e149690f8cc3..69c53e0184ee 100644 --- a/eth/tracers/blocknative/tracer.go +++ b/eth/tracers/blocknative/tracer.go @@ -27,6 +27,9 @@ type tracer struct { evm *vm.EVM decoder *decoder.Decoder + thash common.Hash // transaction has + txIndex int // transaction index + trace Trace startTime time.Time callStack []CallFrame @@ -63,7 +66,11 @@ func NewTracerWithOpts(opts TracerOpts) (Tracer, error) { } return &t, nil +} +func (t *tracer) SetTxContext(thash common.Hash, ti int) { + t.thash = thash + t.txIndex = ti } // SetStateRoot implements core.stateRootSetter and stores the given root in the @@ -135,17 +142,27 @@ func (t *tracer) CaptureEnd(output []byte, gasUsed uint64, err error) { // If the user wants the logs, grab them from the state if t.opts.Logs { - for _, stateLog := range t.evm.StateDB.Logs() { - t.trace.Logs = append(t.trace.Logs, CallLog{ - Address: stateLog.Address, - Data: stateLog.Data, - Topics: stateLog.Topics, - }) + if t.opts.PerHashLogs { + for _, stateLog := range t.evm.StateDB.GetLogs(t.thash, 0, common.Hash{}) { + t.trace.Logs = append(t.trace.Logs, CallLog{ + Address: stateLog.Address, + Data: stateLog.Data, + Topics: stateLog.Topics, + }) + } + } else { + for _, stateLog := range t.evm.StateDB.Logs() { + t.trace.Logs = append(t.trace.Logs, CallLog{ + Address: stateLog.Address, + Data: stateLog.Data, + Topics: stateLog.Topics, + }) + } } } // Add total time duration for this trace request - t.trace.Time = time.Now().Sub(t.startTime).Nanoseconds() + t.trace.Time = time.Since(t.startTime).Nanoseconds() } // CaptureEnter is called before any new sub-call starts.