Skip to content

Commit 805537e

Browse files
committed
evo: add CMNHFManager::(Dis)connectManagers to remove global use
1 parent cbb6828 commit 805537e

File tree

4 files changed

+45
-6
lines changed

4 files changed

+45
-6
lines changed

src/evo/mnhftx.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ static bool extractSignals(const llmq::CQuorumManager& qman, const CBlock& block
192192

193193
std::optional<CMNHFManager::Signals> CMNHFManager::ProcessBlock(const CBlock& block, const CBlockIndex* const pindex, bool fJustCheck, BlockValidationState& state)
194194
{
195+
assert(m_qman);
196+
195197
try {
196198
std::vector<uint8_t> new_signals;
197-
if (!extractSignals(*Assert(llmq::quorumManager), block, pindex, new_signals, state)) {
199+
if (!extractSignals(*m_qman, block, pindex, new_signals, state)) {
198200
// state is set inside extractSignals
199201
return std::nullopt;
200202
}
@@ -244,9 +246,11 @@ std::optional<CMNHFManager::Signals> CMNHFManager::ProcessBlock(const CBlock& bl
244246

245247
bool CMNHFManager::UndoBlock(const CBlock& block, const CBlockIndex* const pindex)
246248
{
249+
assert(m_qman);
250+
247251
std::vector<uint8_t> excluded_signals;
248252
BlockValidationState state;
249-
if (!extractSignals(*Assert(llmq::quorumManager), block, pindex, excluded_signals, state)) {
253+
if (!extractSignals(*m_qman, block, pindex, excluded_signals, state)) {
250254
LogPrintf("CMNHFManager::%s: failed to extract signals\n", __func__);
251255
return false;
252256
}
@@ -350,6 +354,13 @@ void CMNHFManager::AddSignal(const CBlockIndex* const pindex, int bit)
350354
AddToCache(signals, pindex);
351355
}
352356

357+
void CMNHFManager::ConnectManagers(gsl::not_null<llmq::CQuorumManager*> qman)
358+
{
359+
// Do not allow double-initialization
360+
assert(m_qman == nullptr);
361+
m_qman = qman;
362+
}
363+
353364
std::string MNHFTx::ToString() const
354365
{
355366
return strprintf("MNHFTx(versionBit=%d, quorumHash=%s, sig=%s)",

src/evo/mnhftx.h

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#define BITCOIN_EVO_MNHFTX_H
77

88
#include <bls/bls.h>
9+
#include <gsl/pointers.h>
910
#include <primitives/transaction.h>
1011
#include <sync.h>
1112
#include <threadsafety.h>
@@ -99,6 +100,7 @@ class CMNHFManager : public AbstractEHFManager
99100
{
100101
private:
101102
CEvoDB& m_evoDb;
103+
llmq::CQuorumManager* m_qman{nullptr};
102104

103105
static constexpr size_t MNHFCacheSize = 1000;
104106
Mutex cs_cache;
@@ -115,24 +117,45 @@ class CMNHFManager : public AbstractEHFManager
115117
/**
116118
* Every new block should be processed when Tip() is updated by calling of CMNHFManager::ProcessBlock.
117119
* This function actually does only validate EHF transaction for this block and update internal caches/evodb state
120+
*
121+
* @pre Caller must ensure that LLMQContext has been initialized and the llmq::CQuorumManager pointer has been
122+
* set by calling ConnectManagers() for this CMNHFManager instance
118123
*/
119124
std::optional<Signals> ProcessBlock(const CBlock& block, const CBlockIndex* const pindex, bool fJustCheck, BlockValidationState& state);
120125

121126
/**
122127
* Every undo block should be processed when Tip() is updated by calling of CMNHFManager::UndoBlock
123-
* This function actually does nothing at the moment, because status of ancestor block is already know.
128+
* This function actually does nothing at the moment, because status of ancestor block is already known.
124129
* Although it should be still called to do some sanity checks
130+
*
131+
* @pre Caller must ensure that LLMQContext has been initialized and the llmq::CQuorumManager pointer has been
132+
* set by calling ConnectManagers() for this CMNHFManager instance
125133
*/
126134
bool UndoBlock(const CBlock& block, const CBlockIndex* const pindex);
127135

128-
129136
// Implements interface
130137
Signals GetSignalsStage(const CBlockIndex* const pindexPrev) override;
131138

132139
/**
133140
* Helper that used in Unit Test to forcely setup EHF signal for specific block
134141
*/
135142
void AddSignal(const CBlockIndex* const pindex, int bit) EXCLUSIVE_LOCKS_REQUIRED(!cs_cache);
143+
144+
/**
145+
* Set llmq::CQuorumManager pointer.
146+
*
147+
* Separated from constructor to allow LLMQContext to use CMNHFManager in read-only capacity.
148+
* Required to mutate state.
149+
*/
150+
void ConnectManagers(gsl::not_null<llmq::CQuorumManager*> qman);
151+
152+
/**
153+
* Reset llmq::CQuorumManager pointer.
154+
*
155+
* @pre Must be called before LLMQContext (containing llmq::CQuorumManager) is destroyed.
156+
*/
157+
void DisconnectManagers() { m_qman = nullptr; };
158+
136159
private:
137160
void AddToCache(const Signals& signals, const CBlockIndex* const pindex);
138161

src/init.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,10 @@ void PrepareShutdown(NodeContext& node)
339339
}
340340
pblocktree.reset();
341341
node.chain_helper.reset();
342-
if (node.llmq_ctx) {
343-
node.llmq_ctx.reset();
342+
if (node.mnhf_manager) {
343+
node.mnhf_manager->DisconnectManagers();
344344
}
345+
node.llmq_ctx.reset();
345346
llmq::quorumSnapshotManager.reset();
346347
node.mempool->DisconnectManagers();
347348
node.dmnman.reset();
@@ -1973,6 +1974,8 @@ bool AppInitMain(const CoreContext& context, NodeContext& node, interfaces::Bloc
19731974
node.llmq_ctx.reset();
19741975
node.llmq_ctx = std::make_unique<LLMQContext>(chainman.ActiveChainstate(), *node.connman, *node.dmnman, *node.evodb, *node.mn_metaman, *node.mnhf_manager, *node.sporkman,
19751976
*node.mempool, node.mn_activeman.get(), *node.mn_sync, node.peerman, /* unit_tests = */ false, /* wipe = */ fReset || fReindexChainState);
1977+
// Enable CMNHFManager::{Process, Undo}Block
1978+
node.mnhf_manager->ConnectManagers(node.llmq_ctx->qman);
19761979
// Have to start it early to let VerifyDB check ChainLock signatures in coinbase
19771980
node.llmq_ctx->Start();
19781981

src/test/util/setup_common.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ void DashTestSetup(NodeContext& node, const CChainParams& chainparams)
119119
#endif // ENABLE_WALLET
120120
node.llmq_ctx = std::make_unique<LLMQContext>(chainstate, *node.connman, *node.dmnman, *node.evodb, *node.mn_metaman, *node.mnhf_manager, *node.sporkman, *node.mempool,
121121
/* mn_activeman = */ nullptr, *node.mn_sync, node.peerman, /* unit_tests = */ true, /* wipe = */ false);
122+
Assert(node.mnhf_manager)->ConnectManagers(node.llmq_ctx->qman);
122123
node.chain_helper = std::make_unique<CChainstateHelper>(*node.cpoolman, *node.dmnman, *node.mnhf_manager, *node.govman, *(node.llmq_ctx->quorum_block_processor),
123124
chainparams.GetConsensus(), *node.mn_sync, *node.sporkman, *(node.llmq_ctx->clhandler), *(node.llmq_ctx->qman));
124125
}
@@ -128,6 +129,7 @@ void DashTestSetupClose(NodeContext& node)
128129
node.chain_helper.reset();
129130
node.llmq_ctx->Interrupt();
130131
node.llmq_ctx->Stop();
132+
Assert(node.mnhf_manager)->DisconnectManagers();
131133
node.llmq_ctx.reset();
132134
#ifdef ENABLE_WALLET
133135
node.coinjoin_loader.reset();

0 commit comments

Comments
 (0)