Fix loop in CLLMQUtils::GetQuorumConnections to add at least 2 connections (#2796)

* Fix warning about size_t to int conversion

* Fix loop in CLLMQUtils::GetQuorumConnections to add at least 2 connections

When reaching very small quorum sizes, the current algorithm results in
only a single connection to be added. This would be fine usually, but is an
issue when this connection fails. We should always have at least one backup
connection.

This fixes simple PoSe test failures where the quorum size gets down to 4
with one of the 4 members being down. If other nodes are unlucky to connect
to this node, they fail as well even though 3 members in a quorum should
work fine.

* Update src/llmq/quorums_utils.cpp

Co-Authored-By: codablock <ablock84@gmail.com>
This commit is contained in:
Alexander Block 2019-03-22 11:51:12 +01:00 committed by GitHub
parent 071b60deda
commit 02db06658e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -50,10 +50,13 @@ std::set<CService> CLLMQUtils::GetQuorumConnections(Consensus::LLMQType llmqType
for (size_t i = 0; i < mns.size(); i++) {
auto& dmn = mns[i];
if (dmn->proTxHash == forMember) {
// Connect to nodes at indexes (i+2^k)%n, k: 0..floor(log2(n-1))-1, n: size of the quorum/ring
// Connect to nodes at indexes (i+2^k)%n, where
// k: 0..max(1, floor(log2(n-1))-1)
// n: size of the quorum/ring
int gap = 1;
int gap_max = mns.size() - 1;
while (gap_max >>= 1) {
int gap_max = (int)mns.size() - 1;
int k = 0;
while ((gap_max >>= 1) || k <= 1) {
size_t idx = (i + gap) % mns.size();
auto& otherDmn = mns[idx];
if (otherDmn == dmn) {
@ -61,6 +64,7 @@ std::set<CService> CLLMQUtils::GetQuorumConnections(Consensus::LLMQType llmqType
}
result.emplace(otherDmn->pdmnState->addr);
gap <<= 1;
k++;
}
// there can be no two members with the same proTxHash, so return early
break;