@@ -73,13 +73,11 @@ static CUpdatedBlock latestblock GUARDED_BY(cs_blockchange);
73
73
74
74
/* Calculate the difficulty for a given block index.
75
75
*/
76
- double GetDifficulty (const CBlockIndex* blockindex)
76
+ double GetDifficulty (const CBlockIndex& blockindex)
77
77
{
78
- CHECK_NONFATAL (blockindex);
79
-
80
- int nShift = (blockindex->nBits >> 24 ) & 0xff ;
78
+ int nShift = (blockindex.nBits >> 24 ) & 0xff ;
81
79
double dDiff =
82
- (double )0x0000ffff / (double )(blockindex-> nBits & 0x00ffffff );
80
+ (double )0x0000ffff / (double )(blockindex. nBits & 0x00ffffff );
83
81
84
82
while (nShift < 29 )
85
83
{
@@ -95,14 +93,14 @@ double GetDifficulty(const CBlockIndex* blockindex)
95
93
return dDiff;
96
94
}
97
95
98
- static int ComputeNextBlockAndDepth (const CBlockIndex* tip, const CBlockIndex* blockindex, const CBlockIndex*& next)
96
+ static int ComputeNextBlockAndDepth (const CBlockIndex& tip, const CBlockIndex& blockindex, const CBlockIndex*& next)
99
97
{
100
- next = tip-> GetAncestor (blockindex-> nHeight + 1 );
101
- if (next && next->pprev == blockindex) {
102
- return tip-> nHeight - blockindex-> nHeight + 1 ;
98
+ next = tip. GetAncestor (blockindex. nHeight + 1 );
99
+ if (next && next->pprev == & blockindex) {
100
+ return tip. nHeight - blockindex. nHeight + 1 ;
103
101
}
104
102
next = nullptr ;
105
- return blockindex == tip ? 1 : -1 ;
103
+ return & blockindex == & tip ? 1 : -1 ;
106
104
}
107
105
108
106
static const CBlockIndex* ParseHashOrHeight (const UniValue& param, ChainstateManager& chainman)
@@ -133,36 +131,36 @@ static const CBlockIndex* ParseHashOrHeight(const UniValue& param, ChainstateMan
133
131
}
134
132
}
135
133
136
- UniValue blockheaderToJSON (const CBlockIndex* tip, const CBlockIndex* blockindex)
134
+ UniValue blockheaderToJSON (const CBlockIndex& tip, const CBlockIndex& blockindex)
137
135
{
138
136
// Serialize passed information without accessing chain state of the active chain!
139
137
AssertLockNotHeld (cs_main); // For performance reasons
140
138
141
139
UniValue result (UniValue::VOBJ);
142
- result.pushKV (" hash" , blockindex-> GetBlockHash ().GetHex ());
140
+ result.pushKV (" hash" , blockindex. GetBlockHash ().GetHex ());
143
141
const CBlockIndex* pnext;
144
142
int confirmations = ComputeNextBlockAndDepth (tip, blockindex, pnext);
145
143
result.pushKV (" confirmations" , confirmations);
146
- result.pushKV (" height" , blockindex-> nHeight );
147
- result.pushKV (" version" , blockindex-> nVersion );
148
- result.pushKV (" versionHex" , strprintf (" %08x" , blockindex-> nVersion ));
149
- result.pushKV (" merkleroot" , blockindex-> hashMerkleRoot .GetHex ());
150
- result.pushKV (" time" , ( int64_t ) blockindex-> nTime );
151
- result.pushKV (" mediantime" , ( int64_t ) blockindex-> GetMedianTimePast ());
152
- result.pushKV (" nonce" , ( uint64_t ) blockindex-> nNonce );
153
- result.pushKV (" bits" , strprintf (" %08x" , blockindex-> nBits ));
144
+ result.pushKV (" height" , blockindex. nHeight );
145
+ result.pushKV (" version" , blockindex. nVersion );
146
+ result.pushKV (" versionHex" , strprintf (" %08x" , blockindex. nVersion ));
147
+ result.pushKV (" merkleroot" , blockindex. hashMerkleRoot .GetHex ());
148
+ result.pushKV (" time" , blockindex. nTime );
149
+ result.pushKV (" mediantime" , blockindex. GetMedianTimePast ());
150
+ result.pushKV (" nonce" , blockindex. nNonce );
151
+ result.pushKV (" bits" , strprintf (" %08x" , blockindex. nBits ));
154
152
result.pushKV (" difficulty" , GetDifficulty (blockindex));
155
- result.pushKV (" chainwork" , blockindex-> nChainWork .GetHex ());
156
- result.pushKV (" nTx" , ( uint64_t ) blockindex-> nTx );
153
+ result.pushKV (" chainwork" , blockindex. nChainWork .GetHex ());
154
+ result.pushKV (" nTx" , blockindex. nTx );
157
155
158
- if (blockindex-> pprev )
159
- result.pushKV (" previousblockhash" , blockindex-> pprev ->GetBlockHash ().GetHex ());
156
+ if (blockindex. pprev )
157
+ result.pushKV (" previousblockhash" , blockindex. pprev ->GetBlockHash ().GetHex ());
160
158
if (pnext)
161
159
result.pushKV (" nextblockhash" , pnext->GetBlockHash ().GetHex ());
162
160
return result;
163
161
}
164
162
165
- UniValue blockToJSON (BlockManager& blockman, const CBlock& block, const CBlockIndex* tip, const CBlockIndex* blockindex, TxVerbosity verbosity)
163
+ UniValue blockToJSON (BlockManager& blockman, const CBlock& block, const CBlockIndex& tip, const CBlockIndex& blockindex, TxVerbosity verbosity)
166
164
{
167
165
UniValue result = blockheaderToJSON (tip, blockindex);
168
166
@@ -182,7 +180,7 @@ UniValue blockToJSON(BlockManager& blockman, const CBlock& block, const CBlockIn
182
180
case TxVerbosity::SHOW_DETAILS_AND_PREVOUT:
183
181
CBlockUndo blockUndo;
184
182
const bool is_not_pruned{WITH_LOCK (::cs_main, return !blockman.IsBlockPruned (blockindex))};
185
- const bool have_undo{is_not_pruned && blockman.UndoReadFromDisk (blockUndo, * blockindex)};
183
+ const bool have_undo{is_not_pruned && blockman.UndoReadFromDisk (blockUndo, blockindex)};
186
184
187
185
for (size_t i = 0 ; i < block.vtx .size (); ++i) {
188
186
const CTransactionRef& tx = block.vtx .at (i);
@@ -418,7 +416,7 @@ static RPCHelpMan getdifficulty()
418
416
{
419
417
ChainstateManager& chainman = EnsureAnyChainman (request.context );
420
418
LOCK (cs_main);
421
- return GetDifficulty (chainman.ActiveChain ().Tip ());
419
+ return GetDifficulty (* CHECK_NONFATAL ( chainman.ActiveChain ().Tip () ));
422
420
},
423
421
};
424
422
}
@@ -571,22 +569,22 @@ static RPCHelpMan getblockheader()
571
569
return strHex;
572
570
}
573
571
574
- return blockheaderToJSON (tip, pblockindex);
572
+ return blockheaderToJSON (* tip, * pblockindex);
575
573
},
576
574
};
577
575
}
578
576
579
- static CBlock GetBlockChecked (BlockManager& blockman, const CBlockIndex* pblockindex )
577
+ static CBlock GetBlockChecked (BlockManager& blockman, const CBlockIndex& blockindex )
580
578
{
581
579
CBlock block;
582
580
{
583
581
LOCK (cs_main);
584
- if (blockman.IsBlockPruned (pblockindex )) {
582
+ if (blockman.IsBlockPruned (blockindex )) {
585
583
throw JSONRPCError (RPC_MISC_ERROR, " Block not available (pruned data)" );
586
584
}
587
585
}
588
586
589
- if (!blockman.ReadBlockFromDisk (block, *pblockindex )) {
587
+ if (!blockman.ReadBlockFromDisk (block, blockindex )) {
590
588
// Block not found on disk. This could be because we have the block
591
589
// header in our index but not yet have the block or did not accept the
592
590
// block. Or if the block was pruned right after we released the lock above.
@@ -596,21 +594,21 @@ static CBlock GetBlockChecked(BlockManager& blockman, const CBlockIndex* pblocki
596
594
return block;
597
595
}
598
596
599
- static CBlockUndo GetUndoChecked (BlockManager& blockman, const CBlockIndex* pblockindex )
597
+ static CBlockUndo GetUndoChecked (BlockManager& blockman, const CBlockIndex& blockindex )
600
598
{
601
599
CBlockUndo blockUndo;
602
600
603
601
// The Genesis block does not have undo data
604
- if (pblockindex-> nHeight == 0 ) return blockUndo;
602
+ if (blockindex. nHeight == 0 ) return blockUndo;
605
603
606
604
{
607
605
LOCK (cs_main);
608
- if (blockman.IsBlockPruned (pblockindex )) {
606
+ if (blockman.IsBlockPruned (blockindex )) {
609
607
throw JSONRPCError (RPC_MISC_ERROR, " Undo data not available (pruned data)" );
610
608
}
611
609
}
612
610
613
- if (!blockman.UndoReadFromDisk (blockUndo, *pblockindex )) {
611
+ if (!blockman.UndoReadFromDisk (blockUndo, blockindex )) {
614
612
throw JSONRPCError (RPC_MISC_ERROR, " Can't read undo data from disk" );
615
613
}
616
614
@@ -736,7 +734,7 @@ static RPCHelpMan getblock()
736
734
}
737
735
}
738
736
739
- const CBlock block{GetBlockChecked (chainman.m_blockman , pblockindex)};
737
+ const CBlock block{GetBlockChecked (chainman.m_blockman , * pblockindex)};
740
738
741
739
if (verbosity <= 0 )
742
740
{
@@ -755,7 +753,7 @@ static RPCHelpMan getblock()
755
753
tx_verbosity = TxVerbosity::SHOW_DETAILS_AND_PREVOUT;
756
754
}
757
755
758
- return blockToJSON (chainman.m_blockman , block, tip, pblockindex, tx_verbosity);
756
+ return blockToJSON (chainman.m_blockman , block, * tip, * pblockindex, tx_verbosity);
759
757
},
760
758
};
761
759
}
@@ -1257,7 +1255,7 @@ RPCHelpMan getblockchaininfo()
1257
1255
obj.pushKV (" blocks" , height);
1258
1256
obj.pushKV (" headers" , chainman.m_best_header ? chainman.m_best_header ->nHeight : -1 );
1259
1257
obj.pushKV (" bestblockhash" , tip.GetBlockHash ().GetHex ());
1260
- obj.pushKV (" difficulty" , GetDifficulty (& tip));
1258
+ obj.pushKV (" difficulty" , GetDifficulty (tip));
1261
1259
obj.pushKV (" time" , tip.GetBlockTime ());
1262
1260
obj.pushKV (" mediantime" , tip.GetMedianTimePast ());
1263
1261
obj.pushKV (" verificationprogress" , GuessVerificationProgress (chainman.GetParams ().TxData (), &tip));
@@ -1815,8 +1813,8 @@ static RPCHelpMan getblockstats()
1815
1813
}
1816
1814
}
1817
1815
1818
- const CBlock& block = GetBlockChecked (chainman.m_blockman , & pindex);
1819
- const CBlockUndo& blockUndo = GetUndoChecked (chainman.m_blockman , & pindex);
1816
+ const CBlock& block = GetBlockChecked (chainman.m_blockman , pindex);
1817
+ const CBlockUndo& blockUndo = GetUndoChecked (chainman.m_blockman , pindex);
1820
1818
1821
1819
const bool do_all = stats.size () == 0 ; // Calculate everything if nothing selected (default)
1822
1820
const bool do_mediantxsize = do_all || stats.count (" mediantxsize" ) != 0 ;
@@ -2275,8 +2273,8 @@ class BlockFiltersScanReserver
2275
2273
2276
2274
static bool CheckBlockFilterMatches (BlockManager& blockman, const CBlockIndex& blockindex, const GCSFilter::ElementSet& needles)
2277
2275
{
2278
- const CBlock block{GetBlockChecked (blockman, & blockindex)};
2279
- const CBlockUndo block_undo{GetUndoChecked (blockman, & blockindex)};
2276
+ const CBlock block{GetBlockChecked (blockman, blockindex)};
2277
+ const CBlockUndo block_undo{GetUndoChecked (blockman, blockindex)};
2280
2278
2281
2279
// Check if any of the outputs match the scriptPubKey
2282
2280
for (const auto & tx : block.vtx ) {
@@ -2845,7 +2843,7 @@ return RPCHelpMan{
2845
2843
2846
2844
data.pushKV (" blocks" , (int )chain.Height ());
2847
2845
data.pushKV (" bestblockhash" , tip->GetBlockHash ().GetHex ());
2848
- data.pushKV (" difficulty" , ( double ) GetDifficulty (tip));
2846
+ data.pushKV (" difficulty" , GetDifficulty (* tip));
2849
2847
data.pushKV (" verificationprogress" , GuessVerificationProgress (Params ().TxData (), tip));
2850
2848
data.pushKV (" coins_db_cache_bytes" , cs.m_coinsdb_cache_size_bytes );
2851
2849
data.pushKV (" coins_tip_cache_bytes" , cs.m_coinstip_cache_size_bytes );
0 commit comments