Skip to content

Commit

Permalink
chore(protocol): test output frame
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Feb 7, 2025
1 parent b8840db commit c661556
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
33 changes: 33 additions & 0 deletions crates/protocol/src/channel_out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,39 @@ mod tests {
use super::*;
use crate::{test_utils::MockCompressor, CompressorWriter, SingleBatch, SpanBatch};

#[test]
fn test_output_frame_max_size_too_small() {
let config = RollupConfig::default();
let mut channel = ChannelOut::new(ChannelId::default(), &config, MockCompressor::default());
assert_eq!(channel.output_frame(0), Err(ChannelOutError::MaxFrameSizeTooSmall));
}

#[test]
fn test_channel_out_output_frame_no_data() {
let config = RollupConfig::default();
let mut channel = ChannelOut::new(
ChannelId::default(),
&config,
MockCompressor { read_error: true, compressed: Some(Default::default()) },
);
let err = channel.output_frame(FRAME_V0_OVERHEAD).unwrap_err();
assert_eq!(err, ChannelOutError::Compression(CompressorError::Full));
}

#[test]
fn test_channel_out_output() {
let config = RollupConfig::default();
let mut channel = ChannelOut::new(
ChannelId::default(),
&config,
MockCompressor { compressed: Some(Default::default()), ..Default::default() },
);
let frame = channel.output_frame(FRAME_V0_OVERHEAD).unwrap();
assert_eq!(frame.id, ChannelId::default());
assert_eq!(frame.number, 0);
assert!(!frame.is_last);
}

#[test]
fn test_channel_out_reset() {
let config = RollupConfig::default();
Expand Down
10 changes: 8 additions & 2 deletions crates/protocol/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use tracing::{Event, Level, Subscriber};
use tracing_subscriber::{layer::Context, Layer};

use crate::{
BatchValidationProvider, ChannelCompressor, CompressorResult, CompressorWriter,
L1BlockInfoBedrock, L1BlockInfoEcotone, L1BlockInfoInterop, L1BlockInfoIsthmus, L2BlockInfo,
BatchValidationProvider, ChannelCompressor, CompressorError, CompressorResult,
CompressorWriter, L1BlockInfoBedrock, L1BlockInfoEcotone, L1BlockInfoInterop,
L1BlockInfoIsthmus, L2BlockInfo,
};

/// Raw encoded bedrock L1 block info transaction.
Expand All @@ -30,6 +31,8 @@ pub const RAW_INTEROP_INFO_TX: [u8; L1BlockInfoInterop::L1_INFO_TX_LEN] = hex!("
pub struct MockCompressor {
/// Compressed bytes
pub compressed: Option<Bytes>,
/// Whether to throw a read error.
pub read_error: bool,
}

impl CompressorWriter for MockCompressor {
Expand Down Expand Up @@ -57,6 +60,9 @@ impl CompressorWriter for MockCompressor {
}

fn read(&mut self, buf: &mut [u8]) -> CompressorResult<usize> {
if self.read_error {
return Err(CompressorError::Full);
}
let len = self.compressed.as_ref().map(|b| b.len()).unwrap_or(0);
buf[..len].copy_from_slice(self.compressed.as_ref().unwrap());
Ok(len)
Expand Down

0 comments on commit c661556

Please sign in to comment.