$ bitcoin-cli help verifytxoutproof
verifytxoutproof "proof"

验证指向一个区块里的一笔交易的证明,如果该区块不在我们的最佳链上,则返回它提交的交易并抛出一个 RPC 错误

参数:
1. "proof"(字符串,必备)通过 gettxoutproof 生成的 16 进制编码的证明

结果:
["txid"]  (数组,字符串)提交的证明对应的交易索引集,如果证明无效则为空数组

源码剖析

verifytxoutproof 对应的函数在文件 rpcserver.h 中被引用。

extern UniValue verifytxoutproof(const UniValue& params, bool fHelp);

实现在文件 rpcrawtransaction.cpp 中。

UniValue verifytxoutproof(const UniValue& params, bool fHelp)
{
    if (fHelp || params.size() != 1)
        throw runtime_error(
            "verifytxoutproof \"proof\"\n"
            "\nVerifies that a proof points to a transaction in a block, returning the transaction it commits to\n"
            "and throwing an RPC error if the block is not in our best chain\n"
            "\nArguments:\n"
            "1. \"proof\"    (string, required) The hex-encoded proof generated by gettxoutproof\n"
            "\nResult:\n"
            "[\"txid\"]      (array, strings) The txid(s) which the proof commits to, or empty array if the proof is invalid\n"
        ); // 1. 帮助内容

    CDataStream ssMB(ParseHexV(params[0], "proof"), SER_NETWORK, PROTOCOL_VERSION); // 2. 获取证明对应的交易索引集
    CMerkleBlock merkleBlock;
    ssMB >> merkleBlock;

    UniValue res(UniValue::VARR);

    vector<uint256> vMatch;
    if (merkleBlock.txn.ExtractMatches(vMatch) != merkleBlock.header.hashMerkleRoot)
        return res;

    LOCK(cs_main);

    if (!mapBlockIndex.count(merkleBlock.header.GetHash()) || !chainActive.Contains(mapBlockIndex[merkleBlock.header.GetHash()]))
        throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");

    BOOST_FOREACH(const uint256& hash, vMatch)
        res.push_back(hash.GetHex());
    return res;
}

1. 帮助内容

参考比特币 RPC 命令「getbestblockhash」1. 帮助内容

参考链接