From b031b3566e0a8380ae8601ec54c970df763e2672 Mon Sep 17 00:00:00 2001 From: Alex Sharov Date: Fri, 10 Jan 2025 20:03:12 +0700 Subject: [PATCH] trace to set txnIndex and blockHash (#13294) --- polygon/tracer/trace_bor_state_sync_txn.go | 3 ++- turbo/jsonrpc/tracing.go | 16 +++++++++------- turbo/transactions/tracing.go | 8 ++++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/polygon/tracer/trace_bor_state_sync_txn.go b/polygon/tracer/trace_bor_state_sync_txn.go index b87b481cab2..a032b28153c 100644 --- a/polygon/tracer/trace_bor_state_sync_txn.go +++ b/polygon/tracer/trace_bor_state_sync_txn.go @@ -48,9 +48,10 @@ func TraceBorStateSyncTxnDebugAPI( stream *jsoniter.Stream, callTimeout time.Duration, msgs []*types.Message, + txIndex int, ) (usedGas uint64, err error) { txCtx := initStateSyncTxContext(blockNum, blockHash) - tracer, streaming, cancel, err := transactions.AssembleTracer(ctx, traceConfig, txCtx.TxHash, stream, callTimeout) + tracer, streaming, cancel, err := transactions.AssembleTracer(ctx, traceConfig, txCtx.TxHash, blockHash, txIndex, stream, callTimeout) if err != nil { stream.WriteNil() return usedGas, err diff --git a/turbo/jsonrpc/tracing.go b/turbo/jsonrpc/tracing.go index d4361c07e75..b9b53c563c2 100644 --- a/turbo/jsonrpc/tracing.go +++ b/turbo/jsonrpc/tracing.go @@ -137,7 +137,7 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp } var usedGas uint64 - for idx, txn := range txns { + for txnIndex, txn := range txns { isBorStateSyncTxn := borStateSyncTxn == txn var txnHash common.Hash if isBorStateSyncTxn { @@ -159,7 +159,7 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp stream.WriteArrayEnd() return ctx.Err() } - ibs.SetTxContext(idx) + ibs.SetTxContext(txnIndex) msg, _ := txn.AsMessage(*signer, block.BaseFee(), rules) if msg.FeeCap().IsZero() && engine != nil { @@ -196,11 +196,12 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp stream, api.evmCallTimeout, stateSyncEvents, + txnIndex, ) usedGas += _usedGas } else { var _usedGas uint64 - _usedGas, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream, api.evmCallTimeout) + _usedGas, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, block.Hash(), txnIndex, ibs, config, chainConfig, stream, api.evmCallTimeout) usedGas += _usedGas } if err == nil { @@ -214,7 +215,7 @@ func (api *PrivateDebugAPIImpl) traceBlock(ctx context.Context, blockNrOrHash rp } stream.WriteObjectEnd() - if idx != len(txns)-1 { + if txnIndex != len(txns)-1 { stream.WriteMore() } @@ -352,6 +353,7 @@ func (api *PrivateDebugAPIImpl) TraceTransaction(ctx context.Context, hash commo stream, api.evmCallTimeout, stateSyncEvents, + txnIndex, ) return err } @@ -363,7 +365,7 @@ func (api *PrivateDebugAPIImpl) TraceTransaction(ctx context.Context, hash commo } // Trace the transaction and return - _, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream, api.evmCallTimeout) + _, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, block.Hash(), txnIndex, ibs, config, chainConfig, stream, api.evmCallTimeout) return err } @@ -432,7 +434,7 @@ func (api *PrivateDebugAPIImpl) TraceCall(ctx context.Context, args ethapi.CallA blockCtx := transactions.NewEVMBlockContext(engine, header, blockNrOrHash.RequireCanonical, dbtx, api._blockReader, chainConfig) txCtx := core.NewEVMTxContext(msg) // Trace the transaction and return - _, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, ibs, config, chainConfig, stream, api.evmCallTimeout) + _, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, hash, 0, ibs, config, chainConfig, stream, api.evmCallTimeout) return err } @@ -570,7 +572,7 @@ func (api *PrivateDebugAPIImpl) TraceCallMany(ctx context.Context, bundles []Bun } txCtx = core.NewEVMTxContext(msg) ibs.SetTxContext(txnIndex) - _, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, evm.IntraBlockState(), config, chainConfig, stream, api.evmCallTimeout) + _, err = transactions.TraceTx(ctx, msg, blockCtx, txCtx, block.Hash(), txnIndex, evm.IntraBlockState(), config, chainConfig, stream, api.evmCallTimeout) if err != nil { stream.WriteArrayEnd() stream.WriteArrayEnd() diff --git a/turbo/transactions/tracing.go b/turbo/transactions/tracing.go index a36d0ff3aa3..ffd7db24f59 100644 --- a/turbo/transactions/tracing.go +++ b/turbo/transactions/tracing.go @@ -102,13 +102,15 @@ func TraceTx( message core.Message, blockCtx evmtypes.BlockContext, txCtx evmtypes.TxContext, + blockHash libcommon.Hash, + txnIndex int, ibs evmtypes.IntraBlockState, config *tracersConfig.TraceConfig, chainConfig *chain.Config, stream *jsoniter.Stream, callTimeout time.Duration, ) (usedGas uint64, err error) { - tracer, streaming, cancel, err := AssembleTracer(ctx, config, txCtx.TxHash, stream, callTimeout) + tracer, streaming, cancel, err := AssembleTracer(ctx, config, txCtx.TxHash, blockHash, txnIndex, stream, callTimeout) if err != nil { stream.WriteNil() return 0, err @@ -134,6 +136,8 @@ func AssembleTracer( ctx context.Context, config *tracersConfig.TraceConfig, txHash libcommon.Hash, + blockHash libcommon.Hash, + txnIndex int, stream *jsoniter.Stream, callTimeout time.Duration, ) (vm.EVMLogger, bool, context.CancelFunc, error) { @@ -155,7 +159,7 @@ func AssembleTracer( if config != nil && config.TracerConfig != nil { cfg = *config.TracerConfig } - tracer, err := tracers.New(*config.Tracer, &tracers.Context{TxHash: txHash}, cfg) + tracer, err := tracers.New(*config.Tracer, &tracers.Context{TxHash: txHash, TxIndex: txnIndex, BlockHash: blockHash}, cfg) if err != nil { return nil, false, func() {}, err }