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 Undo Data: rev Files, Block Disconnection, and Reorganizations

permalink THAMER
#bitcoin#reorganization#rev-files#undo-data

Connecting a Bitcoin block changes the unspent transaction output set. Disconnecting that block requires reversing those changes. Bitcoin Core stores undo information so that it can restore previously spent coins without reconstructing the entire blockchain for every ordinary reorganization.

What undo data contains

When a transaction spends an existing output, the node removes that coin from the UTXO set. Undo information preserves the coin data needed to restore it, including the output amount, script and relevant creation metadata. Newly created outputs can instead be identified from the block being disconnected. Undo records are not a complete duplicate of the block and do not contain private keys.

Bitcoin Core normally stores undo records in numbered rev files associated with block storage files. These are local implementation artifacts, not additional messages that miners must publish or consensus commitments that peers must transmit. A node derives undo information while validating and connecting blocks.

Reversing a block

Consider a block containing a transaction that spends a 20,000 satoshi coin and creates outputs of 15,000 and 4,500 satoshis. The remaining 500 satoshis represent its fee. When the block is disconnected, the new outputs are removed and the original coin is restored using undo information. Other transactions and coinbase outputs in the block must also be reversed correctly.

Transactions within a block can spend outputs created earlier in that same block. Reversing transaction order is therefore important: a later spend must be undone before removing the output that the earlier transaction created. The resulting UTXO state must correspond to the parent block, not a mixture of states from different heights.

Reorganization and pruning

During a reorganization, the node disconnects blocks from the old active branch and connects validated blocks from the competing branch. Transactions from disconnected blocks may be reconsidered for the mempool, but acceptance is not guaranteed. They may conflict with the new branch or fail current policy.

Pruning removes old block and undo data together under managed conditions. Bitcoin Core retains recent block data for normal operation, but a sufficiently deep reorganization can exceed locally retained history. Recovery may require obtaining historical data again. A pruned node should not be treated as an unlimited historical archive.

Application boundary

Wallet software should not parse rev files as its primary accounting interface. Instead, reconcile against the active chain through supported RPCs, record block hashes, and update transaction states when confirmations change. Undo data is an internal mechanism for node validation; application-level accounting still needs its own consistent reconciliation process.

Primary references

Bitcoin Core undo structures: https://github.com/bitcoin/bitcoin/blob/master/src/undo.h

Bitcoin Core validation implementation: https://github.com/bitcoin/bitcoin/blob/master/src/validation.cpp

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