From b40ebe18341f8000fb840028049a757d953ff094 Mon Sep 17 00:00:00 2001 From: elfedy Date: Thu, 14 Nov 2024 15:04:34 -0300 Subject: [PATCH 1/3] fix: compute stored test address after setting the tx nonce on scripts --- crates/forge/src/runner.rs | 3 + .../tests/fixtures/zk/test_zksync_keystore | 1 + crates/forge/tests/it/zk/mod.rs | 1 + crates/forge/tests/it/zk/script.rs | 57 +++++++++++++++++++ crates/script/src/runner.rs | 10 +++- 5 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 crates/forge/tests/fixtures/zk/test_zksync_keystore create mode 100644 crates/forge/tests/it/zk/script.rs diff --git a/crates/forge/src/runner.rs b/crates/forge/src/runner.rs index 7d5330fe4..566093b71 100644 --- a/crates/forge/src/runner.rs +++ b/crates/forge/src/runner.rs @@ -114,6 +114,9 @@ impl ContractRunner<'_> { } let address = self.sender.create(self.executor.get_nonce(self.sender)?); + // NOTE(zk): the test contract is set here instead of where upstream does it as + // the test contract address needs to be retrieved in order to skip + // zkEVM mode for the creation of the test address (and for calls to it later). self.executor.backend_mut().set_test_contract(address); // Set the contracts initial balance before deployment, so it is available during diff --git a/crates/forge/tests/fixtures/zk/test_zksync_keystore b/crates/forge/tests/fixtures/zk/test_zksync_keystore new file mode 100644 index 000000000..1bb13fe59 --- /dev/null +++ b/crates/forge/tests/fixtures/zk/test_zksync_keystore @@ -0,0 +1 @@ +{"crypto":{"cipher":"aes-128-ctr","cipherparams":{"iv":"baf20aa15d09d4ba2fea20a4f45f3b74"},"ciphertext":"19cbaecaf374fcab41fa5899990b904441e0762899ef3727a5a667398db8b7d8","kdf":"scrypt","kdfparams":{"dklen":32,"n":8192,"p":1,"r":8,"salt":"625b00531450f22ae8f74c9df7582150158fe6a88d9a214d61ec80a328e0601a"},"mac":"1c6f106535db170ab8c838abc6c33f019e35663808812f2c793bb00fd23e4baa"},"id":"f651e741-d76b-4583-b5f2-3ca0b4a697a2","version":3} \ No newline at end of file diff --git a/crates/forge/tests/it/zk/mod.rs b/crates/forge/tests/it/zk/mod.rs index bbe6cda52..52a20905f 100644 --- a/crates/forge/tests/it/zk/mod.rs +++ b/crates/forge/tests/it/zk/mod.rs @@ -16,4 +16,5 @@ mod ownership; mod paymaster; mod proxy; mod repros; +mod script; mod traces; diff --git a/crates/forge/tests/it/zk/script.rs b/crates/forge/tests/it/zk/script.rs new file mode 100644 index 000000000..7fbd6d6f0 --- /dev/null +++ b/crates/forge/tests/it/zk/script.rs @@ -0,0 +1,57 @@ +use foundry_test_utils::{ + forgetest_async, + util::{self, OutputExt}, + ZkSyncNode, +}; +use std::path::Path; + +forgetest_async!(test_zk_can_broadcast_with_keystore_account, |prj, cmd| { + util::initialize(prj.root()); + prj.add_script("Deploy.s.sol", include_str!("../../fixtures/zk/Deploy.s.sol")).unwrap(); + prj.add_source("Greeter.sol", include_str!("../../../../../testdata/zk/Greeter.sol")).unwrap(); + + let config = cmd.config(); + prj.write_config(config); + let node = ZkSyncNode::start(); + let url = node.url(); + + cmd.forge_fuse(); + + let script_path_contract = "./script/Deploy.s.sol:DeployScript"; + let keystore_path = + Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/zk/test_zksync_keystore"); + + let script_args = vec![ + "--zk-startup", + &script_path_contract, + "--broadcast", + "--keystores", + keystore_path.to_str().unwrap(), + "--password", + "password", + "--chain", + "260", + "--gas-estimate-multiplier", + "310", + "--rpc-url", + url.as_str(), + "--slow", + ]; + + cmd.arg("script").args(&script_args); + + cmd.assert_success() + .get_output() + .stdout_lossy() + .contains("ONCHAIN EXECUTION COMPLETE & SUCCESSFUL"); + + let run_latest = foundry_common::fs::json_files(prj.root().join("broadcast").as_path()) + .find(|file| file.ends_with("run-latest.json")) + .expect("No broadcast artifacts"); + + let content = foundry_common::fs::read_to_string(run_latest).unwrap(); + + let json: serde_json::Value = serde_json::from_str(&content).unwrap(); + assert_eq!(json["transactions"].as_array().expect("broadcastable txs").len(), 3); + cmd.forge_fuse(); +}); diff --git a/crates/script/src/runner.rs b/crates/script/src/runner.rs index 52e5efe31..8a698b72e 100644 --- a/crates/script/src/runner.rs +++ b/crates/script/src/runner.rs @@ -132,7 +132,6 @@ impl ScriptRunner { let address = CALLER.create(self.executor.get_nonce(CALLER)?); - self.executor.backend_mut().set_test_contract(address); // Set the contracts initial balance before deployment, so it is available during the // construction self.executor.set_balance(address, self.evm_opts.initial_balance)?; @@ -148,6 +147,14 @@ impl ScriptRunner { self.executor.set_nonce(self.evm_opts.sender, u64::MAX / 2)?; } + // NOTE(zk): Address recomputed again after the nonce modification as it will + // differ in case of evm_opts.sender == CALLER + let zk_actual_script_address = CALLER.create(self.executor.get_nonce(CALLER)?); + // NOTE(zk): the test contract is set here instead of where upstream does it as + // the test contract address needs to be retrieved in order to skip + // zkEVM mode for the creation of the test address (and for calls to it later). + self.executor.backend_mut().set_test_contract(zk_actual_script_address); + // Deploy an instance of the contract let DeployResult { address, @@ -165,6 +172,7 @@ impl ScriptRunner { // Optionally call the `setUp` function let (success, gas_used, labeled_addresses, transactions) = if !setup { + // self.executor.backend_mut().set_test_contract(address); (true, 0, Default::default(), Some(library_transactions)) } else { match self.executor.setup(Some(self.evm_opts.sender), address, None) { From 06f385887891ed16485991a65fe5c3ab5589c415 Mon Sep 17 00:00:00 2001 From: elfedy Date: Fri, 15 Nov 2024 09:35:25 -0300 Subject: [PATCH 2/3] chore: do not write config in test --- crates/forge/tests/it/zk/script.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/crates/forge/tests/it/zk/script.rs b/crates/forge/tests/it/zk/script.rs index 7fbd6d6f0..0cb9f13d5 100644 --- a/crates/forge/tests/it/zk/script.rs +++ b/crates/forge/tests/it/zk/script.rs @@ -10,8 +10,6 @@ forgetest_async!(test_zk_can_broadcast_with_keystore_account, |prj, cmd| { prj.add_script("Deploy.s.sol", include_str!("../../fixtures/zk/Deploy.s.sol")).unwrap(); prj.add_source("Greeter.sol", include_str!("../../../../../testdata/zk/Greeter.sol")).unwrap(); - let config = cmd.config(); - prj.write_config(config); let node = ZkSyncNode::start(); let url = node.url(); From ee03056350ecd2e6948f894059a3ff1d338155f0 Mon Sep 17 00:00:00 2001 From: elfedy Date: Fri, 15 Nov 2024 09:48:42 -0300 Subject: [PATCH 3/3] Add clarifying comment for commented upstream code --- crates/script/src/runner.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/crates/script/src/runner.rs b/crates/script/src/runner.rs index 8a698b72e..9d3b078e6 100644 --- a/crates/script/src/runner.rs +++ b/crates/script/src/runner.rs @@ -172,6 +172,7 @@ impl ScriptRunner { // Optionally call the `setUp` function let (success, gas_used, labeled_addresses, transactions) = if !setup { + // NOTE(zk): keeping upstream code for context. Test contract is only set on this branch // self.executor.backend_mut().set_test_contract(address); (true, 0, Default::default(), Some(library_transactions)) } else {