Skip to content

Commit

Permalink
refactor: move CConnman out of CDKGSession{,Handler,Manager} ctor
Browse files Browse the repository at this point in the history
  • Loading branch information
kwvg committed Dec 5, 2024
1 parent 01876c7 commit c07b522
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 58 deletions.
2 changes: 1 addition & 1 deletion src/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2280,7 +2280,7 @@ bool AppInitMain(NodeContext& node, interfaces::BlockAndHeaderTipInfo* tip_info)

// ********************************************************* Step 10a: schedule Dash-specific tasks

node.llmq_ctx->Start(*node.peerman);
node.llmq_ctx->Start(*node.connman, *node.peerman);

node.scheduler->scheduleEvery(std::bind(&CNetFulfilledRequestManager::DoMaintenance, std::ref(*node.netfulfilledman)), std::chrono::minutes{1});
node.scheduler->scheduleEvery(std::bind(&CMasternodeSync::DoMaintenance, std::ref(*node.mn_sync), std::cref(*node.peerman), std::cref(*node.govman)), std::chrono::seconds{1});
Expand Down
10 changes: 5 additions & 5 deletions src/llmq/context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ LLMQContext::LLMQContext(ChainstateManager& chainman, CConnman& connman, CDeterm
bls_worker{std::make_shared<CBLSWorker>()},
dkg_debugman{std::make_unique<llmq::CDKGDebugManager>()},
quorum_block_processor{std::make_unique<llmq::CQuorumBlockProcessor>(chainman.ActiveChainstate(), dmnman, evo_db)},
qdkgsman{std::make_unique<llmq::CDKGSessionManager>(*bls_worker, chainman.ActiveChainstate(), connman, dmnman,
*dkg_debugman, mn_metaman, *quorum_block_processor,
mn_activeman, sporkman, unit_tests, wipe)},
qdkgsman{std::make_unique<llmq::CDKGSessionManager>(*bls_worker, chainman.ActiveChainstate(), dmnman, *dkg_debugman,
mn_metaman, *quorum_block_processor, mn_activeman, sporkman,
unit_tests, wipe)},
qman{std::make_unique<llmq::CQuorumManager>(*bls_worker, chainman.ActiveChainstate(), connman, dmnman, *qdkgsman,
evo_db, *quorum_block_processor, mn_activeman, mn_sync, sporkman,
unit_tests, wipe)},
Expand Down Expand Up @@ -74,13 +74,13 @@ void LLMQContext::Interrupt() {
llmq::quorumInstantSendManager->InterruptWorkerThread();
}

void LLMQContext::Start(PeerManager& peerman)
void LLMQContext::Start(CConnman& connman, PeerManager& peerman)
{
assert(clhandler == llmq::chainLocksHandler.get());
assert(isman == llmq::quorumInstantSendManager.get());

if (is_masternode) {
qdkgsman->StartThreads(peerman);
qdkgsman->StartThreads(connman, peerman);
}
qman->Start();
shareman->RegisterAsRecoveredSigsListener();
Expand Down
2 changes: 1 addition & 1 deletion src/llmq/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ struct LLMQContext {
~LLMQContext();

void Interrupt();
void Start(PeerManager& peerman);
void Start(CConnman& connman, PeerManager& peerman);
void Stop();

/** Guaranteed if LLMQContext is initialized then all members are valid too
Expand Down
14 changes: 6 additions & 8 deletions src/llmq/dkgsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,12 @@ CDKGMember::CDKGMember(const CDeterministicMNCPtr& _dmn, size_t _idx) :
}

CDKGSession::CDKGSession(const CBlockIndex* pQuorumBaseBlockIndex, const Consensus::LLMQParams& _params,
CBLSWorker& _blsWorker, CConnman& _connman, CDeterministicMNManager& dmnman,
CDKGSessionManager& _dkgManager, CDKGDebugManager& _dkgDebugManager,
CMasternodeMetaMan& mn_metaman, const CActiveMasternodeManager* const mn_activeman,
const CSporkManager& sporkman) :
CBLSWorker& _blsWorker, CDeterministicMNManager& dmnman, CDKGSessionManager& _dkgManager,
CDKGDebugManager& _dkgDebugManager, CMasternodeMetaMan& mn_metaman,
const CActiveMasternodeManager* const mn_activeman, const CSporkManager& sporkman) :
params(_params),
blsWorker(_blsWorker),
cache(_blsWorker),
connman(_connman),
m_dmnman(dmnman),
dkgManager(_dkgManager),
dkgDebugManager(_dkgDebugManager),
Expand Down Expand Up @@ -417,7 +415,7 @@ void CDKGSession::VerifyPendingContributions()
pendingContributionVerifications.clear();
}

void CDKGSession::VerifyAndComplain(CDKGPendingMessages& pendingMessages, PeerManager& peerman)
void CDKGSession::VerifyAndComplain(CConnman& connman, CDKGPendingMessages& pendingMessages, PeerManager& peerman)
{
if (!AreWeMember()) {
return;
Expand Down Expand Up @@ -453,12 +451,12 @@ void CDKGSession::VerifyAndComplain(CDKGPendingMessages& pendingMessages, PeerMa
logger.Batch("verified contributions. time=%d", t1.count());
logger.Flush();

VerifyConnectionAndMinProtoVersions();
VerifyConnectionAndMinProtoVersions(connman);

SendComplaint(pendingMessages, peerman);
}

void CDKGSession::VerifyConnectionAndMinProtoVersions() const
void CDKGSession::VerifyConnectionAndMinProtoVersions(CConnman& connman) const
{
assert(m_mn_metaman.IsValid());

Expand Down
11 changes: 5 additions & 6 deletions src/llmq/dkgsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,6 @@ class CDKGSession

CBLSWorker& blsWorker;
CBLSWorkerCache cache;
CConnman& connman;
CDeterministicMNManager& m_dmnman;
CDKGSessionManager& dkgManager;
CDKGDebugManager& dkgDebugManager;
Expand Down Expand Up @@ -327,9 +326,9 @@ class CDKGSession

public:
CDKGSession(const CBlockIndex* pQuorumBaseBlockIndex, const Consensus::LLMQParams& _params, CBLSWorker& _blsWorker,
CConnman& _connman, CDeterministicMNManager& dmnman, CDKGSessionManager& _dkgManager,
CDKGDebugManager& _dkgDebugManager, CMasternodeMetaMan& mn_metaman,
const CActiveMasternodeManager* const mn_activeman, const CSporkManager& sporkman);
CDeterministicMNManager& dmnman, CDKGSessionManager& _dkgManager, CDKGDebugManager& _dkgDebugManager,
CMasternodeMetaMan& mn_metaman, const CActiveMasternodeManager* const mn_activeman,
const CSporkManager& sporkman);

// TODO: remove Init completely
bool Init(const uint256& _myProTxHash, int _quorumIndex);
Expand Down Expand Up @@ -357,8 +356,8 @@ class CDKGSession
void VerifyPendingContributions() EXCLUSIVE_LOCKS_REQUIRED(cs_pending);

// Phase 2: complaint
void VerifyAndComplain(CDKGPendingMessages& pendingMessages, PeerManager& peerman);
void VerifyConnectionAndMinProtoVersions() const;
void VerifyAndComplain(CConnman& connman, CDKGPendingMessages& pendingMessages, PeerManager& peerman);
void VerifyConnectionAndMinProtoVersions(CConnman& connman) const;
void SendComplaint(CDKGPendingMessages& pendingMessages, PeerManager& peerman);
bool PreVerifyMessage(const CDKGComplaint& qc, bool& retBan) const;
std::optional<CInv> ReceiveMessage(const CDKGComplaint& qc);
Expand Down
36 changes: 18 additions & 18 deletions src/llmq/dkgsessionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
namespace llmq
{

CDKGSessionHandler::CDKGSessionHandler(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman,
CDeterministicMNManager& dmnman, CDKGDebugManager& _dkgDebugManager,
CDKGSessionManager& _dkgManager, CMasternodeMetaMan& mn_metaman,
CQuorumBlockProcessor& _quorumBlockProcessor,
CDKGSessionHandler::CDKGSessionHandler(CBLSWorker& _blsWorker, CChainState& chainstate, CDeterministicMNManager& dmnman,
CDKGDebugManager& _dkgDebugManager, CDKGSessionManager& _dkgManager,
CMasternodeMetaMan& mn_metaman, CQuorumBlockProcessor& _quorumBlockProcessor,
const CActiveMasternodeManager* const mn_activeman, const CSporkManager& sporkman,
const Consensus::LLMQParams& _params, int _quorumIndex) :
blsWorker(_blsWorker),
m_chainstate(chainstate),
connman(_connman),
m_dmnman(dmnman),
dkgDebugManager(_dkgDebugManager),
dkgManager(_dkgManager),
Expand All @@ -42,8 +40,8 @@ CDKGSessionHandler::CDKGSessionHandler(CBLSWorker& _blsWorker, CChainState& chai
m_sporkman(sporkman),
params(_params),
quorumIndex(_quorumIndex),
curSession(std::make_unique<CDKGSession>(nullptr, _params, _blsWorker, _connman, dmnman, _dkgManager,
_dkgDebugManager, m_mn_metaman, m_mn_activeman, sporkman)),
curSession(std::make_unique<CDKGSession>(nullptr, _params, _blsWorker, dmnman, _dkgManager, _dkgDebugManager,
m_mn_metaman, m_mn_activeman, sporkman)),
pendingContributions(
(size_t)_params.size * 2,
MSG_QUORUM_CONTRIB), // we allow size*2 messages as we need to make sure we see bad behavior (double messages)
Expand Down Expand Up @@ -165,15 +163,15 @@ void CDKGSessionHandler::ProcessMessage(const CNode& pfrom, PeerManager& peerman
}
}

void CDKGSessionHandler::StartThread(PeerManager& peerman)
void CDKGSessionHandler::StartThread(CConnman& connman, PeerManager& peerman)
{
if (phaseHandlerThread.joinable()) {
throw std::runtime_error("Tried to start an already started CDKGSessionHandler thread.");
}

m_thread_name = strprintf("llmq-%d-%d", ToUnderlying(params.type), quorumIndex);
phaseHandlerThread = std::thread(&util::TraceThread, m_thread_name.c_str(),
[this, &peerman] { PhaseHandlerThread(peerman); });
[this, &connman, &peerman] { PhaseHandlerThread(connman, peerman); });
}

void CDKGSessionHandler::StopThread()
Expand All @@ -190,7 +188,7 @@ bool CDKGSessionHandler::InitNewQuorum(const CBlockIndex* pQuorumBaseBlockIndex)
return false;
}

curSession = std::make_unique<CDKGSession>(pQuorumBaseBlockIndex, params, blsWorker, connman, m_dmnman, dkgManager,
curSession = std::make_unique<CDKGSession>(pQuorumBaseBlockIndex, params, blsWorker, m_dmnman, dkgManager,
dkgDebugManager, m_mn_metaman, m_mn_activeman, m_sporkman);

if (!curSession->Init(m_mn_activeman->GetProTxHash(), quorumIndex)) {
Expand Down Expand Up @@ -528,7 +526,7 @@ bool ProcessPendingMessageBatch(CConnman& connman, CDKGSession& session, CDKGPen
return true;
}

void CDKGSessionHandler::HandleDKGRound(PeerManager& peerman)
void CDKGSessionHandler::HandleDKGRound(CConnman& connman, PeerManager& peerman)
{
WaitForNextPhase(std::nullopt, QuorumPhase::Initialized);

Expand Down Expand Up @@ -562,31 +560,33 @@ void CDKGSessionHandler::HandleDKGRound(PeerManager& peerman)

// Contribute
auto fContributeStart = [this, &peerman]() { curSession->Contribute(pendingContributions, peerman); };
auto fContributeWait = [this, &peerman] {
auto fContributeWait = [this, &connman, &peerman] {
return ProcessPendingMessageBatch<CDKGContribution, MSG_QUORUM_CONTRIB>(connman, *curSession,
pendingContributions, peerman, 8);
};
HandlePhase(QuorumPhase::Contribute, QuorumPhase::Complain, curQuorumHash, 0.05, fContributeStart, fContributeWait);

// Complain
auto fComplainStart = [this, &peerman]() { curSession->VerifyAndComplain(pendingComplaints, peerman); };
auto fComplainWait = [this, &peerman] {
auto fComplainStart = [this, &connman, &peerman]() {
curSession->VerifyAndComplain(connman, pendingComplaints, peerman);
};
auto fComplainWait = [this, &connman, &peerman] {
return ProcessPendingMessageBatch<CDKGComplaint, MSG_QUORUM_COMPLAINT>(connman, *curSession, pendingComplaints,
peerman, 8);
};
HandlePhase(QuorumPhase::Complain, QuorumPhase::Justify, curQuorumHash, 0.05, fComplainStart, fComplainWait);

// Justify
auto fJustifyStart = [this, &peerman]() { curSession->VerifyAndJustify(pendingJustifications, peerman); };
auto fJustifyWait = [this, &peerman] {
auto fJustifyWait = [this, &connman, &peerman] {
return ProcessPendingMessageBatch<CDKGJustification, MSG_QUORUM_JUSTIFICATION>(connman, *curSession,
pendingJustifications, peerman, 8);
};
HandlePhase(QuorumPhase::Justify, QuorumPhase::Commit, curQuorumHash, 0.05, fJustifyStart, fJustifyWait);

// Commit
auto fCommitStart = [this, &peerman]() { curSession->VerifyAndCommit(pendingPrematureCommitments, peerman); };
auto fCommitWait = [this, &peerman] {
auto fCommitWait = [this, &connman, &peerman] {
return ProcessPendingMessageBatch<CDKGPrematureCommitment, MSG_QUORUM_PREMATURE_COMMITMENT>(
connman, *curSession, pendingPrematureCommitments, peerman, 8);
};
Expand All @@ -600,12 +600,12 @@ void CDKGSessionHandler::HandleDKGRound(PeerManager& peerman)
}
}

void CDKGSessionHandler::PhaseHandlerThread(PeerManager& peerman)
void CDKGSessionHandler::PhaseHandlerThread(CConnman& connman, PeerManager& peerman)
{
while (!stopRequested) {
try {
LogPrint(BCLog::LLMQ_DKG, "CDKGSessionHandler::%s -- %s qi[%d] - starting HandleDKGRound\n", __func__, params.name, quorumIndex);
HandleDKGRound(peerman);
HandleDKGRound(connman, peerman);
} catch (AbortPhaseException& e) {
dkgDebugManager.UpdateLocalSessionStatus(params.type, quorumIndex, [&](CDKGDebugSessionStatus& status) {
status.statusBits.aborted = true;
Expand Down
12 changes: 5 additions & 7 deletions src/llmq/dkgsessionhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ class CDKGSessionHandler

CBLSWorker& blsWorker;
CChainState& m_chainstate;
CConnman& connman;
CDeterministicMNManager& m_dmnman;
CDKGDebugManager& dkgDebugManager;
CDKGSessionManager& dkgManager;
Expand All @@ -156,17 +155,16 @@ class CDKGSessionHandler
CDKGPendingMessages pendingPrematureCommitments;

public:
CDKGSessionHandler(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman,
CDeterministicMNManager& dmnman, CDKGDebugManager& _dkgDebugManager,
CDKGSessionManager& _dkgManager, CMasternodeMetaMan& mn_metaman,
CDKGSessionHandler(CBLSWorker& _blsWorker, CChainState& chainstate, CDeterministicMNManager& dmnman,
CDKGDebugManager& _dkgDebugManager, CDKGSessionManager& _dkgManager, CMasternodeMetaMan& mn_metaman,
CQuorumBlockProcessor& _quorumBlockProcessor, const CActiveMasternodeManager* const mn_activeman,
const CSporkManager& sporkman, const Consensus::LLMQParams& _params, int _quorumIndex);
~CDKGSessionHandler();

void UpdatedBlockTip(const CBlockIndex *pindexNew);
void ProcessMessage(const CNode& pfrom, PeerManager& peerman, const std::string& msg_type, CDataStream& vRecv);

void StartThread(PeerManager& peerman);
void StartThread(CConnman& connman, PeerManager& peerman);
void StopThread();

bool GetContribution(const uint256& hash, CDKGContribution& ret) const;
Expand All @@ -191,8 +189,8 @@ class CDKGSessionHandler
void WaitForNewQuorum(const uint256& oldQuorumHash) const;
void SleepBeforePhase(QuorumPhase curPhase, const uint256& expectedQuorumHash, double randomSleepFactor, const WhileWaitFunc& runWhileWaiting) const;
void HandlePhase(QuorumPhase curPhase, QuorumPhase nextPhase, const uint256& expectedQuorumHash, double randomSleepFactor, const StartPhaseFunc& startPhaseFunc, const WhileWaitFunc& runWhileWaiting);
void HandleDKGRound(PeerManager& peerman);
void PhaseHandlerThread(PeerManager& peerman);
void HandleDKGRound(CConnman& connman, PeerManager& peerman);
void PhaseHandlerThread(CConnman& connman, PeerManager& peerman);
};

} // namespace llmq
Expand Down
15 changes: 7 additions & 8 deletions src/llmq/dkgsessionmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,14 @@ static const std::string DB_VVEC = "qdkg_V";
static const std::string DB_SKCONTRIB = "qdkg_S";
static const std::string DB_ENC_CONTRIB = "qdkg_E";

CDKGSessionManager::CDKGSessionManager(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman,
CDeterministicMNManager& dmnman, CDKGDebugManager& _dkgDebugManager,
CMasternodeMetaMan& mn_metaman, CQuorumBlockProcessor& _quorumBlockProcessor,
CDKGSessionManager::CDKGSessionManager(CBLSWorker& _blsWorker, CChainState& chainstate, CDeterministicMNManager& dmnman,
CDKGDebugManager& _dkgDebugManager, CMasternodeMetaMan& mn_metaman,
CQuorumBlockProcessor& _quorumBlockProcessor,
const CActiveMasternodeManager* const mn_activeman,
const CSporkManager& sporkman, bool unitTests, bool fWipe) :
db(std::make_unique<CDBWrapper>(unitTests ? "" : (gArgs.GetDataDirNet() / "llmq/dkgdb"), 1 << 20, unitTests, fWipe)),
blsWorker(_blsWorker),
m_chainstate(chainstate),
connman(_connman),
m_dmnman(dmnman),
dkgDebugManager(_dkgDebugManager),
quorumBlockProcessor(_quorumBlockProcessor),
Expand All @@ -53,19 +52,19 @@ CDKGSessionManager::CDKGSessionManager(CBLSWorker& _blsWorker, CChainState& chai
auto session_count = (params.useRotation) ? params.signingActiveQuorumCount : 1;
for (const auto i : irange::range(session_count)) {
dkgSessionHandlers.emplace(std::piecewise_construct, std::forward_as_tuple(params.type, i),
std::forward_as_tuple(blsWorker, m_chainstate, connman, dmnman, dkgDebugManager,
*this, mn_metaman, quorumBlockProcessor, mn_activeman,
std::forward_as_tuple(blsWorker, m_chainstate, dmnman, dkgDebugManager, *this,
mn_metaman, quorumBlockProcessor, mn_activeman,
spork_manager, params, i));
}
}
}

CDKGSessionManager::~CDKGSessionManager() = default;

void CDKGSessionManager::StartThreads(PeerManager& peerman)
void CDKGSessionManager::StartThreads(CConnman& connman, PeerManager& peerman)
{
for (auto& it : dkgSessionHandlers) {
it.second.StartThread(peerman);
it.second.StartThread(connman, peerman);
}
}

Expand Down
7 changes: 3 additions & 4 deletions src/llmq/dkgsessionmgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ class CDKGSessionManager

CBLSWorker& blsWorker;
CChainState& m_chainstate;
CConnman& connman;
CDeterministicMNManager& m_dmnman;
CDKGDebugManager& dkgDebugManager;
CQuorumBlockProcessor& quorumBlockProcessor;
Expand Down Expand Up @@ -71,13 +70,13 @@ class CDKGSessionManager
mutable std::map<ContributionsCacheKey, ContributionsCacheEntry> contributionsCache GUARDED_BY(contributionsCacheCs);

public:
CDKGSessionManager(CBLSWorker& _blsWorker, CChainState& chainstate, CConnman& _connman,
CDeterministicMNManager& dmnman, CDKGDebugManager& _dkgDebugManager, CMasternodeMetaMan& mn_metaman,
CDKGSessionManager(CBLSWorker& _blsWorker, CChainState& chainstate, CDeterministicMNManager& dmnman,
CDKGDebugManager& _dkgDebugManager, CMasternodeMetaMan& mn_metaman,
CQuorumBlockProcessor& _quorumBlockProcessor, const CActiveMasternodeManager* const mn_activeman,
const CSporkManager& sporkman, bool unitTests, bool fWipe);
~CDKGSessionManager();

void StartThreads(PeerManager& peerman);
void StartThreads(CConnman& connman, PeerManager& peerman);
void StopThreads();

void UpdatedBlockTip(const CBlockIndex *pindexNew, bool fInitialDownload);
Expand Down

0 comments on commit c07b522

Please sign in to comment.