Skip to content

Commit 22e5d4c

Browse files
committed
feat: get_n_l2_block_data_hint
1 parent 1d44ce6 commit 22e5d4c

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

crates/database/db/src/db.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -339,11 +339,12 @@ impl DatabaseReadOperations for Database {
339339
.await
340340
}
341341

342-
async fn get_l2_block_data_hint(
342+
async fn get_n_l2_block_data_hint(
343343
&self,
344344
block_number: u64,
345-
) -> Result<Option<BlockDataHint>, DatabaseError> {
346-
self.tx(move |tx| async move { tx.get_l2_block_data_hint(block_number).await }).await
345+
n: usize,
346+
) -> Result<Vec<BlockDataHint>, DatabaseError> {
347+
self.tx(move |tx| async move { tx.get_n_l2_block_data_hint(block_number, n).await }).await
347348
}
348349

349350
async fn get_l2_block_and_batch_info_by_hash(
@@ -658,9 +659,9 @@ mod test {
658659
let db = setup_test_db().await;
659660

660661
// db should contain the seeded data after migration.
661-
let data = db.get_l2_block_data_hint(0).await.unwrap();
662+
let data = db.get_n_l2_block_data_hint(0, 1).await.unwrap();
662663

663-
assert!(data.is_some());
664+
assert_eq!(data.len(), 1);
664665
}
665666

666667
#[tokio::test]

crates/database/db/src/operations.rs

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,12 @@ pub trait DatabaseReadOperations {
641641
n: usize,
642642
) -> Result<Vec<L1MessageEnvelope>, DatabaseError>;
643643

644-
/// Get the extra data for the provided block number.
645-
async fn get_l2_block_data_hint(
644+
/// Get the extra data for n block, starting at the provided block number.
645+
async fn get_n_l2_block_data_hint(
646646
&self,
647647
block_number: u64,
648-
) -> Result<Option<BlockDataHint>, DatabaseError>;
648+
n: usize,
649+
) -> Result<Vec<BlockDataHint>, DatabaseError>;
649650

650651
/// Get the [`BlockInfo`] and optional [`BatchInfo`] for the provided block hash.
651652
async fn get_l2_block_and_batch_info_by_hash(
@@ -921,15 +922,19 @@ impl<T: ReadConnectionProvider + Sync + ?Sized> DatabaseReadOperations for T {
921922
}
922923
}
923924

924-
async fn get_l2_block_data_hint(
925+
async fn get_n_l2_block_data_hint(
925926
&self,
926927
block_number: u64,
927-
) -> Result<Option<BlockDataHint>, DatabaseError> {
928+
n: usize,
929+
) -> Result<Vec<BlockDataHint>, DatabaseError> {
928930
Ok(models::block_data::Entity::find()
929931
.filter(models::block_data::Column::Number.eq(block_number as i64))
930-
.one(self.get_connection())
931-
.await
932-
.map(|x| x.map(Into::into))?)
932+
.limit(Some(n as u64))
933+
.all(self.get_connection())
934+
.await?
935+
.into_iter()
936+
.map(Into::into)
937+
.collect())
933938
}
934939

935940
async fn get_l2_block_and_batch_info_by_hash(

crates/derivation-pipeline/src/lib.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,14 @@ pub async fn derive<L1P: L1Provider + Sync + Send, DB: DatabaseReadOperations>(
292292
let blocks = decoded.data.into_l2_blocks();
293293
let mut attributes = Vec::with_capacity(blocks.len());
294294

295-
for mut block in blocks {
295+
let start = blocks.first().map(|b| b.context.number);
296+
let block_data = if let Some(start) = start {
297+
db.get_n_l2_block_data_hint(start, blocks.len()).await?
298+
} else {
299+
vec![]
300+
};
301+
302+
for (i, mut block) in blocks.into_iter().enumerate() {
296303
// query the appropriate amount of l1 messages.
297304
let mut txs = Vec::with_capacity(block.context.num_transactions as usize);
298305
for _ in 0..block.context.num_l1_messages {
@@ -315,8 +322,6 @@ pub async fn derive<L1P: L1Provider + Sync + Send, DB: DatabaseReadOperations>(
315322

316323
// get the block data for the l2 block.
317324
let number = block.context.number;
318-
// TODO(performance): can this be improved by adding block_data_range.
319-
let block_data = db.get_l2_block_data_hint(number).await?;
320325

321326
// construct the payload attributes.
322327
let attribute = DerivedAttributes {
@@ -331,7 +336,7 @@ pub async fn derive<L1P: L1Provider + Sync + Send, DB: DatabaseReadOperations>(
331336
},
332337
transactions: Some(txs),
333338
no_tx_pool: true,
334-
block_data_hint: block_data.unwrap_or_else(BlockDataHint::none),
339+
block_data_hint: block_data.get(i).cloned().unwrap_or_else(BlockDataHint::none),
335340
gas_limit: Some(block.context.gas_limit),
336341
},
337342
};

0 commit comments

Comments
 (0)