From 6eca557dbe6cd51e0956e110ae928856b3022773 Mon Sep 17 00:00:00 2001 From: Dan Cline <6798349+Rjected@users.noreply.github.com> Date: Mon, 1 Jul 2024 17:02:39 -0400 Subject: [PATCH] feat: add pruning related persistence API (#9232) --- crates/engine/tree/src/persistence.rs | 49 ++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) diff --git a/crates/engine/tree/src/persistence.rs b/crates/engine/tree/src/persistence.rs index cbbe50d051fe..6b1afe94d3ab 100644 --- a/crates/engine/tree/src/persistence.rs +++ b/crates/engine/tree/src/persistence.rs @@ -105,6 +105,18 @@ impl Persistence { fn remove_blocks_above(&self, _block_number: u64) -> Vec { todo!("implement this") } + + /// Prunes block data before the given block hash according to the configured prune + /// configuration. + fn prune_before(&self, _block_hash: B256) { + todo!("implement this") + } + + /// Removes static file related data from the database, depending on the current block height in + /// existing static files. + fn clean_static_file_duplicates(&self) { + todo!("implement this") + } } impl Persistence @@ -135,7 +147,6 @@ where while let Ok(action) = self.incoming.recv() { match action { PersistenceAction::RemoveBlocksAbove((new_tip_num, sender)) => { - // spawn blocking so we can poll the thread later let output = self.remove_blocks_above(new_tip_num); sender.send(output).unwrap(); } @@ -147,6 +158,14 @@ where self.write(blocks).unwrap(); sender.send(last_block_hash).unwrap(); } + PersistenceAction::PruneBefore((block_hash, sender)) => { + self.prune_before(block_hash); + sender.send(()).unwrap(); + } + PersistenceAction::CleanStaticFileDuplicates(sender) => { + self.clean_static_file_duplicates(); + sender.send(()).unwrap(); + } } } } @@ -161,6 +180,14 @@ pub enum PersistenceAction { /// Removes the blocks above the given block number from the database. RemoveBlocksAbove((u64, oneshot::Sender>)), + + /// Prune associated block data before the given hash, according to already-configured prune + /// modes. + PruneBefore((B256, oneshot::Sender<()>)), + + /// Trigger a read of static file data, and delete data depending on the highest block in each + /// static file segment. + CleanStaticFileDuplicates(oneshot::Sender<()>), } /// A handle to the persistence task @@ -198,4 +225,24 @@ impl PersistenceHandle { .expect("should be able to send"); rx.await.expect("todo: err handling") } + + /// Tells the persistence task to remove block data before the given hash, according to the + /// configured prune config. + pub async fn prune_before(&self, block_hash: B256) { + let (tx, rx) = oneshot::channel(); + self.sender + .send(PersistenceAction::PruneBefore((block_hash, tx))) + .expect("should be able to send"); + rx.await.expect("todo: err handling") + } + + /// Tells the persistence task to read static file data, and delete data depending on the + /// highest block in each static file segment. + pub async fn clean_static_file_duplicates(&self) { + let (tx, rx) = oneshot::channel(); + self.sender + .send(PersistenceAction::CleanStaticFileDuplicates(tx)) + .expect("should be able to send"); + rx.await.expect("todo: err handling") + } }