diff --git a/crates/forge/tests/it/zk/factory_deps.rs b/crates/forge/tests/it/zk/factory_deps.rs index 60daa8007..1037943c2 100644 --- a/crates/forge/tests/it/zk/factory_deps.rs +++ b/crates/forge/tests/it/zk/factory_deps.rs @@ -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}; @@ -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", diff --git a/crates/zksync/core/src/utils.rs b/crates/zksync/core/src/utils.rs index 45d0e1049..403a4a0ef 100644 --- a/crates/zksync/core/src/utils.rs +++ b/crates/zksync/core/src/utils.rs @@ -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. @@ -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, @@ -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)) } diff --git a/crates/zksync/core/src/vm/env.rs b/crates/zksync/core/src/vm/env.rs index 344118694..51e3e18b7 100644 --- a/crates/zksync/core/src/vm/env.rs +++ b/crates/zksync/core/src/vm/env.rs @@ -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 { @@ -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