Skip to content

Commit 11344f8

Browse files
committed
fix: l1 reorg test
1 parent 1eb8f41 commit 11344f8

File tree

1 file changed

+30
-36
lines changed

1 file changed

+30
-36
lines changed

crates/node/tests/l1_sync.rs

Lines changed: 30 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -311,13 +311,12 @@ async fn test_l1_sync_batch_revert() -> eyre::Result<()> {
311311
fixture.anvil_inject_tx(commit_batch_tx).await?;
312312
}
313313

314-
// Mine blocks to ensure commits are included
315-
fixture.anvil_mine_blocks(64).await?;
316-
317314
// Step 3: Complete L1 sync
318315
fixture.l1().sync().await?;
319316
fixture.expect_event().l1_synced().await?;
320-
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
317+
for _ in 1..=6 {
318+
fixture.expect_event().batch_consolidated().await?;
319+
}
321320

322321
// Verify safe head advanced after processing commits
323322
let new_status = fixture.get_status(0).await?;
@@ -329,9 +328,8 @@ async fn test_l1_sync_batch_revert() -> eyre::Result<()> {
329328
// Step 4: Send BatchRevert transaction to revert some batches
330329
let revert_batch_tx = read_test_transaction("revertBatch", "0")?;
331330
fixture.anvil_inject_tx(revert_batch_tx).await?;
332-
fixture.anvil_mine_blocks(10).await?;
333331

334-
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
332+
fixture.expect_event().batch_reverted().await?;
335333

336334
// Step 5: Verify safe head decreased after revert
337335
let revert_status = fixture.get_status(0).await?;
@@ -384,9 +382,9 @@ async fn test_l1_reorg_batch_commit() -> eyre::Result<()> {
384382
for i in 0..=3 {
385383
let commit_batch_tx = read_test_transaction("commitBatch", &i.to_string())?;
386384
fixture.anvil_inject_tx(commit_batch_tx).await?;
387-
if i != 0 {
388-
fixture.expect_event().batch_consolidated().await?;
389-
}
385+
}
386+
for _ in 1..=3 {
387+
fixture.expect_event().batch_consolidated().await?;
390388
}
391389

392390
// Record safe head after batch 3
@@ -399,8 +397,9 @@ async fn test_l1_reorg_batch_commit() -> eyre::Result<()> {
399397
let commit_batch_tx = read_test_transaction("commitBatch", &i.to_string())?;
400398
fixture.anvil_inject_tx(commit_batch_tx).await?;
401399
}
402-
403-
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
400+
for _ in 1..=3 {
401+
fixture.expect_event().batch_consolidated().await?;
402+
}
404403

405404
// Record advanced safe head after batch 6
406405
let status_after_batch_6 = fixture.get_status(0).await?;
@@ -413,7 +412,6 @@ async fn test_l1_reorg_batch_commit() -> eyre::Result<()> {
413412

414413
// Step 4: Perform L1 reorg to remove batches 4-6 (reorg depth 3)
415414
fixture.anvil_reorg(3).await?;
416-
fixture.anvil_mine_blocks(1).await?;
417415

418416
// Wait for reorg detection
419417
fixture.expect_event().l1_reorg().await?;
@@ -435,9 +433,8 @@ async fn test_l1_reorg_batch_commit() -> eyre::Result<()> {
435433
/// # Test Flow
436434
/// 1. Send `BatchCommit` transactions (batches 0-6) to L1
437435
/// 2. Send `BatchFinalized` transactions (batches 1-2) to L1
438-
/// 3. Complete L1 sync and verify finalized head advanced
439-
/// 4. Perform L1 reorg to remove the `BatchFinalized` events
440-
/// 5. Verify finalized head remains unchanged despite the reorg
436+
/// 3. Perform L1 reorg to remove the `BatchFinalized` events
437+
/// 4. Verify finalized head remains unchanged despite the reorg
441438
///
442439
/// # Expected Behavior
443440
/// The finalized head should NOT change when `BatchFinalized` events are reorged.
@@ -462,39 +459,39 @@ async fn test_l1_reorg_batch_finalized() -> eyre::Result<()> {
462459
.build()
463460
.await?;
464461

462+
fixture.l1().sync().await?;
463+
fixture.expect_event().l1_synced().await?;
464+
465465
// Step 2: Send BatchCommit transactions (batches 0-6)
466466
for i in 0..=6 {
467467
let commit_batch_tx = read_test_transaction("commitBatch", &i.to_string())?;
468468
fixture.anvil_inject_tx(commit_batch_tx).await?;
469469
}
470+
for _ in 1..=6 {
471+
fixture.expect_event().batch_consolidated().await?;
472+
}
470473

471474
// Step 3: Send BatchFinalized transactions (batches 1-2)
472475
for i in 1..=2 {
473476
let finalize_batch_tx = read_test_transaction("finalizeBatch", &i.to_string())?;
474477
fixture.anvil_inject_tx(finalize_batch_tx).await?;
475478
}
476-
477-
// Step 4: Complete L1 sync
478-
fixture.l1().sync().await?;
479-
fixture.expect_event().l1_synced().await?;
480-
481-
// Wait for batch processing
482-
fixture.expect_event().batch_consolidated().await?;
483-
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
479+
for _ in 1..=2 {
480+
fixture.expect_event().batch_finalized().await?;
481+
}
484482

485483
// Record finalized head after finalization
486484
let status_after_finalize = fixture.get_status(0).await?;
487485
let finalized_after = status_after_finalize.l2.fcs.finalized_block_info().number;
488486
tracing::info!("Finalized head after batch finalized: {}", finalized_after);
489487

490-
// Step 5: Perform L1 reorg to remove the BatchFinalized events (depth 2)
488+
// Step 4: Perform L1 reorg to remove the BatchFinalized events (depth 2)
491489
fixture.anvil_reorg(2).await?;
492-
fixture.anvil_mine_blocks(1).await?;
493490

494491
// Wait for reorg detection
495-
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
492+
fixture.expect_event().l1_reorg().await?;
496493

497-
// Step 6: Verify finalized head hasn't changed (reorg has no effect)
494+
// Step 5: Verify finalized head hasn't changed (reorg has no effect)
498495
let status_after_reorg = fixture.get_status(0).await?;
499496
let finalized_after_reorg = status_after_reorg.l2.fcs.finalized_block_info().number;
500497
tracing::info!("Finalized head after reorg: {}", finalized_after_reorg);
@@ -550,8 +547,9 @@ async fn test_l1_reorg_batch_revert() -> eyre::Result<()> {
550547
let commit_batch_tx = read_test_transaction("commitBatch", &i.to_string())?;
551548
fixture.anvil_inject_tx(commit_batch_tx).await?;
552549
}
553-
554-
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
550+
for _ in 1..=6 {
551+
fixture.expect_event().batch_consolidated().await?;
552+
}
555553

556554
// Record safe head after all commits are processed
557555
let status_after_commits = fixture.get_status(0).await?;
@@ -561,9 +559,7 @@ async fn test_l1_reorg_batch_revert() -> eyre::Result<()> {
561559
// Step 3: Send BatchRevert transaction to roll back some batches
562560
let revert_batch_tx = read_test_transaction("revertBatch", "0")?;
563561
fixture.anvil_inject_tx(revert_batch_tx).await?;
564-
fixture.anvil_mine_blocks(1).await?;
565-
566-
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
562+
fixture.expect_event().batch_reverted().await?;
567563

568564
// Step 4: Verify safe head decreased after revert
569565
let status_after_revert = fixture.get_status(0).await?;
@@ -572,10 +568,8 @@ async fn test_l1_reorg_batch_revert() -> eyre::Result<()> {
572568
assert!(safe_after_revert < safe_after_commits, "Safe head should decrease after BatchRevert");
573569

574570
// Step 5: Perform L1 reorg to remove the BatchRevert event (reorg depth 2)
575-
fixture.anvil_reorg(2).await?;
576-
fixture.anvil_mine_blocks(3).await?;
577-
578-
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;
571+
fixture.anvil_reorg(1).await?;
572+
fixture.expect_event().l1_reorg().await?;
579573

580574
// Step 6: Verify safe head restored to pre-revert state
581575
// The batches are no longer reverted, so safe head should be back to full height

0 commit comments

Comments
 (0)