Skip to content

Commit c711ca1

Browse files
committed
assumeutxo: remove snapshot during -reindex{-chainstate}
Removing a snapshot chainstate from disk (and memory) is consistent with existing reindex operations.
1 parent c93ef43 commit c711ca1

File tree

3 files changed

+39
-9
lines changed

3 files changed

+39
-9
lines changed

src/node/chainstate.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,14 @@ ChainstateLoadResult LoadChainstate(ChainstateManager& chainman, const CacheSize
185185
chainman.InitializeChainstate(options.mempool);
186186

187187
// Load a chain created from a UTXO snapshot, if any exist.
188-
chainman.DetectSnapshotChainstate(options.mempool);
188+
bool has_snapshot = chainman.DetectSnapshotChainstate(options.mempool);
189+
190+
if (has_snapshot && (options.reindex || options.reindex_chainstate)) {
191+
LogPrintf("[snapshot] deleting snapshot chainstate due to reindexing\n");
192+
if (!chainman.DeleteSnapshotChainstate()) {
193+
return {ChainstateLoadStatus::FAILURE_FATAL, Untranslated("Couldn't remove snapshot chainstate.")};
194+
}
195+
}
189196

190197
auto [init_status, init_error] = CompleteChainstateInitialization(chainman, cache_sizes, options);
191198
if (init_status != ChainstateLoadStatus::SUCCESS) {

src/validation.cpp

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5111,7 +5111,7 @@ const AssumeutxoData* ExpectedAssumeutxo(
51115111
return nullptr;
51125112
}
51135113

5114-
static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot)
5114+
[[nodiscard]] static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot)
51155115
EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
51165116
{
51175117
AssertLockHeld(::cs_main);
@@ -5750,15 +5750,20 @@ bool IsBIP30Unspendable(const CBlockIndex& block_index)
57505750
(block_index.nHeight==91812 && block_index.GetBlockHash() == uint256S("0x00000000000af0aed4792b1acee3d966af36cf5def14935db8de83d6f9306f2f"));
57515751
}
57525752

5753-
util::Result<void> Chainstate::InvalidateCoinsDBOnDisk()
5753+
static fs::path GetSnapshotCoinsDBPath(Chainstate& cs) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
57545754
{
57555755
AssertLockHeld(::cs_main);
57565756
// Should never be called on a non-snapshot chainstate.
5757-
assert(m_from_snapshot_blockhash);
5758-
auto storage_path_maybe = this->CoinsDB().StoragePath();
5757+
assert(cs.m_from_snapshot_blockhash);
5758+
auto storage_path_maybe = cs.CoinsDB().StoragePath();
57595759
// Should never be called with a non-existent storage path.
57605760
assert(storage_path_maybe);
5761-
fs::path snapshot_datadir = *storage_path_maybe;
5761+
return *storage_path_maybe;
5762+
}
5763+
5764+
util::Result<void> Chainstate::InvalidateCoinsDBOnDisk()
5765+
{
5766+
fs::path snapshot_datadir = GetSnapshotCoinsDBPath(*this);
57625767

57635768
// Coins views no longer usable.
57645769
m_coins_views.reset();
@@ -5789,6 +5794,23 @@ util::Result<void> Chainstate::InvalidateCoinsDBOnDisk()
57895794
return {};
57905795
}
57915796

5797+
bool ChainstateManager::DeleteSnapshotChainstate()
5798+
{
5799+
AssertLockHeld(::cs_main);
5800+
Assert(m_snapshot_chainstate);
5801+
Assert(m_ibd_chainstate);
5802+
5803+
fs::path snapshot_datadir = GetSnapshotCoinsDBPath(*m_snapshot_chainstate);
5804+
if (!DeleteCoinsDBFromDisk(snapshot_datadir, /*is_snapshot=*/ true)) {
5805+
LogPrintf("Deletion of %s failed. Please remove it manually to continue reindexing.\n",
5806+
fs::PathToString(snapshot_datadir));
5807+
return false;
5808+
}
5809+
m_active_chainstate = m_ibd_chainstate.get();
5810+
m_snapshot_chainstate.reset();
5811+
return true;
5812+
}
5813+
57925814
const CBlockIndex* ChainstateManager::GetSnapshotBaseBlock() const
57935815
{
57945816
return m_active_chainstate ? m_active_chainstate->SnapshotBase() : nullptr;

src/validation.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -848,9 +848,6 @@ class ChainstateManager
848848
//! Points to either the ibd or snapshot chainstate; indicates our
849849
//! most-work chain.
850850
//!
851-
//! Once this pointer is set to a corresponding chainstate, it will not
852-
//! be reset until init.cpp:Shutdown().
853-
//!
854851
//! This is especially important when, e.g., calling ActivateBestChain()
855852
//! on all chainstates because we are not able to hold ::cs_main going into
856853
//! that call.
@@ -1203,6 +1200,10 @@ class ChainstateManager
12031200

12041201
void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
12051202

1203+
//! Remove the snapshot-based chainstate and all on-disk artifacts.
1204+
//! Used when reindex{-chainstate} is called during snapshot use.
1205+
[[nodiscard]] bool DeleteSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1206+
12061207
//! Switch the active chainstate to one based on a UTXO snapshot that was loaded
12071208
//! previously.
12081209
Chainstate& ActivateExistingSnapshot(CTxMemPool* mempool, uint256 base_blockhash)

0 commit comments

Comments
 (0)