From b46423bdce2966c7afef3269e89f29c23455ca0d Mon Sep 17 00:00:00 2001 From: Luis Rubio Date: Mon, 23 Nov 2020 16:29:12 +0100 Subject: [PATCH] feat(superblock): spread supervotes along the superepoch --- node/src/actors/chain_manager/mod.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/node/src/actors/chain_manager/mod.rs b/node/src/actors/chain_manager/mod.rs index 951eae0f1..0b5d4d99f 100644 --- a/node/src/actors/chain_manager/mod.rs +++ b/node/src/actors/chain_manager/mod.rs @@ -41,6 +41,7 @@ use ansi_term::Color::{Purple, White, Yellow}; use failure::Fail; use futures::future::{join_all, Future}; use itertools::Itertools; +use rand::Rng; use witnet_crypto::{hash::calculate_sha256, key::CryptoEngine}; use witnet_data_structures::{ chain::{ @@ -744,11 +745,21 @@ impl ChainManager { }) .into_actor(act) .map(|res, act, ctx| { - // Broadcast vote one epoch checkpoint later. + // Broadcast vote between one and ("superblock_period" - 3) epoch checkpoints later. // This is used to prevent the race condition described in issue #1573 + // It is also used to spread the CPU load by checking superblock votes along + // the superblock period with a safe margin + let mut rng = rand::thread_rng(); let checkpoints_period = act.consensus_constants().checkpoints_period; + let superblock_period = act.consensus_constants().superblock_period; + let end_range = if superblock_period > 4 { + (superblock_period - 3) * checkpoints_period + } else { + checkpoints_period + 1 + }; + let random_waiting = rng.gen_range(checkpoints_period, end_range); ctx.run_later( - Duration::from_secs(u64::from(checkpoints_period)), + Duration::from_secs(u64::from(random_waiting)), |act, ctx| match act.add_superblock_vote(res, ctx) { Ok(()) => (), Err(e) => {