Skip to content

Commit

Permalink
move-only: Create WriteBlockIndexDB helper
Browse files Browse the repository at this point in the history
Can be reviewed with --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space
  • Loading branch information
MarcoFalke committed Jan 5, 2022
1 parent fa88cfd commit fa467f3
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
20 changes: 20 additions & 0 deletions src/node/blockstorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,26 @@ void BlockManager::Unload()
m_block_index.clear();
}

bool BlockManager::WriteBlockIndexDB()
{
std::vector<std::pair<int, const CBlockFileInfo*>> vFiles;
vFiles.reserve(setDirtyFileInfo.size());
for (std::set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end();) {
vFiles.push_back(std::make_pair(*it, &vinfoBlockFile[*it]));
setDirtyFileInfo.erase(it++);
}
std::vector<const CBlockIndex*> vBlocks;
vBlocks.reserve(setDirtyBlockIndex.size());
for (std::set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end();) {
vBlocks.push_back(*it);
setDirtyBlockIndex.erase(it++);
}
if (!m_block_tree_db->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
return false;
}
return true;
}

bool BlockManager::LoadBlockIndexDB(ChainstateManager& chainman)
{
if (!LoadBlockIndex(::Params().GetConsensus(), chainman)) {
Expand Down
1 change: 1 addition & 0 deletions src/node/blockstorage.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ class BlockManager

std::unique_ptr<CBlockTreeDB> m_block_tree_db GUARDED_BY(::cs_main);

bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
bool LoadBlockIndexDB(ChainstateManager& chainman) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);

/**
Expand Down
14 changes: 1 addition & 13 deletions src/validation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2276,19 +2276,7 @@ bool CChainState::FlushStateToDisk(
{
LOG_TIME_MILLIS_WITH_CATEGORY("write block index to disk", BCLog::BENCH);

std::vector<std::pair<int, const CBlockFileInfo*> > vFiles;
vFiles.reserve(setDirtyFileInfo.size());
for (std::set<int>::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) {
vFiles.push_back(std::make_pair(*it, &vinfoBlockFile[*it]));
setDirtyFileInfo.erase(it++);
}
std::vector<const CBlockIndex*> vBlocks;
vBlocks.reserve(setDirtyBlockIndex.size());
for (std::set<CBlockIndex*>::iterator it = setDirtyBlockIndex.begin(); it != setDirtyBlockIndex.end(); ) {
vBlocks.push_back(*it);
setDirtyBlockIndex.erase(it++);
}
if (!m_blockman.m_block_tree_db->WriteBatchSync(vFiles, nLastBlockFile, vBlocks)) {
if (!m_blockman.WriteBlockIndexDB()) {
return AbortNode(state, "Failed to write to block index database");
}
}
Expand Down

0 comments on commit fa467f3

Please sign in to comment.