- Category
- Operations
- Reading time
- 10 minutes
- Published
How to backfill EVM transaction traces without gaps
A trace backfill often begins with a list of transaction hashes and a loop around debug_traceTransaction. That works for a few known transactions. It does not prove that every transaction in a block range was traced, or tell a restarted worker which results were safely committed.
For a complete historical export, define the block range and output schema first. Then build a durable inventory, trace every expected transaction, and advance the cursor only after the block is complete. This guide covers that workflow, including method qualification, slow calls, reorganizations, and a final completeness audit. It describes application-side data handling, not a promise that every EVM endpoint exposes the same trace methods or history.
What should count as a complete trace backfill?
Choose the chain, inclusive start and end blocks, required transaction population, tracer or trace family, and output fields. Decide whether the job needs every transaction, only transactions involving known addresses, or only transactions selected by another index. Those are different completeness claims.
For an all-transaction export, record each block number and hash, its ordered transaction hashes, and the expected transaction count. The Ethereum block method can return transaction hashes when its full-transaction flag is false. A block with zero transactions is still a completed block, not a gap. Save the exact tracer configuration and parser version with the job so a later run cannot silently mix incompatible results.
Which trace API should the job use?
Pick the result shape from the downstream question. debug_traceTransaction with callTracer yields a nested call tree for one transaction where supported. A block-level debug method can return a trace for every transaction in one block, but support, timeouts, and response size must be tested with the actual endpoint. The draft block-tracing definition describes results ordered by transaction index and paired with transaction hashes. It does not make that method or a named tracer universal.
The separate trace_* family has a different flat schema and may offer block or address-filtered operations. It is not interchangeable with debug_*. If the output contract depends on one family, qualify that exact chain, method, tracer, options, and oldest required block. The trace API comparison covers the schema decision in detail.
Test a recent quiet block, a busy block, an old block, a reverted transaction, and a block with no transactions. A successful head trace does not establish historical coverage. If the oldest block or required tracer fails, narrow the declared scope or choose an endpoint that can actually complete it before starting the export.
How do you inventory blocks before tracing?
Read each numbered block in the target interval and persist its hash, parent hash, transaction count, and ordered transaction hashes. Verify adjacent parent hashes. A missing block response or broken parent link is an unresolved inventory item, not a block to skip. Record the chain ID so a resumed job cannot mix data from another network.
Use this inventory as the reconciliation target. Per-transaction calls should produce one terminal outcome for each expected hash. For a block-level call, match every returned transaction hash to the inventory, reject duplicates or unexpected hashes, and check that the returned count equals the expected count. Preserve raw responses when practical, since a parser correction should not require paying for the same trace again.
A block number alone is not stable identity near the tip. Anchor stored results to the block hash and choose a finality policy appropriate to the chain and product. For an immutable historical interval, still verify the boundary hashes before a long run and at completion.
What should the checkpoint contain?
Store the highest contiguous completed block number and hash, not the highest block a worker has started. For each block, persist the inventory, trace outcomes, and a completion marker in one database transaction. Advance the contiguous cursor only when every expected transaction has a terminal result and the block hash still matches the inventory. A crash before commit leaves that block eligible for replay.
A terminal failure may be recorded for audit, but it is not a successful trace. Keep failed or unsupported items in a separate unresolved queue and do not call the interval complete while that queue is nonempty. Key stored traces by chain ID, block hash, transaction hash, trace method, tracer options, and parser version. Upserts make a replay safe, while the block hash prevents an orphaned result from masquerading as canonical.
Parallel workers may finish blocks out of order. Let them persist results independently, then move the contiguous cursor across only the fully completed prefix. This prevents a quick later block from hiding a slow earlier one.
How should trace concurrency and retries be bounded?
Trace execution and responses can be much heavier than a block header read. Start with a small, measured worker pool. Set separate limits for concurrent traces, response bytes, per-call deadline, and total retry time. Measure completed transactions per minute and oldest unresolved block rather than only request rate. A wide fan-out that raises timeout and retry rates can reduce completed work.
Retry transport failures and explicitly transient server failures with backoff and a finite attempt budget. Preserve the original block and transaction identity across attempts. Do not retry an unsupported method, unavailable history, or a deterministic parser error as though it were congestion. A timeout is ambiguous, so record it and retry within the budget rather than treating it as a completed trace. Avoid automatic hedging of expensive traces unless measurements show it improves useful completions without unacceptable duplicate work.
If a block-level response is too large or times out, fall back to per-transaction tracing only when the endpoint supports the same tracer and output contract. Reconcile the per-transaction results against the saved inventory before marking the block complete. Do not silently switch schemas.
How do you recover after a restart or reorganization?
On restart, load the last contiguous completed block and hash. Re-read that block and the next work item, verify the chain ID and hashes, then resume unresolved blocks. Replaying an already committed block should produce the same keys and no duplicate application rows. Keep the inventory and database state, since a CSV export alone cannot tell the worker which trace results were committed.
For an unfinalized range, compare stored block hashes with the canonical chain and walk backward to a common ancestor when they differ. Mark orphaned block results invalid, rebuild the affected inventory, and replay forward before advancing the cursor. A transaction hash can appear in a different block after a reorganization, which is why the block hash belongs in the trace key. The block-tag guide explains when to wait for a safer boundary instead of operating near the tip.
How can you prove the export is complete?
Audit the requested block interval against the saved inventory. Every block number must have one canonical hash and a completion marker. For each block, compare the expected transaction hashes and count with the successful trace outcomes. Report missing, duplicate, unexpected, failed, and orphaned records separately. Recheck a sample of stored block hashes and selected trace semantics against current RPC responses.
A finished worker queue is not a completeness proof. The useful final record is a manifest containing chain ID, inclusive block bounds, boundary hashes, method and tracer configuration, parser version, expected and completed transaction counts, unresolved count, and audit time. Only label the export complete when the unresolved count is zero and the contiguous cursor reaches the declared end block.
If the deliverable is a CSV or another derived file, generate it from the committed database state. Keep the database checkpoint so the job can resume or be re-audited without guessing from file length.
Trace backfill checklist
- Define the chain, inclusive block range, transaction population, output schema, and finality policy.
- Qualify the exact trace method, tracer options, oldest block, busy block, and empty block.
- Persist block hashes, parent hashes, ordered transaction hashes, and expected counts.
- Bound trace concurrency, response size, deadlines, and retries.
- Commit each complete block and its trace outcomes atomically, then advance only a contiguous cursor.
- Keep unsupported and failed traces in an unresolved queue.
- Replay safely after restart and invalidate orphaned block results after a reorganization.
- Audit every block and transaction against the original inventory before calling the export complete.
For an event-only export, traces may be unnecessary. The `eth_getLogs` backfill guide describes the smaller path when emitted logs answer the product question.