Skip to content

Commit e41667b

Browse files
ryanofskymzumsande
andcommitted
blockstorage: Don't move cursor backwards in UpdateBlockInfo
Previously, it was possible to move the cursor back to an older file during reindex if blocks are enocuntered out of order during reindex. This would mean that MaxBlockfileNum() would be incorrect, and a wrong DB_LAST_BLOCK could be written to disk. This improves the logic by only ever moving the cursor forward (if possible) but not backwards. Co-authored-by: Martin Zumsande <[email protected]>
1 parent 1710363 commit e41667b

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/node/blockstorage.cpp

+8-13
Original file line numberDiff line numberDiff line change
@@ -942,24 +942,19 @@ void BlockManager::UpdateBlockInfo(const CBlock& block, unsigned int nHeight, co
942942
{
943943
LOCK(cs_LastBlockFile);
944944

945+
// Update the cursor so it points to the last file.
946+
const BlockfileType chain_type{BlockfileTypeForHeight(nHeight)};
947+
auto& cursor{m_blockfile_cursors[chain_type]};
948+
if (!cursor || cursor->file_num < pos.nFile) {
949+
m_blockfile_cursors[chain_type] = BlockfileCursor{pos.nFile};
950+
}
951+
952+
// Update the file information with the current block.
945953
const unsigned int added_size = ::GetSerializeSize(TX_WITH_WITNESS(block));
946-
const BlockfileType chain_type = BlockfileTypeForHeight(nHeight);
947-
// Check that chain type is NORMAL, because this function is only
948-
// called during reindexing, and reindexing deletes snapshot chainstates, so
949-
// chain_type will not be SNAPSHOT. Also check that cursor exists, because
950-
// the normal cursor should never be null.
951-
Assume(chain_type == BlockfileType::NORMAL);
952-
Assume(m_blockfile_cursors[chain_type]);
953954
const int nFile = pos.nFile;
954955
if (static_cast<int>(m_blockfile_info.size()) <= nFile) {
955956
m_blockfile_info.resize(nFile + 1);
956957
}
957-
958-
const int last_blockfile = m_blockfile_cursors[chain_type]->file_num;
959-
if (nFile != last_blockfile) {
960-
// No undo data yet in the new file, so reset our undo-height tracking.
961-
m_blockfile_cursors[chain_type] = BlockfileCursor{nFile};
962-
}
963958
m_blockfile_info[nFile].AddBlock(nHeight, block.GetBlockTime());
964959
m_blockfile_info[nFile].nSize = std::max(pos.nPos + added_size, m_blockfile_info[nFile].nSize);
965960
m_dirty_fileinfo.insert(nFile);

0 commit comments

Comments
 (0)