Skip to content

Commit

Permalink
fix: use zksync contract size limits in --zksync mode (#505)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbaztec authored Aug 8, 2024
1 parent fb6d0c5 commit 624cd93
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 15 deletions.
13 changes: 7 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 39 additions & 8 deletions crates/common/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ pub struct ProjectCompiler {

/// Extra files to include, that are not necessarily in the project's source dir.
files: Vec<PathBuf>,

/// Set zksync specific settings based on context
zksync: bool,
}

impl Default for ProjectCompiler {
Expand All @@ -74,6 +77,7 @@ impl ProjectCompiler {
quiet: Some(crate::shell::verbosity().is_silent()),
bail: None,
files: Vec::new(),
zksync: false,
}
}

Expand Down Expand Up @@ -129,6 +133,13 @@ impl ProjectCompiler {
self
}

/// Enables zksync contract sizes.
#[inline]
pub fn zksync_sizes(mut self) -> Self {
self.zksync = true;
self
}

/// Compiles the project.
pub fn compile<C: Compiler>(mut self, project: &Project<C>) -> Result<ProjectCompileOutput<C>> {
// TODO: Avoid process::exit
Expand Down Expand Up @@ -228,7 +239,7 @@ impl ProjectCompiler {
println!();
}

let mut size_report = SizeReport { contracts: BTreeMap::new() };
let mut size_report = SizeReport { contracts: BTreeMap::new(), zksync: self.zksync };

let artifacts: BTreeMap<_, _> = output
.artifact_ids()
Expand Down Expand Up @@ -454,7 +465,7 @@ impl ProjectCompiler {
println!();
}

let mut size_report = SizeReport { contracts: BTreeMap::new() };
let mut size_report = SizeReport { contracts: BTreeMap::new(), zksync: self.zksync };
let artifacts: BTreeMap<_, _> = output.artifacts().collect();
for (name, artifact) in artifacts {
let bytecode = artifact.get_bytecode_object().unwrap_or_default();
Expand Down Expand Up @@ -620,10 +631,15 @@ impl ContractSources {
// https://eips.ethereum.org/EIPS/eip-170
const CONTRACT_SIZE_LIMIT: usize = 24576;

// https://docs.zksync.io/build/developer-reference/ethereum-differences/contract-deployment#contract-size-limit-and-format-of-bytecode-hash
const ZKSYNC_CONTRACT_SIZE_LIMIT: usize = 450999;

/// Contracts with info about their size
pub struct SizeReport {
/// `contract name -> info`
pub contracts: BTreeMap<String, ContractInfo>,
/// Using zksync size report
pub zksync: bool,
}

impl SizeReport {
Expand All @@ -640,7 +656,11 @@ impl SizeReport {

/// Returns true if any contract exceeds the size limit, excluding test contracts.
pub fn exceeds_size_limit(&self) -> bool {
self.max_size() > CONTRACT_SIZE_LIMIT
if self.zksync {
self.max_size() > ZKSYNC_CONTRACT_SIZE_LIMIT
} else {
self.max_size() > CONTRACT_SIZE_LIMIT
}
}
}

Expand All @@ -657,11 +677,22 @@ impl Display for SizeReport {
// filters out non dev contracts (Test or Script)
let contracts = self.contracts.iter().filter(|(_, c)| !c.is_dev_contract && c.size > 0);
for (name, contract) in contracts {
let margin = CONTRACT_SIZE_LIMIT as isize - contract.size as isize;
let color = match contract.size {
0..=17999 => Color::Reset,
18000..=CONTRACT_SIZE_LIMIT => Color::Yellow,
_ => Color::Red,
let (margin, color) = if self.zksync {
let margin = ZKSYNC_CONTRACT_SIZE_LIMIT as isize - contract.size as isize;
let color = match contract.size {
0..=329999 => Color::Reset,
330000..=ZKSYNC_CONTRACT_SIZE_LIMIT => Color::Yellow,
_ => Color::Red,
};
(margin, color)
} else {
let margin = CONTRACT_SIZE_LIMIT as isize - contract.size as isize;
let color = match contract.size {
0..=17999 => Color::Reset,
18000..=CONTRACT_SIZE_LIMIT => Color::Yellow,
_ => Color::Red,
};
(margin, color)
};

let locale = &Locale::en;
Expand Down
1 change: 1 addition & 0 deletions crates/forge/bin/cmd/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ impl BuildArgs {
let zk_compiler = ProjectCompiler::new()
.print_names(self.names)
.print_sizes(self.sizes)
.zksync_sizes()
.quiet(self.format_json)
.bail(!self.format_json);

Expand Down
7 changes: 7 additions & 0 deletions crates/forge/tests/cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ forgetest_init!(build_sizes_no_forge_std, |prj, cmd| {
assert!(stdout.contains("Counter"), "\n{stdout}");
});

// tests build output is as expected in zksync mode
forgetest_init!(test_zk_build_sizes, |prj, cmd| {
cmd.args(["build", "--sizes", "--zksync", "--evm-version", "shanghai"]);
let stdout = cmd.stdout_lossy();
assert!(stdout.contains("| Counter | 800 | 450,199 |"), "\n{stdout}");
});

// tests that skip key in config can be used to skip non-compilable contract
forgetest_init!(test_can_skip_contract, |prj, cmd| {
prj.add_source(
Expand Down
2 changes: 2 additions & 0 deletions crates/forge/tests/cli/script.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1539,6 +1539,8 @@ contract DeployScript is Script {
"--rpc-url",
node.url().as_str(),
"--slow",
"--evm-version",
"shanghai",
]);

assert!(cmd.stdout_lossy().contains("ONCHAIN EXECUTION COMPLETE & SUCCESSFUL"));
Expand Down
2 changes: 1 addition & 1 deletion crates/forge/tests/cli/zksync_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use futures::{SinkExt, StreamExt};
use jsonrpc_core::IoHandler;
use zksync_types::H160;

const DEFAULT_PORT: u16 = 8011;
const DEFAULT_PORT: u16 = 18011;

/// List of legacy wallets (address, private key) that we seed with tokens at start.
const LEGACY_RICH_WALLETS: [(&str, &str); 10] = [
Expand Down

0 comments on commit 624cd93

Please sign in to comment.