Skip to content

Commit

Permalink
feat: retain pre-1.5.7 zksolc compatibility (#43)
Browse files Browse the repository at this point in the history
* fix(bytecode:zk): pub `link_references` method

* feat(compiler:zk): keep `evm` deserialization

feat(compiler:zk): fallback to `evm` output
  • Loading branch information
Karrq authored Nov 20, 2024
1 parent 4f9527f commit 8855d53
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
29 changes: 22 additions & 7 deletions crates/artifacts/zksolc/src/contract.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Contract related types.
use crate::EraVM;
use crate::{EraVM, Evm};
use alloy_json_abi::JsonAbi;
use foundry_compilers_artifacts_solc::{
Bytecode, CompactBytecode, CompactContractBytecode, CompactContractBytecodeCow,
Expand Down Expand Up @@ -31,9 +31,12 @@ pub struct Contract {
/// The contract factory dependencies.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub factory_dependencies: Option<BTreeMap<String, String>>,
/// EVM-related outputs
/// EraVM-related outputs
#[serde(default, skip_serializing_if = "Option::is_none")]
pub eravm: Option<EraVM>,
/// EVM-related outputs (deprecated)
#[serde(default, skip_serializing_if = "Option::is_none")]
pub evm: Option<Evm>,
/// The contract's unlinked libraries
#[serde(default)]
pub missing_libraries: Vec<String>,
Expand Down Expand Up @@ -69,11 +72,21 @@ impl Contract {
}

pub fn bytecode(&self) -> Option<Bytecode> {
self.eravm.as_ref().and_then(|eravm| eravm.bytecode(self.is_unlinked())).map(|object| {
let mut bytecode: Bytecode = object.into();
bytecode.link_references = self.link_references();
bytecode
})
self.eravm
.as_ref()
.and_then(|eravm| eravm.bytecode(self.is_unlinked()))
.or_else(|| {
self.evm
.as_ref()
.and_then(|evm| evm.bytecode.as_ref())
.map(|bytecode| &bytecode.object)
.cloned()
})
.map(|object| {
let mut bytecode: Bytecode = object.into();
bytecode.link_references = self.link_references();
bytecode
})
}
}

Expand Down Expand Up @@ -115,6 +128,8 @@ impl<'a> From<&'a Contract> for CompactContractRef<'a> {
fn from(c: &'a Contract) -> Self {
let (bin, bin_runtime) = if let Some(ref eravm) = c.eravm {
(eravm.bytecode.as_ref(), eravm.bytecode.as_ref())
} else if let Some(ref evm) = c.evm {
(evm.bytecode.as_ref().map(|c| &c.object), evm.bytecode.as_ref().map(|c| &c.object))
} else {
(None, None)
};
Expand Down
22 changes: 21 additions & 1 deletion crates/artifacts/zksolc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use foundry_compilers_artifacts_solc::{
BytecodeObject, CompactContractRef, FileToContractsMap, SourceFile, SourceFiles,
Bytecode, BytecodeObject, CompactContractRef, FileToContractsMap, SourceFile, SourceFiles,
};

use semver::Version;
Expand Down Expand Up @@ -88,6 +88,26 @@ impl CompilerOutput {
}
}

#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Evm {
/// The contract EraVM assembly code.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub assembly: Option<String>,
/// The contract EVM legacy assembly code.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub legacy_assembly: Option<serde_json::Value>,
/// The contract bytecode.
/// Is reset by that of EraVM before yielding the compiled project artifacts.
pub bytecode: Option<Bytecode>,
/// The list of function hashes
#[serde(default, skip_serializing_if = "::std::collections::BTreeMap::is_empty")]
pub method_identifiers: BTreeMap<String, String>,
/// The extra EVMLA metadata.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extra_metadata: Option<ExtraMetadata>,
}

#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct EraVM {
Expand Down
2 changes: 2 additions & 0 deletions crates/compilers/src/zksync/artifact_output/zk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ impl ZkArtifactOutput {
devdoc,
storage_layout,
eravm,
evm,
ir_optimized,
hash,
factory_dependencies,
Expand All @@ -151,6 +152,7 @@ impl ZkArtifactOutput {

let (bytecode, assembly) = eravm
.map(|eravm| (eravm.bytecode(is_unlinked), eravm.assembly))
.or_else(|| evm.map(|evm| (evm.bytecode.map(|bc| bc.object), evm.assembly)))
.unwrap_or_else(|| (None, None));
let bytecode = bytecode
.map(|object| ZkArtifactBytecode::with_object(object, is_unlinked, missing_libraries));
Expand Down
2 changes: 1 addition & 1 deletion crates/compilers/src/zksync/artifact_output/zk/bytecode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl ZkArtifactBytecode {
Self { object, is_unlinked, missing_libraries }
}

fn link_references(&self) -> BTreeMap<String, BTreeMap<String, Vec<Offsets>>> {
pub fn link_references(&self) -> BTreeMap<String, BTreeMap<String, Vec<Offsets>>> {
Contract::missing_libs_to_link_references(self.missing_libraries.as_slice())
}

Expand Down

0 comments on commit 8855d53

Please sign in to comment.