diff --git a/crates/database/db/src/operations.rs b/crates/database/db/src/operations.rs index 09ec3ed0..353e156e 100644 --- a/crates/database/db/src/operations.rs +++ b/crates/database/db/src/operations.rs @@ -757,6 +757,23 @@ impl DatabaseReadOperations for T { start: Option, n: usize, ) -> Result, DatabaseError> { + // function that returns the queue index of the L1 message after the last included L1 + // message. + let next_l1_message_queue_index = async || { + Ok::( + models::l1_message::Entity::find() + .select_only() + .filter(models::l1_message::Column::L2BlockNumber.is_not_null()) + .column_as(models::l1_message::Column::QueueIndex.max(), "max_queue_index") + .into_tuple::>() + .one(self.get_connection()) + .await? + .flatten() + .map(|idx| idx + 1) + .unwrap_or(0), + ) + }; + match start { // Provides n L1 messages with increasing queue index starting from the provided queue // index. @@ -865,12 +882,16 @@ impl DatabaseReadOperations for T { return Ok(vec![]); }; + // Get the next L1 message queue index after the last included L1 message. + let next_l1_message_queue_index = next_l1_message_queue_index().await?; + // Create a filter condition for messages that have an L1 block number less than or // equal to the finalized block number and have not been included in an L2 block // (i.e. L2BlockNumber is null). let condition = Condition::all() .add(models::l1_message::Column::L1BlockNumber.lte(target_block_number as i64)) - .add(models::l1_message::Column::L2BlockNumber.is_null()); + .add(models::l1_message::Column::L2BlockNumber.is_null()) + .add(models::l1_message::Column::QueueIndex.gte(next_l1_message_queue_index)); // Yield n messages matching the condition ordered by increasing queue index. Ok(models::l1_message::Entity::find() .filter(condition) @@ -897,12 +918,17 @@ impl DatabaseReadOperations for T { } else { return Ok(vec![]); }; + + // Get the next L1 message queue index after the last included L1 message. + let next_l1_message_queue_index = next_l1_message_queue_index().await?; + // Create a filter condition for messages that have an L1 block number less than // or equal to the target block number and have not been included in an L2 block // (i.e. L2BlockNumber is null). let condition = Condition::all() .add(models::l1_message::Column::L1BlockNumber.lte(target_block_number as i64)) - .add(models::l1_message::Column::L2BlockNumber.is_null()); + .add(models::l1_message::Column::L2BlockNumber.is_null()) + .add(models::l1_message::Column::QueueIndex.gte(next_l1_message_queue_index)); // Yield n messages matching the condition ordered by increasing queue index. Ok(models::l1_message::Entity::find() .filter(condition) diff --git a/sequencer-migration/send-l1-messages.sh b/sequencer-migration/send-l1-messages.sh old mode 100644 new mode 100755 index 73ef984f..9e289939 --- a/sequencer-migration/send-l1-messages.sh +++ b/sequencer-migration/send-l1-messages.sh @@ -78,5 +78,7 @@ main() { log_info "Next queue index: $next_queue_index " done - echo "Done" + log_info "Done" } + +main "$@"