Skip to content

Commit

Permalink
fix(derive): add more unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
refcell committed Apr 18, 2024
1 parent ab8548f commit ba17b26
Showing 1 changed file with 111 additions and 2 deletions.
113 changes: 111 additions & 2 deletions crates/derive/src/types/batch/span_batch/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -985,11 +985,120 @@ mod tests {

// TODO: Test missing l1 origin for empty batch

// TODO: Test batch exceeds sequencer time drift
#[tokio::test]
async fn test_check_batch_exceeds_max_seq_drif() {
let trace_store: TraceStorage = Default::default();
let layer = CollectingLayer::new(trace_store.clone());
tracing_subscriber::Registry::default().with(layer).init();

let cfg = RollupConfig {
seq_window_size: 100,
max_sequencer_drift: 0,
delta_time: Some(0),
block_time: 10,
..Default::default()
};
let l1_block_hash =
b256!("3333333333333333333333333333333333333333000000000000000000000000");
let block =
BlockInfo { number: 11, timestamp: 10, hash: l1_block_hash, ..Default::default() };
let second_block =
BlockInfo { number: 12, timestamp: 10, hash: l1_block_hash, ..Default::default() };
let l1_blocks = vec![block, second_block];
let parent_hash = b256!("1111111111111111111111111111111111111111000000000000000000000000");
let l2_safe_head = L2BlockInfo {
block_info: BlockInfo { number: 41, timestamp: 10, parent_hash, ..Default::default() },
l1_origin: BlockID { number: 9, ..Default::default() },
..Default::default()
};
let inclusion_block = BlockInfo { number: 50, ..Default::default() };
let l2_block = L2BlockInfo {
block_info: BlockInfo { number: 40, ..Default::default() },
..Default::default()
};
let mut fetcher = MockBlockFetcher { blocks: vec![l2_block], payloads: vec![] };
let first = SpanBatchElement { epoch_num: 10, timestamp: 20, ..Default::default() };
let second = SpanBatchElement { epoch_num: 10, timestamp: 20, ..Default::default() };
let third = SpanBatchElement { epoch_num: 11, timestamp: 20, ..Default::default() };
let batch = SpanBatch {
batches: vec![first, second, third],
parent_check: FixedBytes::<20>::from_slice(&parent_hash[..20]),
l1_origin_check: FixedBytes::<20>::from_slice(&l1_block_hash[..20]),
..Default::default()
};
assert_eq!(
batch.check_batch(&cfg, &l1_blocks, l2_safe_head, &inclusion_block, &mut fetcher).await,
BatchValidity::Drop
);
let logs = trace_store.get_by_level(Level::INFO);
assert_eq!(logs.len(), 1);
assert!(logs[0].contains("batch exceeded sequencer time drift without adopting next origin, and next L1 origin would have been valid"));
}

// TODO: Test continuing with empty batch info log

// TODO: Test batch exceeds sequencer time drift
#[tokio::test]
async fn test_check_batch_exceeds_sequencer_time_drift() {
let trace_store: TraceStorage = Default::default();
let layer = CollectingLayer::new(trace_store.clone());
tracing_subscriber::Registry::default().with(layer).init();

let cfg = RollupConfig {
seq_window_size: 100,
max_sequencer_drift: 0,
delta_time: Some(0),
block_time: 10,
..Default::default()
};
let l1_block_hash =
b256!("3333333333333333333333333333333333333333000000000000000000000000");
let block =
BlockInfo { number: 11, timestamp: 10, hash: l1_block_hash, ..Default::default() };
let second_block =
BlockInfo { number: 12, timestamp: 10, hash: l1_block_hash, ..Default::default() };
let l1_blocks = vec![block, second_block];
let parent_hash = b256!("1111111111111111111111111111111111111111000000000000000000000000");
let l2_safe_head = L2BlockInfo {
block_info: BlockInfo { number: 41, timestamp: 10, parent_hash, ..Default::default() },
l1_origin: BlockID { number: 9, ..Default::default() },
..Default::default()
};
let inclusion_block = BlockInfo { number: 50, ..Default::default() };
let l2_block = L2BlockInfo {
block_info: BlockInfo { number: 40, ..Default::default() },
..Default::default()
};
let mut fetcher = MockBlockFetcher { blocks: vec![l2_block], payloads: vec![] };
let first = SpanBatchElement {
epoch_num: 10,
timestamp: 20,
transactions: vec![Default::default()],
};
let second = SpanBatchElement {
epoch_num: 10,
timestamp: 20,
transactions: vec![Default::default()],
};
let third = SpanBatchElement {
epoch_num: 11,
timestamp: 20,
transactions: vec![Default::default()],
};
let batch = SpanBatch {
batches: vec![first, second, third],
parent_check: FixedBytes::<20>::from_slice(&parent_hash[..20]),
l1_origin_check: FixedBytes::<20>::from_slice(&l1_block_hash[..20]),
txs: SpanBatchTransactions::default(),
..Default::default()
};
assert_eq!(
batch.check_batch(&cfg, &l1_blocks, l2_safe_head, &inclusion_block, &mut fetcher).await,
BatchValidity::Drop
);
let logs = trace_store.get_by_level(Level::WARN);
assert_eq!(logs.len(), 1);
assert!(logs[0].contains("batch exceeded sequencer time drift, sequencer must adopt new L1 origin to include transactions again, max_time: 10"));
}

// TODO: Test empty transaction

Expand Down

0 comments on commit ba17b26

Please sign in to comment.