Skip to content

Commit

Permalink
node/chainstate: extract Dash post-InitializeChainstate logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Dec 16, 2024
1 parent c06e074 commit 459f339
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 33 deletions.
11 changes: 2 additions & 9 deletions src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,8 @@ void PrepareShutdown(NodeContext& node)
chainstate->ResetCoinsViews();
}
}
node.chain_helper.reset();
if (node.mnhf_manager) {
node.mnhf_manager->DisconnectManagers();
}
node.llmq_ctx.reset();
llmq::quorumSnapshotManager.reset();
node.mempool->DisconnectManagers();
node.dmnman.reset();
node.cpoolman.reset();
DashChainstateSetupClose(node.chain_helper, node.cpoolman, node.dmnman, node.mnhf_manager,
llmq::quorumSnapshotManager, node.llmq_ctx, Assert(node.mempool.get()));
node.mnhf_manager.reset();
node.evodb.reset();
}
Expand Down
91 changes: 67 additions & 24 deletions src/node/chainstate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,30 +77,9 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
pblocktree.reset();
pblocktree.reset(new CBlockTreeDB(nBlockTreeDBCache, block_tree_db_in_memory, fReset));

// Same logic as above with pblocktree
dmnman.reset();
dmnman = std::make_unique<CDeterministicMNManager>(chainman.ActiveChainstate(), *evodb);
mempool->ConnectManagers(dmnman.get());

cpoolman.reset();
cpoolman = std::make_unique<CCreditPoolManager>(*evodb);

qsnapman.reset();
qsnapman.reset(new llmq::CQuorumSnapshotManager(*evodb));

if (llmq_ctx) {
llmq_ctx->Interrupt();
llmq_ctx->Stop();
}
llmq_ctx.reset();
llmq_ctx = std::make_unique<LLMQContext>(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
mnhf_manager->ConnectManagers(&chainman, llmq_ctx->qman.get());

chain_helper.reset();
chain_helper = std::make_unique<CChainstateHelper>(*cpoolman, *dmnman, *mnhf_manager, govman, *(llmq_ctx->quorum_block_processor), chainman,
consensus_params, mn_sync, sporkman, *(llmq_ctx->clhandler), *(llmq_ctx->qman));
DashChainstateSetup(chainman, govman, mn_metaman, mn_sync, sporkman, mn_activeman, chain_helper, cpoolman,
dmnman, evodb, mnhf_manager, qsnapman, llmq_ctx, mempool, fReset, fReindexChainState,
consensus_params);

if (fReset) {
pblocktree->WriteReindexing(true);
Expand Down Expand Up @@ -229,6 +208,70 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
return std::nullopt;
}

void DashChainstateSetup(ChainstateManager& chainman,
CGovernanceManager& govman,
CMasternodeMetaMan& mn_metaman,
CMasternodeSync& mn_sync,
CSporkManager& sporkman,
std::unique_ptr<CActiveMasternodeManager>& mn_activeman,
std::unique_ptr<CChainstateHelper>& chain_helper,
std::unique_ptr<CCreditPoolManager>& cpoolman,
std::unique_ptr<CDeterministicMNManager>& dmnman,
std::unique_ptr<CEvoDB>& evodb,
std::unique_ptr<CMNHFManager>& mnhf_manager,
std::unique_ptr<llmq::CQuorumSnapshotManager>& qsnapman,
std::unique_ptr<LLMQContext>& llmq_ctx,
CTxMemPool* mempool,
bool fReset,
bool fReindexChainState,
const Consensus::Params& consensus_params)
{
// Same logic as pblocktree
dmnman.reset();
dmnman = std::make_unique<CDeterministicMNManager>(chainman.ActiveChainstate(), *evodb);
mempool->ConnectManagers(dmnman.get());

cpoolman.reset();
cpoolman = std::make_unique<CCreditPoolManager>(*evodb);

qsnapman.reset();
qsnapman.reset(new llmq::CQuorumSnapshotManager(*evodb));

if (llmq_ctx) {
llmq_ctx->Interrupt();
llmq_ctx->Stop();
}
llmq_ctx.reset();
llmq_ctx = std::make_unique<LLMQContext>(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
mnhf_manager->ConnectManagers(&chainman, llmq_ctx->qman.get());

chain_helper.reset();
chain_helper = std::make_unique<CChainstateHelper>(*cpoolman, *dmnman, *mnhf_manager, govman, *(llmq_ctx->quorum_block_processor), chainman,
consensus_params, mn_sync, sporkman, *(llmq_ctx->clhandler), *(llmq_ctx->qman));
}

void DashChainstateSetupClose(std::unique_ptr<CChainstateHelper>& chain_helper,
std::unique_ptr<CCreditPoolManager>& cpoolman,
std::unique_ptr<CDeterministicMNManager>& dmnman,
std::unique_ptr<CMNHFManager>& mnhf_manager,
std::unique_ptr<llmq::CQuorumSnapshotManager>& qsnapman,
std::unique_ptr<LLMQContext>& llmq_ctx,
CTxMemPool* mempool)

{
chain_helper.reset();
if (mnhf_manager) {
mnhf_manager->DisconnectManagers();
}
llmq_ctx.reset();
qsnapman.reset();
cpoolman.reset();
mempool->DisconnectManagers();
dmnman.reset();
}

std::optional<ChainstateLoadVerifyError> VerifyLoadedChainstate(ChainstateManager& chainman,
CEvoDB& evodb,
bool fReset,
Expand Down
27 changes: 27 additions & 0 deletions src/node/chainstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,33 @@ std::optional<ChainstateLoadingError> LoadChainstate(bool fReset,
std::function<bool()> shutdown_requested = nullptr,
std::function<void()> coins_error_cb = nullptr);

/** Initialize Dash-specific components during chainstate initialization */
void DashChainstateSetup(ChainstateManager& chainman,
CGovernanceManager& govman,
CMasternodeMetaMan& mn_metaman,
CMasternodeSync& mn_sync,
CSporkManager& sporkman,
std::unique_ptr<CActiveMasternodeManager>& mn_activeman,
std::unique_ptr<CChainstateHelper>& chain_helper,
std::unique_ptr<CCreditPoolManager>& cpoolman,
std::unique_ptr<CDeterministicMNManager>& dmnman,
std::unique_ptr<CEvoDB>& evodb,
std::unique_ptr<CMNHFManager>& mnhf_manager,
std::unique_ptr<llmq::CQuorumSnapshotManager>& qsnapman,
std::unique_ptr<LLMQContext>& llmq_ctx,
CTxMemPool* mempool,
bool fReset,
bool fReindexChainState,
const Consensus::Params& consensus_params);

void DashChainstateSetupClose(std::unique_ptr<CChainstateHelper>& chain_helper,
std::unique_ptr<CCreditPoolManager>& cpoolman,
std::unique_ptr<CDeterministicMNManager>& dmnman,
std::unique_ptr<CMNHFManager>& mnhf_manager,
std::unique_ptr<llmq::CQuorumSnapshotManager>& qsnapman,
std::unique_ptr<LLMQContext>& llmq_ctx,
CTxMemPool* mempool);

enum class ChainstateLoadVerifyError {
ERROR_BLOCK_FROM_FUTURE,
ERROR_CORRUPTED_BLOCK_DB,
Expand Down

0 comments on commit 459f339

Please sign in to comment.