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

fix(en): more correct fees algorithm for external-node #3487

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 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
3 changes: 3 additions & 0 deletions core/lib/types/src/fee_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ impl FeeParamsV2 {
self.convert_to_base_token(self.l1_pubdata_price)
}

pub fn conversion_ratio(&self) -> BaseTokenConversionRatio {
self.conversion_ratio
}
/// Converts the fee param to the base token.
tomg10 marked this conversation as resolved.
Show resolved Hide resolved
fn convert_to_base_token(&self, price_in_wei: u64) -> u64 {
let conversion_ratio = BigDecimal::from(self.conversion_ratio.numerator.get())
Expand Down
53 changes: 51 additions & 2 deletions core/node/fee_model/src/l1_gas_price/main_node_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use std::{
};

use tokio::sync::watch::Receiver;
use zksync_types::fee_model::FeeParams;
use zksync_types::fee_model::{
BatchFeeInput, FeeModelConfigV1, FeeParams, FeeParamsV1, FeeParamsV2,
};
use zksync_web3_decl::{
client::{DynClient, L2},
error::ClientRpcContext,
Expand All @@ -25,13 +27,15 @@ const SLEEP_INTERVAL: Duration = Duration::from_secs(5);
pub struct MainNodeFeeParamsFetcher {
client: Box<DynClient<L2>>,
main_node_fee_params: RwLock<FeeParams>,
main_node_batch_fee_input: RwLock<Option<BatchFeeInput>>,
}

impl MainNodeFeeParamsFetcher {
pub fn new(client: Box<DynClient<L2>>) -> Self {
Self {
client: client.for_component("fee_params_fetcher"),
main_node_fee_params: RwLock::new(FeeParams::sensible_v1_default()),
main_node_batch_fee_input: RwLock::new(None),
}
}

Expand All @@ -58,6 +62,28 @@ impl MainNodeFeeParamsFetcher {
};
*self.main_node_fee_params.write().unwrap() = main_node_fee_params;

let fetch_result = self
.client
.get_batch_fee_input()
.rpc_context("get_batch_fee_input")
.await;
let main_node_fee_params = match fetch_result {
Ok(price) => price,
Err(err) => {
tracing::warn!("Unable to get the batch fee input: {}", err);
// A delay to avoid spamming the main node with requests.
if tokio::time::timeout(SLEEP_INTERVAL, stop_receiver.changed())
.await
.is_ok()
{
break;
}
continue;
}
};
*self.main_node_batch_fee_input.write().unwrap() =
Some(BatchFeeInput::PubdataIndependent(main_node_fee_params));

if tokio::time::timeout(SLEEP_INTERVAL, stop_receiver.changed())
.await
.is_ok()
Expand All @@ -73,6 +99,29 @@ impl MainNodeFeeParamsFetcher {

impl BatchFeeModelInputProvider for MainNodeFeeParamsFetcher {
fn get_fee_model_params(&self) -> FeeParams {
*self.main_node_fee_params.read().unwrap()
let fee_params = *self.main_node_fee_params.read().unwrap();
let batch_fee_input = *self.main_node_batch_fee_input.read().unwrap();
if batch_fee_input.is_none() {
return fee_params;
}
let batch_fee_input = batch_fee_input.unwrap();
tomg10 marked this conversation as resolved.
Show resolved Hide resolved
match fee_params {
FeeParams::V1(..) => FeeParams::V1(FeeParamsV1 {
config: FeeModelConfigV1 {
minimal_l2_gas_price: batch_fee_input.fair_l2_gas_price(),
},
l1_gas_price: batch_fee_input.l1_gas_price(),
}),
FeeParams::V2(params) => {
let mut config = params.config();
config.minimal_l2_gas_price = batch_fee_input.fair_l2_gas_price();
tomg10 marked this conversation as resolved.
Show resolved Hide resolved
return FeeParams::V2(FeeParamsV2::new(
config,
batch_fee_input.l1_gas_price(),
batch_fee_input.fair_pubdata_price(),
params.conversion_ratio(),
));
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need all of this logic? Can't we just override get_batch_fee_input_scaled instead and return *self.main_node_batch_fee_input.read().unwrap() there? EN's scaling factor will be ignored but arguably this logic is not designed well in general (i.e. is there a reason to scale gas price differently on EN?). Also, fn get_fee_model_params(&self) -> FeeParams being a part of the base trait is strange too, it's more like an implementation detail for half of the implementations.

}
}
Loading