From 7071282a2d37c55ff0f1478e5c9bf2bc166a5ca2 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kwvg@users.noreply.github.com> Date: Mon, 16 Dec 2024 12:19:12 +0000 Subject: [PATCH] node/chainstate: Decouple from concept of NodeContext --- src/init.cpp | 16 +++++++++- src/node/chainstate.cpp | 71 ++++++++++++++++++++++++----------------- src/node/chainstate.h | 36 +++++++++++++++++++-- 3 files changed, 91 insertions(+), 32 deletions(-) diff --git a/src/init.cpp b/src/init.cpp index 823c07513a..88d9221451 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -1867,7 +1867,21 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info) const auto load_block_index_start_time{SteadyClock::now()}; auto rv = LoadChainstate(fReset, chainman, - node, + *node.govman, + *node.mn_metaman, + *node.mn_sync, + *node.sporkman, + node.mn_activeman, + node.chain_helper, + node.cpoolman, + node.dmnman, + node.evodb, + node.mnhf_manager, + llmq::chainLocksHandler, + llmq::quorumInstantSendManager, + llmq::quorumSnapshotManager, + node.llmq_ctx, + node.mempool.get(), fPruneMode, args.GetBoolArg("-addressindex", DEFAULT_ADDRESSINDEX), is_governance_enabled, diff --git a/src/node/chainstate.cpp b/src/node/chainstate.cpp index edb246f5db..49894e42eb 100644 --- a/src/node/chainstate.cpp +++ b/src/node/chainstate.cpp @@ -9,7 +9,6 @@ #include // for RPCNotifyBlockChange #include // for GetTime #include // for CleanupBlockRevFiles, fHavePruned, fReindex -#include // for NodeContext #include // for InitError, uiInterface, and CClientUIInterface member access #include // for ShutdownRequested #include // for a lot of things @@ -26,7 +25,21 @@ std::optional LoadChainstate(bool fReset, ChainstateManager& chainman, - NodeContext& node, + CGovernanceManager& govman, + CMasternodeMetaMan& mn_metaman, + CMasternodeSync& mn_sync, + CSporkManager& sporkman, + std::unique_ptr& mn_activeman, + std::unique_ptr& chain_helper, + std::unique_ptr& cpoolman, + std::unique_ptr& dmnman, + std::unique_ptr& evodb, + std::unique_ptr& mnhf_manager, + std::unique_ptr& clhandler, + std::unique_ptr& isman, + std::unique_ptr& qsnapman, + std::unique_ptr& llmq_ctx, + CTxMemPool* mempool, bool fPruneMode, bool is_addrindex_enabled, bool is_governance_enabled, @@ -50,13 +63,13 @@ std::optional LoadChainstate(bool fReset, LOCK(cs_main); int64_t nEvoDbCache{64 * 1024 * 1024}; // TODO - node.evodb.reset(); - node.evodb = std::make_unique(nEvoDbCache, false, fReset || fReindexChainState); + evodb.reset(); + evodb = std::make_unique(nEvoDbCache, false, fReset || fReindexChainState); - node.mnhf_manager.reset(); - node.mnhf_manager = std::make_unique(*node.evodb); + mnhf_manager.reset(); + mnhf_manager = std::make_unique(*evodb); - chainman.InitializeChainstate(Assert(node.mempool.get()), *node.evodb, node.chain_helper, llmq::chainLocksHandler, llmq::quorumInstantSendManager); + chainman.InitializeChainstate(Assert(mempool), *evodb, chain_helper, clhandler, isman); chainman.m_total_coinstip_cache = nCoinCacheUsage; chainman.m_total_coinsdb_cache = nCoinDBCache; @@ -67,29 +80,29 @@ std::optional LoadChainstate(bool fReset, pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, false, fReset)); // Same logic as above with pblocktree - node.dmnman.reset(); - node.dmnman = std::make_unique(chainman.ActiveChainstate(), *node.evodb); - node.mempool->ConnectManagers(node.dmnman.get()); + dmnman.reset(); + dmnman = std::make_unique(chainman.ActiveChainstate(), *evodb); + mempool->ConnectManagers(dmnman.get()); - node.cpoolman.reset(); - node.cpoolman = std::make_unique(*node.evodb); + cpoolman.reset(); + cpoolman = std::make_unique(*evodb); - llmq::quorumSnapshotManager.reset(); - llmq::quorumSnapshotManager.reset(new llmq::CQuorumSnapshotManager(*node.evodb)); + qsnapman.reset(); + qsnapman.reset(new llmq::CQuorumSnapshotManager(*evodb)); - if (node.llmq_ctx) { - node.llmq_ctx->Interrupt(); - node.llmq_ctx->Stop(); + if (llmq_ctx) { + llmq_ctx->Interrupt(); + llmq_ctx->Stop(); } - node.llmq_ctx.reset(); - node.llmq_ctx = std::make_unique(chainman, *node.dmnman, *node.evodb, *node.mn_metaman, *node.mnhf_manager, *node.sporkman, - *node.mempool, node.mn_activeman.get(), *node.mn_sync, /*unit_tests=*/false, /*wipe=*/fReset || fReindexChainState); + llmq_ctx.reset(); + llmq_ctx = std::make_unique(chainman, *dmnman, *evodb, mn_metaman, *mnhf_manager, sporkman, + *mempool, mn_activeman.get(), mn_sync, /*unit_tests=*/false, /*wipe=*/fReset || fReindexChainState); // Enable CMNHFManager::{Process, Undo}Block - node.mnhf_manager->ConnectManagers(node.chainman.get(), node.llmq_ctx->qman.get()); + mnhf_manager->ConnectManagers(&chainman, llmq_ctx->qman.get()); - node.chain_helper.reset(); - node.chain_helper = std::make_unique(*node.cpoolman, *node.dmnman, *node.mnhf_manager, *node.govman, *(node.llmq_ctx->quorum_block_processor), *node.chainman, - chainparams.GetConsensus(), *node.mn_sync, *node.sporkman, *(node.llmq_ctx->clhandler), *(node.llmq_ctx->qman)); + chain_helper.reset(); + chain_helper = std::make_unique(*cpoolman, *dmnman, *mnhf_manager, govman, *(llmq_ctx->quorum_block_processor), chainman, + chainparams.GetConsensus(), mn_sync, sporkman, *(llmq_ctx->clhandler), *(llmq_ctx->qman)); if (fReset) { pblocktree->WriteReindexing(true); @@ -198,7 +211,7 @@ std::optional LoadChainstate(bool fReset, // TODO: CEvoDB instance should probably be a part of CChainState // (for multiple chainstates to actually work in parallel) // and not a global - if (&chainman.ActiveChainstate() == chainstate && !node.evodb->CommitRootTransaction()) { + if (&chainman.ActiveChainstate() == chainstate && !evodb->CommitRootTransaction()) { return ChainstateLoadingError::ERROR_COMMITING_EVO_DB; } @@ -211,10 +224,10 @@ std::optional LoadChainstate(bool fReset, } } - if (!node.dmnman->MigrateDBIfNeeded() || !node.dmnman->MigrateDBIfNeeded2()) { + if (!dmnman->MigrateDBIfNeeded() || !dmnman->MigrateDBIfNeeded2()) { return ChainstateLoadingError::ERROR_UPGRADING_EVO_DB; } - if (!node.mnhf_manager->ForceSignalDBUpdate()) { + if (!mnhf_manager->ForceSignalDBUpdate()) { return ChainstateLoadingError::ERROR_UPGRADING_SIGNALS_DB; } } catch (const std::exception& e) { @@ -246,7 +259,7 @@ std::optional LoadChainstate(bool fReset, if (!CVerifyDB().VerifyDB( *chainstate, chainparams, chainstate->CoinsDB(), - *node.evodb, + *evodb, check_level, check_blocks)) { return ChainstateLoadingError::ERROR_CORRUPTED_BLOCK_DB; @@ -267,7 +280,7 @@ std::optional LoadChainstate(bool fReset, // TODO: CEvoDB instance should probably be a part of CChainState // (for multiple chainstates to actually work in parallel) // and not a global - if (&chainman.ActiveChainstate() == chainstate && !node.evodb->IsEmpty()) { + if (&chainman.ActiveChainstate() == chainstate && !evodb->IsEmpty()) { // EvoDB processed some blocks earlier but we have no blocks anymore, something is wrong return ChainstateLoadingError::ERROR_EVO_DB_SANITY_FAILED; } diff --git a/src/node/chainstate.h b/src/node/chainstate.h index d2bd675c95..0dc31b01c4 100644 --- a/src/node/chainstate.h +++ b/src/node/chainstate.h @@ -6,11 +6,29 @@ #define BITCOIN_NODE_CHAINSTATE_H #include // for int64_t +#include // for std::unique_ptr #include // for std::optional +class CActiveMasternodeManager; class CChainParams; +class CChainstateHelper; +class CCreditPoolManager; +class CDeterministicMNManager; +class CEvoDB; +class CGovernanceManager; class ChainstateManager; -struct NodeContext; +class CMasternodeMetaMan; +class CMasternodeSync; +class CMNHFManager; +class CSporkManager; +class CTxMemPool; +struct LLMQContext; + +namespace llmq { +class CChainLocksHandler; +class CInstantSendManager; +class CQuorumSnapshotManager; +} enum class ChainstateLoadingError { ERROR_LOADING_BLOCK_DB, @@ -63,7 +81,21 @@ enum class ChainstateLoadingError { */ std::optional LoadChainstate(bool fReset, ChainstateManager& chainman, - NodeContext& node, + CGovernanceManager& govman, + CMasternodeMetaMan& mn_metaman, + CMasternodeSync& mn_sync, + CSporkManager& sporkman, + std::unique_ptr& mn_activeman, + std::unique_ptr& chain_helper, + std::unique_ptr& cpoolman, + std::unique_ptr& dmnman, + std::unique_ptr& evodb, + std::unique_ptr& mnhf_manager, + std::unique_ptr& clhandler, + std::unique_ptr& isman, + std::unique_ptr& qsnapman, + std::unique_ptr& llmq_ctx, + CTxMemPool* mempool, bool fPruneMode, bool is_addrindex_enabled, bool is_governance_enabled,