比特币源码学习笔记

比特币源码阅读(RPC命令-BlockChain-getbest

2018-08-11  本文已影响0人  坠叶飘香

getbestblockhash:获得节点的最高区块的hash

命令执行结果:

admin07@admin-MS:~/job/github_source/bitcoin$ bitcoin-cli -regtest getbestblockhash
10f21e2d0f18fe1be18350b393bbabf108a2197ec35b53916163fa8d9991c972

实现源码:

src/rpc/blockchain.cpp

static UniValue getbestblockhash(const JSONRPCRequest& request)
{
    if (request.fHelp || request.params.size() != 0)
        throw std::runtime_error(
            "getbestblockhash\n"
            "\nReturns the hash of the best (tip) block in the longest blockchain.\n"
            "\nResult:\n"
            "\"hex\"      (string) the block hash hex encoded\n"
            "\nExamples:\n"
            + HelpExampleCli("getbestblockhash", "")
            + HelpExampleRpc("getbestblockhash", "")
        );

    LOCK(cs_main);
    return chainActive.Tip()->GetBlockHash().GetHex(); //获取vector的最后一个元素的hash
}

src/chain.h

//最后一个CBlockIndex
    /** Returns the index entry for the tip(末梢) of this chain, or nullptr if none. */
    CBlockIndex *Tip() const {
        return vChain.size() > 0 ? vChain[vChain.size() - 1] : nullptr;
    }
上一篇下一篇

猜你喜欢

热点阅读