Skip to content

Commit 1598a15

Browse files
committed
refactor: Deduplicate Chainstate activation code
Move duplicate code from ChainstateManager::ActivateSnapshot and ChainstateManager::ActivateExistingSnapshot methods to a new ChainstateManager::AddChainstate method. The "AddChainstate" method name doesn't mention snapshots even though it is only used to add snapshot chainstates now, because it becomes more generalized in a later commit in this PR ("refactor: Add ChainstateManager::m_chainstates member")
1 parent 9fe927b commit 1598a15

File tree

3 files changed

+16
-32
lines changed

3 files changed

+16
-32
lines changed

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
6969
// Create a snapshot-based chainstate.
7070
//
7171
const uint256 snapshot_blockhash = active_tip->GetBlockHash();
72-
Chainstate& c2 = WITH_LOCK(::cs_main, return manager.ActivateExistingSnapshot(snapshot_blockhash));
72+
Chainstate& c2{WITH_LOCK(::cs_main, return manager.AddChainstate(std::make_unique<Chainstate>(nullptr, manager.m_blockman, manager, snapshot_blockhash)))};
7373
chainstates.push_back(&c2);
7474
c2.InitCoinsDB(
7575
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
@@ -135,7 +135,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
135135
// Create a snapshot-based chainstate.
136136
//
137137
CBlockIndex* snapshot_base{WITH_LOCK(manager.GetMutex(), return manager.ActiveChain()[manager.ActiveChain().Height() / 2])};
138-
Chainstate& c2 = WITH_LOCK(cs_main, return manager.ActivateExistingSnapshot(*snapshot_base->phashBlock));
138+
Chainstate& c2{WITH_LOCK(::cs_main, return manager.AddChainstate(std::make_unique<Chainstate>(nullptr, manager.m_blockman, manager, *snapshot_base->phashBlock)))};
139139
chainstates.push_back(&c2);
140140
c2.InitCoinsDB(
141141
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
@@ -489,8 +489,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
489489
}
490490

491491
// Note: cs2's tip is not set when ActivateExistingSnapshot is called.
492-
Chainstate& cs2 = WITH_LOCK(::cs_main,
493-
return chainman.ActivateExistingSnapshot(*assumed_base->phashBlock));
492+
Chainstate& cs2{WITH_LOCK(::cs_main, return chainman.AddChainstate(std::make_unique<Chainstate>(nullptr, chainman.m_blockman, chainman, *assumed_base->phashBlock)))};
494493

495494
// Set tip of the fully validated chain to be the validated tip
496495
cs1.m_chain.SetTip(*validated_tip);

src/validation.cpp

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5796,28 +5796,14 @@ util::Result<CBlockIndex*> ChainstateManager::ActivateSnapshot(
57965796
}
57975797
}
57985798

5799-
assert(!m_snapshot_chainstate);
5800-
m_snapshot_chainstate.swap(snapshot_chainstate);
5801-
const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
5799+
Chainstate& chainstate{AddChainstate(std::move(snapshot_chainstate))};
5800+
const bool chaintip_loaded{chainstate.LoadChainTip()};
58025801
assert(chaintip_loaded);
5803-
5804-
// Set snapshot block as the target block for the historical chainstate.
5805-
assert(m_ibd_chainstate.get());
5806-
assert(!m_ibd_chainstate->m_target_blockhash);
5807-
m_ibd_chainstate->SetTargetBlockHash(base_blockhash);
5808-
5809-
// Transfer possession of the mempool to the snapshot chainstate.
5810-
// Mempool is empty at this point because we're still in IBD.
5811-
Assert(m_active_chainstate->m_mempool->size() == 0);
5812-
Assert(!m_snapshot_chainstate->m_mempool);
5813-
m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
5814-
m_active_chainstate->m_mempool = nullptr;
5815-
m_active_chainstate = m_snapshot_chainstate.get();
5816-
m_blockman.m_snapshot_height = this->GetSnapshotBaseHeight();
5802+
m_blockman.m_snapshot_height = Assert(chainstate.SnapshotBase())->nHeight;
58175803

58185804
LogInfo("[snapshot] successfully activated snapshot %s", base_blockhash.ToString());
58195805
LogInfo("[snapshot] (%.2f MB)",
5820-
m_snapshot_chainstate->CoinsTip().DynamicMemoryUsage() / (1000 * 1000));
5806+
chainstate.CoinsTip().DynamicMemoryUsage() / (1000 * 1000));
58215807

58225808
this->MaybeRebalanceCaches();
58235809
return snapshot_start_block;
@@ -6288,21 +6274,21 @@ bool ChainstateManager::DetectSnapshotChainstate()
62886274
LogInfo("[snapshot] detected active snapshot chainstate (%s) - loading",
62896275
fs::PathToString(*path));
62906276

6291-
this->ActivateExistingSnapshot(*base_blockhash);
6277+
auto snapshot_chainstate{std::make_unique<Chainstate>(nullptr, m_blockman, *this, base_blockhash)};
6278+
LogInfo("[snapshot] switching active chainstate to %s", snapshot_chainstate->ToString());
6279+
this->AddChainstate(std::move(snapshot_chainstate));
62926280
return true;
62936281
}
62946282

6295-
Chainstate& ChainstateManager::ActivateExistingSnapshot(uint256 base_blockhash)
6283+
Chainstate& ChainstateManager::AddChainstate(std::unique_ptr<Chainstate> chainstate)
62966284
{
62976285
assert(!m_snapshot_chainstate);
6298-
m_snapshot_chainstate =
6299-
std::make_unique<Chainstate>(nullptr, m_blockman, *this, base_blockhash);
6300-
LogInfo("[snapshot] switching active chainstate to %s", m_snapshot_chainstate->ToString());
6286+
m_snapshot_chainstate = std::move(chainstate);
63016287

63026288
// Set target block for historical chainstate to snapshot block.
63036289
assert(m_ibd_chainstate.get());
63046290
assert(!m_ibd_chainstate->m_target_blockhash);
6305-
m_ibd_chainstate->SetTargetBlockHash(base_blockhash);
6291+
m_ibd_chainstate->SetTargetBlockHash(*Assert(m_snapshot_chainstate->m_from_snapshot_blockhash));
63066292

63076293
// Transfer possession of the mempool to the chainstate.
63086294
// Mempool is empty at this point because we're still in IBD.

src/validation.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1315,16 +1315,15 @@ class ChainstateManager
13151315
//! snapshot that is in the process of being validated.
13161316
bool DetectSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
13171317

1318+
//! Add new chainstate.
1319+
Chainstate& AddChainstate(std::unique_ptr<Chainstate> chainstate) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1320+
13181321
void ResetChainstates() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
13191322

13201323
//! Remove the snapshot-based chainstate and all on-disk artifacts.
13211324
//! Used when reindex{-chainstate} is called during snapshot use.
13221325
[[nodiscard]] bool DeleteSnapshotChainstate() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
13231326

1324-
//! Switch the active chainstate to one based on a UTXO snapshot that was loaded
1325-
//! previously.
1326-
Chainstate& ActivateExistingSnapshot(uint256 base_blockhash) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1327-
13281327
//! If we have validated a snapshot chain during this runtime, copy its
13291328
//! chainstate directory over to the main `chainstate` location, completing
13301329
//! validation of the snapshot.

0 commit comments

Comments
 (0)