From 0e837433c6e8248aed4d004a803a9ae47207edf5 Mon Sep 17 00:00:00 2001 From: raychu86 <14917648+raychu86@users.noreply.github.com> Date: Tue, 21 May 2024 11:51:34 -0700 Subject: [PATCH] Clear solutions queue and worker solutions on new epoch --- node/bft/src/helpers/ready.rs | 8 ++++++++ node/bft/src/primary.rs | 7 +++++++ node/bft/src/worker.rs | 7 +++++++ node/consensus/src/lib.rs | 8 ++++++++ 4 files changed, 30 insertions(+) diff --git a/node/bft/src/helpers/ready.rs b/node/bft/src/helpers/ready.rs index 6fa581ccdb..7a366dff81 100644 --- a/node/bft/src/helpers/ready.rs +++ b/node/bft/src/helpers/ready.rs @@ -126,6 +126,14 @@ impl Ready { // Drain the transmission IDs. transmissions.drain(range).collect::>() } + + /// Clears all solutions from the ready queue. + pub(crate) fn clear_solutions(&self) { + // Acquire the write lock. + let mut transmissions = self.transmissions.write(); + // Remove all solutions. + transmissions.retain(|id, _| !matches!(id, TransmissionID::Solution(..))); + } } #[cfg(test)] diff --git a/node/bft/src/primary.rs b/node/bft/src/primary.rs index 05069d2aa4..d90576a2f9 100644 --- a/node/bft/src/primary.rs +++ b/node/bft/src/primary.rs @@ -319,6 +319,13 @@ impl Primary { } } +impl Primary { + /// Clears the worker solutions. + pub fn clear_worker_solutions(&self) { + self.workers.iter().for_each(Worker::clear_solutions); + } +} + impl Primary { /// Proposes the batch for the current round. /// diff --git a/node/bft/src/worker.rs b/node/bft/src/worker.rs index dc82d775ce..96a3f71622 100644 --- a/node/bft/src/worker.rs +++ b/node/bft/src/worker.rs @@ -151,6 +151,13 @@ impl Worker { } } +impl Worker { + /// Clears the solutions from the ready queue. + pub(super) fn clear_solutions(&self) { + self.ready.clear_solutions() + } +} + impl Worker { /// Returns `true` if the transmission ID exists in the ready queue, proposed batch, storage, or ledger. pub fn contains_transmission(&self, transmission_id: impl Into>) -> bool { diff --git a/node/consensus/src/lib.rs b/node/consensus/src/lib.rs index 530893893d..fdf247dfdd 100644 --- a/node/consensus/src/lib.rs +++ b/node/consensus/src/lib.rs @@ -484,6 +484,14 @@ impl Consensus { // Advance to the next block. self.ledger.advance_to_next_block(&next_block)?; + // If the next block starts a new epoch, clear the existing solutions. + if next_block.height() % N::NUM_BLOCKS_PER_EPOCH == 0 { + // Clear the solutions queue. + self.solutions_queue.lock().clear(); + // Clear the worker solutions. + self.bft.primary().clear_worker_solutions(); + } + #[cfg(feature = "metrics")] { let elapsed = std::time::Duration::from_secs((snarkos_node_bft::helpers::now() - start) as u64);