@@ -78,15 +78,22 @@ double GetDifficulty(const CBlockIndex* blockindex)
7878 return dDiff;
7979}
8080
81- UniValue blockheaderToJSON (const CBlockIndex* blockindex)
81+ static int ComputeNextBlockAndDepth (const CBlockIndex* tip, const CBlockIndex* blockindex, const CBlockIndex*& next)
82+ {
83+ next = tip->GetAncestor (blockindex->nHeight + 1 );
84+ if (next && next->pprev == blockindex) {
85+ return tip->nHeight - blockindex->nHeight + 1 ;
86+ }
87+ next = nullptr ;
88+ return blockindex == tip ? 1 : -1 ;
89+ }
90+
91+ UniValue blockheaderToJSON (const CBlockIndex* tip, const CBlockIndex* blockindex)
8292{
83- AssertLockHeld (cs_main);
8493 UniValue result (UniValue::VOBJ);
8594 result.pushKV (" hash" , blockindex->GetBlockHash ().GetHex ());
86- int confirmations = -1 ;
87- // Only report confirmations if the block is on the main chain
88- if (chainActive.Contains (blockindex))
89- confirmations = chainActive.Height () - blockindex->nHeight + 1 ;
95+ const CBlockIndex* pnext;
96+ int confirmations = ComputeNextBlockAndDepth (tip, blockindex, pnext);
9097 result.pushKV (" confirmations" , confirmations);
9198 result.pushKV (" height" , blockindex->nHeight );
9299 result.pushKV (" version" , blockindex->nVersion );
@@ -102,21 +109,17 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
102109
103110 if (blockindex->pprev )
104111 result.pushKV (" previousblockhash" , blockindex->pprev ->GetBlockHash ().GetHex ());
105- CBlockIndex *pnext = chainActive.Next (blockindex);
106112 if (pnext)
107113 result.pushKV (" nextblockhash" , pnext->GetBlockHash ().GetHex ());
108114 return result;
109115}
110116
111- UniValue blockToJSON (const CBlock& block, const CBlockIndex* blockindex, bool txDetails)
117+ UniValue blockToJSON (const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, bool txDetails)
112118{
113- AssertLockHeld (cs_main);
114119 UniValue result (UniValue::VOBJ);
115120 result.pushKV (" hash" , blockindex->GetBlockHash ().GetHex ());
116- int confirmations = -1 ;
117- // Only report confirmations if the block is on the main chain
118- if (chainActive.Contains (blockindex))
119- confirmations = chainActive.Height () - blockindex->nHeight + 1 ;
121+ const CBlockIndex* pnext;
122+ int confirmations = ComputeNextBlockAndDepth (tip, blockindex, pnext);
120123 result.pushKV (" confirmations" , confirmations);
121124 result.pushKV (" strippedsize" , (int )::GetSerializeSize (block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS));
122125 result.pushKV (" size" , (int )::GetSerializeSize (block, SER_NETWORK, PROTOCOL_VERSION));
@@ -148,7 +151,6 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
148151
149152 if (blockindex->pprev )
150153 result.pushKV (" previousblockhash" , blockindex->pprev ->GetBlockHash ().GetHex ());
151- CBlockIndex *pnext = chainActive.Next (blockindex);
152154 if (pnext)
153155 result.pushKV (" nextblockhash" , pnext->GetBlockHash ().GetHex ());
154156 return result;
@@ -744,7 +746,7 @@ static UniValue getblockheader(const JSONRPCRequest& request)
744746 return strHex;
745747 }
746748
747- return blockheaderToJSON (pblockindex);
749+ return blockheaderToJSON (chainActive. Tip (), pblockindex);
748750}
749751
750752static CBlock GetBlockChecked (const CBlockIndex* pblockindex)
@@ -845,7 +847,7 @@ static UniValue getblock(const JSONRPCRequest& request)
845847 return strHex;
846848 }
847849
848- return blockToJSON (block, pblockindex, verbosity >= 2 );
850+ return blockToJSON (block, chainActive. Tip (), pblockindex, verbosity >= 2 );
849851}
850852
851853struct CCoinsStats
0 commit comments