The Library of Reality: How `getblock` Retrieves History
5. The Library of Reality: How getblock Retrieves History
Now that we know how to find the "Summary" and the "Tip," we need a way to look inside the blocks. We need to read the specific pages of the ledger. This is the job of the getblock command. It is the "Deep Retrieval" tool of the bridge, the "Dissection Table of the Ledger."
Analyzing the "Page Reader" Code
In the source code (src/rpc/blockchain.cpp), the getblock command is a heavy operation that involves reading data from the hard drive. It is the "Manual Labor" of the bridge.
/**
* This function retrieves a full block from the archives.
*/
static RPCMethod getblock()
{
return RPCMethod{
"getblock",
"Returns an Object with information about block <hash>.\n",
{
{"blockhash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
{"verbosity", RPCArg::Type::NUM, RPCArg::Default{1}, "0 for hex, 1 for JSON, 2 for details"},
},
// ... (Example omitted)
[](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
{
uint256 hash(ParseHashV(request.params[0], "blockhash"));
int verbosity = request.params[1].getInt<int>();
ChainstateManager& chainman = EnsureAnyChainman(request.context);
BlockManager& blockman = chainman.m_blockman;
const CBlockIndex* pblockindex = WITH_LOCK(cs_main, return blockman.LookupBlockIndex(hash));
if (!pblockindex) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
CBlock block;
if (!blockman.ReadBlock(block, *pblockindex)) throw JSONRPCError(RPC_MISC_ERROR, "Can't read block from disk");
return blockToJSON(blockman, block, *chainman.ActiveChain().Tip(), *pblockindex, (TxVerbosity)verbosity, chainman.GetConsensus().powLimit);
}
Explaining the Logic to a Non-Coder
-
LookupBlockIndex: The node keeps a "Card Catalog" (the Index) in memory. When you ask for a block, it first looks at the card to find out where the block is stored on the hard drive. It is the "Search for the Shelf." -
ReadBlock: This is the physical act of "Opening the Book." The node goes to the hard drive, finds the correct file, and reads the raw data. This is why the bridge needs a hash—so it can go straight to the right page without searching.
The Power of the Archive
getblock is where you see the "Full Detail" of Bitcoin. You see every transaction, every fee, and every piece of "Witness Data" that secures the network. It is the bridge to the "Universal Memory," a record that can never be erased. By mastering getblock, you are mastering the art of "Witnessing History."
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: