@@ -87,16 +87,23 @@ bool ActiveDKGSessionHandler::InitNewQuorum(gsl::not_null<const CBlockIndex*> pQ
8787 return false ;
8888 }
8989
90- curSession = std::make_unique <ActiveDKGSession>(m_bls_worker, m_dmnman, m_dkgdbgman, m_qdkgsman, m_mn_metaman ,
91- m_qsnapman, m_mn_activeman, m_chainman, m_sporkman ,
92- pQuorumBaseBlockIndex, params);
90+ auto next_session = std::make_shared <ActiveDKGSession>(m_bls_worker, m_dmnman, m_dkgdbgman, m_qdkgsman,
91+ m_mn_metaman, m_qsnapman, m_mn_activeman, m_chainman,
92+ m_sporkman, pQuorumBaseBlockIndex, params);
9393
94- if (!curSession->Init (m_mn_activeman.GetProTxHash (), quorumIndex)) {
94+ {
95+ std::shared_ptr<CDKGSession> previous_session;
96+ WITH_LOCK (cs_session, previous_session.swap (curSession));
97+ }
98+
99+ if (!next_session->Init (m_mn_activeman.GetProTxHash (), quorumIndex)) {
95100 LogPrintf (" ActiveDKGSessionHandler::%s -- height[%d] quorum initialization failed for %s qi[%d]\n " , __func__,
96101 pQuorumBaseBlockIndex->nHeight , params.name , quorumIndex);
97102 return false ;
98103 }
99104
105+ WITH_LOCK (cs_session, curSession = std::move (next_session));
106+
100107 LogPrintf (" ActiveDKGSessionHandler::%s -- height[%d] quorum initialization OK for %s qi[%d]\n " , __func__, pQuorumBaseBlockIndex->nHeight , params.name , quorumIndex);
101108 return true ;
102109}
@@ -166,7 +173,8 @@ void ActiveDKGSessionHandler::WaitForNewQuorum(const uint256& oldQuorumHash) con
166173void ActiveDKGSessionHandler::SleepBeforePhase (QuorumPhase curPhase, const uint256& expectedQuorumHash,
167174 double randomSleepFactor, const WhileWaitFunc& runWhileWaiting) const
168175{
169- if (!curSession->AreWeMember ()) {
176+ const auto session = GetCurSession ();
177+ if (!session || !session->AreWeMember ()) {
170178 // Non-members do not participate and do not create any network load, no need to sleep.
171179 return ;
172180 }
@@ -187,7 +195,7 @@ void ActiveDKGSessionHandler::SleepBeforePhase(QuorumPhase curPhase, const uint2
187195 // Don't expect perfect block times and thus reduce the phase time to be on the secure side (caller chooses factor)
188196 double adjustedPhaseSleepTimePerMember = phaseSleepTimePerMember * randomSleepFactor;
189197
190- int64_t sleepTime = static_cast <int64_t >(adjustedPhaseSleepTimePerMember * curSession ->GetMyMemberIndex ().value_or (0 ));
198+ int64_t sleepTime = static_cast <int64_t >(adjustedPhaseSleepTimePerMember * session ->GetMyMemberIndex ().value_or (0 ));
191199 const auto endTime = SteadyClock::now () + std::chrono::milliseconds{sleepTime};
192200 int heightTmp{currentHeight.load ()};
193201 int heightStart{heightTmp};
@@ -248,22 +256,31 @@ void ActiveDKGSessionHandler::HandlePhase(QuorumPhase curPhase, QuorumPhase next
248256
249257bool ActiveDKGSessionHandler::GetContribution (const uint256& hash, CDKGContribution& ret) const
250258{
251- return curSession && curSession->GetContribution (hash, ret);
259+ const auto session = GetCurSession ();
260+ return session && session->GetContribution (hash, ret);
252261}
253262
254263bool ActiveDKGSessionHandler::GetComplaint (const uint256& hash, CDKGComplaint& ret) const
255264{
256- return curSession && curSession->GetComplaint (hash, ret);
265+ const auto session = GetCurSession ();
266+ return session && session->GetComplaint (hash, ret);
257267}
258268
259269bool ActiveDKGSessionHandler::GetJustification (const uint256& hash, CDKGJustification& ret) const
260270{
261- return curSession && curSession->GetJustification (hash, ret);
271+ const auto session = GetCurSession ();
272+ return session && session->GetJustification (hash, ret);
262273}
263274
264275bool ActiveDKGSessionHandler::GetPrematureCommitment (const uint256& hash, CDKGPrematureCommitment& ret) const
265276{
266- return curSession && curSession->GetPrematureCommitment (hash, ret);
277+ const auto session = GetCurSession ();
278+ return session && session->GetPrematureCommitment (hash, ret);
279+ }
280+
281+ std::shared_ptr<CDKGSession> ActiveDKGSessionHandler::GetCurSession () const
282+ {
283+ return WITH_LOCK (cs_session, return curSession);
267284}
268285
269286QuorumPhase ActiveDKGSessionHandler::GetPhase () const
0 commit comments