Skip to content

Commit

Permalink
A couple of fixes/refactorings for CDKGSessionHandler (#2637)
Browse files Browse the repository at this point in the history
* Fix confusion: `quorumHash` is both a class member and an argument of a function

Rename `height` too while at it

* Make sure height and hash we pass to InitNewQuorum are related

* Don't update expectedQuorumHash, make it const

This also streamlines logic a bit

* Compact phase calculation

* Decouple invCs and cs_vPendingMasternodes

Not an issue atm but we'd better avoid any potential interlocking if possible

* wrap `%` in `()`

Co-Authored-By: UdjinM6 <UdjinM6@users.noreply.github.com>
  • Loading branch information
UdjinM6 committed Jan 22, 2019
1 parent b2b97f2 commit d2ddc2a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 35 deletions.
46 changes: 18 additions & 28 deletions src/llmq/quorums_dkgsessionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,24 +120,11 @@ void CDKGSessionHandler::UpdatedBlockTip(const CBlockIndex* pindexNew, const CBl
quorumHeight = pindexQuorum->nHeight;
quorumHash = pindexQuorum->GetBlockHash();

QuorumPhase newPhase = phase;
if (quorumStageInt == 0) {
newPhase = QuorumPhase_Initialized;
quorumDKGDebugManager->ResetLocalSessionStatus(params.type, quorumHash, quorumHeight);
} else if (quorumStageInt == params.dkgPhaseBlocks * 1) {
newPhase = QuorumPhase_Contribute;
} else if (quorumStageInt == params.dkgPhaseBlocks * 2) {
newPhase = QuorumPhase_Complain;
} else if (quorumStageInt == params.dkgPhaseBlocks * 3) {
newPhase = QuorumPhase_Justify;
} else if (quorumStageInt == params.dkgPhaseBlocks * 4) {
newPhase = QuorumPhase_Commit;
} else if (quorumStageInt == params.dkgPhaseBlocks * 5) {
newPhase = QuorumPhase_Finalize;
} else if (quorumStageInt == params.dkgPhaseBlocks * 6) {
newPhase = QuorumPhase_Idle;
bool fNewPhase = (quorumStageInt % params.dkgPhaseBlocks) == 0;
int phaseInt = quorumStageInt / params.dkgPhaseBlocks;
if (fNewPhase && phaseInt >= QuorumPhase_Initialized && phaseInt <= QuorumPhase_Idle) {
phase = static_cast<QuorumPhase>(phaseInt);
}
phase = newPhase;

quorumDKGDebugManager->UpdateLocalStatus([&](CDKGDebugStatus& status) {
bool changed = status.nHeight != pindexNew->nHeight;
Expand Down Expand Up @@ -165,21 +152,21 @@ void CDKGSessionHandler::ProcessMessage(CNode* pfrom, const std::string& strComm
}
}

bool CDKGSessionHandler::InitNewQuorum(int height, const uint256& quorumHash)
bool CDKGSessionHandler::InitNewQuorum(int newQuorumHeight, const uint256& newQuorumHash)
{
//AssertLockHeld(cs_main);

const auto& consensus = Params().GetConsensus();

curSession = std::make_shared<CDKGSession>(params, evoDb, blsWorker, dkgManager);

if (!deterministicMNManager->IsDIP3Active(height)) {
if (!deterministicMNManager->IsDIP3Active(newQuorumHeight)) {
return false;
}

auto mns = CLLMQUtils::GetAllQuorumMembers(params.type, quorumHash);
auto mns = CLLMQUtils::GetAllQuorumMembers(params.type, newQuorumHash);

if (!curSession->Init(height, quorumHash, mns, activeMasternodeInfo.proTxHash)) {
if (!curSession->Init(newQuorumHeight, newQuorumHash, mns, activeMasternodeInfo.proTxHash)) {
LogPrintf("CDKGSessionManager::%s -- quorum initialiation failed\n", __func__);
return false;
}
Expand All @@ -198,7 +185,7 @@ class AbortPhaseException : public std::exception {

void CDKGSessionHandler::WaitForNextPhase(QuorumPhase curPhase,
QuorumPhase nextPhase,
uint256& expectedQuorumHash,
const uint256& expectedQuorumHash,
const WhileWaitFunc& runWhileWaiting)
{
while (true) {
Expand All @@ -210,7 +197,6 @@ void CDKGSessionHandler::WaitForNextPhase(QuorumPhase curPhase,
throw AbortPhaseException();
}
if (p.first == nextPhase) {
expectedQuorumHash = p.second;
return;
}
if (curPhase != QuorumPhase_None && p.first != curPhase) {
Expand Down Expand Up @@ -238,7 +224,7 @@ void CDKGSessionHandler::WaitForNewQuorum(const uint256& oldQuorumHash)

// Sleep some time to not fully overload the whole network
void CDKGSessionHandler::SleepBeforePhase(QuorumPhase curPhase,
uint256& expectedQuorumHash,
const uint256& expectedQuorumHash,
double randomSleepFactor,
const WhileWaitFunc& runWhileWaiting)
{
Expand Down Expand Up @@ -273,7 +259,7 @@ void CDKGSessionHandler::SleepBeforePhase(QuorumPhase curPhase,

void CDKGSessionHandler::HandlePhase(QuorumPhase curPhase,
QuorumPhase nextPhase,
uint256& expectedQuorumHash,
const uint256& expectedQuorumHash,
double randomSleepFactor,
const StartPhaseFunc& startPhaseFunc,
const WhileWaitFunc& runWhileWaiting)
Expand Down Expand Up @@ -466,18 +452,21 @@ bool ProcessPendingMessageBatch(CDKGSession& session, CDKGPendingMessages& pendi
void CDKGSessionHandler::HandleDKGRound()
{
uint256 curQuorumHash;
int curQuorumHeight;

WaitForNextPhase(QuorumPhase_None, QuorumPhase_Initialized, curQuorumHash, []{return false;});
WaitForNextPhase(QuorumPhase_None, QuorumPhase_Initialized, uint256(), []{return false;});

{
LOCK(cs);
pendingContributions.Clear();
pendingComplaints.Clear();
pendingJustifications.Clear();
pendingPrematureCommitments.Clear();
curQuorumHash = quorumHash;
curQuorumHeight = quorumHeight;
}

if (!InitNewQuorum(quorumHeight, quorumHash)) {
if (!InitNewQuorum(curQuorumHeight, curQuorumHash)) {
// should actually never happen
WaitForNewQuorum(curQuorumHash);
throw AbortPhaseException();
Expand All @@ -501,8 +490,9 @@ void CDKGSessionHandler::HandleDKGRound()
LogPrintf(debugMsg);
g_connman->AddMasternodeQuorumNodes(params.type, curQuorumHash, connections);

auto participatingNodesTmp = g_connman->GetMasternodeQuorumAddresses(params.type, curQuorumHash);
LOCK(curSession->invCs);
curSession->participatingNodes = g_connman->GetMasternodeQuorumAddresses(params.type, curQuorumHash);
curSession->participatingNodes = std::move(participatingNodesTmp);
}
}

Expand Down
13 changes: 6 additions & 7 deletions src/llmq/quorums_dkgsessionhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ namespace llmq
{

enum QuorumPhase {
QuorumPhase_Idle,
QuorumPhase_None = -1,
QuorumPhase_Initialized,
QuorumPhase_Contribute,
QuorumPhase_Complain,
QuorumPhase_Justify,
QuorumPhase_Commit,
QuorumPhase_Finalize,

QuorumPhase_None=-1,
QuorumPhase_Idle,
};

/**
Expand Down Expand Up @@ -127,16 +126,16 @@ class CDKGSessionHandler
void ProcessMessage(CNode* pfrom, const std::string& strCommand, CDataStream& vRecv, CConnman& connman);

private:
bool InitNewQuorum(int height, const uint256& quorumHash);
bool InitNewQuorum(int newQuorumHeight, const uint256& newQuorumHash);

std::pair<QuorumPhase, uint256> GetPhaseAndQuorumHash() const;

typedef std::function<void()> StartPhaseFunc;
typedef std::function<bool()> WhileWaitFunc;
void WaitForNextPhase(QuorumPhase curPhase, QuorumPhase nextPhase, uint256& expectedQuorumHash, const WhileWaitFunc& runWhileWaiting);
void WaitForNextPhase(QuorumPhase curPhase, QuorumPhase nextPhase, const uint256& expectedQuorumHash, const WhileWaitFunc& runWhileWaiting);
void WaitForNewQuorum(const uint256& oldQuorumHash);
void SleepBeforePhase(QuorumPhase curPhase, uint256& expectedQuorumHash, double randomSleepFactor, const WhileWaitFunc& runWhileWaiting);
void HandlePhase(QuorumPhase curPhase, QuorumPhase nextPhase, uint256& expectedQuorumHash, double randomSleepFactor, const StartPhaseFunc& startPhaseFunc, const WhileWaitFunc& runWhileWaiting);
void SleepBeforePhase(QuorumPhase curPhase, const uint256& expectedQuorumHash, double randomSleepFactor, const WhileWaitFunc& runWhileWaiting);
void HandlePhase(QuorumPhase curPhase, QuorumPhase nextPhase, const uint256& expectedQuorumHash, double randomSleepFactor, const StartPhaseFunc& startPhaseFunc, const WhileWaitFunc& runWhileWaiting);
void HandleDKGRound();
void PhaseHandlerThread();
};
Expand Down

0 comments on commit d2ddc2a

Please sign in to comment.