Skip to content

Commit a0643df

Browse files
authored
feat: l1 reorg (#409)
* l1 event handling * l1 event handling * clean up * add test cases * cleanup * address feedback * use alloc String * use alloc ToString * fix L1 watcher revert batches * add revert batch signatures to watcher filter * fix database revert batch operation
1 parent f74b0c6 commit a0643df

38 files changed

+1740
-453
lines changed

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/chain-orchestrator/src/consolidation.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
use super::ChainOrchestratorError;
22
use alloy_provider::Provider;
33
use futures::{stream::FuturesOrdered, TryStreamExt};
4-
use rollup_node_primitives::{BatchConsolidationOutcome, BatchInfo, L2BlockInfoWithL1Messages};
4+
use rollup_node_primitives::{
5+
BatchConsolidationOutcome, BatchInfo, BatchStatus, L2BlockInfoWithL1Messages,
6+
};
57
use scroll_alloy_network::Scroll;
68
use scroll_derivation_pipeline::{BatchDerivationResult, DerivedAttributes};
79
use scroll_engine::{block_matches_attributes, ForkchoiceState};
@@ -53,7 +55,11 @@ pub(crate) async fn reconcile_batch<L2P: Provider<Scroll>>(
5355
}
5456

5557
let actions: Vec<BlockConsolidationAction> = futures.try_collect().await?;
56-
Ok(BatchReconciliationResult { batch_info: batch.batch_info, actions })
58+
Ok(BatchReconciliationResult {
59+
batch_info: batch.batch_info,
60+
actions,
61+
target_status: batch.target_status,
62+
})
5763
}
5864

5965
/// The result of reconciling a batch with the L2 chain.
@@ -63,6 +69,8 @@ pub(crate) struct BatchReconciliationResult {
6369
pub batch_info: BatchInfo,
6470
/// The actions that must be performed on the L2 chain to consolidate the batch.
6571
pub actions: Vec<BlockConsolidationAction>,
72+
/// The target status of the batch after consolidation.
73+
pub target_status: BatchStatus,
6674
}
6775

6876
impl BatchReconciliationResult {
@@ -93,7 +101,8 @@ impl BatchReconciliationResult {
93101
self,
94102
reorg_results: Vec<L2BlockInfoWithL1Messages>,
95103
) -> Result<BatchConsolidationOutcome, ChainOrchestratorError> {
96-
let mut consolidate_chain = BatchConsolidationOutcome::new(self.batch_info);
104+
let mut consolidate_chain =
105+
BatchConsolidationOutcome::new(self.batch_info, self.target_status);
97106

98107
// First append all non-reorg results to the consolidated chain.
99108
self.actions.into_iter().filter(|action| !action.is_reorg()).for_each(|action| {

crates/chain-orchestrator/src/event.rs

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,21 @@ pub enum ChainOrchestratorEvent {
4242
batch_info: BatchInfo,
4343
/// The L1 block number in which the batch was committed.
4444
l1_block_number: u64,
45-
/// The safe L2 block info.
46-
safe_head: Option<BlockInfo>,
4745
},
4846
/// A batch has been finalized returning a list of finalized batches.
49-
BatchFinalized(u64, Vec<BatchInfo>),
47+
BatchFinalized {
48+
/// The L1 block info at which the batch finalization event was received.
49+
l1_block_info: BlockInfo,
50+
/// The list of batches that have been triggered for the derivation pipeline.
51+
triggered_batches: Vec<BatchInfo>,
52+
},
53+
/// A batch has been reverted returning the batch info and the new safe head.
54+
BatchReverted {
55+
/// The latest batch info after the revert.
56+
batch_info: BatchInfo,
57+
/// The new safe head after the revert.
58+
safe_head: BlockInfo,
59+
},
5060
/// A new L1 block has been received returning the L1 block number.
5161
NewL1Block(u64),
5262
/// An L1 block has been finalized returning the L1 block number and the list of finalized

crates/chain-orchestrator/src/handle/command.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pub enum ChainOrchestratorCommand<N: FullNetwork<Primitives = ScrollNetworkPrimi
3030
/// Enable gossiping of blocks to peers.
3131
#[cfg(feature = "test-utils")]
3232
SetGossip((bool, oneshot::Sender<()>)),
33+
/// Returns a database handle for direct database access.
34+
#[cfg(feature = "test-utils")]
35+
DatabaseHandle(oneshot::Sender<std::sync::Arc<scroll_db::Database>>),
3336
}
3437

3538
/// The database queries that can be sent to the rollup manager.

crates/chain-orchestrator/src/handle/mod.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,4 +110,14 @@ impl<N: FullNetwork<Primitives = ScrollNetworkPrimitives>> ChainOrchestratorHand
110110
self.send_command(ChainOrchestratorCommand::SetGossip((enabled, tx)));
111111
rx.await
112112
}
113+
114+
/// Sends a command to the rollup manager to get a database handle for direct database access.
115+
#[cfg(feature = "test-utils")]
116+
pub async fn get_database_handle(
117+
&self,
118+
) -> Result<std::sync::Arc<scroll_db::Database>, oneshot::error::RecvError> {
119+
let (tx, rx) = oneshot::channel();
120+
self.send_command(ChainOrchestratorCommand::DatabaseHandle(tx));
121+
rx.await
122+
}
113123
}

0 commit comments

Comments
 (0)