Skip to content

Commit

Permalink
Merge pull request #727 from matter-labs/elfedy-fix-wallet-script
Browse files Browse the repository at this point in the history
fix: compute stored test address after setting the tx nonce on scripts
  • Loading branch information
elfedy authored Nov 19, 2024
2 parents ffc7855 + ee03056 commit dc26b62
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 1 deletion.
3 changes: 3 additions & 0 deletions crates/forge/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/forge/tests/fixtures/zk/test_zksync_keystore
Original file line number Diff line number Diff line change
@@ -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}
1 change: 1 addition & 0 deletions crates/forge/tests/it/zk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@ mod ownership;
mod paymaster;
mod proxy;
mod repros;
mod script;
mod traces;
55 changes: 55 additions & 0 deletions crates/forge/tests/it/zk/script.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
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 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();
});
11 changes: 10 additions & 1 deletion crates/script/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -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,
Expand All @@ -174,6 +181,8 @@ 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 {
match self.executor.setup(Some(self.evm_opts.sender), address, None) {
Expand Down

0 comments on commit dc26b62

Please sign in to comment.