kona_protocol/batch/
type.rs

1//! Batch Types
2//!
3//! This module contains the batch types for the OP Stack derivation pipeline.
4//!
5//! ## Batch
6//!
7//! A batch is either a `SpanBatch` or a `SingleBatch`.
8//!
9//! The batch type is encoded as a single byte:
10//! - `0x00` for a `SingleBatch`
11//! - `0x01` for a `SpanBatch`
12
13use alloy_rlp::{Decodable, Encodable};
14
15/// The single batch type identifier.
16pub const SINGLE_BATCH_TYPE: u8 = 0x00;
17
18/// The span batch type identifier.
19pub const SPAN_BATCH_TYPE: u8 = 0x01;
20
21/// The Batch Type.
22#[derive(Debug, Clone, PartialEq, Eq)]
23#[repr(u8)]
24pub enum BatchType {
25    /// Single Batch.
26    Single = SINGLE_BATCH_TYPE,
27    /// Span Batch.
28    Span = SPAN_BATCH_TYPE,
29}
30
31impl From<u8> for BatchType {
32    fn from(val: u8) -> Self {
33        match val {
34            SINGLE_BATCH_TYPE => Self::Single,
35            SPAN_BATCH_TYPE => Self::Span,
36            _ => panic!("Invalid batch type: {val}"),
37        }
38    }
39}
40
41impl Encodable for BatchType {
42    fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
43        let val = match self {
44            Self::Single => SINGLE_BATCH_TYPE,
45            Self::Span => SPAN_BATCH_TYPE,
46        };
47        val.encode(out);
48    }
49}
50
51impl Decodable for BatchType {
52    fn decode(buf: &mut &[u8]) -> alloy_rlp::Result<Self> {
53        let val = u8::decode(buf)?;
54        Ok(Self::from(val))
55    }
56}
57
58#[cfg(test)]
59mod test {
60    use super::*;
61    use alloc::vec::Vec;
62
63    #[test]
64    fn test_batch_type_rlp_roundtrip() {
65        let batch_type = BatchType::Single;
66        let mut buf = Vec::new();
67        batch_type.encode(&mut buf);
68        let decoded = BatchType::decode(&mut buf.as_slice()).unwrap();
69        assert_eq!(batch_type, decoded);
70    }
71}