mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-20 07:09:15 +01:00
Merge bitcoin/bitcoin#25667: assumeutxo: snapshot initialization
bf95976061doc: add note about snapshot chainstate init (James O'Beirne)e4d7995286test: add testcases for snapshot initialization (James O'Beirne)cced4e7336test: move-only-ish: factor out LoadVerifyActivateChainstate() (James O'Beirne)51fc9241c0test: allow on-disk coins and block tree dbs in tests (James O'Beirne)3c361391b8test: add reset_chainstate parameter for snapshot unittests (James O'Beirne)00b357c215validation: add ResetChainstates() (James O'Beirne)3a29dfbfb2move-only: test: make snapshot chainstate setup reusable (James O'Beirne)8153bd9247blockmanager: avoid undefined behavior during FlushBlockFile (James O'Beirne)ad67ff377cvalidation: remove snapshot datadirs upon validation failure (James O'Beirne)34d1590331add utilities for deleting on-disk leveldb data (James O'Beirne)252abd1e8binit: add utxo snapshot detection (James O'Beirne)f9f1735f13validation: rename snapshot chainstate dir (James O'Beirne)d14bebf100db: add StoragePath to CDBWrapper/CCoinsViewDB (James O'Beirne) Pull request description: This is part of the [assumeutxo project](https://github.com/bitcoin/bitcoin/projects/11) (parent PR: https://github.com/bitcoin/bitcoin/pull/15606) --- Half of the replacement for #24232. The original PR grew larger than expected throughout the review process. This change adds the ability to initialize a snapshot-based chainstate during init if one is detected on disk. This is of course unused as of now (aside from in unittests) given that we haven't yet enabled actually loading snapshots. Don't be scared! There are some big move-only commits in here. Accompanying changes include: - moving the snapshot coinsdb directory from being called `chainstate_[base blockhash]` to `chainstate_snapshot`, since we only support one snapshot in use at a time. This simplifies some logic, but it necessitates writing that base blockhash out to a file within the coinsdb dir. See [discussion here](https://github.com/bitcoin/bitcoin/pull/24232#discussion_r832762880). - adding a simple fix in `FlushBlockFile()` that avoids a crash when attemping to flush to disk before `LoadBlockIndexDB()` is called, which happens when calling `MaybeRebalanceCaches()` during multiple chainstate init. - improving the unittest to allow testing with on-disk chainstates - necessary to test a simulated restart and re-initialization. ACKs for top commit: naumenkogs: utACKbf95976061ariard: Code Review ACKbf9597606ryanofsky: Code review ACKbf95976061. Changes since last review: rebasing, switching from CAutoFile to AutoFile, adding comments, switching from BOOST_CHECK to Assert in test util, using chainman.GetMutex() in tests, destroying one ChainstateManager before creating a new one in tests fjahr: utACKbf95976061aureleoules: ACKbf95976061Tree-SHA512: 15ae75caf19f8d12a12d2647c52897904d27b265a7af6b4ae7b858592eeadb8f9da6c2394b6baebec90adc28742c053e3eb506119577dae7c1e722ebb3b7bcc0
This commit is contained in:
@@ -1513,7 +1513,7 @@ void Chainstate::InitCoinsDB(
|
||||
fs::path leveldb_name)
|
||||
{
|
||||
if (m_from_snapshot_blockhash) {
|
||||
leveldb_name += "_" + m_from_snapshot_blockhash->ToString();
|
||||
leveldb_name += node::SNAPSHOT_CHAINSTATE_SUFFIX;
|
||||
}
|
||||
|
||||
m_coins_views = std::make_unique<CoinsViews>(
|
||||
@@ -4804,28 +4804,15 @@ std::vector<Chainstate*> ChainstateManager::GetAll()
|
||||
return out;
|
||||
}
|
||||
|
||||
Chainstate& ChainstateManager::InitializeChainstate(
|
||||
CTxMemPool* mempool, const std::optional<uint256>& snapshot_blockhash)
|
||||
Chainstate& ChainstateManager::InitializeChainstate(CTxMemPool* mempool)
|
||||
{
|
||||
AssertLockHeld(::cs_main);
|
||||
bool is_snapshot = snapshot_blockhash.has_value();
|
||||
std::unique_ptr<Chainstate>& to_modify =
|
||||
is_snapshot ? m_snapshot_chainstate : m_ibd_chainstate;
|
||||
assert(!m_ibd_chainstate);
|
||||
assert(!m_active_chainstate);
|
||||
|
||||
if (to_modify) {
|
||||
throw std::logic_error("should not be overwriting a chainstate");
|
||||
}
|
||||
to_modify.reset(new Chainstate(mempool, m_blockman, *this, snapshot_blockhash));
|
||||
|
||||
// Snapshot chainstates and initial IBD chaintates always become active.
|
||||
if (is_snapshot || (!is_snapshot && !m_active_chainstate)) {
|
||||
LogPrintf("Switching active chainstate to %s\n", to_modify->ToString());
|
||||
m_active_chainstate = to_modify.get();
|
||||
} else {
|
||||
throw std::logic_error("unexpected chainstate activation");
|
||||
}
|
||||
|
||||
return *to_modify;
|
||||
m_ibd_chainstate = std::make_unique<Chainstate>(mempool, m_blockman, *this);
|
||||
m_active_chainstate = m_ibd_chainstate.get();
|
||||
return *m_active_chainstate;
|
||||
}
|
||||
|
||||
const AssumeutxoData* ExpectedAssumeutxo(
|
||||
@@ -4840,6 +4827,46 @@ const AssumeutxoData* ExpectedAssumeutxo(
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static bool DeleteCoinsDBFromDisk(const fs::path db_path, bool is_snapshot)
|
||||
EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
|
||||
{
|
||||
AssertLockHeld(::cs_main);
|
||||
|
||||
if (is_snapshot) {
|
||||
fs::path base_blockhash_path = db_path / node::SNAPSHOT_BLOCKHASH_FILENAME;
|
||||
|
||||
if (fs::exists(base_blockhash_path)) {
|
||||
bool removed = fs::remove(base_blockhash_path);
|
||||
if (!removed) {
|
||||
LogPrintf("[snapshot] failed to remove file %s\n",
|
||||
fs::PathToString(base_blockhash_path));
|
||||
}
|
||||
} else {
|
||||
LogPrintf("[snapshot] snapshot chainstate dir being removed lacks %s file\n",
|
||||
fs::PathToString(node::SNAPSHOT_BLOCKHASH_FILENAME));
|
||||
}
|
||||
}
|
||||
|
||||
std::string path_str = fs::PathToString(db_path);
|
||||
LogPrintf("Removing leveldb dir at %s\n", path_str);
|
||||
|
||||
// We have to destruct before this call leveldb::DB in order to release the db
|
||||
// lock, otherwise `DestroyDB` will fail. See `leveldb::~DBImpl()`.
|
||||
const bool destroyed = dbwrapper::DestroyDB(path_str, {}).ok();
|
||||
|
||||
if (!destroyed) {
|
||||
LogPrintf("error: leveldb DestroyDB call failed on %s\n", path_str);
|
||||
}
|
||||
|
||||
// Datadir should be removed from filesystem; otherwise initialization may detect
|
||||
// it on subsequent statups and get confused.
|
||||
//
|
||||
// If the base_blockhash_path removal above fails in the case of snapshot
|
||||
// chainstates, this will return false since leveldb won't remove a non-empty
|
||||
// directory.
|
||||
return destroyed && !fs::exists(db_path);
|
||||
}
|
||||
|
||||
bool ChainstateManager::ActivateSnapshot(
|
||||
AutoFile& coins_file,
|
||||
const SnapshotMetadata& metadata,
|
||||
@@ -4897,11 +4924,34 @@ bool ChainstateManager::ActivateSnapshot(
|
||||
static_cast<size_t>(current_coinstip_cache_size * SNAPSHOT_CACHE_PERC));
|
||||
}
|
||||
|
||||
const bool snapshot_ok = this->PopulateAndValidateSnapshot(
|
||||
bool snapshot_ok = this->PopulateAndValidateSnapshot(
|
||||
*snapshot_chainstate, coins_file, metadata);
|
||||
|
||||
// If not in-memory, persist the base blockhash for use during subsequent
|
||||
// initialization.
|
||||
if (!in_memory) {
|
||||
LOCK(::cs_main);
|
||||
if (!node::WriteSnapshotBaseBlockhash(*snapshot_chainstate)) {
|
||||
snapshot_ok = false;
|
||||
}
|
||||
}
|
||||
if (!snapshot_ok) {
|
||||
WITH_LOCK(::cs_main, this->MaybeRebalanceCaches());
|
||||
LOCK(::cs_main);
|
||||
this->MaybeRebalanceCaches();
|
||||
|
||||
// PopulateAndValidateSnapshot can return (in error) before the leveldb datadir
|
||||
// has been created, so only attempt removal if we got that far.
|
||||
if (auto snapshot_datadir = node::FindSnapshotChainstateDir()) {
|
||||
// We have to destruct leveldb::DB in order to release the db lock, otherwise
|
||||
// DestroyDB() (in DeleteCoinsDBFromDisk()) will fail. See `leveldb::~DBImpl()`.
|
||||
// Destructing the chainstate (and so resetting the coinsviews object) does this.
|
||||
snapshot_chainstate.reset();
|
||||
bool removed = DeleteCoinsDBFromDisk(*snapshot_datadir, /*is_snapshot=*/true);
|
||||
if (!removed) {
|
||||
AbortNode(strprintf("Failed to remove snapshot chainstate dir (%s). "
|
||||
"Manually remove it before restarting.\n", fs::PathToString(*snapshot_datadir)));
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -5175,6 +5225,13 @@ void ChainstateManager::MaybeRebalanceCaches()
|
||||
}
|
||||
}
|
||||
|
||||
void ChainstateManager::ResetChainstates()
|
||||
{
|
||||
m_ibd_chainstate.reset();
|
||||
m_snapshot_chainstate.reset();
|
||||
m_active_chainstate = nullptr;
|
||||
}
|
||||
|
||||
ChainstateManager::~ChainstateManager()
|
||||
{
|
||||
LOCK(::cs_main);
|
||||
@@ -5186,3 +5243,31 @@ ChainstateManager::~ChainstateManager()
|
||||
i.clear();
|
||||
}
|
||||
}
|
||||
|
||||
bool ChainstateManager::DetectSnapshotChainstate(CTxMemPool* mempool)
|
||||
{
|
||||
assert(!m_snapshot_chainstate);
|
||||
std::optional<fs::path> path = node::FindSnapshotChainstateDir();
|
||||
if (!path) {
|
||||
return false;
|
||||
}
|
||||
std::optional<uint256> base_blockhash = node::ReadSnapshotBaseBlockhash(*path);
|
||||
if (!base_blockhash) {
|
||||
return false;
|
||||
}
|
||||
LogPrintf("[snapshot] detected active snapshot chainstate (%s) - loading\n",
|
||||
fs::PathToString(*path));
|
||||
|
||||
this->ActivateExistingSnapshot(mempool, *base_blockhash);
|
||||
return true;
|
||||
}
|
||||
|
||||
Chainstate& ChainstateManager::ActivateExistingSnapshot(CTxMemPool* mempool, uint256 base_blockhash)
|
||||
{
|
||||
assert(!m_snapshot_chainstate);
|
||||
m_snapshot_chainstate =
|
||||
std::make_unique<Chainstate>(mempool, m_blockman, *this, base_blockhash);
|
||||
LogPrintf("[snapshot] switching active chainstate to %s\n", m_snapshot_chainstate->ToString());
|
||||
m_active_chainstate = m_snapshot_chainstate.get();
|
||||
return *m_snapshot_chainstate;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user