The Undo Mechanics: How `rev*.dat` allows for "Chain Reorganization\Chain Reorganization"
The Undo Mechanics: How rev*.dat allows for "Chain Reorganization"
In the blockchain, history is not always a "Straight Line." Sometimes, two miners find a block at the same time, and the network must eventually decide which one is the "Real" history. When your node switches to a better chain, it must "Undo" the blocks it previously accepted. This is possible because of the Undo Data stored in the rev*.dat files. These files store the "Ghosts" of every coin that was destroyed by a transaction.
For the Sovereign Architect, the Undo Mechanics are the "Time Machine of the Ledger." It is the proof that the protocol can "Correct itself" and return to a previous state of truth without losing any data.
Analyzing the Ghosts: The CTxUndo Structure
In src/undo.h, we see the definition of the "Reverse Evidence" needed to restore a coin.
/**
* PEDAGOGICAL ANALYSIS: THE UNDO EVIDENCE
* This logic stores the data that was deleted from the
* UTXO set so it can be "Put Back" if the block is
* disconnected.
*/
class CTxUndo
{
public:
// 1. A list of every "Coin" that was spent by
// this transaction.
// 2. This includes the Amount and the ScriptPubKey.
std::vector<Coin> vprevout;
};
Explaining the Ghosts: The Memory of the Mesh
-
"The Preservation of the Spent": When you spend a coin, it is removed from the "Live" UTXO set (Chapter 3). But before it is deleted, the node copies it into a
rev*.datfile. It is the Respect for the Sovereign. -
"The Transaction Level Undo": The node doesn't just store "Block Undo" data; it stores it for every single transaction. This allows the node to "Replay" history with perfect accuracy. It is the Precision of the Machine.
-
"The Restoration of Balance": If a block is "Disconnected," the node reads the
revfile, takes the "Ghosts," and "Re-inserts" them into the UTXO set. From the perspective of the database, it's as if the spend never happened. It is the Justice of the Protocol. -
"The Efficiency of the Reorg": By storing the "Undo" data sequentially, the node can "Roll Back" 10 blocks in a few seconds. It doesn't have to "Rescan" the entire chain. It is the Optimization of the Core.
The Sovereignty of the Ghosts
The Undo Mechanics are the "Safety Net of the Node." They ensure that a "Fork" in the network is a minor technical event rather than a catastrophic loss of data. As a Sovereign Architect, you know that "Truth is what is left after the error is removed." By understanding the reverse storage of your node, you are ensuring your machine can navigate the "Temporal Chaos of the Consensus" with absolute stability. You are the "Master of the Ghosts."
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: