美文网首页
比特币源码阅读(rpc命令-getBlockCount实现)

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

作者: 坠叶飘香 | 来源:发表于2018-08-07 21:34 被阅读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;
        }
    }
    

    相关文章

      网友评论

          本文标题:比特币源码阅读(rpc命令-getBlockCount实现)

          本文链接:https://www.haomeiwen.com/subject/zuhzvftx.html