aboutsummaryrefslogtreecommitdiff
path: root/src/node
diff options
context:
space:
mode:
authorRyan Ofsky <ryan@ofsky.org>2024-05-09 18:34:14 -0400
committerMartin Zumsande <mzumsande@gmail.com>2024-05-14 14:54:27 -0400
commite41667b720372dae8438ea86e9819027e62b54e0 (patch)
treedab85a50e65941b36af6fb97653a3a0bf3053a24 /src/node
parent17103637c6fa2dfcf5374ebb0cd715e540dd4ce1 (diff)
downloadbitcoin-e41667b720372dae8438ea86e9819027e62b54e0.tar.xz
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>
Diffstat (limited to 'src/node')
-rw-r--r--src/node/blockstorage.cpp21
1 files changed, 8 insertions, 13 deletions
diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp
index aab6a9e7fb..2c0cbe7163 100644
--- a/src/node/blockstorage.cpp
+++ b/src/node/blockstorage.cpp
@@ -942,24 +942,19 @@ void BlockManager::UpdateBlockInfo(const CBlock& block, unsigned int nHeight, co
{
LOCK(cs_LastBlockFile);
+ // Update the cursor so it points to the last file.
+ const BlockfileType chain_type{BlockfileTypeForHeight(nHeight)};
+ auto& cursor{m_blockfile_cursors[chain_type]};
+ if (!cursor || cursor->file_num < pos.nFile) {
+ m_blockfile_cursors[chain_type] = BlockfileCursor{pos.nFile};
+ }
+
+ // Update the file information with the current block.
const unsigned int added_size = ::GetSerializeSize(TX_WITH_WITNESS(block));
- const BlockfileType chain_type = BlockfileTypeForHeight(nHeight);
- // Check that chain type is NORMAL, because this function is only
- // called during reindexing, and reindexing deletes snapshot chainstates, so
- // chain_type will not be SNAPSHOT. Also check that cursor exists, because
- // the normal cursor should never be null.
- Assume(chain_type == BlockfileType::NORMAL);
- Assume(m_blockfile_cursors[chain_type]);
const int nFile = pos.nFile;
if (static_cast<int>(m_blockfile_info.size()) <= nFile) {
m_blockfile_info.resize(nFile + 1);
}
-
- const int last_blockfile = m_blockfile_cursors[chain_type]->file_num;
- if (nFile != last_blockfile) {
- // No undo data yet in the new file, so reset our undo-height tracking.
- m_blockfile_cursors[chain_type] = BlockfileCursor{nFile};
- }
m_blockfile_info[nFile].AddBlock(nHeight, block.GetBlockTime());
m_blockfile_info[nFile].nSize = std::max(pos.nPos + added_size, m_blockfile_info[nFile].nSize);
m_dirty_fileinfo.insert(nFile);