diff options
Diffstat (limited to 'src/index/base.cpp')
-rw-r--r-- | src/index/base.cpp | 20 |
1 files changed, 17 insertions, 3 deletions
diff --git a/src/index/base.cpp b/src/index/base.cpp index 1ebe89ef7c..3eea09b17d 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -18,6 +18,9 @@ #include <validation.h> // For g_chainman #include <warnings.h> +#include <string> +#include <utility> + using node::ReadBlockFromDisk; constexpr uint8_t DB_BEST_BLOCK{'B'}; @@ -62,8 +65,8 @@ void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator batch.Write(DB_BEST_BLOCK, locator); } -BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain) - : m_chain{std::move(chain)} {} +BaseIndex::BaseIndex(std::unique_ptr<interfaces::Chain> chain, std::string name) + : m_chain{std::move(chain)}, m_name{std::move(name)} {} BaseIndex::~BaseIndex() { @@ -295,6 +298,10 @@ void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const } interfaces::BlockInfo block_info = kernel::MakeBlockInfo(pindex, block.get()); if (CustomAppend(block_info)) { + // Setting the best block index is intentionally the last step of this + // function, so BlockUntilSyncedToCurrentChain callers waiting for the + // best block index to be updated can rely on the block being fully + // processed, and the index object being safe to delete. SetBestBlockIndex(pindex); } else { FatalError("%s: Failed to write block %s to index", @@ -411,10 +418,17 @@ IndexSummary BaseIndex::GetSummary() const void BaseIndex::SetBestBlockIndex(const CBlockIndex* block) { assert(!node::fPruneMode || AllowPrune()); - m_best_block_index = block; if (AllowPrune() && block) { node::PruneLockInfo prune_lock; prune_lock.height_first = block->nHeight; WITH_LOCK(::cs_main, m_chainstate->m_blockman.UpdatePruneLock(GetName(), prune_lock)); } + + // Intentionally set m_best_block_index as the last step in this function, + // after updating prune locks above, and after making any other references + // to *this, so the BlockUntilSyncedToCurrentChain function (which checks + // m_best_block_index as an optimization) can be used to wait for the last + // BlockConnected notification and safely assume that prune locks are + // updated and that the index object is safe to delete. + m_best_block_index = block; } |