DEV Community

Cover image for Cross-Chain Deposits: Check Finality Before Crediting a User
HaoJie Luo (Fone)
HaoJie Luo (Fone)

Posted on Edited on Originally published at fone.cc AI-assisted

Cross-Chain Deposits: Check Finality Before Crediting a User

Download the Python lab and run the 14 tests locally. Check conflicting finalized chains and mismatched deposit logs before crediting a deposit.

Both RPCs return the same block when you ask for height 2. One also reports a finalized head at height 3 whose parent is a different block. If your indexer compares only the two answers at height 2, it can miss the inconsistency in the provider's own responses.

Here's a small way to reproduce that failure in Python. The hashes and transactions are synthetic, and the lab runs offline.

Give one head the wrong parent

Download the Python lab and extract it into an empty directory. Run this snippet from the directory containing finality.py.

from dataclasses import replace
from finality import fixture, agreed_anchor, Conflict

chain, log, receipt = fixture()
fork = dict(chain)
fork[3] = replace(chain[3], parent="other-block-2")

try:
    agreed_anchor(fork, fork[3], chain, chain[2])
except Conflict as error:
    print(error)  # tagged branch changed
Enter fullscreen mode Exit fullscreen mode

Both dictionaries still hold the same block at numeric height 2. The primary head points to other-block-2. When the checker walks back from that head, it expects to see other-block-2 and finds block-2 instead.

The checker uses the lower reported finalized height as a meeting point. It walks each branch from its own tagged head, checking that each retrieved block has the hash expected from the previous step. A missing ancestor yields Unavailable. A block with the wrong hash yields Conflict. Either result keeps the deposit waiting, but the latter gives you contradictory evidence to investigate.

Two tagged RPC heads traced by parent hash before checking the receipt and deposit log

Check the receipt, too

Even if the heads agree, the transaction receipt still has to contain the deposit event. The lab checks the contract address, block and transaction identities, success status and the full log. The tests cover cases where the receipt belongs to the right transaction but the claimed event doesn't match.

Run the suite and demo from the extracted directory. Python 3.10 or newer is sufficient.

python3 -m unittest -v
python3 finality.py
Enter fullscreen mode Exit fullscreen mode

The suite runs 14 tests. The demo accepts the synthetic receipt-backed event at block 1, then prints rejected: tagged branch changed. This lab checks the supplied observations. Live use requires a finality policy for the chain and a separate decision about when funds can move.

The full walkthrough shows where the parent walk stops and how the receipt checks handle missing data.

Top comments (0)