Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core): Merge bounded and unbounded gas adjuster #678

Merged
merged 1 commit into from
Dec 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

21 changes: 19 additions & 2 deletions core/lib/zksync_core/src/l1_gas_price/gas_adjuster/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -80,6 +80,19 @@ impl<E: EthInterface> GasAdjuster<E> {
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<Self>, stop_receiver: watch::Receiver<bool>) -> anyhow::Result<()> {
loop {
if *stop_receiver.borrow() {
Expand Down Expand Up @@ -107,7 +120,11 @@ impl<E: EthInterface> L1GasPriceProvider for GasAdjuster<E> {

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)
}
}

Expand Down
2 changes: 1 addition & 1 deletion core/lib/zksync_core/src/l1_gas_price/mod.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
12 changes: 1 addition & 11 deletions core/lib/zksync_core/src/l1_gas_price/singleton.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -53,16 +53,6 @@ impl GasAdjusterSingleton {
adjuster.clone()
}

pub async fn get_or_init_bounded(
&mut self,
) -> anyhow::Result<Arc<BoundedGasAdjuster<GasAdjuster<QueryClient>>>> {
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<bool>,
Expand Down
12 changes: 6 additions & 6 deletions core/lib/zksync_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down