
Interrupt the lab after it rebuilds a transfer view but before it advances the scan cursor. The database should contain none of that interrupted batch. Retry with the same deposit twice, and the view should show 70 synthetic units once. Those two checks give an indexer recovery discussion something concrete to inspect.
This tutorial builds a small SQLite fact store and a derived deposit view. You can replay a batch, detect conflicting data under the same event identity, and rebuild a damaged view from retained facts. The example has no RPC connection and accepts already-verified synthetic deposit events. All amounts and identities are teaching data.
I checked Passage Bridge's scanner implementation for the relevant transaction boundary. Its commit routine records blocks and events, rebuilds projections and advances the scan cursor inside one database transaction. The source also checks previous-block continuity and fences the scan lease before completing that transaction. The lab isolates the atomic database part so it can be run without the product stack.
Download the projection lab with complete Python code, ten tests and a README. Python 3.10 or newer with SQLite support is the target; the test log records the interpreter used for verification.
Give one observation a stable identity
A transaction can emit several logs. A transaction hash alone therefore cannot identify a deposit observation. The example's primary key includes chain, block hash, transaction hash and log index. The event payload additionally retains the block height, recipient and integer units.
That identity deliberately includes the block hash. Handling a replacement branch would require deciding which observations remain canonical before rebuilding the view. This lab accepts a single fixed branch and does not implement chain reorganization recovery. It demonstrates deduplication and atomic projection without inventing a finality decision.
The implementation compares a duplicate's payload with the persisted payload. An identical repeat is harmless. A repeat with a changed amount is rejected, and the enclosing batch rolls back. Silently ignoring every key collision would hide a materially different deposit under an old identity.
Passage's scanner deduplicates fetched logs using block hash, transaction hash and log index, while its database queries include environment and chain context. The source also prevents one event fact from being reused under a different deployment. An implementation serving several environments needs that extra scope. The teaching database models one environment and uses symbolic chain identifiers.
Run the failure before trying the happy path
Extract the ZIP into an empty directory, then run the following commands. The files use only the Python standard library.
python3 -m unittest -v
python3 projection.py
The demonstration first prints cursor zero with an empty view after the injected interruption. It then retries the batch with a duplicate log and prints cursor one with 70 units for the recipient. Ten tests cover duplicates, conflicting payloads and restart recovery, alongside empty ranges and incorrect batch boundaries.
The failure hook raises a Python exception after the projection write. This tests database transaction rollback. It does not reproduce power loss, disk corruption or a killed process with an incomplete filesystem write. Those need a different fault-injection environment and storage assumptions. The article's execution claim is limited to the packaged tests, run after a fresh extraction of the final ZIP.
Keep the cursor in the same transaction
The cursor records the next height to scan. Advancing it before the view commits creates a dangerous recovery position because a restarted process can skip the missing data. Leaving it behind after an already-committed batch is less surprising if event replay is idempotent, but it still creates unnecessary work.
The lab begins a write transaction before it checks the cursor. It rejects a range starting beyond that cursor, accepts a repeated earlier range, and uses the maximum of the old and new cursor values. Replaying an older batch can therefore verify its contents without moving scan progress backward.
An empty range still advances progress. A block without matching logs is a legitimate observation in this model. The caller is responsible for proving that the scanned range really was complete. Supplying an empty list without fetching the range would be an upstream evidence failure that this database API cannot discover.
The following method is the exact projection implementation included in the download.
def _rebuild(self, chain):
totals = {}
for (payload,) in self.db.execute("SELECT payload FROM facts WHERE chain=?", (chain,)):
event = json.loads(payload)
totals[event["recipient"]] = totals.get(event["recipient"], 0) + event["units"]
self.db.execute("DELETE FROM views WHERE chain=?", (chain,))
self.db.executemany("INSERT INTO views VALUES(?,?,?)",
[(chain, recipient, units) for recipient, units in sorted(totals.items())])
It recalculates totals from retained facts, replaces the chain's derived rows, and leaves transaction ownership to its caller. ingest calls it inside the batch transaction; the public rebuild method explicitly acquires a write transaction before reading any facts. This is intentionally a full rebuild suitable for a tiny teaching dataset. A production indexer needs a separately measured approach for its data volume.

Prove that the view can be replaced
The public entry point must lock before calculating totals. A connection context manager alone does not begin a transaction before a SELECT. Without an explicit transaction, another connection could ingest a new event after the totals were read, then have its updated view overwritten by the older calculation.
def rebuild(self, chain):
with self.db:
self.db.execute("BEGIN IMMEDIATE")
self._rebuild(chain)
The concurrency test pauses rebuilding after reading 70 units and before replacing the view. A second connection attempts to ingest another 30 units. It must wait or receive a lock error until rebuilding commits. The test retries after that commit and verifies a final total of 100 with the cursor at two. Without the explicit transaction, the controlled interleaving leaves the cursor at two but overwrites the view with 70.
The rebuild test first ingests a valid deposit, then deliberately changes the view's amount to 999. Rebuilding restores 70 from the event payload. That test establishes which table this design treats as the retained evidence and which table it treats as replaceable output.
A second recovery test closes the database and opens another connection to the same temporary file. Replaying the original batch produces the same total and cursor. This catches designs where the deduplication record lives only in process memory and disappears when the worker restarts.
The separate chain test uses the same symbolic transaction and log identifiers on two chains. Their views remain independent. Within one chain, two distinct log indices count as distinct observations. A developer can inspect these fixtures to decide whether the identity matches the events their adapter actually supplies.
Amounts here fit SQLite's signed integer storage. Real asset units can exceed that range. Choosing a numeric representation and testing its boundaries belongs in the production integration. The code should not be copied into a token ledger with its storage assumptions left implicit.
Ask for the recovery result during evaluation
A useful vendor demonstration interrupts a batch at the point where facts, view and cursor could disagree. The operator then restarts processing and shows the final database state. A screen returning to a green badge is insufficient evidence by itself; the retained facts and projected transfer should explain that badge.
Passage Bridge provides an operated bridge engineering starting point with its own scanner and projections. This independent lab demonstrates a database invariant, without replacing source verification, independent approval or destination inventory checks. The current relayer's signing path remains restricted to local execution, and production signing requires separate implementation and acceptance.
Bring one failing batch and its expected recovery state to the implementation review. Record whether the cursor advanced, whether duplicate observations changed the result, and whether rebuilding from retained facts reproduces the same transfer view.