kona_protocol/batch/
inclusion.rs

1//! Module containing the [BatchWithInclusionBlock] struct.
2
3use crate::{Batch, BatchValidationProvider, BatchValidity, BlockInfo, L2BlockInfo};
4use kona_genesis::RollupConfig;
5
6/// A batch with its inclusion block.
7#[derive(Debug, Clone, PartialEq, Eq)]
8pub struct BatchWithInclusionBlock {
9    /// The inclusion block
10    pub inclusion_block: BlockInfo,
11    /// The batch
12    pub batch: Batch,
13}
14
15impl BatchWithInclusionBlock {
16    /// Creates a new batch with inclusion block.
17    pub const fn new(inclusion_block: BlockInfo, batch: Batch) -> Self {
18        Self { inclusion_block, batch }
19    }
20
21    /// Validates the batch can be applied on top of the specified L2 safe head.
22    /// The first entry of the l1_blocks should match the origin of the l2_safe_head.
23    /// One or more consecutive l1_blocks should be provided.
24    /// In case of only a single L1 block, the decision whether a batch is valid may have to stay
25    /// undecided.
26    pub async fn check_batch<BF: BatchValidationProvider>(
27        &self,
28        cfg: &RollupConfig,
29        l1_blocks: &[BlockInfo],
30        l2_safe_head: L2BlockInfo,
31        fetcher: &mut BF,
32    ) -> BatchValidity {
33        match &self.batch {
34            Batch::Single(single_batch) => {
35                single_batch.check_batch(cfg, l1_blocks, l2_safe_head, &self.inclusion_block)
36            }
37            Batch::Span(span_batch) => {
38                span_batch
39                    .check_batch(cfg, l1_blocks, l2_safe_head, &self.inclusion_block, fetcher)
40                    .await
41            }
42        }
43    }
44}
45
46#[cfg(test)]
47mod tests {
48    use super::*;
49    use crate::test_utils::TestBatchValidator;
50    use alloc::vec;
51
52    #[tokio::test]
53    async fn test_single_batch_with_inclusion_block() {
54        let batch =
55            BatchWithInclusionBlock::new(BlockInfo::default(), Batch::Single(Default::default()));
56        let l1_blocks = vec![BlockInfo::default()];
57        let l2_safe_head = L2BlockInfo::default();
58        let cfg = RollupConfig::default();
59        let mut validator = TestBatchValidator::default();
60        let result = batch.check_batch(&cfg, &l1_blocks, l2_safe_head, &mut validator).await;
61        assert_eq!(result, BatchValidity::Accept);
62    }
63
64    #[tokio::test]
65    async fn test_span_batch_with_inclusion_block() {
66        let batch =
67            BatchWithInclusionBlock::new(BlockInfo::default(), Batch::Span(Default::default()));
68        let l1_blocks = vec![BlockInfo::default()];
69        let l2_safe_head = L2BlockInfo::default();
70        let cfg = RollupConfig::default();
71        let mut validator = TestBatchValidator::default();
72        let result = batch.check_batch(&cfg, &l1_blocks, l2_safe_head, &mut validator).await;
73        assert_eq!(result, BatchValidity::Undecided);
74    }
75}