Skip to content

Commit

Permalink
Submit only per-hash logs
Browse files Browse the repository at this point in the history
  • Loading branch information
lukanus authored and tyler-smith committed Apr 1, 2024
1 parent 81b2a03 commit 971554f
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
1 change: 1 addition & 0 deletions core/vm/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
2 changes: 2 additions & 0 deletions eth/filters/trace_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,15 @@ 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
}

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 {
Expand Down
6 changes: 5 additions & 1 deletion eth/tracers/blocknative/blocknative.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ 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)
}

// 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"`

Expand Down
31 changes: 24 additions & 7 deletions eth/tracers/blocknative/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 971554f

Please sign in to comment.