From d042e2920305ceaa1696b5d85347daff00d74324 Mon Sep 17 00:00:00 2001 From: Daniyar Itegulov Date: Fri, 13 Sep 2024 18:57:13 +1000 Subject: [PATCH] tweak the logic to rely on unsealed batches with a fallback --- ...138f12752acc992e0a9065c9c73b90a7aa4c.json} | 4 +-- core/lib/dal/src/blocks_dal.rs | 24 +++++++------ core/node/fee_model/src/lib.rs | 35 +++++++++++++++---- 3 files changed, 43 insertions(+), 20 deletions(-) rename core/lib/dal/.sqlx/{query-42fc6da4154f25fc711f194931b0613f921b0d9d99c1e0c219f05f8cfb5ad001.json => query-e3edcff8544b05ee2b2c58aa040b138f12752acc992e0a9065c9c73b90a7aa4c.json} (79%) diff --git a/core/lib/dal/.sqlx/query-42fc6da4154f25fc711f194931b0613f921b0d9d99c1e0c219f05f8cfb5ad001.json b/core/lib/dal/.sqlx/query-e3edcff8544b05ee2b2c58aa040b138f12752acc992e0a9065c9c73b90a7aa4c.json similarity index 79% rename from core/lib/dal/.sqlx/query-42fc6da4154f25fc711f194931b0613f921b0d9d99c1e0c219f05f8cfb5ad001.json rename to core/lib/dal/.sqlx/query-e3edcff8544b05ee2b2c58aa040b138f12752acc992e0a9065c9c73b90a7aa4c.json index af01e0671ac1..0039bf67e74b 100644 --- a/core/lib/dal/.sqlx/query-42fc6da4154f25fc711f194931b0613f921b0d9d99c1e0c219f05f8cfb5ad001.json +++ b/core/lib/dal/.sqlx/query-e3edcff8544b05ee2b2c58aa040b138f12752acc992e0a9065c9c73b90a7aa4c.json @@ -1,6 +1,6 @@ { "db_name": "PostgreSQL", - "query": "\n SELECT\n l1_gas_price,\n l2_fair_gas_price,\n fair_pubdata_price\n FROM\n l1_batches\n ORDER BY\n number DESC\n LIMIT\n 1\n ", + "query": "\n SELECT\n l1_gas_price,\n l2_fair_gas_price,\n fair_pubdata_price\n FROM\n l1_batches\n WHERE\n NOT is_sealed\n ", "describe": { "columns": [ { @@ -28,5 +28,5 @@ false ] }, - "hash": "42fc6da4154f25fc711f194931b0613f921b0d9d99c1e0c219f05f8cfb5ad001" + "hash": "e3edcff8544b05ee2b2c58aa040b138f12752acc992e0a9065c9c73b90a7aa4c" } diff --git a/core/lib/dal/src/blocks_dal.rs b/core/lib/dal/src/blocks_dal.rs index 3fde53bd0e18..a95cedb40cbc 100644 --- a/core/lib/dal/src/blocks_dal.rs +++ b/core/lib/dal/src/blocks_dal.rs @@ -817,7 +817,7 @@ impl BlocksDal<'_, '_> { Ok(()) } - pub async fn get_last_l1_batch_fee_input(&mut self) -> DalResult { + pub async fn get_unsealed_l1_batch_fee_input(&mut self) -> DalResult> { let row = sqlx::query!( r#" SELECT @@ -826,21 +826,23 @@ impl BlocksDal<'_, '_> { fair_pubdata_price FROM l1_batches - ORDER BY - number DESC - LIMIT - 1 + WHERE + NOT is_sealed "# ) .instrument("get_last_l1_batch_fee_input") - .fetch_one(self.storage) + .fetch_optional(self.storage) .await?; - Ok(BatchFeeInput::pubdata_independent( - row.l1_gas_price as u64, - row.l2_fair_gas_price as u64, - row.fair_pubdata_price as u64, - )) + if let Some(row) = row { + Ok(Some(BatchFeeInput::pubdata_independent( + row.l1_gas_price as u64, + row.l2_fair_gas_price as u64, + row.fair_pubdata_price as u64, + ))) + } else { + Ok(None) + } } pub async fn get_last_sealed_l2_block_header(&mut self) -> DalResult> { diff --git a/core/node/fee_model/src/lib.rs b/core/node/fee_model/src/lib.rs index 0275e0c0d798..596d42fffdd8 100644 --- a/core/node/fee_model/src/lib.rs +++ b/core/node/fee_model/src/lib.rs @@ -1,5 +1,6 @@ use std::{fmt, fmt::Debug, sync::Arc}; +use anyhow::Context; use async_trait::async_trait; use zksync_dal::{ConnectionPool, Core, CoreDal}; use zksync_types::{ @@ -126,18 +127,38 @@ impl ApiFeeInputProvider { impl BatchFeeModelInputProvider for ApiFeeInputProvider { async fn get_batch_fee_input_scaled( &self, - _l1_gas_price_scale_factor: f64, - _l1_pubdata_price_scale_factor: f64, + l1_gas_price_scale_factor: f64, + l1_pubdata_price_scale_factor: f64, ) -> anyhow::Result { - let batch_fee_input = self + if let Some(batch_fee_input) = self .connection_pool .connection_tagged("api_fee_input_provider") .await? .blocks_dal() - .get_last_l1_batch_fee_input() - .await?; - - Ok(batch_fee_input) + .get_unsealed_l1_batch_fee_input() + .await? + { + Ok(batch_fee_input) + } else { + let inner_input = self + .inner + .get_batch_fee_input_scaled( + l1_gas_price_scale_factor, + l1_pubdata_price_scale_factor, + ) + .await + .context("cannot get batch fee input from base provider")?; + let last_l2_block_params = self + .connection_pool + .connection_tagged("api_fee_input_provider") + .await? + .blocks_dal() + .get_last_sealed_l2_block_header() + .await?; + Ok(last_l2_block_params + .map(|header| inner_input.stricter(header.batch_fee_input)) + .unwrap_or(inner_input)) + } } /// Returns the fee model parameters.