@@ -78,15 +78,22 @@ double GetDifficulty(const CBlockIndex* blockindex)
78
78
return dDiff;
79
79
}
80
80
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)
82
92
{
83
- AssertLockHeld (cs_main);
84
93
UniValue result (UniValue::VOBJ);
85
94
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);
90
97
result.pushKV (" confirmations" , confirmations);
91
98
result.pushKV (" height" , blockindex->nHeight );
92
99
result.pushKV (" version" , blockindex->nVersion );
@@ -102,21 +109,17 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
102
109
103
110
if (blockindex->pprev )
104
111
result.pushKV (" previousblockhash" , blockindex->pprev ->GetBlockHash ().GetHex ());
105
- CBlockIndex *pnext = chainActive.Next (blockindex);
106
112
if (pnext)
107
113
result.pushKV (" nextblockhash" , pnext->GetBlockHash ().GetHex ());
108
114
return result;
109
115
}
110
116
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)
112
118
{
113
- AssertLockHeld (cs_main);
114
119
UniValue result (UniValue::VOBJ);
115
120
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);
120
123
result.pushKV (" confirmations" , confirmations);
121
124
result.pushKV (" strippedsize" , (int )::GetSerializeSize (block, SER_NETWORK, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS));
122
125
result.pushKV (" size" , (int )::GetSerializeSize (block, SER_NETWORK, PROTOCOL_VERSION));
@@ -148,7 +151,6 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
148
151
149
152
if (blockindex->pprev )
150
153
result.pushKV (" previousblockhash" , blockindex->pprev ->GetBlockHash ().GetHex ());
151
- CBlockIndex *pnext = chainActive.Next (blockindex);
152
154
if (pnext)
153
155
result.pushKV (" nextblockhash" , pnext->GetBlockHash ().GetHex ());
154
156
return result;
@@ -744,7 +746,7 @@ static UniValue getblockheader(const JSONRPCRequest& request)
744
746
return strHex;
745
747
}
746
748
747
- return blockheaderToJSON (pblockindex);
749
+ return blockheaderToJSON (chainActive. Tip (), pblockindex);
748
750
}
749
751
750
752
static CBlock GetBlockChecked (const CBlockIndex* pblockindex)
@@ -845,7 +847,7 @@ static UniValue getblock(const JSONRPCRequest& request)
845
847
return strHex;
846
848
}
847
849
848
- return blockToJSON (block, pblockindex, verbosity >= 2 );
850
+ return blockToJSON (block, chainActive. Tip (), pblockindex, verbosity >= 2 );
849
851
}
850
852
851
853
struct CCoinsStats
0 commit comments