Skip to content

Commit 9511fb3

Browse files
committed
validation: assumeutxo: swap m_mempool on snapshot activation
Otherwise we will not receive transactions during background sync until restart.
1 parent 7fcd215 commit 9511fb3

File tree

3 files changed

+19
-13
lines changed

3 files changed

+19
-13
lines changed

src/test/validation_chainstatemanager_tests.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ BOOST_FIXTURE_TEST_SUITE(validation_chainstatemanager_tests, TestingSetup)
3838
BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
3939
{
4040
ChainstateManager& manager = *m_node.chainman;
41-
CTxMemPool& mempool = *m_node.mempool;
42-
4341
std::vector<Chainstate*> chainstates;
4442

4543
BOOST_CHECK(!manager.SnapshotBlockhash().has_value());
@@ -69,8 +67,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
6967
// Create a snapshot-based chainstate.
7068
//
7169
const uint256 snapshot_blockhash = active_tip->GetBlockHash();
72-
Chainstate& c2 = WITH_LOCK(::cs_main, return manager.ActivateExistingSnapshot(
73-
&mempool, snapshot_blockhash));
70+
Chainstate& c2 = WITH_LOCK(::cs_main, return manager.ActivateExistingSnapshot(snapshot_blockhash));
7471
chainstates.push_back(&c2);
7572
c2.InitCoinsDB(
7673
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
@@ -113,7 +110,6 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager, TestChain100Setup)
113110
BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
114111
{
115112
ChainstateManager& manager = *m_node.chainman;
116-
CTxMemPool& mempool = *m_node.mempool;
117113

118114
size_t max_cache = 10000;
119115
manager.m_total_coinsdb_cache = max_cache;
@@ -137,7 +133,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_rebalance_caches, TestChain100Setup)
137133
// Create a snapshot-based chainstate.
138134
//
139135
CBlockIndex* snapshot_base{WITH_LOCK(manager.GetMutex(), return manager.ActiveChain()[manager.ActiveChain().Height() / 2])};
140-
Chainstate& c2 = WITH_LOCK(cs_main, return manager.ActivateExistingSnapshot(&mempool, *snapshot_base->phashBlock));
136+
Chainstate& c2 = WITH_LOCK(cs_main, return manager.ActivateExistingSnapshot(*snapshot_base->phashBlock));
141137
chainstates.push_back(&c2);
142138
c2.InitCoinsDB(
143139
/*cache_size_bytes=*/1 << 23, /*in_memory=*/true, /*should_wipe=*/false);
@@ -423,7 +419,6 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_activate_snapshot, SnapshotTestSetup)
423419
BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
424420
{
425421
ChainstateManager& chainman = *Assert(m_node.chainman);
426-
CTxMemPool& mempool = *m_node.mempool;
427422
Chainstate& cs1 = chainman.ActiveChainstate();
428423

429424
int num_indexes{0};
@@ -493,7 +488,7 @@ BOOST_FIXTURE_TEST_CASE(chainstatemanager_loadblockindex, TestChain100Setup)
493488

494489
// Note: cs2's tip is not set when ActivateExistingSnapshot is called.
495490
Chainstate& cs2 = WITH_LOCK(::cs_main,
496-
return chainman.ActivateExistingSnapshot(&mempool, *assumed_base->phashBlock));
491+
return chainman.ActivateExistingSnapshot(*assumed_base->phashBlock));
497492

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

src/validation.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5268,6 +5268,12 @@ bool ChainstateManager::ActivateSnapshot(
52685268
const bool chaintip_loaded = m_snapshot_chainstate->LoadChainTip();
52695269
assert(chaintip_loaded);
52705270

5271+
// Transfer possession of the mempool to the snapshot chianstate.
5272+
// Mempool is empty at this point because we're still in IBD.
5273+
Assert(m_active_chainstate->m_mempool->size() == 0);
5274+
Assert(!m_snapshot_chainstate->m_mempool);
5275+
m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
5276+
m_active_chainstate->m_mempool = nullptr;
52715277
m_active_chainstate = m_snapshot_chainstate.get();
52725278
m_blockman.m_snapshot_height = this->GetSnapshotBaseHeight();
52735279

@@ -5747,16 +5753,22 @@ bool ChainstateManager::DetectSnapshotChainstate(CTxMemPool* mempool)
57475753
LogPrintf("[snapshot] detected active snapshot chainstate (%s) - loading\n",
57485754
fs::PathToString(*path));
57495755

5750-
this->ActivateExistingSnapshot(mempool, *base_blockhash);
5756+
this->ActivateExistingSnapshot(*base_blockhash);
57515757
return true;
57525758
}
57535759

5754-
Chainstate& ChainstateManager::ActivateExistingSnapshot(CTxMemPool* mempool, uint256 base_blockhash)
5760+
Chainstate& ChainstateManager::ActivateExistingSnapshot(uint256 base_blockhash)
57555761
{
57565762
assert(!m_snapshot_chainstate);
57575763
m_snapshot_chainstate =
5758-
std::make_unique<Chainstate>(mempool, m_blockman, *this, base_blockhash);
5764+
std::make_unique<Chainstate>(nullptr, m_blockman, *this, base_blockhash);
57595765
LogPrintf("[snapshot] switching active chainstate to %s\n", m_snapshot_chainstate->ToString());
5766+
5767+
// Mempool is empty at this point because we're still in IBD.
5768+
Assert(m_active_chainstate->m_mempool->size() == 0);
5769+
Assert(!m_snapshot_chainstate->m_mempool);
5770+
m_snapshot_chainstate->m_mempool = m_active_chainstate->m_mempool;
5771+
m_active_chainstate->m_mempool = nullptr;
57605772
m_active_chainstate = m_snapshot_chainstate.get();
57615773
return *m_snapshot_chainstate;
57625774
}

src/validation.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,8 +1213,7 @@ class ChainstateManager
12131213

12141214
//! Switch the active chainstate to one based on a UTXO snapshot that was loaded
12151215
//! previously.
1216-
Chainstate& ActivateExistingSnapshot(CTxMemPool* mempool, uint256 base_blockhash)
1217-
EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
1216+
Chainstate& ActivateExistingSnapshot(uint256 base_blockhash) EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
12181217

12191218
//! If we have validated a snapshot chain during this runtime, copy its
12201219
//! chainstate directory over to the main `chainstate` location, completing

0 commit comments

Comments
 (0)