Skip to content

Commit 68fac1b

Browse files
committed
feat(tests): Add tests for Checkpoint and CheckpointExt
1 parent f8f44b4 commit 68fac1b

File tree

2 files changed

+172
-0
lines changed

2 files changed

+172
-0
lines changed

src/payload/checkpoint.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,70 @@ impl<P: Platform> Display for Checkpoint<P> {
517517
}
518518
}
519519
}
520+
521+
#[cfg(test)]
522+
mod tests {
523+
use {
524+
rblib::{prelude::*, test_utils::BlockContextMocked},
525+
std::time::Instant,
526+
};
527+
528+
#[test]
529+
fn test_barrier_depth_and_is_barrier() {
530+
let (block, _) = BlockContext::<Ethereum>::mocked();
531+
532+
let checkpoint = block.start();
533+
534+
// Expected: initial checkpoint is depth 0 and is barrier
535+
assert_eq!(checkpoint.depth(), 0);
536+
assert!(checkpoint.is_barrier());
537+
assert!(checkpoint.prev().is_none());
538+
}
539+
540+
#[test]
541+
fn test_named_barrier_and_prev_depth() {
542+
// Outline:
543+
// 1. create initial checkpoint (depth 0)
544+
// 2. create named barrier on top
545+
// 3. verify new depth is 1, prev is initial, and is_named_barrier returns
546+
// true
547+
let root = {
548+
let (block, _) = BlockContext::<Ethereum>::mocked();
549+
550+
let cp = block.start();
551+
cp
552+
};
553+
554+
let named = root.named_barrier("sequencer-synced");
555+
assert_eq!(named.depth(), root.depth() + 1);
556+
assert!(named.is_named_barrier("sequencer-synced"));
557+
assert!(matches!(named.prev(), Some(_)));
558+
assert_eq!(named.prev().unwrap().depth(), root.depth());
559+
}
560+
561+
#[test]
562+
fn test_created_at() {
563+
let (block, _) = BlockContext::<Ethereum>::mocked();
564+
565+
let checkpoint = block.start();
566+
567+
let now = Instant::now();
568+
assert!(checkpoint.created_at() <= now);
569+
}
570+
571+
#[test]
572+
fn test_iter() {
573+
let (block, _) = BlockContext::<Ethereum>::mocked();
574+
575+
let checkpoint = block.start();
576+
577+
let checkpoint2 = checkpoint.barrier();
578+
let checkpoint3 = checkpoint2.barrier();
579+
580+
let history: Vec<_> = checkpoint3.into_iter().collect();
581+
assert_eq!(history.len(), 3);
582+
assert_eq!(history[0].depth(), 2);
583+
assert_eq!(history[1].depth(), 1);
584+
assert_eq!(history[2].depth(), 0);
585+
}
586+
}

src/payload/ext/checkpoint.rs

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,3 +311,108 @@ impl<P: Platform> CheckpointExt<P> for Checkpoint<P> {
311311
self.root().created_at()
312312
}
313313
}
314+
315+
#[cfg(test)]
316+
mod tests {
317+
use {
318+
crate::alloy::primitives::Address,
319+
alloy_origin::primitives::{TxHash, U256},
320+
rblib::{prelude::*, test_utils::BlockContextMocked},
321+
std::{
322+
thread,
323+
time::{Duration, Instant},
324+
},
325+
};
326+
327+
#[test]
328+
fn test_is_empty_and_root() {
329+
let (block, _) = BlockContext::<Ethereum>::mocked();
330+
let root = block.start();
331+
let mid = root.barrier();
332+
let leaf = mid.barrier();
333+
334+
assert!(root.is_empty());
335+
assert_eq!(leaf.root(), root);
336+
}
337+
338+
#[test]
339+
fn test_gas_used_and_cumulative_gas_used() {
340+
let (block, _) = BlockContext::<Ethereum>::mocked();
341+
let cp = block.start();
342+
assert_eq!(cp.gas_used(), 0);
343+
assert_eq!(cp.cumulative_gas_used(), 0);
344+
}
345+
346+
#[test]
347+
fn test_effective_tip_and_blob_gas() {
348+
let (block, _) = BlockContext::<Ethereum>::mocked();
349+
let cp = block.start();
350+
assert_eq!(cp.effective_tip_per_gas(), 0);
351+
assert!(!cp.has_blobs());
352+
assert_eq!(cp.blob_gas_used(), Some(0));
353+
assert_eq!(cp.cumulative_blob_gas_used(), 0);
354+
}
355+
356+
#[test]
357+
fn test_to_between_linear_history() {
358+
let (block, _) = BlockContext::<Ethereum>::mocked();
359+
let a = block.start();
360+
let b = a.barrier();
361+
let c = b.barrier();
362+
363+
let span1 = c.to(&a).unwrap();
364+
let span2 = a.to(&c).unwrap();
365+
366+
assert_eq!(span1.len(), 3);
367+
assert_eq!(span2.len(), 3);
368+
}
369+
370+
#[test]
371+
fn test_balance_and_nonce_defaults() {
372+
let (block, _) = BlockContext::<Ethereum>::mocked();
373+
let cp = block.start();
374+
375+
let addr = Address::ZERO;
376+
assert_eq!(cp.balance_of(addr).unwrap(), U256::ZERO);
377+
assert_eq!(cp.nonce_of(addr).unwrap(), 0);
378+
}
379+
380+
#[test]
381+
fn test_signers_and_nonces_are_empty() {
382+
let (block, _) = BlockContext::<Ethereum>::mocked();
383+
let cp = block.start();
384+
385+
assert!(cp.signers().is_empty());
386+
assert!(cp.nonces().is_empty());
387+
}
388+
389+
#[test]
390+
fn test_hash_and_is_bundle_and_has_failures_defaults() {
391+
let (block, _) = BlockContext::<Ethereum>::mocked();
392+
let cp = block.start();
393+
394+
assert_eq!(cp.hash(), None);
395+
assert!(!cp.is_bundle());
396+
assert!(!cp.has_failures());
397+
assert_eq!(cp.failed_txs().count(), 0);
398+
}
399+
400+
#[test]
401+
fn test_contains_is_false_without_txs() {
402+
let (block, _) = BlockContext::<Ethereum>::mocked();
403+
let cp = block.start();
404+
405+
let fake_hash = TxHash::repeat_byte(0x42);
406+
assert!(!cp.contains(fake_hash));
407+
}
408+
409+
#[test]
410+
fn test_history_timestamps() {
411+
let (block, _) = BlockContext::<Ethereum>::mocked();
412+
let cp1 = block.start();
413+
thread::sleep(Duration::from_millis(5));
414+
let cp2 = cp1.barrier();
415+
assert!(cp2.building_since() <= Instant::now());
416+
assert!(cp2.building_since() >= cp1.created_at());
417+
}
418+
}

0 commit comments

Comments
 (0)