Skip to content

Commit

Permalink
refactor: a new struct CDKGJustification::Contribution instead std::pair
Browse files Browse the repository at this point in the history
  • Loading branch information
knst committed Nov 14, 2023
1 parent 2541457 commit 56f958c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
22 changes: 10 additions & 12 deletions src/llmq/dkgsession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,7 +674,7 @@ void CDKGSession::SendJustification(CDKGPendingMessages& pendingMessages, const
qj.proTxHash = myProTxHash;
qj.contributions.reserve(forMembers.size());

for (const auto i : irange::range(members.size())) {
for (const uint32_t i : irange::range(members.size())) {
const auto& m = members[i];
if (forMembers.count(m->dmn->proTxHash) == 0) {
continue;
Expand All @@ -688,7 +688,7 @@ void CDKGSession::SendJustification(CDKGPendingMessages& pendingMessages, const
skContribution.MakeNewKey();
}

qj.contributions.emplace_back(i, skContribution);
qj.contributions.emplace_back(CDKGJustification::Contribution{i, skContribution});
}

if (ShouldSimulateError(DKGError::type::JUSTIFY_OMIT)) {
Expand Down Expand Up @@ -735,19 +735,19 @@ bool CDKGSession::PreVerifyMessage(const CDKGJustification& qj, bool& retBan) co

std::set<size_t> contributionsSet;
for (const auto& p : qj.contributions) {
if (p.first > members.size()) {
if (p.index > members.size()) {
logger.Batch("invalid contribution index");
retBan = true;
return false;
}

if (!contributionsSet.emplace(p.first).second) {
if (!contributionsSet.emplace(p.index).second) {
logger.Batch("duplicate contribution index");
retBan = true;
return false;
}

const auto& skShare = p.second;
const auto& skShare = p.key;
if (!skShare.IsValid()) {
logger.Batch("invalid contribution");
retBan = true;
Expand Down Expand Up @@ -810,7 +810,7 @@ void CDKGSession::ReceiveMessage(const CDKGJustification& qj, bool& retBan)
}

for (const auto& p : qj.contributions) {
const auto& member2 = members[p.first];
const auto& member2 = members[p.index];

if (member->complaintsFromOthers.count(member2->dmn->proTxHash) == 0) {
logger.Batch("got justification from %s for %s even though he didn't complain",
Expand All @@ -825,17 +825,15 @@ void CDKGSession::ReceiveMessage(const CDKGJustification& qj, bool& retBan)
cxxtimer::Timer t1(true);

std::list<std::future<bool>> futures;
for (const auto& p : qj.contributions) {
const auto& member2 = members[p.first];
const auto& skContribution = p.second;
for (const auto& [index, skContribution] : qj.contributions) {
const auto& member2 = members[index];

// watch out to not bail out before these async calls finish (they rely on valid references)
futures.emplace_back(blsWorker.AsyncVerifyContributionShare(member2->id, receivedVvecs[member->idx], skContribution));
}
auto resultIt = futures.begin();
for (const auto& p : qj.contributions) {
const auto& member2 = members[p.first];
const auto& skContribution = p.second;
for (const auto& [index, skContribution] : qj.contributions) {
const auto& member2 = members[index];

bool result = (resultIt++)->get();
if (!result) {
Expand Down
11 changes: 9 additions & 2 deletions src/llmq/dkgsession.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,15 @@ class CDKGJustification
Consensus::LLMQType llmqType;
uint256 quorumHash;
uint256 proTxHash;
// TODO make this pair a struct with named fields
std::vector<std::pair<uint32_t, CBLSSecretKey>> contributions;
struct Contribution {
uint32_t index;
CBLSSecretKey key;
SERIALIZE_METHODS(Contribution, obj)
{
READWRITE(obj.index, obj.key);
}
};
std::vector<Contribution> contributions;
CBLSSignature sig;

public:
Expand Down

0 comments on commit 56f958c

Please sign in to comment.