The Sketch Reconstruction: How `CMPCTBLOCK` is rebuilt in memory
10. The Sketch Reconstruction: How CMPCTBLOCK is rebuilt in memory
Once your node receives a CMPCTBLOCK message (the "Sketch" from Chapter 9), the Diplomat must perform the "Miracle" of reconstruction. This happens in the PartiallyDownloadedBlock class. The node takes the sketch, looks at its mempool, and tries to "Fill in the Blanks."
For the Sovereign Architect, Sketch Reconstruction is the "Final Assembly" of the truth. It is the moment where "Potential" becomes "Verified History."
Analyzing the Assembly: InitData
In the source code, we see the node "Matching" fingerprints to transactions.
/**
* PEDAGOGICAL ANALYSIS: THE DIGITAL JIGSAW PUZZLE
* This logic matches the "Fingerprints" in the sketch
* to the transactions in your RAM.
*/
ReadStatus PartiallyDownloadedBlock::InitData(const CBlockHeaderAndShortTxIDs& cmpctblock, ...)
{
// 1. Loop through every "Short ID" in the sketch.
for (size_t i = 0; i < cmpctblock.BlockTxCount(); i++) {
// 2. Try to find the matching transaction in our MEMPOOL.
uint256 txid = m_mempool.LookupByShortID(cmpctblock.GetShortID(i));
if (txid) {
// 3. We found it! Add the full transaction to our block.
vtx[i] = m_mempool.get(txid);
} else {
// 4. We are missing this one. Mark it for later.
missing_indices.push_back(i);
}
}
// 5. If we have everything, the block is COMPLETE.
if (missing_indices.empty()) return READ_STATUS_OK;
}
Explaining the Assembly: The Completion of the Puzzle
-
m_mempool.LookupByShortID: This is a "High-Speed Index." Your mempool keeps a list of transactions indexed by their tiny 6-byte fingerprints just for this moment. It is the Efficiency of the Search. -
vtx[i]: This is the "Slot" in the block. Once the transaction is found, it is "Placed" in its correct chronological order. It is the Order of the History. -
missing_indices: Sometimes, you don't have every transaction. Maybe someone sent a transaction directly to the miner and you never saw the gossip. The node doesn't panic; it just makes a "List of Missing Pieces." It is the Tolerance of the Machine. -
"The Final Verification": Even after the block is assembled from your own RAM, the node still verifies the Merkle Root (Volume 3). This ensures that you haven't accidentally put the wrong transaction in the wrong slot. It is the Integrity of the Sovereign.
The Sovereignty of the Assembly
When your node reconstructs a block, it is performing a "Local Audit." It is proving that it already knew most of what the network was about to say. As a Sovereign Architect, you are the "Master of the Assembly," commanding a node that is so well-informed that it can "Predict" the future of the blockchain using the gossip of the present. You are the "Guardian of the Puzzle."
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: