From 19139b810aa637746f13e1c255682a2757bf2921 Mon Sep 17 00:00:00 2001 From: refcell Date: Wed, 9 Oct 2024 13:49:38 -0400 Subject: [PATCH] chore(derive): stage coverage (#673) --- crates/derive/src/stages/batch_queue.rs | 52 ++++++++++++++++++ crates/derive/src/stages/channel_bank.rs | 70 ++++++++++++++++++++++++ 2 files changed, 122 insertions(+) diff --git a/crates/derive/src/stages/batch_queue.rs b/crates/derive/src/stages/batch_queue.rs index 7b9cf9594..46257d4fc 100644 --- a/crates/derive/src/stages/batch_queue.rs +++ b/crates/derive/src/stages/batch_queue.rs @@ -515,6 +515,20 @@ mod tests { BatchReader::from(bytes) } + #[test] + fn test_pop_next_batch() { + let cfg = Arc::new(RollupConfig::default()); + let mock = MockBatchQueueProvider::new(vec![]); + let fetcher = TestL2ChainProvider::default(); + let mut bq = BatchQueue::new(cfg, mock, fetcher); + let parent = L2BlockInfo::default(); + let sb = SingleBatch::default(); + bq.next_spans.push(sb.clone()); + let next = bq.pop_next_batch(parent).unwrap(); + assert_eq!(next, sb); + assert!(bq.next_spans.is_empty()); + } + #[tokio::test] async fn test_batch_queue_reset() { let cfg = Arc::new(RollupConfig::default()); @@ -854,6 +868,44 @@ mod tests { assert!(logs[0].contains(warn_str)); } + #[tokio::test] + async fn test_next_batch_cached_single_batch() { + let mut reader = new_batch_reader(); + let cfg = Arc::new(RollupConfig::default()); + let mut batch_vec: Vec> = vec![]; + while let Some(batch) = reader.next_batch(cfg.as_ref()) { + batch_vec.push(Ok(batch)); + } + let mut mock = MockBatchQueueProvider::new(batch_vec); + mock.origin = Some(BlockInfo::default()); + let fetcher = TestL2ChainProvider::default(); + let mut bq = BatchQueue::new(cfg, mock, fetcher); + let sb = SingleBatch::default(); + bq.next_spans.push(sb.clone()); + let next = bq.next_batch(L2BlockInfo::default()).await.unwrap(); + assert_eq!(next, sb); + assert!(bq.next_spans.is_empty()); + } + + #[tokio::test] + async fn test_next_batch_clear_next_spans() { + let mut reader = new_batch_reader(); + let cfg = Arc::new(RollupConfig { block_time: 100, ..Default::default() }); + let mut batch_vec: Vec> = vec![]; + while let Some(batch) = reader.next_batch(cfg.as_ref()) { + batch_vec.push(Ok(batch)); + } + let mut mock = MockBatchQueueProvider::new(batch_vec); + mock.origin = Some(BlockInfo::default()); + let fetcher = TestL2ChainProvider::default(); + let mut bq = BatchQueue::new(cfg, mock, fetcher); + let sb = SingleBatch::default(); + bq.next_spans.push(sb.clone()); + let res = bq.next_batch(L2BlockInfo::default()).await.unwrap_err(); + assert_eq!(res, PipelineError::NotEnoughData.temp()); + assert!(bq.is_last_in_span()); + } + #[tokio::test] async fn test_next_batch_not_enough_data() { let mut reader = new_batch_reader(); diff --git a/crates/derive/src/stages/channel_bank.rs b/crates/derive/src/stages/channel_bank.rs index cb66afa0a..7a6a8cf71 100644 --- a/crates/derive/src/stages/channel_bank.rs +++ b/crates/derive/src/stages/channel_bank.rs @@ -369,6 +369,76 @@ mod tests { ); } + #[test] + fn test_read_channel_canyon_not_active() { + let mock = MockChannelBankProvider::new(vec![]); + let cfg = Arc::new(RollupConfig::default()); + let mut channel_bank = ChannelBank::new(cfg, mock); + let id: ChannelId = [0xFF; 16]; + channel_bank.channel_queue.push_back(id); + let mut channel = Channel::new(id, BlockInfo::default()); + channel + .add_frame( + Frame { id, number: 0, data: b"seven__".to_vec(), is_last: false }, + BlockInfo::default(), + ) + .unwrap(); + channel + .add_frame( + Frame { id, number: 1, data: b"seven__".to_vec(), is_last: false }, + BlockInfo::default(), + ) + .unwrap(); + channel + .add_frame( + Frame { id, number: 2, data: b"seven__".to_vec(), is_last: true }, + BlockInfo::default(), + ) + .unwrap(); + assert!(channel.is_ready()); + channel_bank.channels.insert([0xFF; 16], channel); + let frame_data = channel_bank.read().unwrap(); + assert_eq!( + frame_data, + Some(alloy_primitives::bytes!("736576656e5f5f736576656e5f5f736576656e5f5f")) + ); + } + + #[test] + fn test_read_channel_active() { + let mock = MockChannelBankProvider::new(vec![]); + let cfg = Arc::new(RollupConfig { canyon_time: Some(0), ..Default::default() }); + let mut channel_bank = ChannelBank::new(cfg, mock); + let id: ChannelId = [0xFF; 16]; + channel_bank.channel_queue.push_back(id); + let mut channel = Channel::new(id, BlockInfo::default()); + channel + .add_frame( + Frame { id, number: 0, data: b"seven__".to_vec(), is_last: false }, + BlockInfo::default(), + ) + .unwrap(); + channel + .add_frame( + Frame { id, number: 1, data: b"seven__".to_vec(), is_last: false }, + BlockInfo::default(), + ) + .unwrap(); + channel + .add_frame( + Frame { id, number: 2, data: b"seven__".to_vec(), is_last: true }, + BlockInfo::default(), + ) + .unwrap(); + assert!(channel.is_ready()); + channel_bank.channels.insert([0xFF; 16], channel); + let frame_data = channel_bank.read().unwrap(); + assert_eq!( + frame_data, + Some(alloy_primitives::bytes!("736576656e5f5f736576656e5f5f736576656e5f5f")) + ); + } + #[test] fn test_ingest_empty_origin() { let mut mock = MockChannelBankProvider::new(vec![]);