Skip to content

Commit

Permalink
fix(db): Make get_expected_l1_batch_timestamp() more efficient (#963)
Browse files Browse the repository at this point in the history
Hopefully. It's not the exact same query as previously, but it should be
equivalent.

## What ❔

Makes `get_expected_l1_batch_timestamp()` DB query more efficient
(hopefully) by getting rid of the `WHERE l1_batch_number IS NULL`
clause.

## Why ❔

Inefficient DB queries are bad, mkay.

## Checklist

- [x] PR title corresponds to the body of PR (we generate changelog
entries from PRs).
- [x] Code has been formatted via `zk fmt` and `zk lint`.
- [x] Spellcheck has been run via `zk spellcheck`.
- [x] Linkcheck has been run via `zk linkcheck`.
  • Loading branch information
slowli authored Jan 29, 2024
1 parent 186d029 commit 7334679
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 47 deletions.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

86 changes: 61 additions & 25 deletions core/lib/dal/src/blocks_web3_dal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,31 +258,67 @@ impl BlocksWeb3Dal<'_, '_> {
&mut self,
l1_batch_number: &ResolvedL1BatchForMiniblock,
) -> sqlx::Result<Option<u64>> {
let timestamp = sqlx::query!(
r#"
SELECT
timestamp
FROM
miniblocks
WHERE
(
$1::BIGINT IS NULL
AND l1_batch_number IS NULL
)
OR (l1_batch_number = $1::BIGINT)
ORDER BY
number
LIMIT
1
"#,
l1_batch_number
.miniblock_l1_batch
.map(|number| i64::from(number.0))
)
.fetch_optional(self.storage.conn())
.await?
.map(|row| row.timestamp as u64);
Ok(timestamp)
if let Some(miniblock_l1_batch) = l1_batch_number.miniblock_l1_batch {
Ok(sqlx::query!(
r#"
SELECT
timestamp
FROM
miniblocks
WHERE
l1_batch_number = $1
ORDER BY
number
LIMIT
1
"#,
i64::from(miniblock_l1_batch.0)
)
.fetch_optional(self.storage.conn())
.await?
.map(|row| row.timestamp as u64))
} else {
// Got a pending miniblock. Searching the timestamp of the first pending miniblock using
// `WHERE l1_batch_number IS NULL` is slow since it potentially locks the `miniblocks` table.
// Instead, we determine its number using the previous L1 batch, taking into the account that
// it may be stored in the `snapshot_recovery` table.
let prev_l1_batch_number = if l1_batch_number.pending_l1_batch == L1BatchNumber(0) {
return Ok(None); // We haven't created the genesis miniblock yet
} else {
l1_batch_number.pending_l1_batch - 1
};
Ok(sqlx::query!(
r#"
SELECT
timestamp
FROM
miniblocks
WHERE
number = COALESCE(
(
SELECT
MAX(number) + 1
FROM
miniblocks
WHERE
l1_batch_number = $1
),
(
SELECT
MAX(miniblock_number) + 1
FROM
snapshot_recovery
WHERE
l1_batch_number = $1
)
)
"#,
i64::from(prev_l1_batch_number.0)
)
.fetch_optional(self.storage.conn())
.await?
.map(|row| row.timestamp as u64))
}
}

pub async fn get_miniblock_hash(
Expand Down

0 comments on commit 7334679

Please sign in to comment.