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

[Optimize] Clear solutions queue and worker solutions on new epoch #3266

Merged
merged 1 commit into from
May 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
8 changes: 8 additions & 0 deletions node/bft/src/helpers/ready.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,14 @@ impl<N: Network> Ready<N> {
// Drain the transmission IDs.
transmissions.drain(range).collect::<IndexMap<_, _>>()
}

/// 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)]
Expand Down
7 changes: 7 additions & 0 deletions node/bft/src/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ impl<N: Network> Primary<N> {
}
}

impl<N: Network> Primary<N> {
/// Clears the worker solutions.
pub fn clear_worker_solutions(&self) {
self.workers.iter().for_each(Worker::clear_solutions);
}
}

impl<N: Network> Primary<N> {
/// Proposes the batch for the current round.
///
Expand Down
7 changes: 7 additions & 0 deletions node/bft/src/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ impl<N: Network> Worker<N> {
}
}

impl<N: Network> Worker<N> {
/// Clears the solutions from the ready queue.
pub(super) fn clear_solutions(&self) {
self.ready.clear_solutions()
}
}

impl<N: Network> Worker<N> {
/// 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<TransmissionID<N>>) -> bool {
Expand Down
8 changes: 8 additions & 0 deletions node/consensus/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,14 @@ impl<N: Network> Consensus<N> {
// 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);
Expand Down