diff options
author | Jim Posen <jim.posen@gmail.com> | 2019-03-02 18:35:55 -0800 |
---|---|---|
committer | Jim Posen <jim.posen@gmail.com> | 2019-04-06 12:03:21 -0700 |
commit | 4368384f1d267b011e03a805f934f5c47e2ca1b2 (patch) | |
tree | 19895592c19ffb3c2ceee7509cdda57988bf1a5f /src/index/base.cpp | |
parent | e439aeb30c0439001a781c5979aec41e1fc2aa50 (diff) | |
download | bitcoin-4368384f1d267b011e03a805f934f5c47e2ca1b2.tar.xz |
index: Allow atomic commits of index state to be extended.
Diffstat (limited to 'src/index/base.cpp')
-rw-r--r-- | src/index/base.cpp | 39 |
1 files changed, 27 insertions, 12 deletions
diff --git a/src/index/base.cpp b/src/index/base.cpp index f6f59572ce..15ad04d462 100644 --- a/src/index/base.cpp +++ b/src/index/base.cpp @@ -41,9 +41,9 @@ bool BaseIndex::DB::ReadBestBlock(CBlockLocator& locator) const return success; } -bool BaseIndex::DB::WriteBestBlock(const CBlockLocator& locator) +void BaseIndex::DB::WriteBestBlock(CDBBatch& batch, const CBlockLocator& locator) { - return Write(DB_BEST_BLOCK, locator); + batch.Write(DB_BEST_BLOCK, locator); } BaseIndex::~BaseIndex() @@ -95,7 +95,11 @@ void BaseIndex::ThreadSync() int64_t last_locator_write_time = 0; while (true) { if (m_interrupt) { - WriteBestBlock(pindex); + m_best_block_index = pindex; + // No need to handle errors in Commit. If it fails, the error will be already be + // logged. The best way to recover is to continue, as index cannot be corrupted by + // a missed commit to disk for an advanced index state. + Commit(); return; } @@ -103,9 +107,10 @@ void BaseIndex::ThreadSync() LOCK(cs_main); const CBlockIndex* pindex_next = NextSyncBlock(pindex); if (!pindex_next) { - WriteBestBlock(pindex); m_best_block_index = pindex; m_synced = true; + // No need to handle errors in Commit. See rationale above. + Commit(); break; } pindex = pindex_next; @@ -119,8 +124,10 @@ void BaseIndex::ThreadSync() } if (last_locator_write_time + SYNC_LOCATOR_WRITE_INTERVAL < current_time) { - WriteBestBlock(pindex); + m_best_block_index = pindex; last_locator_write_time = current_time; + // No need to handle errors in Commit. See rationale above. + Commit(); } CBlock block; @@ -144,15 +151,22 @@ void BaseIndex::ThreadSync() } } -bool BaseIndex::WriteBestBlock(const CBlockIndex* block_index) +bool BaseIndex::Commit() { - LOCK(cs_main); - if (!GetDB().WriteBestBlock(chainActive.GetLocator(block_index))) { - return error("%s: Failed to write locator to disk", __func__); + CDBBatch batch(GetDB()); + if (!CommitInternal(batch) || !GetDB().WriteBatch(batch)) { + return error("%s: Failed to commit latest %s state", __func__, GetName()); } return true; } +bool BaseIndex::CommitInternal(CDBBatch& batch) +{ + LOCK(cs_main); + GetDB().WriteBestBlock(batch, chainActive.GetLocator(m_best_block_index)); + return true; +} + void BaseIndex::BlockConnected(const std::shared_ptr<const CBlock>& block, const CBlockIndex* pindex, const std::vector<CTransactionRef>& txn_conflicted) { @@ -224,9 +238,10 @@ void BaseIndex::ChainStateFlushed(const CBlockLocator& locator) return; } - if (!GetDB().WriteBestBlock(locator)) { - error("%s: Failed to write locator to disk", __func__); - } + // No need to handle errors in Commit. If it fails, the error will be already be logged. The + // best way to recover is to continue, as index cannot be corrupted by a missed commit to disk + // for an advanced index state. + Commit(); } bool BaseIndex::BlockUntilSyncedToCurrentChain() |