From 709331137b3d026765f4bb9747b5e257cd082bda Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Tue, 5 Nov 2024 20:41:42 +0100 Subject: [PATCH 01/16] fix(compiler:zk): `missing_libraries` not optional --- crates/common/src/compile.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/common/src/compile.rs b/crates/common/src/compile.rs index d1786fc8f..b94185dc7 100644 --- a/crates/common/src/compile.rs +++ b/crates/common/src/compile.rs @@ -400,7 +400,7 @@ impl ProjectCompiler { let is_target_file = self.files.is_empty() || self.files.iter().any(|f| artifact_id.path == *f); if is_target_file { - if let Some(mls) = &artifact.missing_libraries { + if let Some(mls) = artifact.bytecode.as_ref().map(|bc| &bc.missing_libraries) { missing_libs_unique.extend(mls.clone()); } } @@ -428,7 +428,7 @@ impl ProjectCompiler { ZkMissingLibrary { contract_path: contract_path.to_string(), contract_name: contract_name.to_string(), - missing_libraries: art.missing_libraries.clone().unwrap_or_default(), + missing_libraries: art.bytecode.as_ref().map(|bc| &bc.missing_libraries).cloned().unwrap_or_default(), } }) .collect(); From e60b3876afe3bae1ae4d5e44e1f3d0e33a99888b Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Wed, 6 Nov 2024 20:42:12 +0100 Subject: [PATCH 02/16] chore: default to zksolc 1.5.7 --- crates/forge/tests/it/test_helpers.rs | 2 +- crates/zksync/compiler/src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/forge/tests/it/test_helpers.rs b/crates/forge/tests/it/test_helpers.rs index 8f1a3ad07..afb8b1256 100644 --- a/crates/forge/tests/it/test_helpers.rs +++ b/crates/forge/tests/it/test_helpers.rs @@ -213,7 +213,7 @@ impl ForgeTestProfile { zk_config.zksync.startup = true; zk_config.zksync.fallback_oz = true; zk_config.zksync.optimizer_mode = '3'; - zk_config.zksync.zksolc = Some(foundry_config::SolcReq::Version(Version::new(1, 5, 4))); + zk_config.zksync.zksolc = Some(foundry_config::SolcReq::Version(Version::new(1, 5, 7))); zk_config.fuzz.no_zksync_reserved_addresses = true; zk_config diff --git a/crates/zksync/compiler/src/lib.rs b/crates/zksync/compiler/src/lib.rs index 194ed318c..69b086b04 100644 --- a/crates/zksync/compiler/src/lib.rs +++ b/crates/zksync/compiler/src/lib.rs @@ -76,7 +76,7 @@ pub fn config_create_project( { zksolc } else if !config.offline { - let default_version = semver::Version::new(1, 5, 4); + let default_version = semver::Version::new(1, 5, 7); let mut zksolc = ZkSolc::find_installed_version(&default_version)?; if zksolc.is_none() { ZkSolc::blocking_install(&default_version)?; From 7d63cf5c1260f3469e0a0dc8722114325ebd6042 Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Thu, 7 Nov 2024 12:13:58 +0100 Subject: [PATCH 03/16] fix(compilers:zk): use `missing_libraries` method --- crates/common/src/compile.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/common/src/compile.rs b/crates/common/src/compile.rs index b94185dc7..ed1f532d5 100644 --- a/crates/common/src/compile.rs +++ b/crates/common/src/compile.rs @@ -400,7 +400,7 @@ impl ProjectCompiler { let is_target_file = self.files.is_empty() || self.files.iter().any(|f| artifact_id.path == *f); if is_target_file { - if let Some(mls) = artifact.bytecode.as_ref().map(|bc| &bc.missing_libraries) { + if let Some(mls) = artifact.missing_libraries() { missing_libs_unique.extend(mls.clone()); } } @@ -428,7 +428,7 @@ impl ProjectCompiler { ZkMissingLibrary { contract_path: contract_path.to_string(), contract_name: contract_name.to_string(), - missing_libraries: art.bytecode.as_ref().map(|bc| &bc.missing_libraries).cloned().unwrap_or_default(), + missing_libraries: art.missing_libraries().cloned().unwrap_or_default(), } }) .collect(); From c7735c8099b0626e5f165b27ed0abe875a76bdc9 Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Fri, 8 Nov 2024 13:59:42 +0100 Subject: [PATCH 04/16] chore: update lockfile --- Cargo.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b95f61895..f8b73ea74 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5116,7 +5116,7 @@ dependencies = [ [[package]] name = "foundry-compilers" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#7c69695e5c75451f158dd2456bf8c94a7492ea0b" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -5156,7 +5156,7 @@ dependencies = [ [[package]] name = "foundry-compilers-artifacts" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#7c69695e5c75451f158dd2456bf8c94a7492ea0b" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" dependencies = [ "foundry-compilers-artifacts-solc", "foundry-compilers-artifacts-vyper", @@ -5166,7 +5166,7 @@ dependencies = [ [[package]] name = "foundry-compilers-artifacts-solc" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#7c69695e5c75451f158dd2456bf8c94a7492ea0b" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -5189,7 +5189,7 @@ dependencies = [ [[package]] name = "foundry-compilers-artifacts-vyper" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#7c69695e5c75451f158dd2456bf8c94a7492ea0b" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -5203,7 +5203,7 @@ dependencies = [ [[package]] name = "foundry-compilers-artifacts-zksolc" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#7c69695e5c75451f158dd2456bf8c94a7492ea0b" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -5224,7 +5224,7 @@ dependencies = [ [[package]] name = "foundry-compilers-core" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#7c69695e5c75451f158dd2456bf8c94a7492ea0b" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" dependencies = [ "alloy-primitives", "cfg-if 1.0.0", From 528c1139a499e6bb971ed98b373ef64361379e8b Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Mon, 11 Nov 2024 13:04:46 +0100 Subject: [PATCH 05/16] feat(create:zk): detect unlinked contract fix(compiler:zk): use `bytecode` method on artifact --- crates/forge/bin/cmd/create.rs | 30 +++++++++++++++++++----- crates/forge/src/multi_runner.rs | 3 ++- crates/script/src/build.rs | 3 ++- crates/zksync/compiler/src/zksolc/mod.rs | 4 ++-- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/crates/forge/bin/cmd/create.rs b/crates/forge/bin/cmd/create.rs index 0b3bf3e8a..e911bd012 100644 --- a/crates/forge/bin/cmd/create.rs +++ b/crates/forge/bin/cmd/create.rs @@ -159,10 +159,28 @@ impl CreateArgs { let ZkContractArtifact { bytecode, hash, factory_dependencies, abi, .. } = artifact; let abi = abi.expect("Abi not found"); - let bin = bytecode.expect("Bytecode not found"); + + let bytecode = match bin.object() { + BytecodeObject::Bytecode(bytes) => bytes.to_vec(), + _ => { + let link_refs = bin + .missing_libraries + .iter() + .map(|library| { + let mut parts = library.split(':'); + let path = parts.next().unwrap(); + let name = parts.next().unwrap(); + format!("\t{name}: {path}") + }) + .collect::>() + .into_iter() + .collect::>() + .join("\n"); + eyre::bail!("Dynamic linking not supported in `create` command - deploy the following library contracts first, then provide the address to link at compile time\n{}", link_refs) + } + }; let bytecode_hash = H256::from_str(&hash.expect("Contract hash not found"))?; - let bytecode = bin.object.clone().into_bytes().unwrap().to_vec(); // Add arguments to constructor let config = self.eth.try_load_config_emit_warnings()?; @@ -222,12 +240,12 @@ impl CreateArgs { queue.push_back(dep.clone()) } + //TODO(zk): ensure factory deps are also linked let fdep_bytecode = fdep_art .bytecode .clone() .expect("Bytecode not found for factory dependency") - .object - .clone() + .object() .into_bytes() .unwrap() .to_vec(); @@ -244,7 +262,7 @@ impl CreateArgs { let sender = self.eth.wallet.from.expect("required"); self.deploy_zk( abi, - bin.object, + bin.object(), params, provider, chain_id, @@ -263,7 +281,7 @@ impl CreateArgs { let provider = ProviderBuilder::<_, _, AnyNetwork>::default().on_provider(provider); self.deploy_zk( abi, - bin.object, + bin.object(), params, provider, chain_id, diff --git a/crates/forge/src/multi_runner.rs b/crates/forge/src/multi_runner.rs index 66fb2e3e1..15ca3b4f6 100644 --- a/crates/forge/src/multi_runner.rs +++ b/crates/forge/src/multi_runner.rs @@ -467,7 +467,8 @@ impl MultiContractRunnerBuilder { if let Some(abi) = contract.abi { let bytecode = contract.bytecode.as_ref(); - if let Some(bytecode_object) = bytecode.map(|b| b.object.clone()) { + //TODO(zk): retrieve link_references + if let Some(bytecode_object) = bytecode.map(|b| b.object()) { let compact_bytecode = CompactBytecode { object: bytecode_object.clone(), source_map: None, diff --git a/crates/script/src/build.rs b/crates/script/src/build.rs index a82dd994a..80938c514 100644 --- a/crates/script/src/build.rs +++ b/crates/script/src/build.rs @@ -154,7 +154,8 @@ impl LinkedBuildData { for (id, contract) in zk_contracts { if let Some(abi) = contract.abi { let bytecode = contract.bytecode.as_ref(); - if let Some(bytecode_object) = bytecode.map(|b| b.object.clone()) { + //TODO(zk): retrieve link_references + if let Some(bytecode_object) = bytecode.map(|b| b.object()) { let compact_bytecode = CompactBytecode { object: bytecode_object.clone(), source_map: None, diff --git a/crates/zksync/compiler/src/zksolc/mod.rs b/crates/zksync/compiler/src/zksolc/mod.rs index 974b881ec..f4e3b0699 100644 --- a/crates/zksync/compiler/src/zksolc/mod.rs +++ b/crates/zksync/compiler/src/zksolc/mod.rs @@ -136,7 +136,7 @@ impl DualCompiledContracts { // at this stage for zksolc, and BytecodeObject as ref will get the bytecode bytes. // We should be careful however and check/handle errors in // case an Unlinked BytecodeObject gets here somehow - let bytes = bytecode.object.clone().into_bytes().unwrap(); + let bytes = bytecode.object().into_bytes().unwrap(); zksolc_all_bytecodes.insert(hash.clone(), bytes.to_vec()); } } @@ -167,7 +167,7 @@ impl DualCompiledContracts { // bytes. However, we should check and // handle errors in case an Unlinked BytecodeObject gets // here somehow - let bytecode_vec = bytecode.object.clone().into_bytes().unwrap().to_vec(); + let bytecode_vec = bytecode.object().into_bytes().unwrap().to_vec(); let mut factory_deps_vec: Vec> = factory_deps_map .keys() .map(|factory_hash| { From 79da00068a73658682e49eb377811a63574365b6 Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Mon, 11 Nov 2024 15:13:55 +0100 Subject: [PATCH 06/16] chore: bump lockfile --- Cargo.lock | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f8b73ea74..264d9bf1d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5116,7 +5116,7 @@ dependencies = [ [[package]] name = "foundry-compilers" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#4f9527f5104c4ed5d5e67cb53404fd054962d416" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -5156,7 +5156,7 @@ dependencies = [ [[package]] name = "foundry-compilers-artifacts" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#4f9527f5104c4ed5d5e67cb53404fd054962d416" dependencies = [ "foundry-compilers-artifacts-solc", "foundry-compilers-artifacts-vyper", @@ -5166,7 +5166,7 @@ dependencies = [ [[package]] name = "foundry-compilers-artifacts-solc" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#4f9527f5104c4ed5d5e67cb53404fd054962d416" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -5189,7 +5189,7 @@ dependencies = [ [[package]] name = "foundry-compilers-artifacts-vyper" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#4f9527f5104c4ed5d5e67cb53404fd054962d416" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -5203,7 +5203,7 @@ dependencies = [ [[package]] name = "foundry-compilers-artifacts-zksolc" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#4f9527f5104c4ed5d5e67cb53404fd054962d416" dependencies = [ "alloy-json-abi", "alloy-primitives", @@ -5224,7 +5224,7 @@ dependencies = [ [[package]] name = "foundry-compilers-core" version = "0.11.6" -source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#9f992849169e6fbe62c9fec73da219303143a91d" +source = "git+https://github.com/Moonsong-Labs/compilers?branch=zksync-v0.11.6#4f9527f5104c4ed5d5e67cb53404fd054962d416" dependencies = [ "alloy-primitives", "cfg-if 1.0.0", From 0e80e10e96dd1f9099a79556ea0c1135163c6a2c Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Mon, 11 Nov 2024 16:11:12 +0100 Subject: [PATCH 07/16] test(zk): add tests with unlinked contracts --- .../forge/tests/fixtures/zk/Libraries.s.sol | 13 +++++ crates/forge/tests/it/zk/linking.rs | 54 +++++++++++++++++++ crates/forge/tests/it/zk/mod.rs | 1 + testdata/zk/WithLibraries.sol | 17 ++++++ 4 files changed, 85 insertions(+) create mode 100644 crates/forge/tests/fixtures/zk/Libraries.s.sol create mode 100644 crates/forge/tests/it/zk/linking.rs create mode 100644 testdata/zk/WithLibraries.sol diff --git a/crates/forge/tests/fixtures/zk/Libraries.s.sol b/crates/forge/tests/fixtures/zk/Libraries.s.sol new file mode 100644 index 000000000..195d4dd7b --- /dev/null +++ b/crates/forge/tests/fixtures/zk/Libraries.s.sol @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: UNLICENSED + +pragma solidity >=0.8.7 <0.9.0; + +import {UsesFoo} from "../src/WithLibraries.sol"; +import "forge-std/Script.sol"; + +contract DeployUsesFoo is Script { + function run () external { + // should fail because `UsesFoo` is unlinked + bytes memory _code = vm.getCode("UsesFoo"); + } +} diff --git a/crates/forge/tests/it/zk/linking.rs b/crates/forge/tests/it/zk/linking.rs new file mode 100644 index 000000000..7ebb21399 --- /dev/null +++ b/crates/forge/tests/it/zk/linking.rs @@ -0,0 +1,54 @@ +use foundry_test_utils::{forgetest_async, util, TestProject}; + +use crate::test_helpers::{deploy_zk_contract, run_zk_script_test}; + +//TODO(zk): add test that actually does the deployment +// of the unlinked contract via script, once recursive linking is supported +// and once we also support doing deploy-time linking + +forgetest_async!( + #[should_panic = "no bytecode for contract; is it abstract or unlinked?"] + script_using_unlinked_fails, + |prj, cmd| { + setup_libs_prj(&mut prj); + run_zk_script_test( + prj.root(), + &mut cmd, + "./script/Libraries.s.sol", + "DeployUsesFoo", + None, + 1, + Some(&["-vvvvv"]), + ); + } +); + +forgetest_async!( + #[should_panic = "Dynamic linking not supported"] + create_using_unlinked_fails, + |prj, cmd| { + setup_libs_prj(&mut prj); + + // we don't really connect to the rpc because + // we expect to fail before that point + let foo_address = deploy_zk_contract( + &mut cmd, + "127.0.0.1:1234", + "0x0000000000000000000000000000000000000000000000000000000000000000", + "./src/WithLibraries.sol:UsesFoo", + ) + .expect("Failed to deploy UsesFoo contract"); + + assert!(!foo_address.is_empty(), "Deployed address should not be empty"); + } +); + +fn setup_libs_prj(prj: &mut TestProject) { + util::initialize(prj.root()); + prj.add_script("Libraries.s.sol", include_str!("../../fixtures/zk/Libraries.s.sol")).unwrap(); + prj.add_source( + "WithLibraries.sol", + include_str!("../../../../../testdata/zk/WithLibraries.sol"), + ) + .unwrap(); +} diff --git a/crates/forge/tests/it/zk/mod.rs b/crates/forge/tests/it/zk/mod.rs index bbe6cda52..6ee28853d 100644 --- a/crates/forge/tests/it/zk/mod.rs +++ b/crates/forge/tests/it/zk/mod.rs @@ -10,6 +10,7 @@ mod factory_deps; mod fork; mod fuzz; mod invariant; +mod linking; mod logs; mod nft; mod ownership; diff --git a/testdata/zk/WithLibraries.sol b/testdata/zk/WithLibraries.sol new file mode 100644 index 000000000..8d54da736 --- /dev/null +++ b/testdata/zk/WithLibraries.sol @@ -0,0 +1,17 @@ +// SPDX-License-Identifier: UNLICENSED + +pragma solidity >=0.8.7 <0.9.0; + +import "forge-std/console.sol"; + +library Foo { + function foo() external pure { + console.log("I'm a library"); + } +} + +contract UsesFoo { + constructor() { + Foo.foo(); + } +} From 5a56c87bc8e55172d50a44aefbdb814e47d5ed8d Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Mon, 11 Nov 2024 17:00:09 +0100 Subject: [PATCH 08/16] chore: fmt chore: add 2 more unmaintained advisories --- deny.toml | 5 +++++ testdata/zk/WithLibraries.sol | 6 +++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/deny.toml b/deny.toml index 303bfec5b..ef0dd7003 100644 --- a/deny.toml +++ b/deny.toml @@ -17,6 +17,11 @@ ignore = [ # Used by alloy # proc-macro-error is unmaintained "RUSTSEC-2024-0370", + # instant is unmaintained + "RUSTSEC-2024-0384", + # Used by boojum + # derivative is unmaintained + "RUSTSEC-2024-0388", ] # This section is considered when running `cargo deny check bans`. diff --git a/testdata/zk/WithLibraries.sol b/testdata/zk/WithLibraries.sol index 8d54da736..228dd25da 100644 --- a/testdata/zk/WithLibraries.sol +++ b/testdata/zk/WithLibraries.sol @@ -5,9 +5,9 @@ pragma solidity >=0.8.7 <0.9.0; import "forge-std/console.sol"; library Foo { - function foo() external pure { - console.log("I'm a library"); - } + function foo() external pure { + console.log("I'm a library"); + } } contract UsesFoo { From 94004c40f128d51c74157b0db68b01596ee41414 Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Mon, 11 Nov 2024 17:23:29 +0100 Subject: [PATCH 09/16] fix(test:zk): remove console dep --- testdata/zk/WithLibraries.sol | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/testdata/zk/WithLibraries.sol b/testdata/zk/WithLibraries.sol index 228dd25da..9e74f0e26 100644 --- a/testdata/zk/WithLibraries.sol +++ b/testdata/zk/WithLibraries.sol @@ -2,16 +2,16 @@ pragma solidity >=0.8.7 <0.9.0; -import "forge-std/console.sol"; - library Foo { - function foo() external pure { - console.log("I'm a library"); + function add(uint a, uint b) external pure returns (uint c){ + c = a + b; } } contract UsesFoo { + uint number; + constructor() { - Foo.foo(); + number = Foo.add(42, 0); } } From 84d5b5160d89737eb72090cb2175009bb1dc5bd3 Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Mon, 11 Nov 2024 17:29:08 +0100 Subject: [PATCH 10/16] chore: fmt --- testdata/zk/WithLibraries.sol | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/testdata/zk/WithLibraries.sol b/testdata/zk/WithLibraries.sol index 9e74f0e26..51ca18879 100644 --- a/testdata/zk/WithLibraries.sol +++ b/testdata/zk/WithLibraries.sol @@ -3,13 +3,13 @@ pragma solidity >=0.8.7 <0.9.0; library Foo { - function add(uint a, uint b) external pure returns (uint c){ + function add(uint256 a, uint256 b) external pure returns (uint256 c) { c = a + b; } } contract UsesFoo { - uint number; + uint256 number; constructor() { number = Foo.add(42, 0); From 71ca065bd944c4955c704ca1e4fd8dedeb88cd93 Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Fri, 15 Nov 2024 12:17:07 +0100 Subject: [PATCH 11/16] feat(inspector:zk): delay startup migration --- crates/cheatcodes/src/inspector.rs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/crates/cheatcodes/src/inspector.rs b/crates/cheatcodes/src/inspector.rs index aad1f7dfd..d481f6a20 100644 --- a/crates/cheatcodes/src/inspector.rs +++ b/crates/cheatcodes/src/inspector.rs @@ -1473,8 +1473,14 @@ where { }; let prev = account.info.nonce; account.info.nonce = prev.saturating_sub(1); + let nonce = account.info.nonce; - trace!(target: "cheatcodes", %sender, nonce=account.info.nonce, prev, "corrected nonce"); + if self.startup_zk { + self.startup_zk = false; // We only do this once. + self.select_zk_vm(ecx_inner, None); + } + + trace!(target: "cheatcodes", %sender, nonce, prev, "corrected nonce"); } if call.target_address == CHEATCODE_ADDRESS { @@ -1974,11 +1980,6 @@ impl Inspector<&mut dyn DatabaseExt> for Cheatcodes { ecx.env.tx.gas_price = gas_price; } - if self.startup_zk && !self.use_zk_vm { - self.startup_zk = false; // We only do this once. - self.select_zk_vm(ecx, None); - } - // Record gas for current frame. if self.gas_metering.paused { self.gas_metering.paused_frames.push(interpreter.gas); From 2d191bd1246746af8c27d05ae67e5192be885fd2 Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Fri, 15 Nov 2024 12:17:54 +0100 Subject: [PATCH 12/16] fix(inspector:zk): test contract code migration --- crates/cheatcodes/src/inspector.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/crates/cheatcodes/src/inspector.rs b/crates/cheatcodes/src/inspector.rs index d481f6a20..150183798 100644 --- a/crates/cheatcodes/src/inspector.rs +++ b/crates/cheatcodes/src/inspector.rs @@ -871,6 +871,8 @@ impl Cheatcodes { let mut known_codes_storage: rHashMap = Default::default(); let mut deployed_codes: HashMap = Default::default(); + let test_contract = data.db.get_test_contract_address(); + for address in data.db.persistent_accounts().into_iter().chain([data.env.tx.caller]) { info!(?address, "importing to zk state"); @@ -886,6 +888,12 @@ impl Cheatcodes { let nonce_key = get_nonce_key(address); nonce_storage.insert(nonce_key, EvmStorageSlot::new(full_nonce.to_ru256())); + if test_contract.map(|test_address| address == test_address).unwrap_or_default() { + // avoid migrating test contract code + tracing::trace!(?address, "ignoring code translation for test contract"); + continue; + } + if let Some(contract) = self.dual_compiled_contracts.iter().find(|contract| { info.code_hash != KECCAK_EMPTY && info.code_hash == contract.evm_bytecode_hash }) { @@ -936,17 +944,13 @@ impl Cheatcodes { journaled_account(data, known_codes_addr).expect("failed to load account"); known_codes_account.storage.extend(known_codes_storage.clone()); - let test_contract = data.db.get_test_contract_address(); for (address, info) in deployed_codes { let account = journaled_account(data, address).expect("failed to load account"); let _ = std::mem::replace(&mut account.info.balance, info.balance); let _ = std::mem::replace(&mut account.info.nonce, info.nonce); - if test_contract.map(|addr| addr == address).unwrap_or_default() { - tracing::trace!(?address, "ignoring code translation for test contract"); - } else { - account.info.code_hash = info.code_hash; - account.info.code.clone_from(&info.code); - } + + account.info.code_hash = info.code_hash; + account.info.code.clone_from(&info.code); } } From 5f1f3b2843590b559f8546ce853fb1b54fb36065 Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Mon, 18 Nov 2024 10:29:00 +0100 Subject: [PATCH 13/16] Revert "fix(inspector:zk): test contract code migration" This reverts commit 2d191bd1246746af8c27d05ae67e5192be885fd2. --- crates/cheatcodes/src/inspector.rs | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/crates/cheatcodes/src/inspector.rs b/crates/cheatcodes/src/inspector.rs index 150183798..d481f6a20 100644 --- a/crates/cheatcodes/src/inspector.rs +++ b/crates/cheatcodes/src/inspector.rs @@ -871,8 +871,6 @@ impl Cheatcodes { let mut known_codes_storage: rHashMap = Default::default(); let mut deployed_codes: HashMap = Default::default(); - let test_contract = data.db.get_test_contract_address(); - for address in data.db.persistent_accounts().into_iter().chain([data.env.tx.caller]) { info!(?address, "importing to zk state"); @@ -888,12 +886,6 @@ impl Cheatcodes { let nonce_key = get_nonce_key(address); nonce_storage.insert(nonce_key, EvmStorageSlot::new(full_nonce.to_ru256())); - if test_contract.map(|test_address| address == test_address).unwrap_or_default() { - // avoid migrating test contract code - tracing::trace!(?address, "ignoring code translation for test contract"); - continue; - } - if let Some(contract) = self.dual_compiled_contracts.iter().find(|contract| { info.code_hash != KECCAK_EMPTY && info.code_hash == contract.evm_bytecode_hash }) { @@ -944,13 +936,17 @@ impl Cheatcodes { journaled_account(data, known_codes_addr).expect("failed to load account"); known_codes_account.storage.extend(known_codes_storage.clone()); + let test_contract = data.db.get_test_contract_address(); for (address, info) in deployed_codes { let account = journaled_account(data, address).expect("failed to load account"); let _ = std::mem::replace(&mut account.info.balance, info.balance); let _ = std::mem::replace(&mut account.info.nonce, info.nonce); - - account.info.code_hash = info.code_hash; - account.info.code.clone_from(&info.code); + if test_contract.map(|addr| addr == address).unwrap_or_default() { + tracing::trace!(?address, "ignoring code translation for test contract"); + } else { + account.info.code_hash = info.code_hash; + account.info.code.clone_from(&info.code); + } } } From a1df91dedc89cb56b7ad6253485e2050bfd03065 Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Mon, 18 Nov 2024 10:29:05 +0100 Subject: [PATCH 14/16] Revert "feat(inspector:zk): delay startup migration" This reverts commit 71ca065bd944c4955c704ca1e4fd8dedeb88cd93. --- crates/cheatcodes/src/inspector.rs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/crates/cheatcodes/src/inspector.rs b/crates/cheatcodes/src/inspector.rs index d481f6a20..aad1f7dfd 100644 --- a/crates/cheatcodes/src/inspector.rs +++ b/crates/cheatcodes/src/inspector.rs @@ -1473,14 +1473,8 @@ where { }; let prev = account.info.nonce; account.info.nonce = prev.saturating_sub(1); - let nonce = account.info.nonce; - if self.startup_zk { - self.startup_zk = false; // We only do this once. - self.select_zk_vm(ecx_inner, None); - } - - trace!(target: "cheatcodes", %sender, nonce, prev, "corrected nonce"); + trace!(target: "cheatcodes", %sender, nonce=account.info.nonce, prev, "corrected nonce"); } if call.target_address == CHEATCODE_ADDRESS { @@ -1980,6 +1974,11 @@ impl Inspector<&mut dyn DatabaseExt> for Cheatcodes { ecx.env.tx.gas_price = gas_price; } + if self.startup_zk && !self.use_zk_vm { + self.startup_zk = false; // We only do this once. + self.select_zk_vm(ecx, None); + } + // Record gas for current frame. if self.gas_metering.paused { self.gas_metering.paused_frames.push(interpreter.gas); From d0a84f9d55ead73e68d2d6f33c451993a1747db4 Mon Sep 17 00:00:00 2001 From: Francesco Dainese Date: Mon, 18 Nov 2024 10:30:32 +0100 Subject: [PATCH 15/16] Revert "chore: fmt" This reverts commit 5a56c87bc8e55172d50a44aefbdb814e47d5ed8d. --- deny.toml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/deny.toml b/deny.toml index ef0dd7003..303bfec5b 100644 --- a/deny.toml +++ b/deny.toml @@ -17,11 +17,6 @@ ignore = [ # Used by alloy # proc-macro-error is unmaintained "RUSTSEC-2024-0370", - # instant is unmaintained - "RUSTSEC-2024-0384", - # Used by boojum - # derivative is unmaintained - "RUSTSEC-2024-0388", ] # This section is considered when running `cargo deny check bans`. From 1e76d7dd4f70c205f232d6abcb333fea5b3e8a7d Mon Sep 17 00:00:00 2001 From: Karrq Date: Mon, 18 Nov 2024 10:32:42 +0100 Subject: [PATCH 16/16] chore: comment formatting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Federico Rodríguez --- crates/forge/bin/cmd/create.rs | 2 +- crates/forge/src/multi_runner.rs | 2 +- crates/forge/tests/it/zk/linking.rs | 2 +- crates/script/src/build.rs | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/forge/bin/cmd/create.rs b/crates/forge/bin/cmd/create.rs index ede1ea69c..20d7c1978 100644 --- a/crates/forge/bin/cmd/create.rs +++ b/crates/forge/bin/cmd/create.rs @@ -239,7 +239,7 @@ impl CreateArgs { queue.push_back(dep.clone()) } - //TODO(zk): ensure factory deps are also linked + // TODO(zk): ensure factory deps are also linked let fdep_bytecode = fdep_art .bytecode .clone() diff --git a/crates/forge/src/multi_runner.rs b/crates/forge/src/multi_runner.rs index 15ca3b4f6..2356d9b99 100644 --- a/crates/forge/src/multi_runner.rs +++ b/crates/forge/src/multi_runner.rs @@ -467,7 +467,7 @@ impl MultiContractRunnerBuilder { if let Some(abi) = contract.abi { let bytecode = contract.bytecode.as_ref(); - //TODO(zk): retrieve link_references + // TODO(zk): retrieve link_references if let Some(bytecode_object) = bytecode.map(|b| b.object()) { let compact_bytecode = CompactBytecode { object: bytecode_object.clone(), diff --git a/crates/forge/tests/it/zk/linking.rs b/crates/forge/tests/it/zk/linking.rs index 7ebb21399..679e34ca5 100644 --- a/crates/forge/tests/it/zk/linking.rs +++ b/crates/forge/tests/it/zk/linking.rs @@ -2,7 +2,7 @@ use foundry_test_utils::{forgetest_async, util, TestProject}; use crate::test_helpers::{deploy_zk_contract, run_zk_script_test}; -//TODO(zk): add test that actually does the deployment +// TODO(zk): add test that actually does the deployment // of the unlinked contract via script, once recursive linking is supported // and once we also support doing deploy-time linking diff --git a/crates/script/src/build.rs b/crates/script/src/build.rs index da04ddb60..3bb0da6a7 100644 --- a/crates/script/src/build.rs +++ b/crates/script/src/build.rs @@ -154,7 +154,7 @@ impl LinkedBuildData { for (id, contract) in zk_contracts { if let Some(abi) = contract.abi { let bytecode = contract.bytecode.as_ref(); - //TODO(zk): retrieve link_references + // TODO(zk): retrieve link_references if let Some(bytecode_object) = bytecode.map(|b| b.object()) { let compact_bytecode = CompactBytecode { object: bytecode_object.clone(),