From fa467f3913918701c765f9bc754203b4591b894f Mon Sep 17 00:00:00 2001 From: MarcoFalke Date: Wed, 5 Jan 2022 15:06:56 +0100 Subject: [PATCH] move-only: Create WriteBlockIndexDB helper Can be reviewed with --color-moved=dimmed-zebra --color-moved-ws=ignore-all-space --- src/node/blockstorage.cpp | 20 ++++++++++++++++++++ src/node/blockstorage.h | 1 + src/validation.cpp | 14 +------------- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/node/blockstorage.cpp b/src/node/blockstorage.cpp index ad2bb3d3e8b24..3cc53e27e2c6c 100644 --- a/src/node/blockstorage.cpp +++ b/src/node/blockstorage.cpp @@ -350,6 +350,26 @@ void BlockManager::Unload() m_block_index.clear(); } +bool BlockManager::WriteBlockIndexDB() +{ + std::vector> vFiles; + vFiles.reserve(setDirtyFileInfo.size()); + for (std::set::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end();) { + vFiles.push_back(std::make_pair(*it, &vinfoBlockFile[*it])); + setDirtyFileInfo.erase(it++); + } + std::vector vBlocks; + vBlocks.reserve(setDirtyBlockIndex.size()); + for (std::set::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)) { diff --git a/src/node/blockstorage.h b/src/node/blockstorage.h index 2850e3618856a..91d651bac2d20 100644 --- a/src/node/blockstorage.h +++ b/src/node/blockstorage.h @@ -102,6 +102,7 @@ class BlockManager std::unique_ptr m_block_tree_db GUARDED_BY(::cs_main); + bool WriteBlockIndexDB() EXCLUSIVE_LOCKS_REQUIRED(::cs_main); bool LoadBlockIndexDB(ChainstateManager& chainman) EXCLUSIVE_LOCKS_REQUIRED(::cs_main); /** diff --git a/src/validation.cpp b/src/validation.cpp index bce4659989307..58bced48c9dba 100644 --- a/src/validation.cpp +++ b/src/validation.cpp @@ -2276,19 +2276,7 @@ bool CChainState::FlushStateToDisk( { LOG_TIME_MILLIS_WITH_CATEGORY("write block index to disk", BCLog::BENCH); - std::vector > vFiles; - vFiles.reserve(setDirtyFileInfo.size()); - for (std::set::iterator it = setDirtyFileInfo.begin(); it != setDirtyFileInfo.end(); ) { - vFiles.push_back(std::make_pair(*it, &vinfoBlockFile[*it])); - setDirtyFileInfo.erase(it++); - } - std::vector vBlocks; - vBlocks.reserve(setDirtyBlockIndex.size()); - for (std::set::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"); } }