From 94eedcaf5b47e641e7a15d841f1576ebece2051a Mon Sep 17 00:00:00 2001 From: Ravin Perera <33562092+ravinsp@users.noreply.github.com> Date: Fri, 5 Feb 2021 17:48:21 +0530 Subject: [PATCH] Added roundtime offset based on contract id. (#238) --- src/consensus.cpp | 7 ++++++- src/consensus.hpp | 1 + 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/consensus.cpp b/src/consensus.cpp index 2bb4ca5d..b1c2d707 100644 --- a/src/consensus.cpp +++ b/src/consensus.cpp @@ -34,6 +34,11 @@ namespace consensus ctx.stage_time = conf::cfg.contract.roundtime / 4; ctx.stage_reset_wait_threshold = conf::cfg.contract.roundtime / 10; + // We use a time window boundry offset based on contract id to vary the window boundries between + // different contracts with same round time. + std::hash str_hasher; + ctx.round_boundry_offset = str_hasher(conf::cfg.contract.id) % conf::cfg.contract.roundtime; + // Starting consensus processing thread. ctx.consensus_thread = std::thread(run_consensus); @@ -306,7 +311,7 @@ namespace consensus if (ctx.stage == 0) { // This gets the start time of current round window. Stage 0 must start in the window after that. - const uint64_t previous_round_start = (((uint64_t)(now / conf::cfg.contract.roundtime)) * conf::cfg.contract.roundtime); + const uint64_t previous_round_start = (((uint64_t)((now - ctx.round_boundry_offset) / conf::cfg.contract.roundtime)) * conf::cfg.contract.roundtime) + ctx.round_boundry_offset; // Stage 0 must start in the next round window. // (This makes sure stage 3 gets whichever the remaining time in the round after stages 0,1,2) diff --git a/src/consensus.hpp b/src/consensus.hpp index d6e9863f..d1534cd6 100644 --- a/src/consensus.hpp +++ b/src/consensus.hpp @@ -71,6 +71,7 @@ namespace consensus uint64_t round_start_time = 0; uint16_t stage_time = 0; // Time allocated to a consensus stage. uint16_t stage_reset_wait_threshold = 0; // Minimum stage wait time to reset the stage. + uint64_t round_boundry_offset = 0; // Time window boundry offset based on contract id. std::optional contract_ctx; std::mutex contract_ctx_mutex;