Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
clabby committed Sep 30, 2024
1 parent 4c8c8da commit 70b3227
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
23 changes: 11 additions & 12 deletions crates/derive/src/batch/span_batch/batch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ impl SpanBatch {
&self,
cfg: &RollupConfig,
l1_origins: &[BlockInfo],
l2_safe_head: L2BlockInfo,
l2_safe_head: BlockInfo,
fetcher: &mut BF,
) -> BatchValidity {
if l1_origins.is_empty() {
Expand All @@ -361,41 +361,40 @@ impl SpanBatch {
return BatchValidity::Undecided;
}

let next_timestamp = l2_safe_head.block_info.timestamp + cfg.block_time;
let next_timestamp = l2_safe_head.timestamp + cfg.block_time;

// Find the parent block of the span batch.
// If the span batch does not overlap the current safe chain, parent block should be the L2
// safe head.
let mut parent_num = l2_safe_head.block_info.number;
let mut parent_num = l2_safe_head.number;
let mut parent_block = l2_safe_head;
if self.starting_timestamp() < next_timestamp {
if self.starting_timestamp() > l2_safe_head.block_info.timestamp {
if self.starting_timestamp() > l2_safe_head.timestamp {
// Batch timestamp cannot be between safe head and next timestamp.
warn!("batch has misaligned timestamp, block time is too short");
return BatchValidity::Drop;
}
if (l2_safe_head.block_info.timestamp - self.starting_timestamp()) % cfg.block_time != 0
{
if (l2_safe_head.timestamp - self.starting_timestamp()) % cfg.block_time != 0 {
warn!("batch has misaligned timestamp, not overlapped exactly");
return BatchValidity::Drop;
}
parent_num = l2_safe_head.block_info.number -
(l2_safe_head.block_info.timestamp - self.starting_timestamp()) / cfg.block_time -
parent_num = l2_safe_head.number -
(l2_safe_head.timestamp - self.starting_timestamp()) / cfg.block_time -
1;
parent_block = match fetcher.l2_block_info_by_number(parent_num).await {
Ok(block) => block,
Ok(block) => block.block_info,
Err(e) => {
warn!("failed to fetch L2 block number {parent_num}: {e}");
// Unable to validate the batch for now. Retry later.
return BatchValidity::Undecided;
}
};
}
if !self.check_parent_hash(parent_block.block_info.hash) {
if !self.check_parent_hash(parent_block.hash) {
warn!(
"parent block mismatch, expected: {parent_num}, received: {}. parent hash: {}, parent hash check: {}",
parent_block.block_info.number,
parent_block.block_info.hash,
parent_block.number,
parent_block.hash,
self.parent_check,
);
return BatchValidity::Drop;
Expand Down
27 changes: 16 additions & 11 deletions crates/derive/src/stages/batch_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ where
.check_batch_prefix(
self.config.as_ref(),
l1_origins,
parent,
parent.block_info,
&mut self.fetcher,
)
.await;
Expand Down Expand Up @@ -206,7 +206,8 @@ mod test {
use super::*;
use crate::{
batch::{SingleBatch, SpanBatchElement},
stages::test_utils::{CollectingLayer, MockBatchStreamProvider, TraceStorage}, traits::test_utils::TestL2ChainProvider,
stages::test_utils::{CollectingLayer, MockBatchStreamProvider, TraceStorage},
traits::test_utils::TestL2ChainProvider,
};
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};

Expand Down Expand Up @@ -234,21 +235,25 @@ mod test {
}

#[tokio::test]
#[ignore]
async fn test_span_buffer() {
let mock_batch = SpanBatch {
batches: vec![
SpanBatchElement { epoch_num: 10, timestamp: 10, ..Default::default() },
SpanBatchElement { epoch_num: 10, timestamp: 12, ..Default::default() },
SpanBatchElement { epoch_num: 10, timestamp: 2, ..Default::default() },
SpanBatchElement { epoch_num: 10, timestamp: 4, ..Default::default() },
],
..Default::default()
};
let mock_origins = [BlockInfo { number: 10, timestamp: 12, ..Default::default() }];

let data = vec![Ok(Batch::Span(mock_batch.clone()))];
let config = Arc::new(RollupConfig { holocene_time: Some(0), ..RollupConfig::default() });
let config = Arc::new(RollupConfig {
holocene_time: Some(0),
block_time: 2,
..RollupConfig::default()
});
let prev = MockBatchStreamProvider::new(data);
let mut stream = BatchStream::new(prev, config.clone(), TestL2ChainProvider::default());
let provider = TestL2ChainProvider::default();
let mut stream = BatchStream::new(prev, config.clone(), provider);

// The stage should be active.
assert!(stream.is_active().unwrap());
Expand All @@ -257,15 +262,15 @@ mod test {
let batch = stream.next_batch(Default::default(), &mock_origins).await.unwrap();
if let Batch::Single(single) = batch {
assert_eq!(single.epoch_num, 10);
assert_eq!(single.timestamp, 10);
assert_eq!(single.timestamp, 2);
} else {
panic!("Wrong batch type");
}

let batch = stream.next_batch(Default::default(), &mock_origins).await.unwrap();
if let Batch::Single(single) = batch {
assert_eq!(single.epoch_num, 10);
assert_eq!(single.timestamp, 12);
assert_eq!(single.timestamp, 4);
} else {
panic!("Wrong batch type");
}
Expand All @@ -282,15 +287,15 @@ mod test {
let batch = stream.next_batch(Default::default(), &mock_origins).await.unwrap();
if let Batch::Single(single) = batch {
assert_eq!(single.epoch_num, 10);
assert_eq!(single.timestamp, 10);
assert_eq!(single.timestamp, 2);
} else {
panic!("Wrong batch type");
}

let batch = stream.next_batch(Default::default(), &mock_origins).await.unwrap();
if let Batch::Single(single) = batch {
assert_eq!(single.epoch_num, 10);
assert_eq!(single.timestamp, 12);
assert_eq!(single.timestamp, 4);
} else {
panic!("Wrong batch type");
}
Expand Down

0 comments on commit 70b3227

Please sign in to comment.