Skip to content

Commit

Permalink
feat(core): Merge bounded and unbounded gas adjuster (#678)
Browse files Browse the repository at this point in the history
## What ❔

The bounded gas adjuster was used everywhere but in EthTxManager, but in
EthTxManager it's used as tx params provider rather than as an L1 gas
provider.
It is safe to merge them and keep a single entity.

## Why ❔

One extra entity less.

## 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 `zk fmt` and `zk lint`.
- [ ] Spellcheck has been run via `cargo spellcheck
--cfg=./spellcheck/era.cfg --code 1`.
  • Loading branch information
popzxc committed Dec 13, 2023
1 parent a6a6a90 commit f3c3bf5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 63 deletions.

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

0 comments on commit f3c3bf5

Please sign in to comment.