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.
This commit is contained in:
James O'Beirne 2021-06-12 10:52:40 -04:00
parent d86e6625e8
commit 9f6bb53935
No known key found for this signature in database
GPG key ID: 7A935DADB2C44F05
3 changed files with 18 additions and 6 deletions

View file

@ -9,7 +9,7 @@
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, BasicTestingSetup) BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, ChainTestingSetup)
//! Test utilities for detecting when we need to flush the coins cache based //! Test utilities for detecting when we need to flush the coins cache based
//! on estimated memory usage. //! on estimated memory usage.
@ -20,7 +20,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
{ {
CTxMemPool mempool; CTxMemPool mempool;
BlockManager blockman{}; BlockManager blockman{};
CChainState chainstate{&mempool, blockman}; CChainState chainstate{&mempool, blockman, *Assert(m_node.chainman)};
chainstate.InitCoinsDB(/*cache_size_bytes*/ 1 << 10, /*in_memory*/ true, /*should_wipe*/ false); chainstate.InitCoinsDB(/*cache_size_bytes*/ 1 << 10, /*in_memory*/ true, /*should_wipe*/ false);
WITH_LOCK(::cs_main, chainstate.InitCoinsCache(1 << 10)); WITH_LOCK(::cs_main, chainstate.InitCoinsCache(1 << 10));

View file

@ -1209,10 +1209,15 @@ void CoinsViews::InitCache()
m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview); m_cacheview = std::make_unique<CCoinsViewCache>(&m_catcherview);
} }
CChainState::CChainState(CTxMemPool* mempool, BlockManager& blockman, std::optional<uint256> from_snapshot_blockhash) CChainState::CChainState(
CTxMemPool* mempool,
BlockManager& blockman,
ChainstateManager& chainman,
std::optional<uint256> from_snapshot_blockhash)
: m_mempool(mempool), : m_mempool(mempool),
m_params(::Params()), m_params(::Params()),
m_blockman(blockman), m_blockman(blockman),
m_chainman(chainman),
m_from_snapshot_blockhash(from_snapshot_blockhash) {} m_from_snapshot_blockhash(from_snapshot_blockhash) {}
void CChainState::InitCoinsDB( void CChainState::InitCoinsDB(
@ -4699,7 +4704,7 @@ CChainState& ChainstateManager::InitializeChainstate(
if (to_modify) { if (to_modify) {
throw std::logic_error("should not be overwriting a chainstate"); throw std::logic_error("should not be overwriting a chainstate");
} }
to_modify.reset(new CChainState(mempool, m_blockman, snapshot_blockhash)); to_modify.reset(new CChainState(mempool, m_blockman, *this, snapshot_blockhash));
// Snapshot chainstates and initial IBD chaintates always become active. // Snapshot chainstates and initial IBD chaintates always become active.
if (is_snapshot || (!is_snapshot && !m_active_chainstate)) { if (is_snapshot || (!is_snapshot && !m_active_chainstate)) {
@ -4768,8 +4773,9 @@ bool ChainstateManager::ActivateSnapshot(
static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC)); static_cast<size_t>(current_coinsdb_cache_size * IBD_CACHE_PERC));
} }
auto snapshot_chainstate = WITH_LOCK(::cs_main, return std::make_unique<CChainState>( auto snapshot_chainstate = WITH_LOCK(::cs_main,
/* mempool */ nullptr, m_blockman, base_blockhash)); return std::make_unique<CChainState>(
/* mempool */ nullptr, m_blockman, *this, base_blockhash));
{ {
LOCK(::cs_main); LOCK(::cs_main);

View file

@ -601,9 +601,15 @@ public:
//! CChainState instances. //! CChainState instances.
BlockManager& m_blockman; BlockManager& m_blockman;
//! The chainstate manager that owns this chainstate. The reference is
//! necessary so that this instance can check whether it is the active
//! chainstate within deeply nested method calls.
ChainstateManager& m_chainman;
explicit CChainState( explicit CChainState(
CTxMemPool* mempool, CTxMemPool* mempool,
BlockManager& blockman, BlockManager& blockman,
ChainstateManager& chainman,
std::optional<uint256> from_snapshot_blockhash = std::nullopt); std::optional<uint256> from_snapshot_blockhash = std::nullopt);
/** /**