evo: add CMNHFManager::(Dis)connectManagers to remove global use

This commit is contained in:
Kittywhiskers Van Gogh 2024-05-28 15:20:13 +00:00
parent cbb6828ec7
commit 805537e695
No known key found for this signature in database
GPG Key ID: 30CD0C065E5C4AAD
4 changed files with 45 additions and 6 deletions

View File

@ -192,9 +192,11 @@ static bool extractSignals(const llmq::CQuorumManager& qman, const CBlock& block
std::optional<CMNHFManager::Signals> CMNHFManager::ProcessBlock(const CBlock& block, const CBlockIndex* const pindex, bool fJustCheck, BlockValidationState& state) std::optional<CMNHFManager::Signals> CMNHFManager::ProcessBlock(const CBlock& block, const CBlockIndex* const pindex, bool fJustCheck, BlockValidationState& state)
{ {
assert(m_qman);
try { try {
std::vector<uint8_t> new_signals; std::vector<uint8_t> new_signals;
if (!extractSignals(*Assert(llmq::quorumManager), block, pindex, new_signals, state)) { if (!extractSignals(*m_qman, block, pindex, new_signals, state)) {
// state is set inside extractSignals // state is set inside extractSignals
return std::nullopt; return std::nullopt;
} }
@ -244,9 +246,11 @@ std::optional<CMNHFManager::Signals> CMNHFManager::ProcessBlock(const CBlock& bl
bool CMNHFManager::UndoBlock(const CBlock& block, const CBlockIndex* const pindex) bool CMNHFManager::UndoBlock(const CBlock& block, const CBlockIndex* const pindex)
{ {
assert(m_qman);
std::vector<uint8_t> excluded_signals; std::vector<uint8_t> excluded_signals;
BlockValidationState state; BlockValidationState state;
if (!extractSignals(*Assert(llmq::quorumManager), block, pindex, excluded_signals, state)) { if (!extractSignals(*m_qman, block, pindex, excluded_signals, state)) {
LogPrintf("CMNHFManager::%s: failed to extract signals\n", __func__); LogPrintf("CMNHFManager::%s: failed to extract signals\n", __func__);
return false; return false;
} }
@ -350,6 +354,13 @@ void CMNHFManager::AddSignal(const CBlockIndex* const pindex, int bit)
AddToCache(signals, pindex); AddToCache(signals, pindex);
} }
void CMNHFManager::ConnectManagers(gsl::not_null<llmq::CQuorumManager*> qman)
{
// Do not allow double-initialization
assert(m_qman == nullptr);
m_qman = qman;
}
std::string MNHFTx::ToString() const std::string MNHFTx::ToString() const
{ {
return strprintf("MNHFTx(versionBit=%d, quorumHash=%s, sig=%s)", return strprintf("MNHFTx(versionBit=%d, quorumHash=%s, sig=%s)",

View File

@ -6,6 +6,7 @@
#define BITCOIN_EVO_MNHFTX_H #define BITCOIN_EVO_MNHFTX_H
#include <bls/bls.h> #include <bls/bls.h>
#include <gsl/pointers.h>
#include <primitives/transaction.h> #include <primitives/transaction.h>
#include <sync.h> #include <sync.h>
#include <threadsafety.h> #include <threadsafety.h>
@ -99,6 +100,7 @@ class CMNHFManager : public AbstractEHFManager
{ {
private: private:
CEvoDB& m_evoDb; CEvoDB& m_evoDb;
llmq::CQuorumManager* m_qman{nullptr};
static constexpr size_t MNHFCacheSize = 1000; static constexpr size_t MNHFCacheSize = 1000;
Mutex cs_cache; Mutex cs_cache;
@ -115,17 +117,22 @@ public:
/** /**
* Every new block should be processed when Tip() is updated by calling of CMNHFManager::ProcessBlock. * Every new block should be processed when Tip() is updated by calling of CMNHFManager::ProcessBlock.
* This function actually does only validate EHF transaction for this block and update internal caches/evodb state * This function actually does only validate EHF transaction for this block and update internal caches/evodb state
*
* @pre Caller must ensure that LLMQContext has been initialized and the llmq::CQuorumManager pointer has been
* set by calling ConnectManagers() for this CMNHFManager instance
*/ */
std::optional<Signals> ProcessBlock(const CBlock& block, const CBlockIndex* const pindex, bool fJustCheck, BlockValidationState& state); std::optional<Signals> ProcessBlock(const CBlock& block, const CBlockIndex* const pindex, bool fJustCheck, BlockValidationState& state);
/** /**
* Every undo block should be processed when Tip() is updated by calling of CMNHFManager::UndoBlock * Every undo block should be processed when Tip() is updated by calling of CMNHFManager::UndoBlock
* This function actually does nothing at the moment, because status of ancestor block is already know. * This function actually does nothing at the moment, because status of ancestor block is already known.
* Although it should be still called to do some sanity checks * Although it should be still called to do some sanity checks
*
* @pre Caller must ensure that LLMQContext has been initialized and the llmq::CQuorumManager pointer has been
* set by calling ConnectManagers() for this CMNHFManager instance
*/ */
bool UndoBlock(const CBlock& block, const CBlockIndex* const pindex); bool UndoBlock(const CBlock& block, const CBlockIndex* const pindex);
// Implements interface // Implements interface
Signals GetSignalsStage(const CBlockIndex* const pindexPrev) override; Signals GetSignalsStage(const CBlockIndex* const pindexPrev) override;
@ -133,6 +140,22 @@ public:
* Helper that used in Unit Test to forcely setup EHF signal for specific block * Helper that used in Unit Test to forcely setup EHF signal for specific block
*/ */
void AddSignal(const CBlockIndex* const pindex, int bit) EXCLUSIVE_LOCKS_REQUIRED(!cs_cache); void AddSignal(const CBlockIndex* const pindex, int bit) EXCLUSIVE_LOCKS_REQUIRED(!cs_cache);
/**
* Set llmq::CQuorumManager pointer.
*
* Separated from constructor to allow LLMQContext to use CMNHFManager in read-only capacity.
* Required to mutate state.
*/
void ConnectManagers(gsl::not_null<llmq::CQuorumManager*> qman);
/**
* Reset llmq::CQuorumManager pointer.
*
* @pre Must be called before LLMQContext (containing llmq::CQuorumManager) is destroyed.
*/
void DisconnectManagers() { m_qman = nullptr; };
private: private:
void AddToCache(const Signals& signals, const CBlockIndex* const pindex); void AddToCache(const Signals& signals, const CBlockIndex* const pindex);

View File

@ -339,9 +339,10 @@ void PrepareShutdown(NodeContext& node)
} }
pblocktree.reset(); pblocktree.reset();
node.chain_helper.reset(); node.chain_helper.reset();
if (node.llmq_ctx) { if (node.mnhf_manager) {
node.llmq_ctx.reset(); node.mnhf_manager->DisconnectManagers();
} }
node.llmq_ctx.reset();
llmq::quorumSnapshotManager.reset(); llmq::quorumSnapshotManager.reset();
node.mempool->DisconnectManagers(); node.mempool->DisconnectManagers();
node.dmnman.reset(); node.dmnman.reset();
@ -1973,6 +1974,8 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
node.llmq_ctx.reset(); node.llmq_ctx.reset();
node.llmq_ctx = std::make_unique<LLMQContext>(chainman.ActiveChainstate(), *node.connman, *node.dmnman, *node.evodb, *node.mn_metaman, *node.mnhf_manager, *node.sporkman, node.llmq_ctx = std::make_unique<LLMQContext>(chainman.ActiveChainstate(), *node.connman, *node.dmnman, *node.evodb, *node.mn_metaman, *node.mnhf_manager, *node.sporkman,
*node.mempool, node.mn_activeman.get(), *node.mn_sync, node.peerman, /* unit_tests = */ false, /* wipe = */ fReset || fReindexChainState); *node.mempool, node.mn_activeman.get(), *node.mn_sync, node.peerman, /* unit_tests = */ false, /* wipe = */ fReset || fReindexChainState);
// Enable CMNHFManager::{Process, Undo}Block
node.mnhf_manager->ConnectManagers(node.llmq_ctx->qman);
// Have to start it early to let VerifyDB check ChainLock signatures in coinbase // Have to start it early to let VerifyDB check ChainLock signatures in coinbase
node.llmq_ctx->Start(); node.llmq_ctx->Start();

View File

@ -119,6 +119,7 @@ void DashTestSetup(NodeContext& node, const CChainParams& chainparams)
#endif // ENABLE_WALLET #endif // ENABLE_WALLET
node.llmq_ctx = std::make_unique<LLMQContext>(chainstate, *node.connman, *node.dmnman, *node.evodb, *node.mn_metaman, *node.mnhf_manager, *node.sporkman, *node.mempool, node.llmq_ctx = std::make_unique<LLMQContext>(chainstate, *node.connman, *node.dmnman, *node.evodb, *node.mn_metaman, *node.mnhf_manager, *node.sporkman, *node.mempool,
/* mn_activeman = */ nullptr, *node.mn_sync, node.peerman, /* unit_tests = */ true, /* wipe = */ false); /* mn_activeman = */ nullptr, *node.mn_sync, node.peerman, /* unit_tests = */ true, /* wipe = */ false);
Assert(node.mnhf_manager)->ConnectManagers(node.llmq_ctx->qman);
node.chain_helper = std::make_unique<CChainstateHelper>(*node.cpoolman, *node.dmnman, *node.mnhf_manager, *node.govman, *(node.llmq_ctx->quorum_block_processor), node.chain_helper = std::make_unique<CChainstateHelper>(*node.cpoolman, *node.dmnman, *node.mnhf_manager, *node.govman, *(node.llmq_ctx->quorum_block_processor),
chainparams.GetConsensus(), *node.mn_sync, *node.sporkman, *(node.llmq_ctx->clhandler), *(node.llmq_ctx->qman)); chainparams.GetConsensus(), *node.mn_sync, *node.sporkman, *(node.llmq_ctx->clhandler), *(node.llmq_ctx->qman));
} }
@ -128,6 +129,7 @@ void DashTestSetupClose(NodeContext& node)
node.chain_helper.reset(); node.chain_helper.reset();
node.llmq_ctx->Interrupt(); node.llmq_ctx->Interrupt();
node.llmq_ctx->Stop(); node.llmq_ctx->Stop();
Assert(node.mnhf_manager)->DisconnectManagers();
node.llmq_ctx.reset(); node.llmq_ctx.reset();
#ifdef ENABLE_WALLET #ifdef ENABLE_WALLET
node.coinjoin_loader.reset(); node.coinjoin_loader.reset();