Skip to content

Commit

Permalink
chore: make values into constants
Browse files Browse the repository at this point in the history
  • Loading branch information
elfedy committed Dec 13, 2024
1 parent e65df48 commit c6cb287
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 10 deletions.
3 changes: 2 additions & 1 deletion crates/forge/tests/it/zk/factory_deps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use foundry_test_utils::{
util::{self, OutputExt},
Filter, ZkSyncNode,
};
use foundry_zksync_core::utils::MAX_L2_GAS_LIMIT;

use crate::{config::TestConfig, test_helpers::TEST_DATA_DEFAULT};

Expand Down Expand Up @@ -46,7 +47,7 @@ contract ZkLargeFactoryDependenciesScript is Script {

// foundry default gas-limit is not enough to pay for factory deps
// with era_test_node's environment
let gas_limit = (u32::MAX >> 1) * 2;
let gas_limit = MAX_L2_GAS_LIMIT;

cmd.arg("script").args([
"--zk-startup",
Expand Down
11 changes: 5 additions & 6 deletions crates/zksync/core/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ use url::Url;
use zksync_basic_types::U256;
use zksync_types::H256;

/// Max l2 gas limit to use in transactions. Determined empirically to be good enough
/// for all use cases.
pub const MAX_L2_GAS_LIMIT: u64 = ((u32::MAX >> 1) as u64) * 2;

/// Gets the RPC URL for Ethereum.
///
/// If the `eth.rpc_url` is `None`, an error is returned.
Expand Down Expand Up @@ -112,11 +116,6 @@ pub fn fix_l2_gas_price(gas_price: U256) -> U256 {
}

/// Limits the gas_limit proportional to a user's available balance given the gas_price.
///
/// Additionally, fixes the gas limit to be maximum of 2^31 * 2, which is below the VM gas limit of
/// 2^32. This is required so the bootloader does not throw an error for not having enough balance
/// to pay for gas.
///
/// TODO: Remove this later to allow for dynamic gas prices that work in both tests and scripts.
pub fn fix_l2_gas_limit(
proposed_gas_limit: U256,
Expand All @@ -131,5 +130,5 @@ pub fn fix_l2_gas_limit(
U256::min(proposed_gas_limit, max_gas_limit)
};

U256::min(gas_limit, U256::from((u32::MAX >> 1) * 2))
U256::min(gas_limit, U256::from(MAX_L2_GAS_LIMIT))
}
8 changes: 5 additions & 3 deletions crates/zksync/core/src/vm/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ use zksync_types::{
};
use zksync_utils::h256_to_u256;

// https://github.com/matter-labs/era-contracts/blob/aafee035db892689df3f7afe4b89fd6467a39313/system-contracts/bootloader/bootloader.yul#L86
const MAX_L2_GAS_PER_PUBDATA: u64 = 50000;

#[derive(Debug, Clone)]
/// Values related to the era vm environment
pub struct ZkEnv {
Expand All @@ -34,11 +37,10 @@ impl Default for ZkEnv {
impl ZkEnv {
/// Compute gas per pubdata
pub fn gas_per_pubdata(&self) -> u64 {
// https://github.com/matter-labs/era-contracts/blob/aafee035db892689df3f7afe4b89fd6467a39313/system-contracts/bootloader/bootloader.yul#L86
let max_l2_gas_per_pubdata: u64 = 50000;
// source: https://github.com/matter-labs/era-contracts/blob/aafee035db892689df3f7afe4b89fd6467a39313/system-contracts/bootloader/bootloader.yul#L59
let base_fee = std::cmp::max(
self.fair_l2_gas_price,
self.fair_pubdata_price.div_ceil(max_l2_gas_per_pubdata),
self.fair_pubdata_price.div_ceil(MAX_L2_GAS_PER_PUBDATA),
);
if base_fee == 0 {
0
Expand Down

0 comments on commit c6cb287

Please sign in to comment.