Skip to content

Commit

Permalink
simplify approach
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrigada committed Jan 20, 2025
1 parent c9bedce commit b0c1a0a
Show file tree
Hide file tree
Showing 12 changed files with 19 additions and 60 deletions.
2 changes: 1 addition & 1 deletion crates/cast/bin/cmd/send.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ async fn cast_send_zk<P: Provider<T, AnyNetwork>, Z: ZksyncProvider<T>, T: Trans
zk_tx.set_paymaster_params(paymaster_params);
}

foundry_zksync_core::estimate_fee(&mut zk_tx, &zk_provider, 130).await?;
foundry_zksync_core::estimate_fee(&mut zk_tx, &zk_provider, 130, None).await?;

let cast = ZkCast::new(zk_provider, Cast::new(provider));
let pending_tx = cast.send_zk(zk_tx).await?;
Expand Down
5 changes: 0 additions & 5 deletions crates/cli/src/opts/build/zksync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ pub struct ZkSyncArgs {
)]
#[serde(skip_serializing_if = "Option::is_none")]
pub suppressed_errors: Option<Vec<ZkSolcError>>,

/// Gas per pubdata to be set on transactions when broadcasting on scripts
#[clap(long = "zk-gas-per-pubdata", value_name = "GAS_PER_PUBDATA")]
pub gas_per_pubdata: Option<u64>,
}

impl ZkSyncArgs {
Expand Down Expand Up @@ -176,7 +172,6 @@ impl ZkSyncArgs {
let suppressed_errors =
self.suppressed_errors.clone().map(|values| values.into_iter().collect::<HashSet<_>>());
set_if_some!(suppressed_errors, zksync.suppressed_errors);
set_if_some!(self.gas_per_pubdata, zksync.gas_per_pubdata);

zksync
}
Expand Down
4 changes: 0 additions & 4 deletions crates/config/src/zksync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ pub struct ZkSyncConfig {

// zksolc suppressed errors.
pub suppressed_errors: HashSet<ZkSolcError>,

// Gas per pubdata
pub gas_per_pubdata: Option<u64>,
}

impl Default for ZkSyncConfig {
Expand All @@ -96,7 +93,6 @@ impl Default for ZkSyncConfig {
optimizer_details: Default::default(),
suppressed_errors: Default::default(),
suppressed_warnings: Default::default(),
gas_per_pubdata: Default::default(),
}
}
}
Expand Down
7 changes: 0 additions & 7 deletions crates/evm/evm/src/executors/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,6 @@ pub trait ExecutorStrategyExt {
) {
}

fn zksync_set_gas_per_pubdata(
&self,
_ctx: &mut dyn ExecutorStrategyContext,
_gas_per_pubdata: u64,
) {
}

/// Set the fork environment on the context.
fn zksync_set_fork_env(
&self,
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/bin/cmd/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ impl CreateArgs {
deployer.tx.set_gas_price(gas_price);

// estimate fee
foundry_zksync_core::estimate_fee(&mut deployer.tx, &provider, 130).await?;
foundry_zksync_core::estimate_fee(&mut deployer.tx, &provider, 130, None).await?;

if !is_legacy {
let estimate = provider.estimate_eip1559_fees(None).await.wrap_err("Failed to estimate EIP1559 fees. This chain might not support EIP1559, try adding --legacy to your command.")?;
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/tests/it/zk/gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ forgetest_async!(zk_script_execution_with_gas_multiplier, |prj, cmd| {
cmd.arg("script").args(&sufficient_multiplier_args);
let stdout = cmd.assert_success().get_output().stdout_lossy();
assert!(stdout.contains("ONCHAIN EXECUTION COMPLETE & SUCCESSFUL"));
});
});

forgetest_async!(zk_script_execution_with_gas_per_pubdata, |prj, cmd| {
// Setup
Expand Down
8 changes: 3 additions & 5 deletions crates/script/src/broadcast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub async fn send_transaction(
is_fixed_gas_limit: bool,
estimate_via_rpc: bool,
estimate_multiplier: u64,
gas_per_pubdata: Option<u64>,
) -> Result<TxHash> {
let zk_tx_meta =
if let SendTransactionKind::Raw(tx, _) | SendTransactionKind::Unlocked(tx) = &mut kind {
Expand Down Expand Up @@ -142,11 +143,7 @@ pub async fn send_transaction(
);
}

if let Some(gas_per_pubdata) = zk_tx_meta.gas_per_pubdata {
zk_tx.set_gas_per_pubdata(alloy_primitives::Uint::from(gas_per_pubdata));
}

foundry_zksync_core::estimate_fee(&mut zk_tx, &zk_provider, estimate_multiplier)
foundry_zksync_core::estimate_fee(&mut zk_tx, &zk_provider, estimate_multiplier, gas_per_pubdata)
.await?;

let zk_signer = alloy_zksync::wallet::ZksyncWallet::new(signer.default_signer());
Expand Down Expand Up @@ -445,6 +442,7 @@ impl BundledState {
*is_fixed_gas_limit,
estimate_via_rpc,
self.args.gas_estimate_multiplier,
self.args.gas_per_pubdata,
);
pending_transactions.push(fut);
}
Expand Down
9 changes: 4 additions & 5 deletions crates/script/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,10 @@ pub struct ScriptArgs {

#[command(flatten)]
pub retry: RetryArgs,

/// Gas per pubdata
#[clap(long = "zk-gas-per-pubdata", value_name = "GAS_PER_PUBDATA")]
pub gas_per_pubdata: Option<u64>,
}

impl ScriptArgs {
Expand Down Expand Up @@ -638,11 +642,6 @@ impl ScriptConfig {
strategy.context.as_mut(),
dual_compiled_contracts,
);
if let Some(gas_per_pubdata) = self.config.zksync.gas_per_pubdata {
strategy
.runner
.zksync_set_gas_per_pubdata(strategy.context.as_mut(), gas_per_pubdata);
}

if let Some(fork_url) = &self.evm_opts.fork_url {
strategy.runner.zksync_set_fork_env(strategy.context.as_mut(), fork_url, &env)?;
Expand Down
7 changes: 1 addition & 6 deletions crates/strategy/zksync/src/cheatcode/runner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,6 @@ impl CheatcodeInspectorStrategyRunner for ZksyncCheatcodeInspectorStrategyRunner
paymaster_input: paymaster_data.input.to_vec(),
});

let gas_per_pubdata = ctx.zk_env.gas_per_pubdata;

let rpc = ecx_inner.db.active_fork_url();

let injected_factory_deps = ctx
Expand Down Expand Up @@ -167,7 +165,6 @@ impl CheatcodeInspectorStrategyRunner for ZksyncCheatcodeInspectorStrategyRunner
serde_json::to_value(ZkTransactionMetadata::new(
factory_deps,
paymaster_params.clone(),
gas_per_pubdata,
))
.expect("failed encoding json"),
);
Expand Down Expand Up @@ -195,7 +192,6 @@ impl CheatcodeInspectorStrategyRunner for ZksyncCheatcodeInspectorStrategyRunner
serde_json::to_value(ZkTransactionMetadata::new(
zk_tx_factory_deps,
paymaster_params,
gas_per_pubdata,
))
.expect("failed encoding json"),
);
Expand Down Expand Up @@ -263,15 +259,14 @@ impl CheatcodeInspectorStrategyRunner for ZksyncCheatcodeInspectorStrategyRunner
paymaster: paymaster_data.address.to_h160(),
paymaster_input: paymaster_data.input.to_vec(),
});
let gas_per_pubdata = ctx.zk_env.gas_per_pubdata;
let factory_deps = if call.target_address == DEFAULT_CREATE2_DEPLOYER_ZKSYNC {
// We shouldn't need factory_deps for CALLs
factory_deps.clone()
} else {
// For this case we use only the injected factory deps
injected_factory_deps
};
let zk_tx = ZkTransactionMetadata::new(factory_deps, paymaster_params, gas_per_pubdata);
let zk_tx = ZkTransactionMetadata::new(factory_deps, paymaster_params);
let mut tx_req = TransactionRequest {
from: Some(broadcast.new_origin),
to: Some(TxKind::from(Some(call.target_address))),
Expand Down
10 changes: 0 additions & 10 deletions crates/strategy/zksync/src/executor/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,15 +154,6 @@ impl ExecutorStrategyExt for ZksyncExecutorStrategyRunner {
ctx.dual_compiled_contracts = dual_compiled_contracts;
}

fn zksync_set_gas_per_pubdata(
&self,
ctx: &mut dyn ExecutorStrategyContext,
gas_per_pubdata: u64,
) {
let ctx = get_context(ctx);
ctx.zk_env.gas_per_pubdata = Some(gas_per_pubdata);
}

fn zksync_set_fork_env(
&self,
ctx: &mut dyn ExecutorStrategyContext,
Expand All @@ -182,7 +173,6 @@ impl ExecutorStrategyExt for ZksyncExecutorStrategyRunner {

if let Some(block_details) = maybe_block_details {
ctx.zk_env = ZkEnv {
gas_per_pubdata: ctx.zk_env.gas_per_pubdata,
l1_gas_price: block_details
.l1_gas_price
.try_into()
Expand Down
20 changes: 8 additions & 12 deletions crates/zksync/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,12 @@ pub struct ZkTransactionMetadata {
pub factory_deps: Vec<Vec<u8>>,
/// Paymaster data for ZK transactions.
pub paymaster_data: Option<PaymasterParams>,
/// Gas per pubdata for ZK transactions.
pub gas_per_pubdata: Option<u64>,
}

impl ZkTransactionMetadata {
/// Create a new [`ZkTransactionMetadata`] with the given factory deps
pub fn new(
factory_deps: Vec<Vec<u8>>,
paymaster_data: Option<PaymasterParams>,
gas_per_pubdata: Option<u64>,
) -> Self {
Self { factory_deps, paymaster_data, gas_per_pubdata }
pub fn new(factory_deps: Vec<Vec<u8>>, paymaster_data: Option<PaymasterParams>) -> Self {
Self { factory_deps, paymaster_data }
}
}
/// Estimated gas from a ZK network.
Expand All @@ -141,18 +135,20 @@ pub async fn estimate_fee<P: ZksyncProvider<T>, T: Transport + Clone>(
tx: &mut ZkTransactionRequest,
provider: P,
estimate_multiplier: u64,
gas_per_pubdata: Option<u64>,
) -> Result<()> {
let fee = provider.estimate_fee(tx.clone()).await?;
tx.set_gas_limit(fee.gas_limit * estimate_multiplier / 100);
// If user provided a gas price, use it for both maxFeePerGas
let max_fee = tx.max_fee_per_gas().unwrap_or(fee.max_fee_per_gas);
let max_priority_fee = tx.max_priority_fee_per_gas().unwrap_or(fee.max_priority_fee_per_gas);
let gas_per_pubdata = match gas_per_pubdata {
Some(value) => rU256::from(value),
None => fee.gas_per_pubdata_limit,
};
tx.set_max_fee_per_gas(max_fee);
tx.set_max_priority_fee_per_gas(max_priority_fee);
match tx.gas_per_pubdata() {
Some(v) if !v.is_zero() => (),
_ => tx.set_gas_per_pubdata(fee.gas_per_pubdata_limit),
}
tx.set_gas_per_pubdata(gas_per_pubdata);

Ok(())
}
Expand Down
3 changes: 0 additions & 3 deletions crates/zksync/core/src/vm/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ pub struct ZkEnv {
pub fair_l2_gas_price: u64,
/// fair pubdata price
pub fair_pubdata_price: u64,
/// gas per pubdata
pub gas_per_pubdata: Option<u64>,
}

impl Default for ZkEnv {
Expand All @@ -35,7 +33,6 @@ impl Default for ZkEnv {
l1_gas_price: 0,
fair_l2_gas_price: 0,
fair_pubdata_price: 1000,
gas_per_pubdata: None,
}
}
}
Expand Down

0 comments on commit b0c1a0a

Please sign in to comment.