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

2018-08-07  本文已影响0人  坠叶飘香
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;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读