0
0
Fork 0
mirror of https://github.com/bitcoin/bitcoin.git synced 2025-02-10 10:52:31 -05:00

Pass mempool reference to chainstate constructor

This commit is contained in:
MarcoFalke 2020-07-28 07:40:49 +02:00
parent 862fde88be
commit fa0572d0f3
No known key found for this signature in database
GPG key ID: CE2B75697E69A548
7 changed files with 55 additions and 37 deletions

View file

@ -1550,7 +1550,7 @@ bool AppInitMain(const util::Ref& context, NodeContext& node, interfaces::BlockA
const int64_t load_block_index_start_time = GetTimeMillis(); const int64_t load_block_index_start_time = GetTimeMillis();
try { try {
LOCK(cs_main); LOCK(cs_main);
chainman.InitializeChainstate(); chainman.InitializeChainstate(*Assert(node.mempool));
chainman.m_total_coinstip_cache = nCoinCacheUsage; chainman.m_total_coinstip_cache = nCoinCacheUsage;
chainman.m_total_coinsdb_cache = nCoinDBCache; chainman.m_total_coinsdb_cache = nCoinDBCache;

View file

@ -141,8 +141,11 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
pblocktree.reset(new CBlockTreeDB(1 << 20, true)); pblocktree.reset(new CBlockTreeDB(1 << 20, true));
m_node.mempool = &::mempool;
m_node.mempool->setSanityCheck(1.0);
m_node.chainman = &::g_chainman; m_node.chainman = &::g_chainman;
m_node.chainman->InitializeChainstate(); m_node.chainman->InitializeChainstate(*m_node.mempool);
::ChainstateActive().InitCoinsDB( ::ChainstateActive().InitCoinsDB(
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false); /* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
assert(!::ChainstateActive().CanFlushToDisk()); assert(!::ChainstateActive().CanFlushToDisk());
@ -164,8 +167,6 @@ TestingSetup::TestingSetup(const std::string& chainName, const std::vector<const
} }
g_parallel_script_checks = true; g_parallel_script_checks = true;
m_node.mempool = &::mempool;
m_node.mempool->setSanityCheck(1.0);
m_node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME); m_node.banman = MakeUnique<BanMan>(GetDataDir() / "banlist.dat", nullptr, DEFAULT_MISBEHAVING_BANTIME);
m_node.connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests. m_node.connman = MakeUnique<CConnman>(0x1337, 0x1337); // Deterministic randomness for tests.
m_node.peer_logic = MakeUnique<PeerLogicValidation>(*m_node.connman, m_node.banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool); m_node.peer_logic = MakeUnique<PeerLogicValidation>(*m_node.connman, m_node.banman.get(), *m_node.scheduler, *m_node.chainman, *m_node.mempool);

View file

@ -20,6 +20,7 @@ BOOST_FIXTURE_TEST_SUITE(validation_chainstate_tests, TestingSetup)
BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches) BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
{ {
ChainstateManager manager; ChainstateManager manager;
CTxMemPool mempool;
//! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view. //! Create and add a Coin with DynamicMemoryUsage of 80 bytes to the given view.
auto add_coin = [](CCoinsViewCache& coins_view) -> COutPoint { auto add_coin = [](CCoinsViewCache& coins_view) -> COutPoint {
@ -34,7 +35,7 @@ BOOST_AUTO_TEST_CASE(validation_chainstate_resize_caches)
return outp; return outp;
}; };
CChainState& c1 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate()); CChainState& c1 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate(mempool));
c1.InitCoinsDB( c1.InitCoinsDB(
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false); /* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
WITH_LOCK(::cs_main, c1.InitCoinsCache(1 << 23)); WITH_LOCK(::cs_main, c1.InitCoinsCache(1 << 23));

View file

@ -23,12 +23,13 @@ BOOST_FIXTURE_TEST_SUITE(validation_chainstatemanager_tests, TestingSetup)
BOOST_AUTO_TEST_CASE(chainstatemanager) BOOST_AUTO_TEST_CASE(chainstatemanager)
{ {
ChainstateManager manager; ChainstateManager manager;
CTxMemPool mempool;
std::vector<CChainState*> chainstates; std::vector<CChainState*> chainstates;
const CChainParams& chainparams = Params(); const CChainParams& chainparams = Params();
// Create a legacy (IBD) chainstate. // Create a legacy (IBD) chainstate.
// //
CChainState& c1 = *WITH_LOCK(::cs_main, return &manager.InitializeChainstate()); CChainState& c1 = *WITH_LOCK(::cs_main, return &manager.InitializeChainstate(mempool));
chainstates.push_back(&c1); chainstates.push_back(&c1);
c1.InitCoinsDB( c1.InitCoinsDB(
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false); /* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@ -54,7 +55,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
// Create a snapshot-based chainstate. // Create a snapshot-based chainstate.
// //
CChainState& c2 = *WITH_LOCK(::cs_main, return &manager.InitializeChainstate(GetRandHash())); CChainState& c2 = *WITH_LOCK(::cs_main, return &manager.InitializeChainstate(mempool, GetRandHash()));
chainstates.push_back(&c2); chainstates.push_back(&c2);
c2.InitCoinsDB( c2.InitCoinsDB(
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false); /* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@ -104,6 +105,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager)
BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches) BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
{ {
ChainstateManager manager; ChainstateManager manager;
CTxMemPool mempool;
size_t max_cache = 10000; size_t max_cache = 10000;
manager.m_total_coinsdb_cache = max_cache; manager.m_total_coinsdb_cache = max_cache;
manager.m_total_coinstip_cache = max_cache; manager.m_total_coinstip_cache = max_cache;
@ -112,7 +114,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
// Create a legacy (IBD) chainstate. // Create a legacy (IBD) chainstate.
// //
CChainState& c1 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate()); CChainState& c1 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate(mempool));
chainstates.push_back(&c1); chainstates.push_back(&c1);
c1.InitCoinsDB( c1.InitCoinsDB(
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false); /* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@ -129,7 +131,7 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
// Create a snapshot-based chainstate. // Create a snapshot-based chainstate.
// //
CChainState& c2 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate(GetRandHash())); CChainState& c2 = *WITH_LOCK(cs_main, return &manager.InitializeChainstate(mempool, GetRandHash()));
chainstates.push_back(&c2); chainstates.push_back(&c2);
c2.InitCoinsDB( c2.InitCoinsDB(
/* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false); /* cache_size_bytes */ 1 << 23, /* in_memory */ true, /* should_wipe */ false);
@ -147,7 +149,6 @@ BOOST_AUTO_TEST_CASE(chainstatemanager_rebalance_caches)
BOOST_CHECK_CLOSE(c1.m_coinsdb_cache_size_bytes, max_cache * 0.05, 1); BOOST_CHECK_CLOSE(c1.m_coinsdb_cache_size_bytes, max_cache * 0.05, 1);
BOOST_CHECK_CLOSE(c2.m_coinstip_cache_size_bytes, max_cache * 0.95, 1); BOOST_CHECK_CLOSE(c2.m_coinstip_cache_size_bytes, max_cache * 0.95, 1);
BOOST_CHECK_CLOSE(c2.m_coinsdb_cache_size_bytes, max_cache * 0.95, 1); BOOST_CHECK_CLOSE(c2.m_coinsdb_cache_size_bytes, max_cache * 0.95, 1);
} }
BOOST_AUTO_TEST_SUITE_END() BOOST_AUTO_TEST_SUITE_END()

View file

@ -18,8 +18,9 @@ BOOST_FIXTURE_TEST_SUITE(validation_flush_tests, BasicTestingSetup)
//! //!
BOOST_AUTO_TEST_CASE(getcoinscachesizestate) BOOST_AUTO_TEST_CASE(getcoinscachesizestate)
{ {
CTxMemPool mempool;
BlockManager blockman{}; BlockManager blockman{};
CChainState chainstate{blockman}; CChainState chainstate{mempool, blockman};
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));
CTxMemPool tx_pool{}; CTxMemPool tx_pool{};

View file

@ -370,9 +370,10 @@ static bool IsCurrentForFeeEstimation() EXCLUSIVE_LOCKS_REQUIRED(cs_main)
* and instead just erase from the mempool as needed. * and instead just erase from the mempool as needed.
*/ */
static void UpdateMempoolForReorg(DisconnectedBlockTransactions& disconnectpool, bool fAddToMempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs) static void UpdateMempoolForReorg(CTxMemPool& mempool, DisconnectedBlockTransactions& disconnectpool, bool fAddToMempool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, mempool.cs)
{ {
AssertLockHeld(cs_main); AssertLockHeld(cs_main);
AssertLockHeld(mempool.cs);
std::vector<uint256> vHashUpdate; std::vector<uint256> vHashUpdate;
// disconnectpool's insertion_order index sorts the entries from // disconnectpool's insertion_order index sorts the entries from
// oldest to newest, but the oldest entry will be the last tx from the // oldest to newest, but the oldest entry will be the last tx from the
@ -1254,8 +1255,9 @@ void CoinsViews::InitCache()
m_cacheview = MakeUnique<CCoinsViewCache>(&m_catcherview); m_cacheview = MakeUnique<CCoinsViewCache>(&m_catcherview);
} }
CChainState::CChainState(BlockManager& blockman, uint256 from_snapshot_blockhash) CChainState::CChainState(CTxMemPool& mempool, BlockManager& blockman, uint256 from_snapshot_blockhash)
: m_blockman(blockman), : m_blockman(blockman),
m_mempool(mempool),
m_from_snapshot_blockhash(from_snapshot_blockhash) {} m_from_snapshot_blockhash(from_snapshot_blockhash) {}
void CChainState::InitCoinsDB( void CChainState::InitCoinsDB(
@ -2280,7 +2282,7 @@ bool CChainState::FlushStateToDisk(
{ {
bool fFlushForPrune = false; bool fFlushForPrune = false;
bool fDoFullFlush = false; bool fDoFullFlush = false;
CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(&::mempool); CoinsCacheSizeState cache_state = GetCoinsCacheSizeState(&m_mempool);
LOCK(cs_LastBlockFile); LOCK(cs_LastBlockFile);
if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) { if (fPruneMode && (fCheckForPruning || nManualPruneHeight > 0) && !fReindex) {
if (nManualPruneHeight > 0) { if (nManualPruneHeight > 0) {
@ -2426,7 +2428,7 @@ static void AppendWarning(bilingual_str& res, const bilingual_str& warn)
} }
/** Check warning conditions and do some notifications on new chain tip set. */ /** Check warning conditions and do some notifications on new chain tip set. */
void static UpdateTip(const CBlockIndex* pindexNew, const CChainParams& chainParams) static void UpdateTip(CTxMemPool& mempool, const CBlockIndex* pindexNew, const CChainParams& chainParams)
EXCLUSIVE_LOCKS_REQUIRED(::cs_main) EXCLUSIVE_LOCKS_REQUIRED(::cs_main)
{ {
// New best block // New best block
@ -2472,7 +2474,6 @@ void static UpdateTip(const CBlockIndex* pindexNew, const CChainParams& chainPar
FormatISO8601DateTime(pindexNew->GetBlockTime()), FormatISO8601DateTime(pindexNew->GetBlockTime()),
GuessVerificationProgress(chainParams.TxData(), pindexNew), ::ChainstateActive().CoinsTip().DynamicMemoryUsage() * (1.0 / (1<<20)), ::ChainstateActive().CoinsTip().GetCacheSize(), GuessVerificationProgress(chainParams.TxData(), pindexNew), ::ChainstateActive().CoinsTip().DynamicMemoryUsage() * (1.0 / (1<<20)), ::ChainstateActive().CoinsTip().GetCacheSize(),
!warning_messages.empty() ? strprintf(" warning='%s'", warning_messages.original) : ""); !warning_messages.empty() ? strprintf(" warning='%s'", warning_messages.original) : "");
} }
/** Disconnect m_chain's tip. /** Disconnect m_chain's tip.
@ -2487,6 +2488,9 @@ void static UpdateTip(const CBlockIndex* pindexNew, const CChainParams& chainPar
*/ */
bool CChainState::DisconnectTip(BlockValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions* disconnectpool) bool CChainState::DisconnectTip(BlockValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions* disconnectpool)
{ {
AssertLockHeld(cs_main);
AssertLockHeld(m_mempool.cs);
CBlockIndex *pindexDelete = m_chain.Tip(); CBlockIndex *pindexDelete = m_chain.Tip();
assert(pindexDelete); assert(pindexDelete);
// Read block from disk. // Read block from disk.
@ -2517,14 +2521,14 @@ bool CChainState::DisconnectTip(BlockValidationState& state, const CChainParams&
while (disconnectpool->DynamicMemoryUsage() > MAX_DISCONNECTED_TX_POOL_SIZE * 1000) { while (disconnectpool->DynamicMemoryUsage() > MAX_DISCONNECTED_TX_POOL_SIZE * 1000) {
// Drop the earliest entry, and remove its children from the mempool. // Drop the earliest entry, and remove its children from the mempool.
auto it = disconnectpool->queuedTx.get<insertion_order>().begin(); auto it = disconnectpool->queuedTx.get<insertion_order>().begin();
mempool.removeRecursive(**it, MemPoolRemovalReason::REORG); m_mempool.removeRecursive(**it, MemPoolRemovalReason::REORG);
disconnectpool->removeEntry(it); disconnectpool->removeEntry(it);
} }
} }
m_chain.SetTip(pindexDelete->pprev); m_chain.SetTip(pindexDelete->pprev);
UpdateTip(pindexDelete->pprev, chainparams); UpdateTip(m_mempool, pindexDelete->pprev, chainparams);
// Let wallets know transactions went from 1-confirmed to // Let wallets know transactions went from 1-confirmed to
// 0-confirmed or conflicted: // 0-confirmed or conflicted:
GetMainSignals().BlockDisconnected(pblock, pindexDelete); GetMainSignals().BlockDisconnected(pblock, pindexDelete);
@ -2585,6 +2589,9 @@ public:
*/ */
bool CChainState::ConnectTip(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions &disconnectpool) bool CChainState::ConnectTip(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions &disconnectpool)
{ {
AssertLockHeld(cs_main);
AssertLockHeld(m_mempool.cs);
assert(pindexNew->pprev == m_chain.Tip()); assert(pindexNew->pprev == m_chain.Tip());
// Read block from disk. // Read block from disk.
int64_t nTime1 = GetTimeMicros(); int64_t nTime1 = GetTimeMicros();
@ -2625,11 +2632,11 @@ bool CChainState::ConnectTip(BlockValidationState& state, const CChainParams& ch
int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4; int64_t nTime5 = GetTimeMicros(); nTimeChainState += nTime5 - nTime4;
LogPrint(BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, nTimeChainState * MILLI / nBlocksTotal); LogPrint(BCLog::BENCH, " - Writing chainstate: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime5 - nTime4) * MILLI, nTimeChainState * MICRO, nTimeChainState * MILLI / nBlocksTotal);
// Remove conflicting transactions from the mempool.; // Remove conflicting transactions from the mempool.;
mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight); m_mempool.removeForBlock(blockConnecting.vtx, pindexNew->nHeight);
disconnectpool.removeForBlock(blockConnecting.vtx); disconnectpool.removeForBlock(blockConnecting.vtx);
// Update m_chain & related variables. // Update m_chain & related variables.
m_chain.SetTip(pindexNew); m_chain.SetTip(pindexNew);
UpdateTip(pindexNew, chainparams); UpdateTip(m_mempool, pindexNew, chainparams);
int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1; int64_t nTime6 = GetTimeMicros(); nTimePostConnect += nTime6 - nTime5; nTimeTotal += nTime6 - nTime1;
LogPrint(BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO, nTimePostConnect * MILLI / nBlocksTotal); LogPrint(BCLog::BENCH, " - Connect postprocess: %.2fms [%.2fs (%.2fms/blk)]\n", (nTime6 - nTime5) * MILLI, nTimePostConnect * MICRO, nTimePostConnect * MILLI / nBlocksTotal);
@ -2719,6 +2726,7 @@ void CChainState::PruneBlockIndexCandidates() {
bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace)
{ {
AssertLockHeld(cs_main); AssertLockHeld(cs_main);
AssertLockHeld(m_mempool.cs);
const CBlockIndex *pindexOldTip = m_chain.Tip(); const CBlockIndex *pindexOldTip = m_chain.Tip();
const CBlockIndex *pindexFork = m_chain.FindFork(pindexMostWork); const CBlockIndex *pindexFork = m_chain.FindFork(pindexMostWork);
@ -2730,7 +2738,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
if (!DisconnectTip(state, chainparams, &disconnectpool)) { if (!DisconnectTip(state, chainparams, &disconnectpool)) {
// This is likely a fatal error, but keep the mempool consistent, // This is likely a fatal error, but keep the mempool consistent,
// just in case. Only remove from the mempool in this case. // just in case. Only remove from the mempool in this case.
UpdateMempoolForReorg(disconnectpool, false); UpdateMempoolForReorg(m_mempool, disconnectpool, false);
// If we're unable to disconnect a block during normal operation, // If we're unable to disconnect a block during normal operation,
// then that is a failure of our local system -- we should abort // then that is a failure of our local system -- we should abort
@ -2774,7 +2782,7 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
// A system error occurred (disk space, database error, ...). // A system error occurred (disk space, database error, ...).
// Make the mempool consistent with the current tip, just in case // Make the mempool consistent with the current tip, just in case
// any observers try to use it before shutdown. // any observers try to use it before shutdown.
UpdateMempoolForReorg(disconnectpool, false); UpdateMempoolForReorg(m_mempool, disconnectpool, false);
return false; return false;
} }
} else { } else {
@ -2791,9 +2799,9 @@ bool CChainState::ActivateBestChainStep(BlockValidationState& state, const CChai
if (fBlocksDisconnected) { if (fBlocksDisconnected) {
// If any blocks were disconnected, disconnectpool may be non empty. Add // If any blocks were disconnected, disconnectpool may be non empty. Add
// any disconnected transactions back to the mempool. // any disconnected transactions back to the mempool.
UpdateMempoolForReorg(disconnectpool, true); UpdateMempoolForReorg(m_mempool, disconnectpool, true);
} }
mempool.check(&CoinsTip()); m_mempool.check(&CoinsTip());
// Callbacks/notifications for a new best chain. // Callbacks/notifications for a new best chain.
if (fInvalidFound) if (fInvalidFound)
@ -2867,7 +2875,8 @@ bool CChainState::ActivateBestChain(BlockValidationState &state, const CChainPar
LimitValidationInterfaceQueue(); LimitValidationInterfaceQueue();
{ {
LOCK2(cs_main, ::mempool.cs); // Lock transaction pool for at least as long as it takes for connectTrace to be consumed LOCK(cs_main);
LOCK(m_mempool.cs); // Lock transaction pool for at least as long as it takes for connectTrace to be consumed
CBlockIndex* starting_tip = m_chain.Tip(); CBlockIndex* starting_tip = m_chain.Tip();
bool blocks_connected = false; bool blocks_connected = false;
do { do {
@ -3020,7 +3029,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, const CChainParam
LimitValidationInterfaceQueue(); LimitValidationInterfaceQueue();
LOCK(cs_main); LOCK(cs_main);
LOCK(::mempool.cs); // Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is called after DisconnectTip without unlocking in between LOCK(m_mempool.cs); // Lock for as long as disconnectpool is in scope to make sure UpdateMempoolForReorg is called after DisconnectTip without unlocking in between
if (!m_chain.Contains(pindex)) break; if (!m_chain.Contains(pindex)) break;
pindex_was_in_chain = true; pindex_was_in_chain = true;
CBlockIndex *invalid_walk_tip = m_chain.Tip(); CBlockIndex *invalid_walk_tip = m_chain.Tip();
@ -3034,7 +3043,7 @@ bool CChainState::InvalidateBlock(BlockValidationState& state, const CChainParam
// transactions back to the mempool if disconnecting was successful, // transactions back to the mempool if disconnecting was successful,
// and we're not doing a very deep invalidation (in which case // and we're not doing a very deep invalidation (in which case
// keeping the mempool up to date is probably futile anyway). // keeping the mempool up to date is probably futile anyway).
UpdateMempoolForReorg(disconnectpool, /* fAddToMempool = */ (++disconnected <= 10) && ret); UpdateMempoolForReorg(m_mempool, disconnectpool, /* fAddToMempool = */ (++disconnected <= 10) && ret);
if (!ret) return false; if (!ret) return false;
assert(invalid_walk_tip->pprev == m_chain.Tip()); assert(invalid_walk_tip->pprev == m_chain.Tip());
@ -4517,7 +4526,8 @@ bool CChainState::RewindBlockIndex(const CChainParams& params)
// Loop until the tip is below nHeight, or we reach a pruned block. // Loop until the tip is below nHeight, or we reach a pruned block.
while (!ShutdownRequested()) { while (!ShutdownRequested()) {
{ {
LOCK2(cs_main, ::mempool.cs); LOCK(cs_main);
LOCK(m_mempool.cs);
// Make sure nothing changed from under us (this won't happen because RewindBlockIndex runs before importing/network are active) // Make sure nothing changed from under us (this won't happen because RewindBlockIndex runs before importing/network are active)
assert(tip == m_chain.Tip()); assert(tip == m_chain.Tip());
if (tip == nullptr || tip->nHeight < nHeight) break; if (tip == nullptr || tip->nHeight < nHeight) break;
@ -5246,7 +5256,7 @@ std::vector<CChainState*> ChainstateManager::GetAll()
return out; return out;
} }
CChainState& ChainstateManager::InitializeChainstate(const uint256& snapshot_blockhash) CChainState& ChainstateManager::InitializeChainstate(CTxMemPool& mempool, const uint256& snapshot_blockhash)
{ {
bool is_snapshot = !snapshot_blockhash.IsNull(); bool is_snapshot = !snapshot_blockhash.IsNull();
std::unique_ptr<CChainState>& to_modify = std::unique_ptr<CChainState>& to_modify =
@ -5255,8 +5265,7 @@ CChainState& ChainstateManager::InitializeChainstate(const uint256& snapshot_blo
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(m_blockman, 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)) {

View file

@ -511,11 +511,14 @@ private:
//! easily as opposed to referencing a global. //! easily as opposed to referencing a global.
BlockManager& m_blockman; BlockManager& m_blockman;
//! mempool that is kept in sync with the chain
CTxMemPool& m_mempool;
//! Manages the UTXO set, which is a reflection of the contents of `m_chain`. //! Manages the UTXO set, which is a reflection of the contents of `m_chain`.
std::unique_ptr<CoinsViews> m_coins_views; std::unique_ptr<CoinsViews> m_coins_views;
public: public:
explicit CChainState(BlockManager& blockman, uint256 from_snapshot_blockhash = uint256()); explicit CChainState(CTxMemPool& mempool, BlockManager& blockman, uint256 from_snapshot_blockhash = uint256());
/** /**
* Initialize the CoinsViews UTXO set database management data structures. The in-memory * Initialize the CoinsViews UTXO set database management data structures. The in-memory
@ -642,7 +645,7 @@ public:
CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main); CCoinsViewCache& view, const CChainParams& chainparams, bool fJustCheck = false) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
// Apply the effects of a block disconnection on the UTXO set. // Apply the effects of a block disconnection on the UTXO set.
bool DisconnectTip(BlockValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions* disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs); bool DisconnectTip(BlockValidationState& state, const CChainParams& chainparams, DisconnectedBlockTransactions* disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool.cs);
// Manual block validity manipulation: // Manual block validity manipulation:
bool PreciousBlock(BlockValidationState& state, const CChainParams& params, CBlockIndex* pindex) LOCKS_EXCLUDED(cs_main); bool PreciousBlock(BlockValidationState& state, const CChainParams& params, CBlockIndex* pindex) LOCKS_EXCLUDED(cs_main);
@ -685,8 +688,8 @@ public:
std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(::cs_main); std::string ToString() EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
private: private:
bool ActivateBestChainStep(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs); bool ActivateBestChainStep(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexMostWork, const std::shared_ptr<const CBlock>& pblock, bool& fInvalidFound, ConnectTrace& connectTrace) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool.cs);
bool ConnectTip(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, ::mempool.cs); bool ConnectTip(BlockValidationState& state, const CChainParams& chainparams, CBlockIndex* pindexNew, const std::shared_ptr<const CBlock>& pblock, ConnectTrace& connectTrace, DisconnectedBlockTransactions& disconnectpool) EXCLUSIVE_LOCKS_REQUIRED(cs_main, m_mempool.cs);
void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main); void InvalidBlockFound(CBlockIndex *pindex, const BlockValidationState &state) EXCLUSIVE_LOCKS_REQUIRED(cs_main);
CBlockIndex* FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main); CBlockIndex* FindMostWorkChain() EXCLUSIVE_LOCKS_REQUIRED(cs_main);
@ -818,9 +821,11 @@ public:
//! Instantiate a new chainstate and assign it based upon whether it is //! Instantiate a new chainstate and assign it based upon whether it is
//! from a snapshot. //! from a snapshot.
//! //!
//! @param[in] mempool The mempool to pass to the chainstate
// constructor
//! @param[in] snapshot_blockhash If given, signify that this chainstate //! @param[in] snapshot_blockhash If given, signify that this chainstate
//! is based on a snapshot. //! is based on a snapshot.
CChainState& InitializeChainstate(const uint256& snapshot_blockhash = uint256()) CChainState& InitializeChainstate(CTxMemPool& mempool, const uint256& snapshot_blockhash = uint256())
EXCLUSIVE_LOCKS_REQUIRED(::cs_main); EXCLUSIVE_LOCKS_REQUIRED(::cs_main);
//! Get all chainstates currently being used. //! Get all chainstates currently being used.