diff --git a/crates/protocol/src/batch/mod.rs b/crates/protocol/src/batch/mod.rs index 6c1020137..571793d45 100644 --- a/crates/protocol/src/batch/mod.rs +++ b/crates/protocol/src/batch/mod.rs @@ -2,3 +2,6 @@ mod r#type; pub use r#type::*; + +mod validity; +pub use validity::BatchValidity; diff --git a/crates/protocol/src/batch/validity.rs b/crates/protocol/src/batch/validity.rs new file mode 100644 index 000000000..1f68d0c52 --- /dev/null +++ b/crates/protocol/src/batch/validity.rs @@ -0,0 +1,53 @@ +//! Contains the [BatchValidity] and its encodings. + +/// Batch Validity +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BatchValidity { + /// The batch is invalid now and in the future, unless we reorg, so it can be discarded. + Drop, + /// The batch is valid and should be processed + Accept, + /// We are lacking L1 information until we can proceed batch filtering + Undecided, + /// The batch may be valid, but cannot be processed yet and should be checked again later + Future, + /// Introduced in Holocene, a special variant of the `Drop` variant that signals not to flush + /// the active batch and channel, in the case of processing an old batch + Past, +} + +impl BatchValidity { + /// Returns whether the batch is accepted. + pub const fn is_accept(&self) -> bool { + matches!(self, Self::Accept) + } + + /// Returns whether the batch is dropped. + pub const fn is_drop(&self) -> bool { + matches!(self, Self::Drop) + } + + /// Returns whether the batch is outdated. + pub const fn is_outdated(&self) -> bool { + matches!(self, Self::Past) + } + + /// Returns whether the batch is future. + pub const fn is_future(&self) -> bool { + matches!(self, Self::Future) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_batch_validity() { + assert!(BatchValidity::Accept.is_accept()); + assert!(BatchValidity::Drop.is_drop()); + assert!(BatchValidity::Past.is_outdated()); + assert!(BatchValidity::Future.is_future()); + } +} diff --git a/crates/protocol/src/lib.rs b/crates/protocol/src/lib.rs index a0f7fa082..8e1542139 100644 --- a/crates/protocol/src/lib.rs +++ b/crates/protocol/src/lib.rs @@ -10,7 +10,7 @@ extern crate alloc; mod batch; -pub use batch::{BatchType, SINGLE_BATCH_TYPE, SPAN_BATCH_TYPE}; +pub use batch::{BatchType, BatchValidity, SINGLE_BATCH_TYPE, SPAN_BATCH_TYPE}; mod block; pub use block::{BlockInfo, FromBlockError, L2BlockInfo};