From b4210200050955837aac2960b7dd75432308e69a Mon Sep 17 00:00:00 2001 From: Nikita Masych Date: Wed, 11 Sep 2024 05:15:53 +0300 Subject: [PATCH] fix: changed scheduling to instant --- src/config.rs | 7 ++++--- src/party.rs | 6 ++---- tests/mod.rs | 2 +- 3 files changed, 7 insertions(+), 8 deletions(-) diff --git a/src/config.rs b/src/config.rs index 572f8bb..e880b29 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,6 +1,7 @@ //! Definitions central to BPCon configuration. use std::time::Duration; +use tokio::time::Instant; /// Configuration structure for BPCon. /// @@ -22,11 +23,11 @@ pub struct BPConConfig { /// The quorum is the minimum weight required to make decisions in the BPCon protocol. pub threshold: u128, - /// Timeout before the ballot is launched. + /// Absolute time, at which party begins to work. /// /// This timeout differs from `launch1a_timeout` as it applies to a distinct status /// and does not involve listening to external events and messages. - pub launch_timeout: Duration, + pub launch_at: Instant, /// Timeout before the 1a stage is launched. /// @@ -96,7 +97,7 @@ impl BPConConfig { party_weights, threshold, // TODO: deduce actually good defaults. - launch_timeout: Duration::from_millis(0), + launch_at: Instant::now(), launch1a_timeout: Duration::from_millis(200), launch1b_timeout: Duration::from_millis(400), launch2a_timeout: Duration::from_millis(600), diff --git a/src/party.rs b/src/party.rs index 75a23db..7e78c8a 100644 --- a/src/party.rs +++ b/src/party.rs @@ -25,7 +25,7 @@ use std::cmp::PartialEq; use std::collections::hash_map::Entry::Vacant; use std::collections::{HashMap, HashSet}; use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver, UnboundedSender}; -use tokio::time::sleep; +use tokio::time::{sleep, sleep_until}; /// Represents the status of a `Party` in the BPCon consensus protocol. /// @@ -256,7 +256,7 @@ impl> Party { pub async fn launch_ballot(&mut self) -> Result { self.prepare_next_ballot()?; - sleep(self.cfg.launch_timeout).await; + sleep_until(self.cfg.launch_at).await; let launch1a_timer = sleep(self.cfg.launch1a_timeout); let launch1b_timer = sleep(self.cfg.launch1b_timeout); @@ -939,8 +939,6 @@ mod tests { _ = party.launch_ballot().await; }); - time::advance(cfg.launch_timeout).await; - // Sequential time advance and event check. time::advance(cfg.launch1a_timeout).await; diff --git a/tests/mod.rs b/tests/mod.rs index 4967d10..e81927f 100644 --- a/tests/mod.rs +++ b/tests/mod.rs @@ -267,7 +267,7 @@ async fn test_ballot_many_parties() { let cfg = BPConConfig { party_weights, threshold, - launch_timeout: Duration::from_secs(0), + launch_at: Instant::now(), launch1a_timeout: Duration::from_secs(0), // 1a's and 2a's are sent only by leader launch1b_timeout: Duration::from_secs(1), // meaning we need to wait less. launch2a_timeout: Duration::from_secs(5),