op_alloy_protocol/batch/
core.rsuse crate::{BatchDecodingError, BatchType, RawSpanBatch, SingleBatch, SpanBatch};
use alloy_rlp::{Buf, Decodable};
use op_alloy_genesis::RollupConfig;
#[derive(Debug, Clone, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum Batch {
Single(SingleBatch),
Span(SpanBatch),
}
impl Batch {
pub fn timestamp(&self) -> u64 {
match self {
Self::Single(sb) => sb.timestamp,
Self::Span(sb) => sb.starting_timestamp(),
}
}
pub fn decode(r: &mut &[u8], cfg: &RollupConfig) -> Result<Self, BatchDecodingError> {
if r.is_empty() {
return Err(BatchDecodingError::EmptyBuffer);
}
let batch_type = BatchType::from(r[0]);
r.advance(1);
match batch_type {
BatchType::Single => {
let single_batch =
SingleBatch::decode(r).map_err(BatchDecodingError::AlloyRlpError)?;
Ok(Self::Single(single_batch))
}
BatchType::Span => {
let mut raw_span_batch = RawSpanBatch::decode(r)?;
let span_batch = raw_span_batch
.derive(cfg.block_time, cfg.genesis.l2_time, cfg.l2_chain_id)
.map_err(BatchDecodingError::SpanBatchError)?;
Ok(Self::Span(span_batch))
}
}
}
}