File tree Expand file tree Collapse file tree 4 files changed +18
-14
lines changed Expand file tree Collapse file tree 4 files changed +18
-14
lines changed Original file line number Diff line number Diff line change @@ -75,11 +75,7 @@ bool BaseIndex::Init()
75
75
if (!m_best_block_index) {
76
76
// index is not built yet
77
77
// make sure we have all block data back to the genesis
78
- const CBlockIndex* block = active_chain.Tip ();
79
- while (block->pprev && (block->pprev ->nStatus & BLOCK_HAVE_DATA)) {
80
- block = block->pprev ;
81
- }
82
- prune_violation = block != active_chain.Genesis ();
78
+ prune_violation = node::GetFirstStoredBlock (active_chain.Tip ()) != active_chain.Genesis ();
83
79
}
84
80
// in case the index has a best block set and is not fully synced
85
81
// check if we have the required blocks to continue building the index
Original file line number Diff line number Diff line change @@ -397,6 +397,16 @@ bool BlockManager::IsBlockPruned(const CBlockIndex* pblockindex)
397
397
return (m_have_pruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0 );
398
398
}
399
399
400
+ const CBlockIndex* GetFirstStoredBlock (const CBlockIndex* start_block) {
401
+ AssertLockHeld (::cs_main);
402
+ assert (start_block);
403
+ const CBlockIndex* last_block = start_block;
404
+ while (last_block->pprev && (last_block->pprev ->nStatus & BLOCK_HAVE_DATA)) {
405
+ last_block = last_block->pprev ;
406
+ }
407
+ return last_block;
408
+ }
409
+
400
410
// If we're using -prune with -reindex, then delete block files that will be ignored by the
401
411
// reindex. Since reindexing works by starting at block file 0 and looping until a blockfile
402
412
// is missing, do the same here to delete any later block files after a gap. Also delete all
Original file line number Diff line number Diff line change @@ -181,6 +181,9 @@ class BlockManager
181
181
}
182
182
};
183
183
184
+ // ! Find the first block that is not pruned
185
+ const CBlockIndex* GetFirstStoredBlock (const CBlockIndex* start_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
186
+
184
187
void CleanupBlockRevFiles ();
185
188
186
189
/* * Open a block file (blk?????.dat) */
Original file line number Diff line number Diff line change @@ -787,10 +787,9 @@ static RPCHelpMan pruneblockchain()
787
787
788
788
PruneBlockFilesManual (active_chainstate, height);
789
789
const CBlockIndex* block = CHECK_NONFATAL (active_chain.Tip ());
790
- while (block->pprev && (block->pprev ->nStatus & BLOCK_HAVE_DATA)) {
791
- block = block->pprev ;
792
- }
793
- return uint64_t (block->nHeight );
790
+ const CBlockIndex* last_block = node::GetFirstStoredBlock (block);
791
+
792
+ return static_cast <uint64_t >(last_block->nHeight );
794
793
},
795
794
};
796
795
}
@@ -1217,11 +1216,7 @@ RPCHelpMan getblockchaininfo()
1217
1216
obj.pushKV (" pruned" , node::fPruneMode );
1218
1217
if (node::fPruneMode ) {
1219
1218
const CBlockIndex* block = CHECK_NONFATAL (tip);
1220
- while (block->pprev && (block->pprev ->nStatus & BLOCK_HAVE_DATA)) {
1221
- block = block->pprev ;
1222
- }
1223
-
1224
- obj.pushKV (" pruneheight" , block->nHeight );
1219
+ obj.pushKV (" pruneheight" , node::GetFirstStoredBlock (block)->nHeight );
1225
1220
1226
1221
// if 0, execution bypasses the whole if block.
1227
1222
bool automatic_pruning{args.GetIntArg (" -prune" , 0 ) != 1 };
You can’t perform that action at this time.
0 commit comments