THAMER
Sovereign Bitcoin Wallet

WALLET AI BITCOIN

Your Bitcoin Wallet. Your Keys. Your Engine.

Self-hosted Bitcoin software built for developers, applications, and AI agents.

CREATE WALLET

Bitcoin Core Block Notifications: ZMQ Reorganizations and Wallet Reconciliation

permalink THAMER
#bitcoin#block-notifications#reorg#wallet#zmq

A block notification tells an application that something changed at its Bitcoin node. It does not establish that every previously observed block remains in the active chain. A reliable wallet service must reconcile block identities, transaction confirmations and UTXO changes.

Track block identity, not only height

Two competing blocks can have the same height. Store the last processed block hash alongside its height. When a new notification arrives, query the node for the current active tip and compare it with the stored checkpoint.

Useful RPC calls include getbestblockhash, getblockheader and getblockhash. The header response includes the previous block hash for non-genesis blocks. A header known to the node is not necessarily part of its active chain.

Detect and process a fork

If the new active tip does not descend from the stored checkpoint, locate the common ancestor using available headers. Reverse the application changes associated with disconnected blocks and apply the newly connected branch in ascending height order. The sequence topic exposes explicit C and D events, but the application must still tolerate missing notifications.

A wallet database can store a journal of changes per block: outputs created, outputs spent, and transaction status transitions. Rollback should restore the previous application state rather than simply subtracting the total value of a disconnected block. Transactions may appear on both branches.

Atomic persistence

Update the wallet records and block checkpoint within the same database transaction. If a process crashes before committing, the next run can repeat the update. Make block processing idempotent so that repeated notifications do not duplicate credits or debits.

After processing, check the active tip again. The chain can change while RPC queries are running, so a series of independent calls does not automatically form a consistent snapshot. Repeat reconciliation when the observed chain changes.

Pruned node limitations

A pruned node retains headers and current chainstate but may no longer have historical block bodies. An application that needs old transaction details must retain its own relevant history or obtain the missing data from a suitable source. Never silently treat unavailable block data as an empty block.

A payment's confirmation count should be derived from its membership in the active chain. Notifications are useful for reducing latency, while reconciliation provides the evidence needed to maintain a correct wallet balance.

Primary references

Bitcoin Core ZMQ documentation: https://github.com/bitcoin/bitcoin/blob/master/doc/zmq.md
Bitcoin Core RPC documentation: https://bitcoincore.org/en/doc/

Questions & Answers

Explore all questions →

Latest from the Lab

THAMER illustrated developer portrait and signature

ABOUT THE DEVELOPER

Independent Software Developer specializing in Bitcoin Script, Golang, and AI Agents.

Software engineering · Independent research · Open-source projects