Skip to content

Commit 99d90e5

Browse files
authored
fix(eth-sender): Check the existance of eth txs before getting the correct block for statistics (#4359)
## What ❔ Execute light query for checking the existance of eth tx with necessary type. If it exists, we can execute statistics query. Which are more expensive ## Why ❔ For majority of l2 blocks, the eth transcations doesn't exist. So it leads to extremely expensive queries. ## Is this a breaking change? - [ ] Yes - [ ] No ## Operational changes <!-- Any config changes? Any new flags? Any changes to any scripts? --> <!-- Please add anything that non-Matter Labs entities running their own ZK Chain may need to know --> ## Checklist <!-- Check your PR fulfills the following items. --> <!-- For draft PRs check the boxes as you complete them. --> - [ ] PR title corresponds to the body of PR (we generate changelog entries from PRs). - [ ] Tests for the changes have been added / updated. - [ ] Documentation comments have been added / updated. - [ ] Code has been formatted via `zkstack dev fmt` and `zkstack dev lint`. Signed-off-by: Danil <[email protected]>
1 parent e76c079 commit 99d90e5

File tree

2 files changed

+47
-2
lines changed

2 files changed

+47
-2
lines changed

core/lib/dal/.sqlx/query-b882586949146ef1e7867b133ad6cb00d88e73118347e1f33c041ef27bf62ebc.json

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

core/lib/dal/src/eth_sender_dal.rs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl EthSenderDal<'_, '_> {
209209
Ok(count.try_into().unwrap())
210210
}
211211

212-
pub async fn get_eth_all_blocks_stat(&mut self) -> sqlx::Result<BlocksEthSenderStats> {
212+
pub async fn get_eth_all_blocks_stat(&mut self) -> DalResult<BlocksEthSenderStats> {
213213
struct EthTxRow {
214214
number: i64,
215215
confirmed: bool,
@@ -224,6 +224,24 @@ impl EthSenderDal<'_, '_> {
224224

225225
let mut stats = BlocksEthSenderStats::default();
226226
for &tx_type in TX_TYPES {
227+
// Execute cheap query to check if there are any transactions of this type.
228+
let eth_txs_type_exist: bool = sqlx::query!(
229+
r#"
230+
SELECT EXISTS(
231+
SELECT 1
232+
FROM eth_txs WHERE tx_type = $1
233+
)
234+
"#,
235+
tx_type.to_string()
236+
)
237+
.instrument("get_existence_of_eth_txs_by_type")
238+
.fetch_one(self.storage)
239+
.await?
240+
.exists
241+
.unwrap_or_default();
242+
if !eth_txs_type_exist {
243+
continue;
244+
}
227245
let mut tx_rows = vec![];
228246
for confirmed in [true, false] {
229247
let query = match_query_as!(
@@ -253,7 +271,12 @@ impl EthSenderDal<'_, '_> {
253271
),
254272
}
255273
);
256-
tx_rows.extend(query.fetch_all(self.storage.conn()).await?);
274+
tx_rows.extend(
275+
query
276+
.instrument("get_eth_blocks_stat")
277+
.fetch_all(self.storage)
278+
.await?,
279+
);
257280
}
258281

259282
for row in tx_rows {

0 commit comments

Comments
 (0)