-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Implement managed tasks for SubscriptionManager * Fix clippy * Add comment about trace task * Use broadcast instead of mpsc for l2 blocks events * Fix spawns in DaService * Add TaskManager * Use task manager in sequencer * Document TaskManager * Handle shutdown event * Use TaskTracker * Add comment about using a cancellation token * Use JoinHandles instead of TaskTracker * Use TaskManager in fullnode and prover * Improve bitcoin-da service * Force spawned tasks to accept a cancellation token * Use biased polling * Satisfy clippy * WIP * Add pruning tables * Pruning skeleton implementation * Use pruner in nodes * Use biased polling based on order * WIP * Fix how config is done * Derive default * Add logs * Let the tasks finish without panicing * Use pruning config in fullnode and prover * Add simple run test * Use option instead of PruningMode * Unneccessary changes * l2_receiver * Cleanup prints * Use last pruned block in calculation * Implement pruning criteria * Lint and add comment * Set the last_pruned_block to up_to_block value * Don't store config internally * Remove from constructor * Should not change * Move config value * Remove pruning from sequencer / prover
- Loading branch information
Showing
21 changed files
with
301 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
[package] | ||
name = "citrea-pruning" | ||
version.workspace = true | ||
authors.workspace = true | ||
edition.workspace = true | ||
homepage.workspace = true | ||
license.workspace = true | ||
publish.workspace = true | ||
repository.workspace = true | ||
|
||
[dependencies] | ||
# Citrea dependencies | ||
citrea-evm = { path = "../evm", features = ["native"] } | ||
citrea-primitives = { path = "../primitives", features = ["native"] } | ||
|
||
# Sov SDK deps | ||
sov-db = { path = "../sovereign-sdk/full-node/db/sov-db" } | ||
sov-modules-api = { path = "../sovereign-sdk/module-system/sov-modules-api", default-features = false } | ||
|
||
# 3rd-party dependencies | ||
anyhow = { workspace = true } | ||
futures = { workspace = true } | ||
serde = { workspace = true } | ||
tokio = { workspace = true } | ||
tokio-util = { workspace = true } | ||
tracing = { workspace = true } | ||
|
||
[dev-dependencies] | ||
tempfile = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
/// This defines the interface of a pruning criteria. | ||
pub(crate) trait Criteria { | ||
/// Decides whether pruning should be done or not. | ||
/// | ||
/// If None is returned, no pruning should happen. | ||
/// Otherwise, it will return the `up_to_block` value. | ||
fn should_prune(&self, last_pruned_block: u64, current_block_number: u64) -> Option<u64>; | ||
} | ||
|
||
/// This distance criteria prunes blocks up to `last_pruned_block + distance`. | ||
/// However, to keep `distance` amount of blocks, we have to wait for at least twice | ||
/// the `distance` value to prune up to that point. | ||
pub(crate) struct DistanceCriteria { | ||
pub(crate) distance: u64, | ||
} | ||
|
||
impl Criteria for DistanceCriteria { | ||
fn should_prune(&self, last_pruned_block: u64, current_block_number: u64) -> Option<u64> { | ||
let trigger_block = last_pruned_block + (2 * self.distance) + 1; | ||
if current_block_number >= trigger_block { | ||
return Some(last_pruned_block + self.distance); | ||
} | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use criteria::DistanceCriteria; | ||
use futures::future; | ||
use serde::{Deserialize, Serialize}; | ||
use sov_db::ledger_db::SharedLedgerOps; | ||
use tokio::select; | ||
use tokio::sync::broadcast; | ||
use tokio_util::sync::CancellationToken; | ||
use tracing::{error, info}; | ||
|
||
use crate::criteria::Criteria; | ||
use crate::pruners::{prune_evm, prune_ledger}; | ||
|
||
mod criteria; | ||
mod pruners; | ||
#[cfg(test)] | ||
mod tests; | ||
|
||
/// A configuration type to define the behaviour of the pruner. | ||
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] | ||
pub struct PruningConfig { | ||
/// Defines the number of blocks from the tip of the chain to remove. | ||
pub distance: u64, | ||
} | ||
|
||
impl Default for PruningConfig { | ||
fn default() -> Self { | ||
Self { distance: 256 } | ||
} | ||
} | ||
|
||
pub struct Pruner<DB> | ||
where | ||
DB: SharedLedgerOps, | ||
{ | ||
/// The last block number which was pruned. | ||
last_pruned_block: u64, | ||
/// A channel receiver which gets notified of new L2 blocks. | ||
l2_receiver: broadcast::Receiver<u64>, | ||
/// Access to ledger tables. | ||
ledger_db: DB, | ||
/// Criteria to decide pruning | ||
criteria: Box<dyn Criteria + Send + Sync>, | ||
} | ||
|
||
impl<DB> Pruner<DB> | ||
where | ||
DB: SharedLedgerOps + Send + Sync + Clone + 'static, | ||
{ | ||
pub fn new( | ||
config: PruningConfig, | ||
last_pruned_block: u64, | ||
l2_receiver: broadcast::Receiver<u64>, | ||
ledger_db: DB, | ||
) -> Self { | ||
// distance is the only criteria implemented at the moment. | ||
let criteria = Box::new(DistanceCriteria { | ||
distance: config.distance, | ||
}); | ||
Self { | ||
last_pruned_block, | ||
l2_receiver, | ||
ledger_db, | ||
criteria, | ||
} | ||
} | ||
|
||
/// Prune everything | ||
pub async fn prune(&self, up_to_block: u64) { | ||
info!("Pruning up to L2 block: {}", up_to_block); | ||
let ledger_db = self.ledger_db.clone(); | ||
let ledger_pruning_handle = | ||
tokio::task::spawn_blocking(move || prune_ledger(ledger_db, up_to_block)); | ||
let evm_pruning_handle = tokio::task::spawn_blocking(move || prune_evm(up_to_block)); | ||
|
||
future::join_all([ledger_pruning_handle, evm_pruning_handle]).await; | ||
} | ||
|
||
pub async fn run(mut self, cancellation_token: CancellationToken) { | ||
loop { | ||
select! { | ||
biased; | ||
_ = cancellation_token.cancelled() => { | ||
// Store the last pruned l2 height in ledger DB to be restored in the next initialization. | ||
if let Err(e) = self.ledger_db.set_last_pruned_l2_height(self.last_pruned_block) { | ||
error!("Failed to store last pruned L2 height {}: {:?}", self.last_pruned_block, e); | ||
} | ||
return; | ||
} | ||
current_l2_block = self.l2_receiver.recv() => { | ||
if let Ok(current_l2_block) = current_l2_block { | ||
if let Some(up_to_block) = self.criteria.should_prune(self.last_pruned_block, current_l2_block) { | ||
self.prune(up_to_block).await; | ||
self.last_pruned_block = up_to_block; | ||
} | ||
} | ||
}, | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
use citrea_evm::Evm; | ||
use sov_modules_api::default_context::DefaultContext; | ||
use tracing::debug; | ||
|
||
/// Prune evm | ||
pub(crate) fn prune_evm(up_to_block: u64) { | ||
debug!("Pruning EVM, up to L2 block {}", up_to_block); | ||
let _evm = Evm::<DefaultContext>::default(); | ||
// unimplemented!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
use sov_db::ledger_db::SharedLedgerOps; | ||
use tracing::debug; | ||
|
||
/// Prune ledger | ||
pub(crate) fn prune_ledger<DB: SharedLedgerOps>(_ledger_db: DB, up_to_block: u64) { | ||
debug!("Pruning Ledger, up to L2 block {}", up_to_block); | ||
// unimplemented!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
mod evm; | ||
mod ledger; | ||
|
||
pub(crate) use evm::*; | ||
pub(crate) use ledger::*; |
Oops, something went wrong.