From 339455bf383a7b8abf2dacec19e4c0c9f30f68f2 Mon Sep 17 00:00:00 2001 From: Lukasz Rzasik Date: Thu, 12 Dec 2024 14:40:18 +0100 Subject: [PATCH] Use saturating_sub consistently --- crates/task-impls/src/network.rs | 11 ++++------- crates/task-impls/src/quorum_proposal/mod.rs | 12 ++++++++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/crates/task-impls/src/network.rs b/crates/task-impls/src/network.rs index 1ca2dc2379..a25bc47962 100644 --- a/crates/task-impls/src/network.rs +++ b/crates/task-impls/src/network.rs @@ -361,11 +361,7 @@ impl< /// Cancel all tasks for previous views pub fn cancel_tasks(&mut self, view: TYPES::View) { - if *view == 0 { - return; - } - let keep_view = view - 1; - let keep = self.transmit_tasks.split_off(&keep_view); + let keep = self.transmit_tasks.split_off(&view); while let Some((_, tasks)) = self.transmit_tasks.pop_first() { for task in tasks { @@ -695,12 +691,13 @@ impl< if epoch > self.epoch { self.epoch = epoch; } - self.cancel_tasks(view); + let keep_view = TYPES::View::new(view.saturating_sub(1)); + self.cancel_tasks(keep_view); let net = Arc::clone(&self.network); let epoch = self.epoch.u64(); let mem = self.membership.clone(); spawn(async move { - net.update_view::(view.saturating_sub(1), epoch, &mem) + net.update_view::(keep_view, epoch, &mem) .await; }); None diff --git a/crates/task-impls/src/quorum_proposal/mod.rs b/crates/task-impls/src/quorum_proposal/mod.rs index 49875c6480..7cef08bd38 100644 --- a/crates/task-impls/src/quorum_proposal/mod.rs +++ b/crates/task-impls/src/quorum_proposal/mod.rs @@ -498,8 +498,16 @@ impl, V: Versions> Arc::clone(&event), )?; } - HotShotEvent::ViewChange(view, _) | HotShotEvent::Timeout(view) => { - self.cancel_tasks(*view); + HotShotEvent::ViewChange(view, epoch) => { + if epoch > &self.cur_epoch { + self.cur_epoch = *epoch; + } + let keep_view = TYPES::View::new(view.saturating_sub(1)); + self.cancel_tasks(keep_view); + } + HotShotEvent::Timeout(view, ..) => { + let keep_view = TYPES::View::new(view.saturating_sub(1)); + self.cancel_tasks(keep_view); } HotShotEvent::HighQcSend(qc, ..) => { ensure!(qc.view_number() > self.highest_qc.view_number());