比特币源码阅读(rpc命令-getBlockCount实现)

admin07@admin-MS:~/liang/job/github_source/bitcoin$ bitcoin-cli -regtest getblockcount
117

src/rpc/blockchain.cpp

static UniValue getblockcount(const JSONRPCRequest& request)
{
    if (request.fHelp || request.params.size() != 0)
        throw std::runtime_error(
            "getblockcount\n"
            "\nReturns the number of blocks in the longest blockchain.\n"
            "\nResult:\n"
            "n    (numeric) The current block count\n"
            "\nExamples:\n"
            + HelpExampleCli("getblockcount", "")
            + HelpExampleRpc("getblockcount", "")
        );

    LOCK(cs_main); //多线程安全
    return chainActive.Height();
}
//最大高度
/** Return the maximal height in the chain. Is equal to chain.Tip() ? chain.Tip()->nHeight : -1. */
class CChain {
    private:
    std::vector<CBlockIndex*> vChain; //vector是一个能够存放任意类型的动态数组,能够增加和压缩数据

    int Height() const {
        return vChain.size() - 1;
    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容