Skip to content

Commit ad67ff3

Browse files
committed
validation: remove snapshot datadirs upon validation failure
If a UTXO snapshot fails to validate, don't leave the resulting datadir on disk as this will confuse initialization on next startup and we'll get an assertion error.
1 parent 34d1590 commit ad67ff3

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/validation.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4876,7 +4876,22 @@ bool ChainstateManager::ActivateSnapshot(
48764876
}
48774877
}
48784878
if (!snapshot_ok) {
4879-
WITH_LOCK(::cs_main, this->MaybeRebalanceCaches());
4879+
LOCK(::cs_main);
4880+
this->MaybeRebalanceCaches();
4881+
4882+
// PopulateAndValidateSnapshot can return (in error) before the leveldb datadir
4883+
// has been created, so only attempt removal if we got that far.
4884+
if (auto snapshot_datadir = node::FindSnapshotChainstateDir()) {
4885+
// We have to destruct leveldb::DB in order to release the db lock, otherwise
4886+
// DestroyDB() (in DeleteCoinsDBFromDisk()) will fail. See `leveldb::~DBImpl()`.
4887+
// Destructing the chainstate (and so resetting the coinsviews object) does this.
4888+
snapshot_chainstate.reset();
4889+
bool removed = DeleteCoinsDBFromDisk(*snapshot_datadir, /*is_snapshot=*/true);
4890+
if (!removed) {
4891+
AbortNode(strprintf("Failed to remove snapshot chainstate dir (%s). "
4892+
"Manually remove it before restarting.\n", fs::PathToString(*snapshot_datadir)));
4893+
}
4894+
}
48804895
return false;
48814896
}
48824897

0 commit comments

Comments
 (0)