@@ -59,10 +59,7 @@ static CUpdatedBlock latestblock;
59
59
*/
60
60
double GetDifficulty (const CBlockIndex* blockindex)
61
61
{
62
- if (blockindex == nullptr )
63
- {
64
- return 1.0 ;
65
- }
62
+ assert (blockindex);
66
63
67
64
int nShift = (blockindex->nBits >> 24 ) & 0xff ;
68
65
double dDiff =
@@ -82,15 +79,22 @@ double GetDifficulty(const CBlockIndex* blockindex)
82
79
return dDiff;
83
80
}
84
81
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)
86
93
{
87
- AssertLockHeld (cs_main);
88
94
UniValue result (UniValue::VOBJ);
89
95
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);
94
98
result.pushKV (" confirmations" , confirmations);
95
99
result.pushKV (" height" , blockindex->nHeight );
96
100
result.pushKV (" version" , blockindex->nVersion );
@@ -106,21 +110,17 @@ UniValue blockheaderToJSON(const CBlockIndex* blockindex)
106
110
107
111
if (blockindex->pprev )
108
112
result.pushKV (" previousblockhash" , blockindex->pprev ->GetBlockHash ().GetHex ());
109
- CBlockIndex *pnext = chainActive.Next (blockindex);
110
113
if (pnext)
111
114
result.pushKV (" nextblockhash" , pnext->GetBlockHash ().GetHex ());
112
115
return result;
113
116
}
114
117
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)
116
119
{
117
- AssertLockHeld (cs_main);
118
120
UniValue result (UniValue::VOBJ);
119
121
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);
124
124
result.pushKV (" confirmations" , confirmations);
125
125
result.pushKV (" strippedsize" , (int )::GetSerializeSize (block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS));
126
126
result.pushKV (" size" , (int )::GetSerializeSize (block, PROTOCOL_VERSION));
@@ -152,7 +152,6 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* blockindex, bool tx
152
152
153
153
if (blockindex->pprev )
154
154
result.pushKV (" previousblockhash" , blockindex->pprev ->GetBlockHash ().GetHex ());
155
- CBlockIndex *pnext = chainActive.Next (blockindex);
156
155
if (pnext)
157
156
result.pushKV (" nextblockhash" , pnext->GetBlockHash ().GetHex ());
158
157
return result;
@@ -769,7 +768,7 @@ static UniValue getblockheader(const JSONRPCRequest& request)
769
768
return strHex;
770
769
}
771
770
772
- return blockheaderToJSON (pblockindex);
771
+ return blockheaderToJSON (chainActive. Tip (), pblockindex);
773
772
}
774
773
775
774
static CBlock GetBlockChecked (const CBlockIndex* pblockindex)
@@ -871,7 +870,7 @@ static UniValue getblock(const JSONRPCRequest& request)
871
870
return strHex;
872
871
}
873
872
874
- return blockToJSON (block, pblockindex, verbosity >= 2 );
873
+ return blockToJSON (block, chainActive. Tip (), pblockindex, verbosity >= 2 );
875
874
}
876
875
877
876
struct CCoinsStats
@@ -1150,7 +1149,7 @@ static UniValue verifychain(const JSONRPCRequest& request)
1150
1149
}
1151
1150
1152
1151
/* * 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)
1154
1153
{
1155
1154
UniValue rv (UniValue::VOBJ);
1156
1155
bool activated = false ;
@@ -1170,7 +1169,7 @@ static UniValue SoftForkMajorityDesc(int version, CBlockIndex* pindex, const Con
1170
1169
return rv;
1171
1170
}
1172
1171
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)
1174
1173
{
1175
1174
UniValue rv (UniValue::VOBJ);
1176
1175
rv.pushKV (" id" , name);
@@ -1277,20 +1276,21 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
1277
1276
1278
1277
LOCK (cs_main);
1279
1278
1279
+ const CBlockIndex* tip = chainActive.Tip ();
1280
1280
UniValue obj (UniValue::VOBJ);
1281
1281
obj.pushKV (" chain" , Params ().NetworkIDString ());
1282
1282
obj.pushKV (" blocks" , (int )chainActive.Height ());
1283
1283
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 ));
1288
1288
obj.pushKV (" initialblockdownload" , IsInitialBlockDownload ());
1289
- obj.pushKV (" chainwork" , chainActive. Tip () ->nChainWork .GetHex ());
1289
+ obj.pushKV (" chainwork" , tip ->nChainWork .GetHex ());
1290
1290
obj.pushKV (" size_on_disk" , CalculateCurrentUsage ());
1291
1291
obj.pushKV (" pruned" , fPruneMode );
1292
1292
if (fPruneMode ) {
1293
- CBlockIndex* block = chainActive. Tip () ;
1293
+ const CBlockIndex* block = tip ;
1294
1294
assert (block);
1295
1295
while (block->pprev && (block->pprev ->nStatus & BLOCK_HAVE_DATA)) {
1296
1296
block = block->pprev ;
@@ -1307,7 +1307,6 @@ UniValue getblockchaininfo(const JSONRPCRequest& request)
1307
1307
}
1308
1308
1309
1309
const Consensus::Params& consensusParams = Params ().GetConsensus ();
1310
- CBlockIndex* tip = chainActive.Tip ();
1311
1310
UniValue softforks (UniValue::VARR);
1312
1311
UniValue bip9_softforks (UniValue::VOBJ);
1313
1312
softforks.push_back (SoftForkDesc (" bip34" , 2 , tip, consensusParams));
0 commit comments