The Block Intake Logic: How `block` messages are verified before validation
The Block Intake Logic: How block messages are verified before validation
When a peer sends your node a BLOCK message, they are sending you a "Piece of History." This is the most important message in the protocol, but it is also the most "Dangerous," because a block can be up to 4MB of data. If your node blindly validated every block it received, a hacker could crash your CPU with "Fake Blocks."
To solve this, net_processing.cpp performs Block Intake Logic. This is a series of "Cheap Checks" that happen before the expensive "Validation Audit."
Analyzing the Intake: ProcessBlock
In the source code, we see how the node "Screens" a block before handing it to the Guardian.
/**
* PEDAGOGICAL ANALYSIS: THE BLOCK SCREENER
* This logic ensures a block is "Worth" validating before spending CPU time on it.
*/
void PeerManagerImpl::ProcessMessage(...)
{
if (msg_type == NetMsgType::BLOCK) {
// 1. Deserialize the raw bytes into a Block object.
std::shared_ptr<CBlock> pblock = std::make_shared<CBlock>();
vRecv >> *pblock;
// 2. Do we already have this block?
// If so, don't waste time on it.
if (m_chainman.m_blockman.LookupBlockIndex(pblock->GetHash())) return;
// 3. Is the block "Orphaned" (missing its parent)?
// If so, save it for later.
if (!m_chainman.m_blockman.LookupBlockIndex(pblock->hashPrevBlock)) {
ProcessOrphanBlock(pfrom.GetId(), pblock);
return;
}
// 4. Finally, hand it to the "Chainman" for the real audit.
m_chainman.ProcessNewBlock(pblock, ...);
}
}
Explaining the Intake: The Security Screening
-
LookupBlockIndex: Your node has a "Library Index" of every block it has ever seen. The first thing it does is check the index. If the peer is sending you a block from 2015, the node says: "I already have that," and stops immediately. This is the Efficiency of the Sovereign. -
"The Orphanage": If a peer sends you "Block 100" but you haven't seen "Block 99" yet, your node can't validate it. But it doesn't throw it away! It puts it in the Orphanage (a temporary memory area). When Block 99 eventually arrives, the node will "Reconnect" the orphan. It is the Patience of the Machine.
-
m_chainman.ProcessNewBlock: This is the "Hand-off." The Diplomat has finished the screening and is now asking the General (validation.cpp) to perform the full mathematical audit. It is the Cooperation of the Layers. -
"The Header-First Check": Actually, modern Bitcoin nodes won't even accept a
BLOCKmessage unless they have already received the 80-byteHEADERfirst (Chapter 5). This ensures the node knows there is "Proof of Work" behind the block before it downloads the full 4MB. It is the Strategy of the Core.
The Sovereignty of the Screening
As a Sovereign Architect, you know that "Time is Money." By screening blocks before validating them, your node protects your computer's resources from being wasted on "Garbage History." You are the "Master of the Intake," ensuring that your node's "Verification Engine" is only used for the highest-quality, most likely-to-be-true data.
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: