TeachMeBitcoin

The Specifics of Wealth: Using `gettxout`

From TeachMeBitcoin, the free encyclopedia Reading time: 2 min

12. The Specifics of Wealth: Using gettxout

While gettxoutsetinfo gives you the big picture, sometimes you need to look at a "Specific Bill" in the global wallet. You want to know: "Is this specific output still unspent? Is it real?" The command for this is gettxout. It is the "Verifier of Individual Bills," the "Microscope of the Bridge."

Analyzing the "Coin Retrieval" Code

In the source code (src/rpc/blockchain.cpp), the node looks directly into its "Active Cache" to see if a coin exists.

/**
 * This function retrieves a single "Unspent Output."
 */
static RPCMethod gettxout()
{
    // ... (Arguments omitted)
    [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
{
    uint256 hash(ParseHashV(request.params[0], "txid"));
    int n = request.params[1].getInt<int>();

    ChainstateManager& chainman = EnsureAnyChainman(request.context);
    CCoinsViewCache& view = chainman.ActiveChainstate().CoinsTip();

    // We look for the "Coin" in the cache.
    Coin coin;
    if (view.GetCoin(COutPoint(hash, n), coin)) {
        UniValue obj(UniValue::VOBJ);
        obj.pushKV("value", ValueFromAmount(coin.out.nValue));
        obj.pushKV("confirmations", chainman.ActiveChain().Height() - coin.nHeight + 1);
        return obj;
    }

    return UniValue::VNULL; // Coin was spent!
}

Explaining the Bill to a Non-Coder

The "Spent" vs "Unspent" Reality

If gettxout returns null, it means the money has already been "Spent." This is the primary tool for preventing "Double Spending." When a merchant receives a payment, they can use this command to ensure the money they are being sent actually exists in the "Active Vault." It is the "Verification of Truth" at the point of sale.


☕ 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!