From 38ada6f93e0f0738dde40c86d8bb85deb3da11fc Mon Sep 17 00:00:00 2001 From: Savinda Senevirathne Date: Mon, 28 Jun 2021 17:03:58 +0530 Subject: [PATCH] Fork condition detection fix. (#328) --- src/consensus.cpp | 28 ++++++++++++---------------- src/consensus.hpp | 14 ++++++++++++++ 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/src/consensus.cpp b/src/consensus.cpp index 78c19b3c..7f6e6a6b 100644 --- a/src/consensus.cpp +++ b/src/consensus.cpp @@ -161,6 +161,8 @@ namespace consensus // and we need to make the final vote status check after proper pruning rules are applied. LOG_DEBUG << "Rechecking vote status after becoming in-sync."; + // Reset the voter for the new votes. + votes.reset(); revise_candidate_proposals(true); new_sync_status = check_sync_status(unl_count, votes, lcl_id); } @@ -995,9 +997,13 @@ namespace consensus } } - // If winning last primary shard hash is not matched with our last primary shard hash, that means we are not on the consensus ledger. - // If that's the case we should request shards straight away. - if (ledger::ctx.get_last_primary_shard_id() != majority_primary_shard_id) + const uint32_t min_wins_required = ceil(MAJORITY_THRESHOLD * ctx.candidate_proposals.size()); + if (winning_votes < min_wins_required) + { + LOG_INFO << "No consensus on last shard hash. Possible fork condition. won:" << winning_votes << " needed:" << min_wins_required; + return false; + } + else if (ledger::ctx.get_last_primary_shard_id() != majority_primary_shard_id) { LOG_INFO << "We are not on the consensus ledger, we must request history from a peer."; is_desync = true; @@ -1005,19 +1011,9 @@ namespace consensus } else { - // Check wheher there are enough winning votes for the last shard to be reliable. - const uint32_t min_wins_required = ceil(MAJORITY_THRESHOLD * ctx.candidate_proposals.size()); - if (winning_votes < min_wins_required) - { - LOG_INFO << "No consensus on last shard hash. Possible fork condition. won:" << winning_votes << " needed:" << min_wins_required; - return false; - } - else - { - // Reaching here means we have reliable amount of winning last shard hash votes and our last shard hash matches with majority last shard hash. - is_desync = false; - return true; - } + // Reaching here means we have reliable amount of winning last shard hash votes and our last shard hash matches with majority last shard hash. + is_desync = false; + return true; } } diff --git a/src/consensus.hpp b/src/consensus.hpp index 2a22b23d..12f662c8 100644 --- a/src/consensus.hpp +++ b/src/consensus.hpp @@ -146,6 +146,20 @@ namespace consensus std::map patch_hash; std::map last_ledger_primary_shard; std::map last_ledger_raw_shard; + + void reset() + { + time.clear(); + nonce.clear(); + lcl.clear(); + users.clear(); + inputs.clear(); + output_hash.clear(); + state_hash.clear(); + patch_hash.clear(); + last_ledger_primary_shard.clear(); + last_ledger_raw_shard.clear(); + } }; extern std::atomic is_patch_update_pending; // Keep track whether the patch file is changed by the SC and is not yet applied to runtime.