@@ -59,10 +59,7 @@ static CUpdatedBlock latestblock;
5959 */
6060double GetDifficulty (const CBlockIndex* blockindex)
6161{
62- if (blockindex == nullptr )
63- {
64- return 1.0 ;
65- }
62+ assert (blockindex);
6663
6764 int nShift = (blockindex->nBits >> 24 ) & 0xff ;
6865 double dDiff =
@@ -82,15 +79,22 @@ double GetDifficulty(const CBlockIndex* blockindex)
8279 return dDiff;
8380}
8481
85- UniValue blockheaderToJSON (const CBlockIndex* blockindex)
82+ static int ComputeNextBlockAndDepth (const CBlockIndex* tip, const CBlockIndex* blockindex, const CBlockIndex*& next)
83+ {
84+ next = tip->GetAncestor (blockindex->nHeight + 1 );
85+ if (next && next->pprev == blockindex) {
86+ return tip->nHeight - blockindex->nHeight + 1 ;
87+ }
88+ next = nullptr ;
89+ return blockindex == tip ? 1 : -1 ;
90+ }
91+
92+ UniValue blockheaderToJSON (const CBlockIndex* tip, const CBlockIndex* blockindex)
8693{
87- AssertLockHeld (cs_main);
8894 UniValue result (UniValue::VOBJ);
8995 result.pushKV (" hash" , blockindex->GetBlockHash ().GetHex ());
90- int confirmations = -1 ;
91- // Only report confirmations if the block is on the main chain
92- if (chainActive.Contains (blockindex))
93- confirmations = chainActive.Height () - blockindex->nHeight + 1 ;
96+ const CBlockIndex* pnext;
97+ int confirmations = ComputeNextBlockAndDepth (tip, blockindex, pnext);
9498 result.pushKV (" confirmations" , confirmations);
9599 result.pushKV (" height" , blockindex->nHeight );
96100 result.pushKV (" version" , blockindex->nVersion );
@@ -106,21 +110,17 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
106110
107111 if (blockindex->pprev )
108112 result.pushKV (" previousblockhash" , blockindex->pprev ->GetBlockHash ().GetHex ());
109- CBlockIndex *pnext = chainActive.Next (blockindex);
110113 if (pnext)
111114 result.pushKV (" nextblockhash" , pnext->GetBlockHash ().GetHex ());
112115 return result;
113116}
114117
115- UniValue blockToJSON (const CBlock& block, const CBlockIndex* blockindex, bool txDetails)
118+ UniValue blockToJSON (const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails)
116119{
117- AssertLockHeld (cs_main);
118120 UniValue result (UniValue::VOBJ);
119121 result.pushKV (" hash" , blockindex->GetBlockHash ().GetHex ());
120- int confirmations = -1 ;
121- // Only report confirmations if the block is on the main chain
122- if (chainActive.Contains (blockindex))
123- confirmations = chainActive.Height () - blockindex->nHeight + 1 ;
122+ const CBlockIndex* pnext;
123+ int confirmations = ComputeNextBlockAndDepth (tip, blockindex, pnext);
124124 result.pushKV (" confirmations" , confirmations);
125125 result.pushKV (" strippedsize" , (int )::GetSerializeSize (block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS));
126126 result.pushKV (" size" , (int )::GetSerializeSize (block, PROTOCOL_VERSION));
@@ -152,7 +152,6 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
152152
153153 if (blockindex->pprev )
154154 result.pushKV (" previousblockhash" , blockindex->pprev ->GetBlockHash ().GetHex ());
155- CBlockIndex *pnext = chainActive.Next (blockindex);
156155 if (pnext)
157156 result.pushKV (" nextblockhash" , pnext->GetBlockHash ().GetHex ());
158157 return result;
@@ -769,7 +768,7 @@ static UniValue getblockheader(const JSONRPCRequest& request)
769768 return strHex;
770769 }
771770
772- return blockheaderToJSON (pblockindex);
771+ return blockheaderToJSON (chainActive. Tip (), pblockindex);
773772}
774773
775774static CBlock GetBlockChecked (const CBlockIndex* pblockindex)
@@ -871,7 +870,7 @@ static UniValue getblock(const JSONRPCRequest& request)
871870 return strHex;
872871 }
873872
874- return blockToJSON (block, pblockindex, verbosity >= 2 );
873+ return blockToJSON (block, chainActive. Tip (), pblockindex, verbosity >= 2 );
875874}
876875
877876struct CCoinsStats
@@ -1150,7 +1149,7 @@ static UniValue verifychain(const JSONRPCRequest& request)
11501149}
11511150
11521151/* * Implementation of IsSuperMajority with better feedback */
1153- static UniValue SoftForkMajorityDesc (int version, CBlockIndex* pindex, const Consensus::Params& consensusParams)
1152+ static UniValue SoftForkMajorityDesc (int version, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
11541153{
11551154 UniValue rv (UniValue::VOBJ);
11561155 bool activated = false ;
@@ -1170,7 +1169,7 @@ static UniValue SoftForkMajorityDesc(int version, CBlockIndex* pindex, const Con
11701169 return rv;
11711170}
11721171
1173- static UniValue SoftForkDesc (const std::string &name, int version, CBlockIndex* pindex, const Consensus::Params& consensusParams)
1172+ static UniValue SoftForkDesc (const std::string &name, int version, const CBlockIndex* pindex, const Consensus::Params& consensusParams)
11741173{
11751174 UniValue rv (UniValue::VOBJ);
11761175 rv.pushKV (" id" , name);
@@ -1277,20 +1276,21 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
12771276
12781277 LOCK (cs_main);
12791278
1279+ const CBlockIndex* tip = chainActive.Tip ();
12801280 UniValue obj (UniValue::VOBJ);
12811281 obj.pushKV (" chain" , Params ().NetworkIDString ());
12821282 obj.pushKV (" blocks" , (int )chainActive.Height ());
12831283 obj.pushKV (" headers" , pindexBestHeader ? pindexBestHeader->nHeight : -1 );
1284- obj.pushKV (" bestblockhash" , chainActive. Tip () ->GetBlockHash ().GetHex ());
1285- obj.pushKV (" difficulty" , (double )GetDifficulty (chainActive. Tip () ));
1286- obj.pushKV (" mediantime" , (int64_t )chainActive. Tip () ->GetMedianTimePast ());
1287- obj.pushKV (" verificationprogress" , GuessVerificationProgress (Params ().TxData (), chainActive. Tip () ));
1284+ obj.pushKV (" bestblockhash" , tip ->GetBlockHash ().GetHex ());
1285+ obj.pushKV (" difficulty" , (double )GetDifficulty (tip ));
1286+ obj.pushKV (" mediantime" , (int64_t )tip ->GetMedianTimePast ());
1287+ obj.pushKV (" verificationprogress" , GuessVerificationProgress (Params ().TxData (), tip ));
12881288 obj.pushKV (" initialblockdownload" , IsInitialBlockDownload ());
1289- obj.pushKV (" chainwork" , chainActive. Tip () ->nChainWork .GetHex ());
1289+ obj.pushKV (" chainwork" , tip ->nChainWork .GetHex ());
12901290 obj.pushKV (" size_on_disk" , CalculateCurrentUsage ());
12911291 obj.pushKV (" pruned" , fPruneMode );
12921292 if (fPruneMode ) {
1293- CBlockIndex* block = chainActive. Tip () ;
1293+ const CBlockIndex* block = tip ;
12941294 assert (block);
12951295 while (block->pprev && (block->pprev ->nStatus & BLOCK_HAVE_DATA)) {
12961296 block = block->pprev ;
@@ -1307,7 +1307,6 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
13071307 }
13081308
13091309 const Consensus::Params& consensusParams = Params ().GetConsensus ();
1310- CBlockIndex* tip = chainActive.Tip ();
13111310 UniValue softforks (UniValue::VARR);
13121311 UniValue bip9_softforks (UniValue::VOBJ);
13131312 softforks.push_back (SoftForkDesc (" bip34" , 2 , tip, consensusParams));
0 commit comments