refactor: misc refactoring prefer std algorithm, range for loops; fix broken loop (#4593)

* fix: replace seemingly buggy loop with std::adjacent_find

* Remove redundant variable declaration

* use std::fill instead of a loop

* remove a few raw for loops
This commit is contained in:
PastaPastaPasta 2021-11-30 00:03:08 -05:00 committed by GitHub
parent ae98cd41af
commit b8c560479e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 14 additions and 28 deletions

View File

@ -379,30 +379,19 @@ std::set<NodeId> BatchVerifyMessageSigs(CDKGSession& session, const std::vector<
messageHashes.emplace_back(msgHash); messageHashes.emplace_back(msgHash);
} }
if (!revertToSingleVerification) { if (!revertToSingleVerification) {
bool valid = aggSig.VerifyInsecureAggregated(pubKeys, messageHashes); if (aggSig.VerifyInsecureAggregated(pubKeys, messageHashes)) {
if (valid) {
// all good // all good
return ret; return ret;
} }
// are all messages from the same node? // are all messages from the same node?
NodeId firstNodeId{-1}; bool nodeIdsAllSame = std::adjacent_find( messages.begin(), messages.end(), [](const auto& first, const auto& second){
first = true; return first.first != second.first;
bool nodeIdsAllSame = true; }) == messages.end();
for (auto it = messages.begin(); it != messages.end(); ++it) {
if (first) {
firstNodeId = it->first;
} else {
first = false;
if (it->first != firstNodeId) {
nodeIdsAllSame = false;
break;
}
}
}
// if yes, take a short path and return a set with only him // if yes, take a short path and return a set with only him
if (nodeIdsAllSame) { if (nodeIdsAllSame) {
ret.emplace(firstNodeId); ret.emplace(messages[0].first);
return ret; return ret;
} }
// different nodes, let's figure out who are the bad ones // different nodes, let's figure out who are the bad ones

View File

@ -88,9 +88,7 @@ void CSigSharesInv::Set(uint16_t quorumMember, bool v)
void CSigSharesInv::SetAll(bool v) void CSigSharesInv::SetAll(bool v)
{ {
for (size_t i = 0; i < inv.size(); i++) { std::fill(inv.begin(), inv.end(), v);
inv[i] = v;
}
} }
std::string CBatchedSigShares::ToInvString() const std::string CBatchedSigShares::ToInvString() const
@ -98,8 +96,8 @@ std::string CBatchedSigShares::ToInvString() const
CSigSharesInv inv; CSigSharesInv inv;
// we use 400 here no matter what the real size is. We don't really care about that size as we just want to call ToString() // we use 400 here no matter what the real size is. We don't really care about that size as we just want to call ToString()
inv.Init(400); inv.Init(400);
for (size_t i = 0; i < sigShares.size(); i++) { for (const auto& sigShare : sigShares) {
inv.inv[sigShares[i].first] = true; inv.inv[sigShare.first] = true;
} }
return inv.ToString(); return inv.ToString();
} }
@ -437,8 +435,8 @@ bool CSigSharesManager::ProcessMessageBatchedSigShares(const CNode* pfrom, const
LOCK(cs); LOCK(cs);
auto& nodeState = nodeStates[pfrom->GetId()]; auto& nodeState = nodeStates[pfrom->GetId()];
for (size_t i = 0; i < batchedSigShares.sigShares.size(); i++) { for (const auto& sigSharetmp : batchedSigShares.sigShares) {
CSigShare sigShare = RebuildSigShare(sessionInfo, batchedSigShares, i); CSigShare sigShare = RebuildSigShare(sessionInfo, sigSharetmp);
nodeState.requestedSigShares.Erase(sigShare.GetKey()); nodeState.requestedSigShares.Erase(sigShare.GetKey());
// TODO track invalid sig shares received for PoSe? // TODO track invalid sig shares received for PoSe?
@ -1250,10 +1248,9 @@ bool CSigSharesManager::GetSessionInfoByRecvId(NodeId nodeId, uint32_t sessionId
return nodeStates[nodeId].GetSessionInfoByRecvId(sessionId, retInfo); return nodeStates[nodeId].GetSessionInfoByRecvId(sessionId, retInfo);
} }
CSigShare CSigSharesManager::RebuildSigShare(const CSigSharesNodeState::SessionInfo& session, const CBatchedSigShares& batchedSigShares, size_t idx) CSigShare CSigSharesManager::RebuildSigShare(const CSigSharesNodeState::SessionInfo& session, const std::pair<uint16_t, CBLSLazySignature>& in)
{ {
assert(idx < batchedSigShares.sigShares.size()); const auto& [member, sig] = in;
const auto& [member, sig] = batchedSigShares.sigShares[idx];
CSigShare sigShare; CSigShare sigShare;
sigShare.llmqType = session.llmqType; sigShare.llmqType = session.llmqType;
sigShare.quorumHash = session.quorumHash; sigShare.quorumHash = session.quorumHash;

View File

@ -421,7 +421,7 @@ private:
void TryRecoverSig(const CQuorumCPtr& quorum, const uint256& id, const uint256& msgHash); void TryRecoverSig(const CQuorumCPtr& quorum, const uint256& id, const uint256& msgHash);
bool GetSessionInfoByRecvId(NodeId nodeId, uint32_t sessionId, CSigSharesNodeState::SessionInfo& retInfo); bool GetSessionInfoByRecvId(NodeId nodeId, uint32_t sessionId, CSigSharesNodeState::SessionInfo& retInfo);
static CSigShare RebuildSigShare(const CSigSharesNodeState::SessionInfo& session, const CBatchedSigShares& batchedSigShares, size_t idx); static CSigShare RebuildSigShare(const CSigSharesNodeState::SessionInfo& session, const std::pair<uint16_t, CBLSLazySignature>& in);
void Cleanup(); void Cleanup();
void RemoveSigSharesForSession(const uint256& signHash) EXCLUSIVE_LOCKS_REQUIRED(cs); void RemoveSigSharesForSession(const uint256& signHash) EXCLUSIVE_LOCKS_REQUIRED(cs);