Skip to content

Commit

Permalink
inline adjust_pubdata_price_for_tx
Browse files Browse the repository at this point in the history
  • Loading branch information
perekopskiy committed Feb 23, 2024
1 parent d3f75a6 commit afcc1c3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 41 deletions.
38 changes: 32 additions & 6 deletions core/lib/multivm/src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use zksync_types::{fee_model::BatchFeeInput, VmVersion, U256};
use zksync_types::{
fee_model::{BatchFeeInput, L1PeggedBatchFeeModelInput, PubdataIndependentBatchFeeModelInput},
VmVersion, U256,
};

use crate::vm_latest::L1BatchEnv;

Expand Down Expand Up @@ -79,14 +82,37 @@ pub fn adjust_pubdata_price_for_tx(
if U256::from(derive_base_fee_and_gas_per_pubdata(batch_fee_input, vm_version).1)
<= tx_gas_per_pubdata_limit
{
// gas per pubdata is already smaller than or equal to `tx_gas_per_pubdata_limit`.
return batch_fee_input;
}

// The latest VM supports adjusting the pubdata price for all the types of the fee models.
crate::vm_latest::utils::fee::adjust_pubdata_price_for_tx(
batch_fee_input,
tx_gas_per_pubdata_limit,
)
match batch_fee_input {
BatchFeeInput::L1Pegged(fee_input) => {
// `gasPerPubdata = ceil(17 * l1gasprice / fair_l2_gas_price)`
// `gasPerPubdata <= 17 * l1gasprice / fair_l2_gas_price + 1`
// `fair_l2_gas_price(gasPerPubdata - 1) / 17 <= l1gasprice`
let new_l1_gas_price = U256::from(fee_input.fair_l2_gas_price)
* (tx_gas_per_pubdata_limit - U256::from(1u32))
/ U256::from(17);

BatchFeeInput::L1Pegged(L1PeggedBatchFeeModelInput {
l1_gas_price: new_l1_gas_price.as_u64(),
..fee_input
})
}
BatchFeeInput::PubdataIndependent(fee_input) => {
// `gasPerPubdata = ceil(fair_pubdata_price / fair_l2_gas_price)`
// `gasPerPubdata <= fair_pubdata_price / fair_l2_gas_price + 1`
// `fair_l2_gas_price(gasPerPubdata - 1) <= fair_pubdata_price`
let new_fair_pubdata_price = U256::from(fee_input.fair_l2_gas_price)
* (tx_gas_per_pubdata_limit - U256::from(1u32));

BatchFeeInput::PubdataIndependent(PubdataIndependentBatchFeeModelInput {
fair_pubdata_price: new_fair_pubdata_price.as_u64(),
..fee_input
})
}
}
}

pub fn derive_overhead(
Expand Down
36 changes: 1 addition & 35 deletions core/lib/multivm/src/versions/vm_latest/utils/fee.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! Utility functions for vm
use zksync_types::{
fee_model::{BatchFeeInput, PubdataIndependentBatchFeeModelInput},
U256,
};
use zksync_types::fee_model::PubdataIndependentBatchFeeModelInput;
use zksync_utils::ceil_div;

use crate::vm_latest::{constants::MAX_GAS_PER_PUBDATA_BYTE, L1BatchEnv};
Expand Down Expand Up @@ -37,34 +34,3 @@ pub(crate) fn get_batch_base_fee(l1_batch_env: &L1BatchEnv) -> u64 {
derive_base_fee_and_gas_per_pubdata(l1_batch_env.fee_input.into_pubdata_independent());
base_fee
}

/// Changes the fee model output so that the expected gas per pubdata is smaller than or the `tx_gas_per_pubdata_limit`.
/// This function expects that the currently expected gas per pubdata is greater than the `tx_gas_per_pubdata_limit`.
pub(crate) fn adjust_pubdata_price_for_tx(
mut batch_fee_input: BatchFeeInput,
tx_gas_per_pubdata_limit: U256,
) -> BatchFeeInput {
match &mut batch_fee_input {
BatchFeeInput::L1Pegged(fee_input) => {
// `gasPerPubdata = ceil(17 * l1gasprice / fair_l2_gas_price)`
// `gasPerPubdata <= 17 * l1gasprice / fair_l2_gas_price + 1`
// `fair_l2_gas_price(gasPerPubdata - 1) / 17 <= l1gasprice`
let new_l1_gas_price = U256::from(fee_input.fair_l2_gas_price)
* (tx_gas_per_pubdata_limit - U256::from(1u32))
/ U256::from(17);

fee_input.l1_gas_price = new_l1_gas_price.as_u64();
}
BatchFeeInput::PubdataIndependent(fee_input) => {
// `gasPerPubdata = ceil(fair_pubdata_price / fair_l2_gas_price)`
// `gasPerPubdata <= fair_pubdata_price / fair_l2_gas_price + 1`
// `fair_l2_gas_price(gasPerPubdata - 1) <= fair_pubdata_price`
let new_fair_pubdata_price = U256::from(fee_input.fair_l2_gas_price)
* (tx_gas_per_pubdata_limit - U256::from(1u32));

fee_input.fair_pubdata_price = new_fair_pubdata_price.as_u64();
}
}

batch_fee_input
}

0 comments on commit afcc1c3

Please sign in to comment.