Skip to content

Commit

Permalink
feat(protocol): Batch Validity (#187)
Browse files Browse the repository at this point in the history
### Description

Part of a port migrating the batch types from `kona-derive` to
`op-alloy`. See
[`kona#695`](anton-rs/kona#695).

This PR introduces the `BatchValidity` type.
  • Loading branch information
refcell authored Oct 27, 2024
1 parent 8ab745d commit 242d739
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
3 changes: 3 additions & 0 deletions crates/protocol/src/batch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

mod r#type;
pub use r#type::*;

mod validity;
pub use validity::BatchValidity;
53 changes: 53 additions & 0 deletions crates/protocol/src/batch/validity.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}
2 changes: 1 addition & 1 deletion crates/protocol/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down

0 comments on commit 242d739

Please sign in to comment.