Skip to content

Commit

Permalink
Pass self-created message to CDKGPendingMessages instead of processin…
Browse files Browse the repository at this point in the history
…g them
  • Loading branch information
codablock committed Jan 11, 2019
1 parent c948c0f commit 0b1347c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 69 deletions.
70 changes: 16 additions & 54 deletions src/llmq/quorums_dkgsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ bool CDKGSession::Init(int _height, const uint256& _quorumHash, const std::vecto
return true;
}

void CDKGSession::Contribute()
void CDKGSession::Contribute(CDKGPendingMessages& pendingMessages)
{
CDKGLogger logger(*this, __func__);

Expand All @@ -122,10 +122,10 @@ void CDKGSession::Contribute()
}
logger.Batch("generated contributions. time=%d", t1.count());

SendContributions();
SendContributions(pendingMessages);
}

void CDKGSession::SendContributions()
void CDKGSession::SendContributions(CDKGPendingMessages& pendingMessages)
{
CDKGLogger logger(*this, __func__);

Expand Down Expand Up @@ -174,11 +174,7 @@ void CDKGSession::SendContributions()
return true;
});

uint256 hash = ::SerializeHash(qc);
bool ban = false;
if (PreVerifyMessage(hash, qc, ban)) {
ReceiveMessage(hash, qc, ban);
}
pendingMessages.PushPendingMessage(-1, qc);
}

// only performs cheap verifications, but not the signature of the message. this is checked with batched verification
Expand All @@ -195,10 +191,6 @@ bool CDKGSession::PreVerifyMessage(const uint256& hash, const CDKGContribution&
return false;
}

if (Seen(hash)) {
return false;
}

auto member = GetMember(qc.proTxHash);
if (!member) {
logger.Batch("contributor not a member of this quorum, rejecting contribution");
Expand Down Expand Up @@ -383,7 +375,7 @@ void CDKGSession::VerifyPendingContributions()
logger.Batch("verified %d pending contributions. time=%d", pend.size(), t1.count());
}

void CDKGSession::VerifyAndComplain()
void CDKGSession::VerifyAndComplain(CDKGPendingMessages& pendingMessages)
{
if (!AreWeMember()) {
return;
Expand Down Expand Up @@ -416,10 +408,10 @@ void CDKGSession::VerifyAndComplain()
logger.Batch("verified contributions. time=%d", t1.count());
logger.Flush();

SendComplaint();
SendComplaint(pendingMessages);
}

void CDKGSession::SendComplaint()
void CDKGSession::SendComplaint(CDKGPendingMessages& pendingMessages)
{
CDKGLogger logger(*this, __func__);

Expand Down Expand Up @@ -458,11 +450,7 @@ void CDKGSession::SendComplaint()
return true;
});

uint256 hash = ::SerializeHash(qc);
bool ban = false;
if (PreVerifyMessage(hash, qc, ban)) {
ReceiveMessage(hash, qc, ban);
}
pendingMessages.PushPendingMessage(-1, qc);
}

// only performs cheap verifications, but not the signature of the message. this is checked with batched verification
Expand All @@ -477,10 +465,6 @@ bool CDKGSession::PreVerifyMessage(const uint256& hash, const CDKGComplaint& qc,
return false;
}

if (Seen(hash)) {
return false;
}

auto member = GetMember(qc.proTxHash);
if (!member) {
logger.Batch("complainer not a member of this quorum, rejecting complaint");
Expand Down Expand Up @@ -579,7 +563,7 @@ void CDKGSession::ReceiveMessage(const uint256& hash, const CDKGComplaint& qc, b
logger.Batch("received and relayed complaint. received=%d", receivedCount);
}

void CDKGSession::VerifyAndJustify()
void CDKGSession::VerifyAndJustify(CDKGPendingMessages& pendingMessages)
{
if (!AreWeMember()) {
return;
Expand Down Expand Up @@ -615,11 +599,11 @@ void CDKGSession::VerifyAndJustify()

logger.Flush();
if (!justifyFor.empty()) {
SendJustification(justifyFor);
SendJustification(pendingMessages, justifyFor);
}
}

void CDKGSession::SendJustification(const std::set<uint256>& forMembers)
void CDKGSession::SendJustification(CDKGPendingMessages& pendingMessages, const std::set<uint256>& forMembers)
{
CDKGLogger logger(*this, __func__);

Expand Down Expand Up @@ -664,11 +648,7 @@ void CDKGSession::SendJustification(const std::set<uint256>& forMembers)
return true;
});

uint256 hash = ::SerializeHash(qj);
bool ban = false;
if (PreVerifyMessage(hash, qj, ban)) {
ReceiveMessage(hash, qj, ban);
}
pendingMessages.PushPendingMessage(-1, qj);
}

// only performs cheap verifications, but not the signature of the message. this is checked with batched verification
Expand All @@ -683,10 +663,6 @@ bool CDKGSession::PreVerifyMessage(const uint256& hash, const CDKGJustification&
return false;
}

if (Seen(hash)) {
return false;
}

auto member = GetMember(qj.proTxHash);
if (!member) {
logger.Batch("justifier not a member of this quorum, rejecting justification");
Expand Down Expand Up @@ -840,7 +816,7 @@ void CDKGSession::ReceiveMessage(const uint256& hash, const CDKGJustification& q
logger.Batch("verified justification: received=%d/%d time=%d", receivedCount, expectedCount, t1.count());
}

void CDKGSession::VerifyAndCommit()
void CDKGSession::VerifyAndCommit(CDKGPendingMessages& pendingMessages)
{
if (!AreWeMember()) {
return;
Expand Down Expand Up @@ -880,10 +856,10 @@ void CDKGSession::VerifyAndCommit()

logger.Flush();

SendCommitment();
SendCommitment(pendingMessages);
}

void CDKGSession::SendCommitment()
void CDKGSession::SendCommitment(CDKGPendingMessages& pendingMessages)
{
CDKGLogger logger(*this, __func__);

Expand Down Expand Up @@ -994,11 +970,7 @@ void CDKGSession::SendCommitment()
return true;
});

uint256 hash = ::SerializeHash(qc);
bool ban = false;
if (PreVerifyMessage(hash, qc, ban)) {
ReceiveMessage(hash, qc, ban);
}
pendingMessages.PushPendingMessage(-1, qc);
}

// only performs cheap verifications, but not the signature of the message. this is checked with batched verification
Expand All @@ -1015,11 +987,6 @@ bool CDKGSession::PreVerifyMessage(const uint256& hash, const CDKGPrematureCommi
return false;
}

if (Seen(hash)) {
logger.Batch("already received premature commitment");
return false;
}

auto member = GetMember(qc.proTxHash);
if (!member) {
logger.Batch("committer not a member of this quorum, rejecting premature commitment");
Expand Down Expand Up @@ -1273,11 +1240,6 @@ void CDKGSession::MarkBadMember(size_t idx)
member->bad = true;
}

bool CDKGSession::Seen(const uint256& msgHash)
{
return !seenMessages.emplace(msgHash).second;
}

void CDKGSession::AddParticipatingNode(NodeId nodeId)
{
LOCK(invCs);
Expand Down
20 changes: 9 additions & 11 deletions src/llmq/quorums_dkgsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ namespace llmq
class CFinalCommitment;
class CDKGSession;
class CDKGSessionManager;
class CDKGPendingMessages;

class CDKGLogger : public CBatchedLogger
{
Expand Down Expand Up @@ -280,8 +281,6 @@ class CDKGSession
std::set<CInv> invSet;
std::set<CService> participatingNodes;

std::set<uint256> seenMessages;

std::vector<size_t> pendingContributionVerifications;

// filled by ReceivePrematureCommitment and used by FinalizeCommitments
Expand Down Expand Up @@ -309,27 +308,27 @@ class CDKGSession
*/

// Phase 1: contribution
void Contribute();
void SendContributions();
void Contribute(CDKGPendingMessages& pendingMessages);
void SendContributions(CDKGPendingMessages& pendingMessages);
bool PreVerifyMessage(const uint256& hash, const CDKGContribution& qc, bool& retBan);
void ReceiveMessage(const uint256& hash, const CDKGContribution& qc, bool& retBan);
void VerifyPendingContributions();

// Phase 2: complaint
void VerifyAndComplain();
void SendComplaint();
void VerifyAndComplain(CDKGPendingMessages& pendingMessages);
void SendComplaint(CDKGPendingMessages& pendingMessages);
bool PreVerifyMessage(const uint256& hash, const CDKGComplaint& qc, bool& retBan);
void ReceiveMessage(const uint256& hash, const CDKGComplaint& qc, bool& retBan);

// Phase 3: justification
void VerifyAndJustify();
void SendJustification(const std::set<uint256>& forMembers);
void VerifyAndJustify(CDKGPendingMessages& pendingMessages);
void SendJustification(CDKGPendingMessages& pendingMessages, const std::set<uint256>& forMembers);
bool PreVerifyMessage(const uint256& hash, const CDKGJustification& qj, bool& retBan);
void ReceiveMessage(const uint256& hash, const CDKGJustification& qj, bool& retBan);

// Phase 4: commit
void VerifyAndCommit();
void SendCommitment();
void VerifyAndCommit(CDKGPendingMessages& pendingMessages);
void SendCommitment(CDKGPendingMessages& pendingMessages);
bool PreVerifyMessage(const uint256& hash, const CDKGPrematureCommitment& qc, bool& retBan);
void ReceiveMessage(const uint256& hash, const CDKGPrematureCommitment& qc, bool& retBan);

Expand All @@ -339,7 +338,6 @@ class CDKGSession
bool AreWeMember() const { return !myProTxHash.IsNull(); }
void MarkBadMember(size_t idx);

bool Seen(const uint256& msgHash);
void AddParticipatingNode(NodeId nodeId);
void RelayInvToParticipants(const CInv& inv);

Expand Down
8 changes: 4 additions & 4 deletions src/llmq/quorums_dkgsessionhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ void CDKGSessionHandler::HandleDKGRound()

// Contribute
auto fContributeStart = [this]() {
curSession->Contribute();
curSession->Contribute(pendingContributions);
};
auto fContributeWait = [this] {
return ProcessPendingMessageBatch<CDKGContribution>(*curSession, pendingContributions, 8);
Expand All @@ -519,7 +519,7 @@ void CDKGSessionHandler::HandleDKGRound()

// Complain
auto fComplainStart = [this]() {
curSession->VerifyAndComplain();
curSession->VerifyAndComplain(pendingComplaints);
};
auto fComplainWait = [this] {
return ProcessPendingMessageBatch<CDKGComplaint>(*curSession, pendingComplaints, 8);
Expand All @@ -528,7 +528,7 @@ void CDKGSessionHandler::HandleDKGRound()

// Justify
auto fJustifyStart = [this]() {
curSession->VerifyAndJustify();
curSession->VerifyAndJustify(pendingJustifications);
};
auto fJustifyWait = [this] {
return ProcessPendingMessageBatch<CDKGJustification>(*curSession, pendingJustifications, 8);
Expand All @@ -537,7 +537,7 @@ void CDKGSessionHandler::HandleDKGRound()

// Commit
auto fCommitStart = [this]() {
curSession->VerifyAndCommit();
curSession->VerifyAndCommit(pendingPrematureCommitments);
};
auto fCommitWait = [this] {
return ProcessPendingMessageBatch<CDKGPrematureCommitment>(*curSession, pendingPrematureCommitments, 8);
Expand Down
8 changes: 8 additions & 0 deletions src/llmq/quorums_dkgsessionhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,14 @@ class CDKGPendingMessages
bool HasSeen(const uint256& hash) const;
void Clear();

template<typename Message>
void PushPendingMessage(NodeId from, Message& msg)
{
CDataStream ds(SER_NETWORK, PROTOCOL_VERSION);
ds << msg;
PushPendingMessage(from, ds);
}

// Might return nullptr messages, which indicates that deserialization failed for some reason
template<typename Message>
std::vector<std::pair<NodeId, std::shared_ptr<Message>>> PopAndDeserializeMessages(size_t maxCount)
Expand Down

0 comments on commit 0b1347c

Please sign in to comment.