Skip to content

Commit

Permalink
Make leader wait at start of round
Browse files Browse the repository at this point in the history
In the is_leader branch, insert a configurable wait period before starting the pre-commit phase in
earnest, so that the network has time to propose some transactions.

Closes #26
  • Loading branch information
nmccarty committed Jul 12, 2021
1 parent 2ff725f commit 254b760
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions examples/dentry-simulator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ async fn get_phaselock(
known_nodes,
next_view_timeout: 10000,
timeout_ratio: (11, 10),
round_start_delay: 1,
};
debug!(?config);
let genesis = DEntryBlock::default();
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ use std::sync::Arc;
use std::time::Duration;

use async_std::sync::RwLock;
use async_std::task::{spawn, yield_now, JoinHandle};
use async_std::task::{sleep, spawn, yield_now, JoinHandle};
use dashmap::DashMap;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use snafu::ResultExt;
Expand Down Expand Up @@ -214,6 +214,8 @@ pub struct PhaseLockConfig {
pub next_view_timeout: u64,
/// The exponential backoff ration for the next-view timeout
pub timeout_ratio: (u64, u64),
/// The delay a leader inserts before starting pre-commit, in milliseconds
pub round_start_delay: u64,
}

/// Holds the state needed to participate in `PhaseLock` consensus
Expand Down Expand Up @@ -484,6 +486,10 @@ impl<B: BlockContents<N> + Sync + Send + 'static, const N: usize> PhaseLock<B, N
let the_block;
let the_hash;
if is_leader {
// Insert our artificial delay to allow transactions to come in
let round_start_delay = Duration::from_millis(self.inner.config.round_start_delay);
sleep(round_start_delay).await;

// Prepare our block
let mut block = B::next_block(&state);
// spin while the transaction_queue is empty
Expand Down
2 changes: 2 additions & 0 deletions tests/failures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ async fn single_permanent_failure() {
known_nodes,
next_view_timeout: 1_000,
timeout_ratio: (11, 10),
round_start_delay: 1,
};
debug!(?config);
let gensis = DEntryBlock::default();
Expand Down Expand Up @@ -190,6 +191,7 @@ async fn double_permanent_failure() {
known_nodes,
next_view_timeout: 800,
timeout_ratio: (15, 10),
round_start_delay: 1,
};
debug!(?config);
let gensis = DEntryBlock::default();
Expand Down
2 changes: 2 additions & 0 deletions tests/smoke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ async fn ten_tx_seven_nodes() {
known_nodes,
next_view_timeout: 10_000,
timeout_ratio: (11, 10),
round_start_delay: 1,
};
debug!(?config);
let gensis = DEntryBlock::default();
Expand Down Expand Up @@ -159,6 +160,7 @@ async fn ten_tx_five_nodes() {
known_nodes,
next_view_timeout: 10_000,
timeout_ratio: (11, 10),
round_start_delay: 1,
};
debug!(?config);
let gensis = DEntryBlock::default();
Expand Down

0 comments on commit 254b760

Please sign in to comment.