Skip to content

Commit d592bb8

Browse files
ryanofskymzumsande
authored 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 <mzumsande@gmail.com>
1 parent 81e8e73 commit d592bb8

File tree

1 file changed

+8
-13
lines changed

1 file changed

+8
-13
lines changed

src/node/blockstorage.cpp

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

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

0 commit comments

Comments
 (0)