ReorgDetector

interface ReorgDetector

Detects and handles blockchain reorganizations (reorgs).

What is a Reorg? When two competing blocks are mined at same height, blockchain temporarily forks. Eventually one fork becomes canonical, invalidating events in the other fork.

Example:

Before Reorg:
... → Block 100 → Block 101A → Block 102A

After Reorg (longer chain found):
... → Block 100 → Block 101B → Block 102B → Block 103B

Events in 101A and 102A are now INVALID and must be discarded.

Why This Matters:

  • Without reorg handling, wallet shows incorrect balance

  • User may spend funds they don't actually have

  • Double-spend attacks possible

Detection Strategy:

  1. Track latest block hash we've seen

  2. When new block arrives, verify parent hash matches our latest

  3. If mismatch, reorg detected at that height

  4. Roll back to last common ancestor

  5. Re-sync from that point forward

Inheritors

Functions

Link copied to clipboard
abstract suspend fun getChainTip(): BlockInfo?

Get current canonical chain tip.

Link copied to clipboard
abstract fun observeReorgs(): Flow<ReorgEvent>

Observe reorg events as they occur.

Link copied to clipboard
abstract suspend fun recordBlock(block: BlockInfo, parentHash: String): ReorgEvent?

Record a new block in the chain.

Link copied to clipboard
abstract suspend fun reset()

Reset detector state (for testing or recovery).