Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consensus: fix proposal early timeout #2735

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions consensus/src/execution_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl<'a, T: Operations + 'static, DB: Database> ExecutionCtx<'a, T, DB> {
pub async fn event_loop<C: MsgHandler>(
&mut self,
phase: Arc<Mutex<C>>,
additional_timeout: Option<Duration>,
) -> Result<Message, ConsensusError> {
let open_consensus_mode = self.last_step_running();

Expand All @@ -141,8 +142,8 @@ impl<'a, T: Operations + 'static, DB: Database> ExecutionCtx<'a, T, DB> {
dur
} else {
let dur = self.iter_ctx.get_timeout(self.step_name());
debug!(event = "run event_loop", ?dur);
dur
debug!(event = "run event_loop", ?dur, ?additional_timeout);
dur + additional_timeout.unwrap_or_default()
};

let deadline = Instant::now().checked_add(timeout).unwrap();
Expand Down
38 changes: 22 additions & 16 deletions consensus/src/proposal/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ impl<T: Operations + 'static, D: Database> ProposalStep<T, D> {
.get_current_committee()
.expect("committee to be created before run");

let tip_timestamp = ctx.round_update.timestamp();

if ctx.am_member(committee) {
let iteration =
cmp::min(config::RELAX_ITERATION_THRESHOLD, ctx.iteration);
Expand Down Expand Up @@ -88,10 +90,7 @@ impl<T: Operations + 'static, D: Database> ProposalStep<T, D> {
.await
{
Ok(HandleMsgOutput::Ready(msg)) => {
Self::wait_until_next_slot(
ctx.round_update.timestamp(),
)
.await;
Self::wait_until_next_slot(tip_timestamp).await;
return Ok(msg);
}
Err(e) => {
Expand All @@ -106,14 +105,17 @@ impl<T: Operations + 'static, D: Database> ProposalStep<T, D> {

// handle queued messages for current round and step.
if let Some(m) = ctx.handle_future_msgs(self.handler.clone()).await {
Self::wait_until_next_slot(ctx.round_update.timestamp()).await;
Self::wait_until_next_slot(tip_timestamp).await;
return Ok(m);
}

match ctx.event_loop(self.handler.clone()).await {
let additional_timeout = Self::next_slot_in(tip_timestamp);
match ctx
.event_loop(self.handler.clone(), additional_timeout)
.await
{
Ok(msg) => {
Self::wait_until_next_slot(ctx.round_update.timestamp()).await;

Self::wait_until_next_slot(tip_timestamp).await;
Ok(msg)
}
Err(err) => Err(err),
Expand All @@ -122,19 +124,23 @@ impl<T: Operations + 'static, D: Database> ProposalStep<T, D> {

/// Waits until the next slot is reached
async fn wait_until_next_slot(tip_timestamp: u64) {
if let Some(delay) = Self::next_slot_in(tip_timestamp) {
info!(event = "next_slot", ?delay);
tokio::time::sleep(delay).await;
}
}

/// Calculate the duration needed to the next slot
fn next_slot_in(tip_timestamp: u64) -> Option<Duration> {
let current_time_secs = get_current_timestamp();

let next_slot_timestamp = tip_timestamp + MINIMUM_BLOCK_TIME;
if current_time_secs >= next_slot_timestamp {
return;
None
} else {
// block_timestamp - localtime
Some(Duration::from_secs(next_slot_timestamp - current_time_secs))
}

// block_timestamp - localtime
let delay =
Duration::from_secs(next_slot_timestamp - current_time_secs);

info!(event = "next_slot", ?delay);
tokio::time::sleep(delay).await;
}

pub fn name(&self) -> &'static str {
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/ratification/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl RatificationStep {
return Ok(m);
}

ctx.event_loop(self.handler.clone()).await
ctx.event_loop(self.handler.clone(), None).await
}

pub fn name(&self) -> &'static str {
Expand Down
2 changes: 1 addition & 1 deletion consensus/src/validation/step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl<T: Operations + 'static> ValidationStep<T> {
return Ok(m);
}

ctx.event_loop(self.handler.clone()).await
ctx.event_loop(self.handler.clone(), None).await
}

pub fn name(&self) -> &'static str {
Expand Down
Loading