From b42e4c96bad60916043cd2609a65165a0fd2fdd4 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Wed, 13 Dec 2023 13:55:57 +0400 Subject: [PATCH] Remove bounded gas adjuster --- .../gas_adjuster/bounded_gas_adjuster.rs | 43 ------------------- .../src/l1_gas_price/gas_adjuster/mod.rs | 21 ++++++++- core/lib/zksync_core/src/l1_gas_price/mod.rs | 2 +- .../zksync_core/src/l1_gas_price/singleton.rs | 12 +----- core/lib/zksync_core/src/lib.rs | 12 +++--- 5 files changed, 27 insertions(+), 63 deletions(-) delete mode 100644 core/lib/zksync_core/src/l1_gas_price/gas_adjuster/bounded_gas_adjuster.rs diff --git a/core/lib/zksync_core/src/l1_gas_price/gas_adjuster/bounded_gas_adjuster.rs b/core/lib/zksync_core/src/l1_gas_price/gas_adjuster/bounded_gas_adjuster.rs deleted file mode 100644 index a4e949c4704b..000000000000 --- a/core/lib/zksync_core/src/l1_gas_price/gas_adjuster/bounded_gas_adjuster.rs +++ /dev/null @@ -1,43 +0,0 @@ -use std::{fmt, sync::Arc}; - -use crate::{l1_gas_price::L1GasPriceProvider, state_keeper::metrics::KEEPER_METRICS}; - -/// Gas adjuster that bounds the gas price to the specified value. -/// We need this to prevent the gas price from growing too much, because our bootloader is sensitive for the gas price and can fail if it's too high. -/// And for mainnet it's not the case, but for testnet we can have a situation when the gas price is too high. -pub struct BoundedGasAdjuster { - max_gas_price: u64, - default_gas_adjuster: Arc, -} - -impl fmt::Debug for BoundedGasAdjuster { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - f.debug_struct("BoundedGasAdjuster") - .field("max_gas_price", &self.max_gas_price) - .finish() - } -} - -impl BoundedGasAdjuster { - pub fn new(max_gas_price: u64, default_gas_adjuster: Arc) -> Self { - Self { - max_gas_price, - default_gas_adjuster, - } - } -} - -impl L1GasPriceProvider for BoundedGasAdjuster { - fn estimate_effective_gas_price(&self) -> u64 { - let default_gas_price = self.default_gas_adjuster.estimate_effective_gas_price(); - if default_gas_price > self.max_gas_price { - tracing::warn!( - "Effective gas price is too high: {default_gas_price}, using max allowed: {}", - self.max_gas_price - ); - KEEPER_METRICS.gas_price_too_high.inc(); - return self.max_gas_price; - } - default_gas_price - } -} diff --git a/core/lib/zksync_core/src/l1_gas_price/gas_adjuster/mod.rs b/core/lib/zksync_core/src/l1_gas_price/gas_adjuster/mod.rs index dd9806f998c1..8fce05f02e3b 100644 --- a/core/lib/zksync_core/src/l1_gas_price/gas_adjuster/mod.rs +++ b/core/lib/zksync_core/src/l1_gas_price/gas_adjuster/mod.rs @@ -11,8 +11,8 @@ use zksync_eth_client::{types::Error, EthInterface}; use self::metrics::METRICS; use super::{L1GasPriceProvider, L1TxParamsProvider}; +use crate::state_keeper::metrics::KEEPER_METRICS; -pub mod bounded_gas_adjuster; mod metrics; #[cfg(test)] mod tests; @@ -80,6 +80,19 @@ impl GasAdjuster { Ok(()) } + fn bound_gas_price(&self, gas_price: u64) -> u64 { + let max_l1_gas_price = self.config.max_l1_gas_price(); + if gas_price > max_l1_gas_price { + tracing::warn!( + "Effective gas price is too high: {gas_price}, using max allowed: {}", + max_l1_gas_price + ); + KEEPER_METRICS.gas_price_too_high.inc(); + return max_l1_gas_price; + } + gas_price + } + pub async fn run(self: Arc, stop_receiver: watch::Receiver) -> anyhow::Result<()> { loop { if *stop_receiver.borrow() { @@ -107,7 +120,11 @@ impl L1GasPriceProvider for GasAdjuster { let effective_gas_price = self.get_base_fee(0) + self.get_priority_fee(); - (self.config.internal_l1_pricing_multiplier * effective_gas_price as f64) as u64 + let calculated_price = + (self.config.internal_l1_pricing_multiplier * effective_gas_price as f64) as u64; + + // Bound the price if it's too high. + self.bound_gas_price(calculated_price) } } diff --git a/core/lib/zksync_core/src/l1_gas_price/mod.rs b/core/lib/zksync_core/src/l1_gas_price/mod.rs index bab30c035843..c2e5ab8adb7c 100644 --- a/core/lib/zksync_core/src/l1_gas_price/mod.rs +++ b/core/lib/zksync_core/src/l1_gas_price/mod.rs @@ -1,6 +1,6 @@ //! This module determines the fees to pay in txs containing blocks submitted to the L1. -pub use gas_adjuster::{bounded_gas_adjuster::BoundedGasAdjuster, GasAdjuster}; +pub use gas_adjuster::GasAdjuster; pub use main_node_fetcher::MainNodeGasPriceFetcher; pub use singleton::GasAdjusterSingleton; diff --git a/core/lib/zksync_core/src/l1_gas_price/singleton.rs b/core/lib/zksync_core/src/l1_gas_price/singleton.rs index 0c70ba2466ca..d4d04c196961 100644 --- a/core/lib/zksync_core/src/l1_gas_price/singleton.rs +++ b/core/lib/zksync_core/src/l1_gas_price/singleton.rs @@ -8,7 +8,7 @@ use tokio::{ use zksync_config::GasAdjusterConfig; use zksync_eth_client::clients::http::QueryClient; -use crate::l1_gas_price::{BoundedGasAdjuster, GasAdjuster}; +use crate::l1_gas_price::GasAdjuster; /// Special struct for creating a singleton of `GasAdjuster`. /// This is needed only for running the server. @@ -53,16 +53,6 @@ impl GasAdjusterSingleton { adjuster.clone() } - pub async fn get_or_init_bounded( - &mut self, - ) -> anyhow::Result>>> { - let adjuster = self.get_or_init().await.context("get_or_init()")?; - Ok(Arc::new(BoundedGasAdjuster::new( - self.gas_adjuster_config.max_l1_gas_price(), - adjuster, - ))) - } - pub fn run_if_initialized( self, stop_signal: watch::Receiver, diff --git a/core/lib/zksync_core/src/lib.rs b/core/lib/zksync_core/src/lib.rs index 79d96c77e56b..23c15f6a772f 100644 --- a/core/lib/zksync_core/src/lib.rs +++ b/core/lib/zksync_core/src/lib.rs @@ -404,9 +404,9 @@ pub async fn initialize_components( let started_at = Instant::now(); tracing::info!("Initializing HTTP API"); let bounded_gas_adjuster = gas_adjuster - .get_or_init_bounded() + .get_or_init() .await - .context("gas_adjuster.get_or_init_bounded()")?; + .context("gas_adjuster.get_or_init()")?; let server_handles = run_http_api( &postgres_config, &tx_sender_config, @@ -444,9 +444,9 @@ pub async fn initialize_components( let started_at = Instant::now(); tracing::info!("initializing WS API"); let bounded_gas_adjuster = gas_adjuster - .get_or_init_bounded() + .get_or_init() .await - .context("gas_adjuster.get_or_init_bounded()")?; + .context("gas_adjuster.get_or_init()")?; let server_handles = run_ws_api( &postgres_config, &tx_sender_config, @@ -498,9 +498,9 @@ pub async fn initialize_components( let started_at = Instant::now(); tracing::info!("initializing State Keeper"); let bounded_gas_adjuster = gas_adjuster - .get_or_init_bounded() + .get_or_init() .await - .context("gas_adjuster.get_or_init_bounded()")?; + .context("gas_adjuster.get_or_init()")?; add_state_keeper_to_task_futures( &mut task_futures, &postgres_config,