From fac42cd31d8497a121f0c002484620ba74e9562d Mon Sep 17 00:00:00 2001 From: Daniel Sanchez Date: Tue, 1 Aug 2023 16:47:47 +0200 Subject: [PATCH] Prune blocks in consensus engine (#275) * Add prune older blocks call * Prune older blocks in simulation app * Use latest safe view for failure case * Pick parent view from high qc * Add safety assertion on pruning method --- consensus-engine/src/lib.rs | 8 +++++++- simulations/src/node/carnot/mod.rs | 14 +++++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/consensus-engine/src/lib.rs b/consensus-engine/src/lib.rs index db467c34..371c8c26 100644 --- a/consensus-engine/src/lib.rs +++ b/consensus-engine/src/lib.rs @@ -220,7 +220,7 @@ impl Carnot { ) } - /// Upon a configurable amout of time has elapsed since the last view change + /// Upon a configurable amount of time has elapsed since the last view change /// /// Preconditions: none! /// Just notice that the timer only reset after a view change, i.e. a node can't timeout @@ -396,6 +396,12 @@ impl Carnot { Err(e) => Err(e), } } + + /// Blocks newer than the last committed block are not safe to be pruned + pub fn prune_older_blocks_by_view(&mut self, threshold_view: View) { + assert!(threshold_view < self.latest_committed_block().view); + self.safe_blocks.retain(|_, b| b.view < threshold_view); + } } #[cfg(test)] diff --git a/simulations/src/node/carnot/mod.rs b/simulations/src/node/carnot/mod.rs index efc8042b..5b6484a5 100644 --- a/simulations/src/node/carnot/mod.rs +++ b/simulations/src/node/carnot/mod.rs @@ -389,8 +389,20 @@ impl> CarnotNode qc.view, + Qc::Aggregated(qc) => { + self.engine + .safe_blocks() + .get(&qc.high_qc.id) + .expect("Parent block must be present") + .view + } + } - View::new(3); + let (mut new, out) = self.engine.approve_block(block); tracing::info!(vote=?out, node=%self.id); + // pruning old blocks older than the grandparent block needed to check validity + new.prune_older_blocks_by_view(block_grandparent_view); output = Some(Output::Send(out)); self.engine = new; }