Skip to content

Commit 47bc99c

Browse files
committed
blockindex: do not point to file 0 if data does not exist
better to store an invalid file number to describe block indexes with no data on disk.
1 parent ea41aba commit 47bc99c

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

src/chain.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ class CBlockIndex
163163
int nHeight{0};
164164

165165
//! Which # file this block is stored in (blk?????.dat)
166-
int nFile GUARDED_BY(::cs_main){0};
166+
int nFile GUARDED_BY(::cs_main){-1};
167167

168168
//! Byte offset within blk?????.dat where this block's data is stored
169169
unsigned int nDataPos GUARDED_BY(::cs_main){0};

src/node/blockstorage.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ void BlockManager::PruneOneBlockFile(const int fileNumber)
127127
if (pindex->nFile == fileNumber) {
128128
pindex->nStatus &= ~BLOCK_HAVE_DATA;
129129
pindex->nStatus &= ~BLOCK_HAVE_UNDO;
130-
pindex->nFile = 0;
130+
pindex->nFile = -1;
131131
pindex->nDataPos = 0;
132132
pindex->nUndoPos = 0;
133133
m_dirty_blockindex.insert(pindex);
@@ -345,9 +345,15 @@ bool BlockManager::LoadBlockIndexDB(const Consensus::Params& consensus_params)
345345
// Check presence of blk files
346346
LogPrintf("Checking all blk files are present...\n");
347347
std::set<int> setBlkDataFiles;
348-
for (const auto& [_, block_index] : m_block_index) {
348+
for (auto& [_, block_index] : m_block_index) {
349349
if (block_index.nStatus & BLOCK_HAVE_DATA) {
350350
setBlkDataFiles.insert(block_index.nFile);
351+
} else {
352+
// In case we don't have the block, the position must be -1.
353+
// (applies to older clients that set 'nFile=0' during pruning)
354+
if (block_index.nFile == 0) {
355+
block_index.nFile = -1;
356+
}
351357
}
352358
}
353359
for (std::set<int>::iterator it = setBlkDataFiles.begin(); it != setBlkDataFiles.end(); it++) {

0 commit comments

Comments
 (0)