Skip to content

feat: l2 block data model update and reorg handling #95

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
May 7, 2025
Prev Previous commit
Next Next commit
enhance test
  • Loading branch information
frisitano committed Apr 30, 2025
commit 115a41067478e59b10da50643339d8b835594d56
32 changes: 25 additions & 7 deletions crates/indexer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,8 @@ impl<ChainSpec: ScrollHardforks + 'static> Stream for Indexer<ChainSpec> {

#[cfg(test)]
mod test {
use std::vec;

use super::*;
use alloy_primitives::{address, bytes, U256};

Expand Down Expand Up @@ -483,14 +485,14 @@ mod test {
let mut u = Unstructured::new(&bytes);

const UNITS_FOR_TESTING: u64 = 20;
const L1_MESSAGE_EXECUTED_COUNT: u64 = 7;
const L1_MESSAGES_NOT_EXECUTED_COUNT: u64 = 7;
let mut l1_messages = Vec::with_capacity(UNITS_FOR_TESTING as usize);
for l1_message_que_index in 0..UNITS_FOR_TESTING {
let l1_message = L1MessageEnvelope {
queue_hash: None,
l1_block_number: l1_message_que_index,
l2_block_number: (UNITS_FOR_TESTING - l1_message_que_index >
L1_MESSAGE_EXECUTED_COUNT)
L1_MESSAGES_NOT_EXECUTED_COUNT)
.then_some(l1_message_que_index),
transaction: TxL1Message {
queue_index: l1_message_que_index,
Expand All @@ -513,15 +515,18 @@ mod test {
number: block_number,
hash: Arbitrary::arbitrary(&mut u).unwrap(),
},
l1_messages: vec![l1_messages[block_number as usize].transaction.tx_hash()],
l1_messages: (UNITS_FOR_TESTING - block_number > L1_MESSAGES_NOT_EXECUTED_COUNT)
.then_some(vec![l1_messages[block_number as usize].transaction.tx_hash()])
.unwrap_or_default(),
};
println!("L2 block: {:?}", l2_block);
indexer.handle_block(l2_block.clone(), None);
indexer.next().await.unwrap().unwrap();
blocks.push(l2_block);
}

// First we assert that we dont reorg the L2 or message queue hash for a higher block
// reorg.
// than any of the L1 messages.
indexer.handle_l1_notification(L1Notification::Reorg(30));
let event = indexer.next().await.unwrap().unwrap();
assert_eq!(
Expand All @@ -533,16 +538,29 @@ mod test {
}
);

// Reorg at block
// Reorg at block 17 which is one of he blocks that has not been executed yet. No reorg.
indexer.handle_l1_notification(L1Notification::Reorg(17));
let event = indexer.next().await.unwrap().unwrap();

assert_eq!(
event,
IndexerEvent::ReorgIndexed {
l1_block_number: 17,
queue_index: Some(18),
l2_block_info: Some(blocks[17].block_info)
queue_index: None,
l2_block_info: None
}
);

// Now reorg at block 11 which contains L1 messages that have been executed .
indexer.handle_l1_notification(L1Notification::Reorg(11));
let event = indexer.next().await.unwrap().unwrap();

assert_eq!(
event,
IndexerEvent::ReorgIndexed {
l1_block_number: 11,
queue_index: Some(12),
l2_block_info: Some(blocks[11 as usize].block_info)
}
);
}
Expand Down