From 7bf6e77c66aff5ba1f467e5cc65ff6ab1644d9df Mon Sep 17 00:00:00 2001 From: refcell Date: Fri, 25 Oct 2024 15:55:12 -0400 Subject: [PATCH 1/4] feat: batch type --- crates/protocol/src/batch_type.rs | 77 +++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 crates/protocol/src/batch_type.rs diff --git a/crates/protocol/src/batch_type.rs b/crates/protocol/src/batch_type.rs new file mode 100644 index 000000000..df4169924 --- /dev/null +++ b/crates/protocol/src/batch_type.rs @@ -0,0 +1,77 @@ +//! Batch Types +//! +//! This module contains the batch types for the OP Stack derivation pipeline. +//! +//! ## Batch +//! +//! A batch is either a `SpanBatch` or a `SingleBatch`. +//! +//! The batch type is encoded as a single byte: +//! - `0x00` for a `SingleBatch` +//! - `0x01` for a `SpanBatch` + +use alloy_rlp::{Decodable, Encodable}; + +/// The single batch type identifier. +pub const SINGLE_BATCH_TYPE: u8 = 0x00; + +/// The span batch type identifier. +pub const SPAN_BATCH_TYPE: u8 = 0x01; + +/// The Batch Type. +#[derive(Debug, Clone, PartialEq, Eq)] +#[repr(u8)] +pub enum BatchType { + /// Single Batch. + Single = SINGLE_BATCH_TYPE, + /// Span Batch. + Span = SPAN_BATCH_TYPE, +} + +impl From for BatchType { + fn from(val: u8) -> Self { + match val { + SINGLE_BATCH_TYPE => Self::Single, + SPAN_BATCH_TYPE => Self::Span, + _ => panic!("Invalid batch type: {val}"), + } + } +} + +impl From<&[u8]> for BatchType { + fn from(buf: &[u8]) -> Self { + Self::from(buf[0]) + } +} + +impl Encodable for BatchType { + fn encode(&self, out: &mut dyn alloy_rlp::BufMut) { + let val = match self { + Self::Single => SINGLE_BATCH_TYPE, + Self::Span => SPAN_BATCH_TYPE, + }; + val.encode(out); + } +} + +impl Decodable for BatchType { + fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { + let val = u8::decode(buf)?; + Ok(Self::from(val)) + } +} + +#[cfg(test)] +mod test { + use super::*; + use alloc::vec::Vec; + + #[test] + fn test_batch_type_rlp_roundtrip() { + let batch_type = BatchType::Single; + let mut buf = Vec::new(); + batch_type.encode(&mut buf); + let decoded = BatchType::decode(&mut buf.as_slice()).unwrap(); + assert_eq!(batch_type, decoded); + } +} From d22ca72abea611efbf33d6d2c7cd1c4eaea8bdf3 Mon Sep 17 00:00:00 2001 From: refcell Date: Fri, 25 Oct 2024 16:10:56 -0400 Subject: [PATCH 2/4] fix: move into a subdir --- crates/protocol/src/batch_type.rs | 77 ------------------------------- 1 file changed, 77 deletions(-) delete mode 100644 crates/protocol/src/batch_type.rs diff --git a/crates/protocol/src/batch_type.rs b/crates/protocol/src/batch_type.rs deleted file mode 100644 index df4169924..000000000 --- a/crates/protocol/src/batch_type.rs +++ /dev/null @@ -1,77 +0,0 @@ -//! Batch Types -//! -//! This module contains the batch types for the OP Stack derivation pipeline. -//! -//! ## Batch -//! -//! A batch is either a `SpanBatch` or a `SingleBatch`. -//! -//! The batch type is encoded as a single byte: -//! - `0x00` for a `SingleBatch` -//! - `0x01` for a `SpanBatch` - -use alloy_rlp::{Decodable, Encodable}; - -/// The single batch type identifier. -pub const SINGLE_BATCH_TYPE: u8 = 0x00; - -/// The span batch type identifier. -pub const SPAN_BATCH_TYPE: u8 = 0x01; - -/// The Batch Type. -#[derive(Debug, Clone, PartialEq, Eq)] -#[repr(u8)] -pub enum BatchType { - /// Single Batch. - Single = SINGLE_BATCH_TYPE, - /// Span Batch. - Span = SPAN_BATCH_TYPE, -} - -impl From for BatchType { - fn from(val: u8) -> Self { - match val { - SINGLE_BATCH_TYPE => Self::Single, - SPAN_BATCH_TYPE => Self::Span, - _ => panic!("Invalid batch type: {val}"), - } - } -} - -impl From<&[u8]> for BatchType { - fn from(buf: &[u8]) -> Self { - Self::from(buf[0]) - } -} - -impl Encodable for BatchType { - fn encode(&self, out: &mut dyn alloy_rlp::BufMut) { - let val = match self { - Self::Single => SINGLE_BATCH_TYPE, - Self::Span => SPAN_BATCH_TYPE, - }; - val.encode(out); - } -} - -impl Decodable for BatchType { - fn decode(buf: &mut &[u8]) -> alloy_rlp::Result { - let val = u8::decode(buf)?; - Ok(Self::from(val)) - } -} - -#[cfg(test)] -mod test { - use super::*; - use alloc::vec::Vec; - - #[test] - fn test_batch_type_rlp_roundtrip() { - let batch_type = BatchType::Single; - let mut buf = Vec::new(); - batch_type.encode(&mut buf); - let decoded = BatchType::decode(&mut buf.as_slice()).unwrap(); - assert_eq!(batch_type, decoded); - } -} From cda485ca0be5f1114893fd1c72a3972873a25864 Mon Sep 17 00:00:00 2001 From: refcell Date: Fri, 25 Oct 2024 15:59:39 -0400 Subject: [PATCH 3/4] feat: batch validity --- crates/protocol/src/batch/mod.rs | 3 ++ crates/protocol/src/batch/validity.rs | 53 +++++++++++++++++++++++++++ crates/protocol/src/lib.rs | 2 +- 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 crates/protocol/src/batch/validity.rs 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..9cfce4294 --- /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 + 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 if the batch is accepted. + pub const fn is_accept(&self) -> bool { + matches!(self, Self::Accept) + } + + /// Returns if the batch is dropped. + pub const fn is_drop(&self) -> bool { + matches!(self, Self::Drop) + } + + /// Returns if the batch is outdated. + pub const fn is_outdated(&self) -> bool { + matches!(self, Self::Past) + } + + /// Returns if 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}; From 202e6175063f4d4eedf1ef8947640c7279ebd050 Mon Sep 17 00:00:00 2001 From: refcell Date: Sun, 27 Oct 2024 18:21:36 -0400 Subject: [PATCH 4/4] fix: validity: --- crates/protocol/src/batch/validity.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/crates/protocol/src/batch/validity.rs b/crates/protocol/src/batch/validity.rs index 9cfce4294..1f68d0c52 100644 --- a/crates/protocol/src/batch/validity.rs +++ b/crates/protocol/src/batch/validity.rs @@ -4,7 +4,7 @@ #[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 + /// 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, @@ -12,28 +12,28 @@ pub enum BatchValidity { 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 + /// 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 if the batch is accepted. + /// Returns whether the batch is accepted. pub const fn is_accept(&self) -> bool { matches!(self, Self::Accept) } - /// Returns if the batch is dropped. + /// Returns whether the batch is dropped. pub const fn is_drop(&self) -> bool { matches!(self, Self::Drop) } - /// Returns if the batch is outdated. + /// Returns whether the batch is outdated. pub const fn is_outdated(&self) -> bool { matches!(self, Self::Past) } - /// Returns if the batch is future. + /// Returns whether the batch is future. pub const fn is_future(&self) -> bool { matches!(self, Self::Future) }