Skip to content

Commit 9f6bb53

Browse files
committed
validation: add chainman ref to CChainState
Add an upwards reference to chainstate instances to the owning ChainstateManager. This is necessary because there are a number of `this_chainstate == chainman.ActiveChainstate()` checks that will happen (as a result of assumeutxo) in functions that otherwise don't have an easily-accessible reference to the chainstate's ChainManager.
1 parent d86e662 commit 9f6bb53

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

src/test/validation_flush_tests.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
#include <boost/test/unit_test.hpp>
1111

12-
BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, BasicTestingSetup)
12+
BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, ChainTestingSetup)
1313

1414
//! Test utilities for detecting when we need to flush the coins cache based
1515
//! on estimated memory usage.
@@ -20,7 +20,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
2020
{
2121
CTxMemPool mempool;
2222
BlockManager blockman{};
23-
CChainState chainstate{&mempool, blockman};
23+
CChainState chainstate{&mempool, blockman, *Assert(m_node.chainman)};
2424
chainstate.InitCoinsDB(/*cache_size_bytes*/ 1 << 10, /*in_memory*/ true, /*should_wipe*/ false);
2525
WITH_LOCK(::cs_main, chainstate.InitCoinsCache(1 << 10));
2626

src/validation.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1209,10 +1209,15 @@ void CoinsViews::InitCache()
12091209
m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
12101210
}
12111211

1212-
CChainState::CChainState(CTxMemPool* mempool, BlockManager& blockman, std::optional<uint256> from_snapshot_blockhash)
1212+
CChainState::CChainState(
1213+
CTxMemPool* mempool,
1214+
BlockManager& blockman,
1215+
ChainstateManager& chainman,
1216+
std::optional<uint256> from_snapshot_blockhash)
12131217
: m_mempool(mempool),
12141218
m_params(::Params()),
12151219
m_blockman(blockman),
1220+
m_chainman(chainman),
12161221
m_from_snapshot_blockhash(from_snapshot_blockhash) {}
12171222

12181223
void CChainState::InitCoinsDB(
@@ -4699,7 +4704,7 @@ CChainState& ChainstateManager::InitializeChainstate(
46994704
if (to_modify) {
47004705
throw std::logic_error("should not be overwriting a chainstate");
47014706
}
4702-
to_modify.reset(new CChainState(mempool, m_blockman, snapshot_blockhash));
4707+
to_modify.reset(new CChainState(mempool, m_blockman, *this, snapshot_blockhash));
47034708

47044709
// Snapshot chainstates and initial IBD chaintates always become active.
47054710
if (is_snapshot || (!is_snapshot && !m_active_chainstate)) {
@@ -4768,8 +4773,9 @@ bool ChainstateManager::ActivateSnapshot(
47684773
static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
47694774
}
47704775

4771-
auto snapshot_chainstate = WITH_LOCK(::cs_main, return std::make_unique<CChainState>(
4772-
/* mempool */ nullptr, m_blockman, base_blockhash));
4776+
auto snapshot_chainstate = WITH_LOCK(::cs_main,
4777+
return std::make_unique<CChainState>(
4778+
/* mempool */ nullptr, m_blockman, *this, base_blockhash));
47734779

47744780
{
47754781
LOCK(::cs_main);

src/validation.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,9 +601,15 @@ class CChainState
601601
//! CChainState instances.
602602
BlockManager& m_blockman;
603603

604+
//! The chainstate manager that owns this chainstate. The reference is
605+
//! necessary so that this instance can check whether it is the active
606+
//! chainstate within deeply nested method calls.
607+
ChainstateManager& m_chainman;
608+
604609
explicit CChainState(
605610
CTxMemPool* mempool,
606611
BlockManager& blockman,
612+
ChainstateManager& chainman,
607613
std::optional<uint256> from_snapshot_blockhash = std::nullopt);
608614

609615
/**

0 commit comments

Comments
 (0)