Skip to content

Commit

Permalink
feat: add pruning related persistence API (#9232)
Browse files Browse the repository at this point in the history
  • Loading branch information
Rjected authored Jul 1, 2024
1 parent 116d7a3 commit 6eca557
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion crates/engine/tree/src/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,18 @@ impl<DB: Database> Persistence<DB> {
fn remove_blocks_above(&self, _block_number: u64) -> Vec<ExecutedBlock> {
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<DB> Persistence<DB>
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
}
}
}
}
Expand All @@ -161,6 +180,14 @@ pub enum PersistenceAction {

/// Removes the blocks above the given block number from the database.
RemoveBlocksAbove((u64, oneshot::Sender<Vec<ExecutedBlock>>)),

/// 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
Expand Down Expand Up @@ -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")
}
}

0 comments on commit 6eca557

Please sign in to comment.