Skip to content

Commit 231fc7b

Browse files
committed
refactor: Introduce GetFirstStoredBlock helper function
1 parent 1e7db37 commit 231fc7b

File tree

4 files changed

+18
-14
lines changed

4 files changed

+18
-14
lines changed

src/index/base.cpp

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,7 @@ bool BaseIndex::Init()
7575
if (!m_best_block_index) {
7676
// index is not built yet
7777
// 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();
8379
}
8480
// in case the index has a best block set and is not fully synced
8581
// check if we have the required blocks to continue building the index

src/node/blockstorage.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,16 @@ bool BlockManager::IsBlockPruned(const CBlockIndex* pblockindex)
397397
return (m_have_pruned && !(pblockindex->nStatus & BLOCK_HAVE_DATA) && pblockindex->nTx > 0);
398398
}
399399

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+
400410
// If we're using -prune with -reindex, then delete block files that will be ignored by the
401411
// reindex. Since reindexing works by starting at block file 0 and looping until a blockfile
402412
// is missing, do the same here to delete any later block files after a gap. Also delete all

src/node/blockstorage.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,9 @@ class BlockManager
181181
}
182182
};
183183

184+
//! Find the first block that is not pruned
185+
const CBlockIndex* GetFirstStoredBlock(const CBlockIndex* start_block) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
186+
184187
void CleanupBlockRevFiles();
185188

186189
/** Open a block file (blk?????.dat) */

src/rpc/blockchain.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -787,10 +787,9 @@ static RPCHelpMan pruneblockchain()
787787

788788
PruneBlockFilesManual(active_chainstate, height);
789789
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);
794793
},
795794
};
796795
}
@@ -1217,11 +1216,7 @@ RPCHelpMan getblockchaininfo()
12171216
obj.pushKV("pruned", node::fPruneMode);
12181217
if (node::fPruneMode) {
12191218
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);
12251220

12261221
// if 0, execution bypasses the whole if block.
12271222
bool automatic_pruning{args.GetIntArg("-prune", 0) != 1};

0 commit comments

Comments
 (0)