Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sanitize yul artifact contract names #819

Merged
merged 6 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
elfedy marked this conversation as resolved.
Show resolved Hide resolved
.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");
}
Loading