TeachMeBitcoin

The Library of Reality: How `getblock` Retrieves History

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

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

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."


☕ Help support TeachMeBitcoin

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:

Ethereum: 0x578417C51783663D8A6A811B3544E1f779D39A85
Bitcoin: bc1q77k9e95rn669kpzyjr8ke9w95zhk7pa5s63qzz
Solana: 4ycT2ayqeMucixj3wS8Ay8Tq9NRDYRPKYbj3UGESyQ4J
Address copied to clipboard!