Bitcoin Wallet Reorganization Reconciliation: UTXOs, Balances, and Transaction Status
A Bitcoin wallet's balance is a derived view of transaction outputs and their current spend status. It is not a number permanently attached to an address. When the active chain reorganizes, a wallet must reconcile outputs created and spent in disconnected blocks against the replacement branch.
Why naive balance caching fails
Imagine a wallet receives 100,000 satoshis in block A and immediately records that amount as confirmed. If block A is later disconnected, the wallet cannot keep the credit marked confirmed merely because it previously observed a valid block. The transaction may be unconfirmed, replaced by a conflicting spend, or included again in a later active block.
A similar problem occurs with outgoing transactions. A spend in a disconnected block may cease to consume an input on the active chain. Whether that input becomes available depends on replacement-chain transactions, mempool conflicts, and the wallet's own pending-spend tracking.
Separate observations from active-chain state
Store a transaction's txid, relevant output indexes, amounts, scriptPubKeys, observed block hash, and current status. Record block identity separately from height. A status such as confirmed must be supported by a block that is still in the active chain.
Confirmed balance should be computed from wallet-controlled unspent outputs in the active chain, according to the wallet's confirmation policy. Pending incoming amounts and pending outgoing spends require distinct treatment. A single cached total without provenance is difficult to repair correctly.
A reorg-aware update sequence
First detect that a previously tracked active block is no longer on the active branch. Find the last common ancestor with the newly active chain. Reverse disconnected blocks in descending order, then apply newly connected blocks in ascending order. Re-evaluate transactions that are no longer confirmed and update UTXO spendability and balances atomically.
Persist a checkpoint containing the processed block hash. On restart, compare that checkpoint to the node's active chain before continuing. If it is stale, reconcile from a valid ancestor rather than assuming the stored height is sufficient.
For descriptor wallets, rescan or rediscovery may be needed when historical records are missing. Scanning must use the wallet's actual script or descriptor scope, not merely addresses previously displayed by the interface. Recovery and gap limits are wallet-specific concerns distinct from consensus.
Mempool handling after disconnection
Do not assume that every transaction removed from a block will be accepted into the mempool. Policy, fee conditions, conflicts, and node configuration can affect admission. Track pending transactions separately from confirmed UTXOs and make status transitions reversible.
Testing reconciliation
Regtest is suitable for deterministic experiments: mine a payment, verify the balance, invalidate or reconsider blocks in a controlled environment, and inspect whether the wallet reverses and reapplies state. Bitcoin Core's invalidateblock and reconsiderblock RPCs are testing or administrative tools, not a normal payment-finality mechanism.
Test repeated notifications, process restarts, reorgs of different depths, conflicting spends, and transactions that are mined again. The same event processed twice must not double-credit or double-debit a wallet.
Primary references
Bitcoin Developer Guide — Transactions: https://developer.bitcoin.org/devguide/transactions.html
Bitcoin Core RPC — invalidateblock: https://bitcoincore.org/en/doc/
Bitcoin Core RPC — gettxout: https://bitcoincore.org/en/doc/
