assumeutxo: remove snapshot during -reindex{-chainstate}

Removing a snapshot chainstate from disk (and memory) is consistent with
existing reindex operations.
This commit is contained in:
James O'Beirne 2023-05-03 15:39:51 -04:00
parent c93ef43e4f
commit c711ca186f
3 changed files with 39 additions and 9 deletions

View file

@ -185,7 +185,14 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
chainman.InitializeChainstate(options.mempool); chainman.InitializeChainstate(options.mempool);
// Load a chain created from a UTXO snapshot, if any exist. // Load a chain created from a UTXO snapshot, if any exist.
chainman.DetectSnapshotChainstate(options.mempool); bool has_snapshot = chainman.DetectSnapshotChainstate(options.mempool);
if (has_snapshot && (options.reindex || options.reindex_chainstate)) {
LogPrintf("[snapshot] deleting snapshot chainstate due to reindexing\n");
if (!chainman.DeleteSnapshotChainstate()) {
return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated("Couldn't remove snapshot chainstate.")};
}
}
auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options); auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options);
if (init_status != ChainstateLoadStatus::SUCCESS) { if (init_status != ChainstateLoadStatus::SUCCESS) {

View file

@ -5111,7 +5111,7 @@ const AssumeutxoData* ExpectedAssumeutxo(
return nullptr; return nullptr;
} }
static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot) [[nodiscard]] static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
{ {
AssertLockHeld(::cs_main); AssertLockHeld(::cs_main);
@ -5750,15 +5750,20 @@ bool IsBIP30Unspendable(const CBlockIndex& block_index)
(block_index.nHeight==91812 && block_index.GetBlockHash() == uint256S("0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f")); (block_index.nHeight==91812 && block_index.GetBlockHash() == uint256S("0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"));
} }
util::Result<void> Chainstate::InvalidateCoinsDBOnDisk() static fs::path GetSnapshotCoinsDBPath(Chainstate& cs) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
{ {
AssertLockHeld(::cs_main); AssertLockHeld(::cs_main);
// Should never be called on a non-snapshot chainstate. // Should never be called on a non-snapshot chainstate.
assert(m_from_snapshot_blockhash); assert(cs.m_from_snapshot_blockhash);
auto storage_path_maybe = this->CoinsDB().StoragePath(); auto storage_path_maybe = cs.CoinsDB().StoragePath();
// Should never be called with a non-existent storage path. // Should never be called with a non-existent storage path.
assert(storage_path_maybe); assert(storage_path_maybe);
fs::path snapshot_datadir = *storage_path_maybe; return *storage_path_maybe;
}
util::Result<void> Chainstate::InvalidateCoinsDBOnDisk()
{
fs::path snapshot_datadir = GetSnapshotCoinsDBPath(*this);
// Coins views no longer usable. // Coins views no longer usable.
m_coins_views.reset(); m_coins_views.reset();
@ -5789,6 +5794,23 @@ util::Result<void> Chainstate::InvalidateCoinsDBOnDisk()
return {}; return {};
} }
bool ChainstateManager::DeleteSnapshotChainstate()
{
AssertLockHeld(::cs_main);
Assert(m_snapshot_chainstate);
Assert(m_ibd_chainstate);
fs::path snapshot_datadir = GetSnapshotCoinsDBPath(*m_snapshot_chainstate);
if (!DeleteCoinsDBFromDisk(snapshot_datadir, /*is_snapshot=*/ true)) {
LogPrintf("Deletion of %s failed. Please remove it manually to continue reindexing.\n",
fs::PathToString(snapshot_datadir));
return false;
}
m_active_chainstate = m_ibd_chainstate.get();
m_snapshot_chainstate.reset();
return true;
}
const CBlockIndex* ChainstateManager::GetSnapshotBaseBlock() const const CBlockIndex* ChainstateManager::GetSnapshotBaseBlock() const
{ {
return m_active_chainstate ? m_active_chainstate->SnapshotBase() : nullptr; return m_active_chainstate ? m_active_chainstate->SnapshotBase() : nullptr;

View file

@ -848,9 +848,6 @@ private:
//! Points to either the ibd or snapshot chainstate; indicates our //! Points to either the ibd or snapshot chainstate; indicates our
//! most-work chain. //! most-work chain.
//! //!
//! Once this pointer is set to a corresponding chainstate, it will not
//! be reset until init.cpp:Shutdown().
//!
//! This is especially important when, e.g., calling ActivateBestChain() //! This is especially important when, e.g., calling ActivateBestChain()
//! on all chainstates because we are not able to hold ::cs_main going into //! on all chainstates because we are not able to hold ::cs_main going into
//! that call. //! that call.
@ -1203,6 +1200,10 @@ public:
void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main); void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
//! Remove the snapshot-based chainstate and all on-disk artifacts.
//! Used when reindex{-chainstate} is called during snapshot use.
[[nodiscard]] bool DeleteSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
//! Switch the active chainstate to one based on a UTXO snapshot that was loaded //! Switch the active chainstate to one based on a UTXO snapshot that was loaded
//! previously. //! previously.
Chainstate& ActivateExistingSnapshot(CTxMemPool* mempool, uint256 base_blockhash) Chainstate& ActivateExistingSnapshot(CTxMemPool* mempool, uint256 base_blockhash)