Skip to content

Commit 52f5b24

Browse files
committed
add L1 message finalized with depth inclusion rule
1 parent d7fa9ec commit 52f5b24

File tree

3 files changed

+64
-44
lines changed

3 files changed

+64
-44
lines changed

crates/database/db/src/operations.rs

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -714,18 +714,25 @@ pub trait DatabaseReadOperations: ReadConnectionProvider + Sync {
714714
// Provides a stream over all L1 messages with increasing queue index starting that have
715715
// not been included in an L2 block and have a block number less than or equal to the
716716
// finalized L1 block number (they have been finalized on L1).
717-
Some(L1MessageKey::NotIncluded(NotIncludedStart::Finalized)) => {
717+
Some(L1MessageKey::NotIncluded(NotIncludedStart::FinalizedWithBlockDepth(depth))) => {
718718
// Lookup the finalized L1 block number.
719719
let finalized_block_number = self.get_finalized_l1_block_number().await?;
720720

721+
// Calculate the target block number by subtracting the depth from the finalized
722+
// block number. If the depth is greater than the finalized block number, we return
723+
// None as there are no messages that satisfy the condition.
724+
let target_block_number =
725+
if let Some(target_block_number) = finalized_block_number.checked_sub(depth) {
726+
target_block_number
727+
} else {
728+
return Ok(None);
729+
};
730+
721731
// Create a filter condition for messages that have an L1 block number less than or
722732
// equal to the finalized block number and have not been included in an L2 block
723733
// (i.e. L2BlockNumber is null).
724734
let condition = Condition::all()
725-
.add(
726-
models::l1_message::Column::L1BlockNumber
727-
.lte(finalized_block_number as i64),
728-
)
735+
.add(models::l1_message::Column::L1BlockNumber.lte(target_block_number as i64))
729736
.add(models::l1_message::Column::L2BlockNumber.is_null());
730737
// Yield a stream of messages matching the condition ordered by increasing queue
731738
// index.
@@ -749,32 +756,28 @@ pub trait DatabaseReadOperations: ReadConnectionProvider + Sync {
749756
// Calculate the target block number by subtracting the depth from the latest block
750757
// number. If the depth is greater than the latest block number, we return None as
751758
// there are no messages that satisfy the condition.
752-
let target_block_number = latest_block_number.checked_sub(depth);
753-
if let Some(target_block_number) = target_block_number {
754-
// Create a filter condition for messages that have an L1 block number less than
755-
// or equal to the target block number and have not been included in an L2 block
756-
// (i.e. L2BlockNumber is null).
757-
let condition = Condition::all()
758-
.add(
759-
models::l1_message::Column::L1BlockNumber
760-
.lte(target_block_number as i64),
761-
)
762-
.add(models::l1_message::Column::L2BlockNumber.is_null());
763-
// Yield a stream of messages matching the condition ordered by increasing
764-
// queue index.
765-
Ok(Some(
766-
models::l1_message::Entity::find()
767-
.filter(condition)
768-
.order_by_asc(models::l1_message::Column::QueueIndex)
769-
.stream(self.get_connection())
770-
.await?
771-
.map(map_l1_message_result),
772-
))
773-
} else {
774-
// If the depth is greater than the latest block number, return None as there
775-
// are no messages that satisfy the condition.
776-
Ok(None)
777-
}
759+
let target_block_number =
760+
if let Some(target_block_number) = latest_block_number.checked_sub(depth) {
761+
target_block_number
762+
} else {
763+
return Ok(None);
764+
};
765+
// Create a filter condition for messages that have an L1 block number less than
766+
// or equal to the target block number and have not been included in an L2 block
767+
// (i.e. L2BlockNumber is null).
768+
let condition = Condition::all()
769+
.add(models::l1_message::Column::L1BlockNumber.lte(target_block_number as i64))
770+
.add(models::l1_message::Column::L2BlockNumber.is_null());
771+
// Yield a stream of messages matching the condition ordered by increasing
772+
// queue index.
773+
Ok(Some(
774+
models::l1_message::Entity::find()
775+
.filter(condition)
776+
.order_by_asc(models::l1_message::Column::QueueIndex)
777+
.stream(self.get_connection())
778+
.await?
779+
.map(map_l1_message_result),
780+
))
778781
}
779782
// Provides a stream over all L1 messages with increasing queue index starting from the
780783
// beginning.
@@ -966,8 +969,10 @@ impl L1MessageKey {
966969
/// block yet.
967970
#[derive(Debug, Clone, PartialEq, Eq)]
968971
pub enum NotIncludedStart {
969-
/// Start from finalized messages that have not been included in a block yet.
970-
Finalized,
972+
/// Start from finalized messages that have not been included in a block yet and have a L1
973+
/// block number that is a specified number of blocks below the current finalized L1 block
974+
/// number.
975+
FinalizedWithBlockDepth(u64),
971976
/// Start from unfinalized messages that are included in L1 blocks at a specific depth.
972977
BlockDepth(u64),
973978
}
@@ -986,7 +991,9 @@ impl fmt::Display for L1MessageKey {
986991
Self::TransactionHash(hash) => write!(f, "TransactionHash({hash:#x})"),
987992
Self::BlockNumber(number) => write!(f, "BlockNumber({number})"),
988993
Self::NotIncluded(start) => match start {
989-
NotIncludedStart::Finalized => write!(f, "NotIncluded(Finalized)"),
994+
NotIncludedStart::FinalizedWithBlockDepth(depth) => {
995+
write!(f, "NotIncluded(Finalized:{depth})")
996+
}
990997
NotIncludedStart::BlockDepth(depth) => {
991998
write!(f, "NotIncluded(BlockDepth({depth}))")
992999
}

crates/sequencer/src/config.rs

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,21 +33,32 @@ pub struct PayloadBuildingConfig {
3333
}
3434

3535
/// Configuration for L1 message inclusion strategy.
36-
#[derive(Debug, Default, Clone, Copy)]
36+
#[derive(Debug, Clone, Copy)]
3737
pub enum L1MessageInclusionMode {
3838
/// Include L1 messages based on block depth.
3939
BlockDepth(u64),
40-
/// Include only finalized L1 messages.
41-
#[default]
42-
Finalized,
40+
/// Include only finalized L1 messages with an additional block depth.
41+
FinalizedWithBlockDepth(u64),
42+
}
43+
44+
// The default is to include finalized L1 messages with a depth of 2 blocks below the current
45+
// finalized block number.
46+
impl Default for L1MessageInclusionMode {
47+
fn default() -> Self {
48+
Self::FinalizedWithBlockDepth(2)
49+
}
4350
}
4451

4552
impl FromStr for L1MessageInclusionMode {
4653
type Err = String;
4754

4855
fn from_str(s: &str) -> Result<Self, Self::Err> {
49-
if s.eq_ignore_ascii_case("finalized") {
50-
Ok(Self::Finalized)
56+
if let Some(rest) = s.strip_prefix("finalized:") {
57+
rest.parse::<u64>()
58+
.map(Self::FinalizedWithBlockDepth)
59+
.map_err(|_| format!("Expected a valid number after 'finalized:', got '{rest}'"))
60+
} else if s.eq_ignore_ascii_case("finalized") {
61+
Ok(Self::FinalizedWithBlockDepth(0))
5162
} else if let Some(rest) = s.strip_prefix("depth:") {
5263
rest.parse::<u64>()
5364
.map(Self::BlockDepth)
@@ -61,7 +72,7 @@ impl FromStr for L1MessageInclusionMode {
6172
impl fmt::Display for L1MessageInclusionMode {
6273
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
6374
match self {
64-
Self::Finalized => write!(f, "finalized"),
75+
Self::FinalizedWithBlockDepth(depth) => write!(f, "finalized:{depth}"),
6576
Self::BlockDepth(depth) => write!(f, "depth:{depth}"),
6677
}
6778
}
@@ -70,7 +81,9 @@ impl fmt::Display for L1MessageInclusionMode {
7081
impl From<L1MessageInclusionMode> for L1MessageKey {
7182
fn from(mode: L1MessageInclusionMode) -> Self {
7283
match mode {
73-
L1MessageInclusionMode::Finalized => Self::NotIncluded(NotIncludedStart::Finalized),
84+
L1MessageInclusionMode::FinalizedWithBlockDepth(depth) => {
85+
Self::NotIncluded(NotIncludedStart::FinalizedWithBlockDepth(depth))
86+
}
7487
L1MessageInclusionMode::BlockDepth(depth) => {
7588
Self::NotIncluded(NotIncludedStart::BlockDepth(depth))
7689
}

crates/sequencer/tests/e2e.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ async fn can_build_blocks_with_finalized_l1_messages() {
385385
payload_building_config: PayloadBuildingConfig {
386386
block_gas_limit: SCROLL_GAS_LIMIT,
387387
max_l1_messages_per_block: 4,
388-
l1_message_inclusion_mode: L1MessageInclusionMode::Finalized,
388+
l1_message_inclusion_mode: L1MessageInclusionMode::FinalizedWithBlockDepth(0),
389389
},
390390
block_time: 0,
391391
payload_building_duration: 0,
@@ -906,7 +906,7 @@ async fn should_limit_l1_message_cumulative_gas() {
906906
payload_building_config: PayloadBuildingConfig {
907907
block_gas_limit: SCROLL_GAS_LIMIT,
908908
max_l1_messages_per_block: 4,
909-
l1_message_inclusion_mode: L1MessageInclusionMode::Finalized,
909+
l1_message_inclusion_mode: L1MessageInclusionMode::FinalizedWithBlockDepth(0),
910910
},
911911
block_time: 0,
912912
payload_building_duration: 0,

0 commit comments

Comments
 (0)