From 164595c4d741188d472d3a69878916b1f153be3d Mon Sep 17 00:00:00 2001 From: chalith Date: Fri, 29 Jul 2022 13:51:37 +0530 Subject: [PATCH] Calculate majority threshold when needed --- examples/c_contract/hotpocket_contract.h | 6 +++--- src/consensus.cpp | 27 ++++++++++-------------- 2 files changed, 14 insertions(+), 19 deletions(-) diff --git a/examples/c_contract/hotpocket_contract.h b/examples/c_contract/hotpocket_contract.h index caf6f9e9..4bbbb5bb 100644 --- a/examples/c_contract/hotpocket_contract.h +++ b/examples/c_contract/hotpocket_contract.h @@ -205,8 +205,8 @@ struct hp_contract_context char contract_id[HP_CONTRACT_ID_SIZE + 1]; // +1 for null char. struct hp_public_key public_key; struct hp_private_key private_key; - uint64_t lcl_seq_no; // lcl sequence no. - char lcl_hash[HP_HASH_SIZE + 1]; // +1 for null char. + uint64_t lcl_seq_no; // lcl sequence no. + char lcl_hash[HP_HASH_SIZE + 1]; // +1 for null char. struct hp_users_collection users; struct hp_unl_collection unl; }; @@ -924,7 +924,7 @@ void __hp_populate_patch_from_json_object(struct hp_config *config, const struct if (sub_ele->value->type == json_type_string) { const struct json_string_s *value = (struct json_string_s *)sub_ele->value->payload; - config->npl.mode = (strcmp(value->string, "public") == 0) ? PUBLIC : PRIVATE; + config->consensus.mode = (strcmp(value->string, "public") == 0) ? PUBLIC : PRIVATE; } } sub_ele = sub_ele->next; diff --git a/src/consensus.cpp b/src/consensus.cpp index 1b5f837a..5b7918ba 100644 --- a/src/consensus.cpp +++ b/src/consensus.cpp @@ -23,16 +23,13 @@ namespace p2pmsg = msg::fbuf::p2pmsg; namespace consensus { + constexpr float STAGE_THRESHOLD_RATIO[3] = {0.5, 0.65, 0.8}; constexpr size_t ROUND_NONCE_SIZE = 64; constexpr const char *HPFS_SESSION_NAME = "ro_patch_file_to_hp"; // Max no. of time to get unreliable votes before we try heuristics to increase vote receiving reliability. constexpr uint16_t MAX_UNRELIABLE_VOTES_ATTEMPTS = 5; - float stage_threshold[3] = {}; - constexpr float STAGE_THRESHOLD_RATIO[3] = {0.5, 0.65, 0.8}; - double majority_threshold; - consensus_context ctx; bool init_success = false; std::atomic is_patch_update_pending = false; // Keep track whether the patch file is changed by the SC and is not yet applied to runtime. @@ -45,14 +42,6 @@ namespace consensus ctx.consensus_thread = std::thread(run_consensus); init_success = true; - - // To maintain 0.5, 0.65, 0.8 ratio. - majority_threshold = conf::cfg.contract.consensus.threshold / 100.0; - - // Creating a array similar to {0.5, 0.65, 0.8} with correct ratio values. - stage_threshold[0] = (STAGE_THRESHOLD_RATIO[0] * majority_threshold) / STAGE_THRESHOLD_RATIO[2];; - stage_threshold[1] = (STAGE_THRESHOLD_RATIO[1] * majority_threshold) / STAGE_THRESHOLD_RATIO[2]; - stage_threshold[2] = majority_threshold; return 0; } @@ -270,7 +259,8 @@ namespace consensus cp_root_hash.try_emplace(cp.root_hash, cp); } } - const uint32_t min_votes_required = ceil(majority_threshold * unl::count()); + // Threshold is devided by 100 to convert average to decimal. + const uint32_t min_votes_required = ceil((conf::cfg.contract.consensus.threshold * unl::count()) / 100.0); if (stage3_prop_count < min_votes_required) { // We don't have enough stage 3 proposals to create a ledger. @@ -987,7 +977,10 @@ namespace consensus increment(votes.output_hash, cp.output_hash); } - uint32_t required_votes = ceil(stage_threshold[ctx.stage - 1] * unl_count); + // Compute the stage tresholds using ratios. + // Threshold is devided by 100 to convert average to decimal. + const float stage_threshold = ((STAGE_THRESHOLD_RATIO[ctx.stage - 1] * conf::cfg.contract.consensus.threshold) / (STAGE_THRESHOLD_RATIO[2] * 100.0)); + uint32_t required_votes = ceil(stage_threshold * unl_count); // todo: check if inputs being proposed by another node are actually spoofed inputs // from a user locally connected to this node. @@ -1005,7 +998,8 @@ namespace consensus p.input_ordered_hashes.emplace(hash); // Reset required votes for majority votes. - required_votes = ceil(majority_threshold * unl_count); + // Threshold is devided by 100 to convert average to decimal. + required_votes = ceil((conf::cfg.contract.consensus.threshold * unl_count) / 100.0); // Add the output hash which has most votes over stage threshold to proposal. uint32_t highest_output_vote = 0; @@ -1082,7 +1076,8 @@ namespace consensus } // Check whether we have received enough votes in total. - const uint32_t min_votes_required = ceil(majority_threshold * unl_count); + // Threshold is devided by 100 to convert average to decimal. + const uint32_t min_votes_required = ceil((conf::cfg.contract.consensus.threshold / 100.0) * unl_count); if (total_ledger_primary_hash_votes < min_votes_required) { LOG_INFO << "Not enough peers proposing to perform consensus. votes:" << total_ledger_primary_hash_votes << " needed:" << min_votes_required;