Skip to content

Commit

Permalink
fix: sanitize yul artifact contract names
Browse files Browse the repository at this point in the history
  • Loading branch information
elfedy committed Jan 8, 2025
1 parent e85875e commit 28433c6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
23 changes: 22 additions & 1 deletion crates/zksync/compilers/src/compilers/zksolc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,13 +358,34 @@ impl ZkSolc {

/// Compiles with `--standard-json` and deserializes the output as [`CompilerOutput`].
pub fn compile(&self, input: &ZkSolcInput) -> Result<ZkCompilerOutput> {
// If solc is zksync solc, override the returned version to put the complete zksolc one
let output = self.compile_output(input)?;
// Only run UTF-8 validation once.
let output = std::str::from_utf8(&output).map_err(|_| SolcError::InvalidUtf8)?;

let mut compiler_output: ZkCompilerOutput = serde_json::from_str(output)?;

// Sanitize contract names that are source file paths, using the file name without
// path or .yul extension. This happens in older zksolc versions and creates issues.
// See: https://github.com/matter-labs/era-compiler-solidity/issues/243
if input.is_yul() {
for contracts in compiler_output.contracts.values_mut() {
let contract_names = contracts.keys().cloned().collect::<Vec<String>>();
for name in contract_names {
if name.ends_with(".yul") {
let sanitized_name = name
.split('/')
.last()
.unwrap()
.strip_suffix(".yul")
.expect("Error sanitizing path into name");
// Removing and inserting should be fine because there cannot be
// two contracts named the same in a source file
let contract = contracts.remove(&name).expect("Error replacing yul key");
contracts.insert(sanitized_name.into(), contract);
}
}
}
}
// Add zksync version so that there's some way to identify if zksync solc was used
// by looking at build info
compiler_output.zksync_solc_version = self.solc_version_info.zksync_version.clone();
Expand Down
6 changes: 3 additions & 3 deletions crates/zksync/compilers/tests/zksync_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,16 +581,16 @@ fn zksync_can_compile_yul_sample() {
.find_map(|contracts| {
contracts
.iter()
.find(|(name, _)| name.ends_with("SimpleStore.yul"))
.find(|(name, _)| name.ends_with("SimpleStore"))
.and_then(|(_, artifacts)| artifacts.first())
})
.expect("SimpleStore.yul artifact not found")
.expect("SimpleStore artifact not found")
.artifact
.bytecode
.clone()
.unwrap();

let yul_bytecode = simple_store_artifact.object().into_bytes().unwrap();

assert!(!yul_bytecode.is_empty(), "SimpleStore.yul bytecode is empty");
assert!(!yul_bytecode.is_empty(), "SimpleStore bytecode is empty");
}

0 comments on commit 28433c6

Please sign in to comment.