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

feat(resolc): integrate revive compiler for evm to resolc compilation #1

Draft
wants to merge 55 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
55 commits
Select commit Hold shift + click to select a range
73ecaba
update: resolc code fixes
Brianspha Nov 28, 2024
946010e
update: resolc code fixes
Brianspha Nov 28, 2024
34e018a
update: fix import issues
Brianspha Nov 28, 2024
b2150f7
update: format code using cargo fmt
Brianspha Nov 28, 2024
5f3eecd
fix: revive version
Brianspha Dec 2, 2024
69221e8
update: code fixes and removal of redundant code
Brianspha Dec 3, 2024
cdf55a1
update: code fixes and removal of redundant code
Brianspha Dec 3, 2024
d9611dd
update: fix private mod to public
Brianspha Dec 6, 2024
b647eca
update: fix issues with settings and resolc artifacts pipeline
Brianspha Dec 7, 2024
ddfb893
update: add missing init function for optimiser
Brianspha Dec 9, 2024
5bd1490
update: add new artifacts
Brianspha Dec 10, 2024
de13ddd
update: add new artifacts
Brianspha Dec 10, 2024
5500c81
update: fix access on artifact struct
Brianspha Dec 10, 2024
867dd1b
update: fix abi issue
Brianspha Dec 10, 2024
48a00ef
update: fix abi issue
Brianspha Dec 10, 2024
11b211a
update: fix abi issue
Brianspha Dec 10, 2024
30f36be
update: fix abi issue
Brianspha Dec 10, 2024
802a4ba
update:add missing trait implementations
Brianspha Dec 11, 2024
eb449ae
update:fix output naming issue
Brianspha Dec 11, 2024
7f2fe3f
update:add os support for binaries
Brianspha Dec 12, 2024
a3e9324
update:fix os binary version issue
Brianspha Dec 12, 2024
c2f4bcf
update:fix os binary version issue
Brianspha Dec 16, 2024
26c73b0
update:fix os binary version issue
Brianspha Dec 16, 2024
26d1520
update:fix os binary version issue
Brianspha Dec 16, 2024
fefe171
update:fix remove solc path support for now
Brianspha Dec 16, 2024
092b9b1
update:fix remove solc path support for now
Brianspha Dec 16, 2024
6c5cd7e
update:fix error on revive url for git releases
Brianspha Dec 16, 2024
15f7776
update: fix logic error
Brianspha Dec 17, 2024
2293759
update: fix mac os file
Brianspha Dec 17, 2024
f870e2d
update: resolc
Brianspha Dec 17, 2024
e2c6cf2
fix: revert resolc to use resolc instead of wasm
Brianspha Dec 17, 2024
4e46757
fix: getting version from a given executable path
Brianspha Dec 18, 2024
35b16ac
fix: getting version from a given executable path
Brianspha Dec 18, 2024
1668f33
fix: getting version from a given executable path
Brianspha Dec 18, 2024
01859e8
fix: optimiser impl
Brianspha Dec 18, 2024
522d47b
fix: version input impl
Brianspha Dec 18, 2024
5da7974
fix: silent compilation and added logic for solc and tests
Brianspha Dec 18, 2024
4fdb4dd
fix: dead code warnings
Brianspha Dec 18, 2024
337427e
fix: solc build json parsing
Brianspha Dec 18, 2024
22d58a7
fix: solc download url for m1
Brianspha Dec 19, 2024
2931315
fix: solc download url for m1
Brianspha Dec 19, 2024
0dce64e
fix: SolcBuild struct
Brianspha Dec 19, 2024
bf8de07
fix: checksum prefix issue
Brianspha Dec 19, 2024
35e7426
fix: solc installing path issue
Brianspha Dec 19, 2024
bc9d6c3
fix: solc installing path issue
Brianspha Dec 19, 2024
1752f67
fix: solc installing path issue
Brianspha Dec 19, 2024
98df26f
fix: solc installing path issue
Brianspha Dec 19, 2024
8c88bbc
fix: solc installing path issue
Brianspha Dec 20, 2024
d84d3ea
fix: solc installing path issue
Brianspha Dec 21, 2024
58a2a55
fix: solc installing path issue
Brianspha Dec 21, 2024
b7789f5
fix: solc installing path issue
Brianspha Dec 21, 2024
9c0f03d
fix: solc installing path issue
Brianspha Dec 21, 2024
88eb481
fix: solc installing path issue
Brianspha Dec 21, 2024
07d4f87
fix: solc installing path issue
Brianspha Dec 21, 2024
61184f6
fix: solc installing path issue
Brianspha Dec 21, 2024
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
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ foundry-compilers = { path = "crates/compilers", version = "0.12.3" }
foundry-compilers-artifacts = { path = "crates/artifacts/artifacts", version = "0.12.3" }
foundry-compilers-artifacts-solc = { path = "crates/artifacts/solc", version = "0.12.3" }
foundry-compilers-artifacts-vyper = { path = "crates/artifacts/vyper", version = "0.12.3" }
foundry-compilers-artifacts-resolc = { path = "crates/artifacts/resolc", version = "0.12.3" }
foundry-compilers-core = { path = "crates/core", version = "0.12.3" }
revive-solidity = { git = "https://github.com/paritytech/revive", tag = "v0.1.0-dev-4", package = "revive-solidity" }
revive-llvm-context = { git = "https://github.com/paritytech/revive", tag = "v0.1.0-dev-4", package = "revive-llvm-context" }

alloy-json-abi = { version = "0.8", features = ["serde_json"] }
alloy-primitives = { version = "0.8", features = ["serde", "rand"] }
Expand Down
1 change: 1 addition & 0 deletions crates/artifacts/artifacts/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ workspace = true
[dependencies]
foundry-compilers-artifacts-solc.workspace = true
foundry-compilers-artifacts-vyper.workspace = true
foundry-compilers-artifacts-resolc.workspace = true

[features]
async = ["foundry-compilers-artifacts-solc/async"]
2 changes: 2 additions & 0 deletions crates/artifacts/artifacts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
#![cfg_attr(not(test), warn(unused_crate_dependencies))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]

pub use foundry_compilers_artifacts_resolc as resolc;
pub use foundry_compilers_artifacts_solc as solc;
pub use foundry_compilers_artifacts_vyper as vyper;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

accidental?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line 6? If yes nope just following integration similar to solc and vyper

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

9, the new newline

pub use solc::*;
46 changes: 46 additions & 0 deletions crates/artifacts/resolc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[package]
name = "foundry-compilers-artifacts-resolc"
description = "Rust bindings for Revive JSON artifacts"

version.workspace = true
edition.workspace = true
rust-version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
repository.workspace = true
exclude.workspace = true

[lints]
workspace = true

[dependencies]
foundry-compilers-core.workspace = true
foundry-compilers-artifacts-solc.workspace = true

serde.workspace = true
semver.workspace = true
serde_json.workspace = true
tracing.workspace = true
alloy-primitives.workspace = true
alloy-json-abi.workspace = true
rayon.workspace = true
thiserror.workspace = true
md-5.workspace = true
yansi.workspace = true
futures-util = { workspace = true, optional = true }
tokio = { workspace = true, optional = true }
revive-solidity.workspace = true
revive-llvm-context.workspace = true
walkdir = "2.4"

[target.'cfg(windows)'.dependencies]
path-slash.workspace = true

[dev-dependencies]
serde_path_to_error = "0.1"
similar-asserts.workspace = true
foundry-compilers-core = { workspace = true, features = ["test-utils"] }

[features]
async = ["dep:tokio", "futures-util", "tokio/fs"]
161 changes: 161 additions & 0 deletions crates/artifacts/resolc/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
use std::{
borrow::Cow,
collections::{BTreeMap, HashSet},
};

use alloy_json_abi::JsonAbi;
use foundry_compilers_artifacts_solc::{
CompactBytecode, CompactContractBytecode, CompactContractBytecodeCow, CompactContractRef,
CompactDeployedBytecode, DevDoc, StorageLayout, UserDoc,
};
use serde::{Deserialize, Serialize};

use crate::{ResolcEVM, EVM};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct ResolcContract {
/// The contract ABI.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub abi: Option<JsonAbi>,
/// The contract metadata.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub metadata: Option<serde_json::Value>,
/// The contract developer documentation.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub devdoc: Option<DevDoc>,
/// The contract user documentation.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub userdoc: Option<UserDoc>,
/// The contract storage layout.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub storage_layout: Option<StorageLayout>,
/// Contract's bytecode and related objects
#[serde(default, skip_serializing_if = "Option::is_none")]
pub evm: Option<EVM>,
/// Revive related output
/// We are going to use structs defined locally
/// as opposed to revive defined
#[serde(default, skip_serializing_if = "Option::is_none")]
pub resolc_evm: Option<ResolcEVM>,
/// The contract optimized IR code.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ir_optimized: Option<String>,
/// The contract PolkaVM bytecode hash.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub hash: Option<String>,
/// The contract factory dependencies.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub factory_dependencies: Option<BTreeMap<String, String>>,
/// The contract missing libraries.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub missing_libraries: Option<HashSet<String>>,
}

impl Default for ResolcContract {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i can't find where it's used?

fn default() -> Self {
Self {
abi: None,
metadata: None,
devdoc: None,
userdoc: None,
storage_layout: None,
evm: None,
ir_optimized: None,
hash: None,
factory_dependencies: None,
missing_libraries: None,
resolc_evm: None,
}
}
}

impl<'a> From<&'a ResolcContract> for CompactContractBytecodeCow<'a> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
impl<'a> From<&'a ResolcContract> for CompactContractBytecodeCow<'a> {
impl<'a> From<&'a ResolcContract> for CompactContractBytecodeCow<'a> {
fn from(value: &'a ResolcContract) -> Self {
Self {
abi: value.json_abi().map(Cow::Owned),
bytecode: value.compact_bytecode().map(Cow::Owned),
deployed_bytecode: value.compact_deployed_bytecode().map(Cow::Owned),
}
}
}

fn from(value: &'a ResolcContract) -> Self {
if let Some((standard_abi, compact_bytecode, compact_deployed_bytecode)) =
create_compact_bytecode(value)
{
Self {
abi: Some(Cow::Owned(standard_abi)),
bytecode: Some(Cow::Owned(compact_bytecode)),
deployed_bytecode: Some(Cow::Owned(compact_deployed_bytecode)),
}
} else {
Self { abi: None, bytecode: None, deployed_bytecode: None }
}
}
}

impl From<ResolcContract> for CompactContractBytecode {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
impl From<ResolcContract> for CompactContractBytecode {
impl From<ResolcContract> for CompactContractBytecode {
fn from(value: ResolcContract) -> Self {
Self {
abi: value.json_abi(),
bytecode: value.compact_bytecode(),
deployed_bytecode: value.compact_deployed_bytecode(),
}
}
}

or it's intended to always return every field to None if at least one of them is None?

fn from(value: ResolcContract) -> Self {
if let Some((standard_abi, compact_bytecode, compact_deployed_bytecode)) =
create_compact_bytecode(&value)
{
Self {
abi: Some(standard_abi),
bytecode: Some(compact_bytecode),
deployed_bytecode: Some(compact_deployed_bytecode),
}
} else {
Self { abi: None, bytecode: None, deployed_bytecode: None }
}
}
}

impl<'a> From<&'a ResolcContract> for CompactContractRef<'a> {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
impl<'a> From<&'a ResolcContract> for CompactContractRef<'a> {
impl<'a> From<&'a ResolcContract> for CompactContractRef<'a> {
fn from(c: &'a ResolcContract) -> Self {
let (bin, bin_runtime) = match &c.resolc_evm {
Some(evm) => {
let bin = evm.bytecode.as_ref().map(|code| &code.object);
let bin_runtime = evm
.deployed_bytecode
.as_ref()
.and_then(|deployed| deployed.bytecode.as_ref().map(|code| &code.object));
(bin, bin_runtime)
}
_ => (None, None),
};
Self { abi: c.abi.as_ref(), bin, bin_runtime }
}
}

fn from(c: &'a ResolcContract) -> Self {
let (bin, bin_runtime) = if let Some(ref evm) = c.resolc_evm {
(
evm.bytecode.as_ref().map(|code| &code.object),
evm.deployed_bytecode
.as_ref()
.and_then(|deployed| deployed.bytecode.as_ref().map(|code| &code.object)),
)
} else {
(None, None)
};

Self { abi: c.abi.as_ref(), bin, bin_runtime }
}
}
fn create_compact_bytecode(
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn create_compact_bytecode(
impl ResolcContract {
fn json_abi(&self) -> Option<JsonAbi> {
self.abi.clone()
}
fn compact_bytecode(&self) -> Option<CompactBytecode> {
self.resolc_evm.as_ref().and_then(|resolc_evm| resolc_evm.bytecode.as_ref()).map(
|bytecode| CompactBytecode {
object: bytecode.object.clone(),
source_map: None,
link_references: BTreeMap::default(),
},
)
}
fn compact_deployed_bytecode(&self) -> Option<CompactDeployedBytecode> {
self.resolc_evm
.as_ref()
.and_then(|resolc_evm| resolc_evm.deployed_bytecode.as_ref())
.and_then(|item| item.bytecode.as_ref())
.map(|deployed_bytecode| {
let compact_bytecode_deployed = CompactBytecode {
object: deployed_bytecode.object.clone(),
source_map: None,
link_references: BTreeMap::default(),
};
CompactDeployedBytecode {
bytecode: Some(compact_bytecode_deployed),
immutable_references: BTreeMap::default(),
}
})
.to_owned()
}
}

parent_contract: &ResolcContract,
) -> Option<(JsonAbi, CompactBytecode, CompactDeployedBytecode)> {
let Some(resolc_evm) = &parent_contract.resolc_evm else {
return None;
};

let Some(bytecode) = &resolc_evm.bytecode else {
return None;
};

let Some(deployed) = &resolc_evm.deployed_bytecode else {
return None;
};

let Some(deployed_bytecode) = &deployed.bytecode else {
return None;
};

let compact_bytecode = CompactBytecode {
object: bytecode.object.clone(),
source_map: None,
link_references: BTreeMap::default(),
};

let compact_bytecode_deployed = CompactBytecode {
object: deployed_bytecode.object.clone(),
source_map: None,
link_references: BTreeMap::default(),
};

let compact_deployed_bytecode = CompactDeployedBytecode {
bytecode: Some(compact_bytecode_deployed),
immutable_references: BTreeMap::default(),
};

Some((
parent_contract.abi.clone().unwrap_or_default(),
compact_bytecode,
compact_deployed_bytecode,
))
}
154 changes: 154 additions & 0 deletions crates/artifacts/resolc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
use std::{
collections::{BTreeMap, HashSet},
path::{Path, PathBuf},
};

pub mod contract;
use contract::ResolcContract;
use foundry_compilers_artifacts_solc::{
Bytecode, DeployedBytecode, Error, FileToContractsMap, SourceFile, SourceFiles,
};
use serde::{Deserialize, Serialize};

/// This file contains data structures that we need defined locally as some of them need to be used in trait
/// Implementation in such a way that they are owned so if we use existing structures from Revive
/// We will run into issues

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct ResolcCompilerOutput {
/// The file-contract hashmap.
#[serde(default)]
pub contracts: FileToContractsMap<ResolcContract>,
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub contracts: FileToContractsMap<ResolcContract>,
pub contracts: ResolcContracts,

/// The source code mapping data.
#[serde(default, skip_serializing_if = "BTreeMap::is_empty")]
pub sources: BTreeMap<PathBuf, SourceFile>,
/// The compilation errors and warnings.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub errors: Vec<Error>,
/// The `solc` compiler version.
#[serde(skip_serializing_if = "Option::is_none")]
pub version: Option<String>,
/// The `solc` compiler long version.
#[serde(skip_serializing_if = "Option::is_none")]
pub long_version: Option<String>,
/// The `resolc` compiler version.
#[serde(skip_serializing_if = "Option::is_none")]
pub revive_version: Option<String>,
}
#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct RecursiveFunction {
/// The function name.
pub name: String,
/// The creation code function block tag.
pub creation_tag: Option<usize>,
/// The runtime code function block tag.
pub runtime_tag: Option<usize>,
/// The number of input arguments.
#[serde(rename = "totalParamSize")]
pub input_size: usize,
/// The number of output arguments.
#[serde(rename = "totalRetParamSize")]
pub output_size: usize,
}
#[derive(Debug, Default, Serialize, Deserialize, Clone, Eq, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ExtraMetadata {
/// The list of recursive functions.
#[serde(default = "Vec::new")]
pub recursive_functions: Vec<RecursiveFunction>,
}
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct ResolcEVM {
/// The contract EVM legacy assembly code.
#[serde(rename = "legacyAssembly", skip_serializing_if = "Option::is_none")]
pub assembly: Option<serde_json::Value>,
/// The contract PolkaVM assembly code.
#[serde(rename = "assembly", skip_serializing_if = "Option::is_none")]
pub assembly_text: Option<String>,
/// The contract bytecode.
/// Is reset by that of PolkaVM before yielding the compiled project artifacts.
#[serde(skip_serializing_if = "Option::is_none")]
pub bytecode: Option<Bytecode>,
/// The deployed bytecode of the contract.
/// It is overwritten with the PolkaVM blob before yielding the compiled project artifacts.
/// Hence it will be the same as the runtime code but we keep both for compatibility reasons.
#[serde(skip_serializing_if = "Option::is_none")]
pub deployed_bytecode: Option<DeployedBytecode>,
/// The contract function signatures.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub method_identifiers: Option<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 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>,
}
pub type ResolcContracts = FileToContractsMap<ResolcContract>;

/// A wrapper helper type for the `Contracts` type alias
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct OutputContracts(pub ResolcContracts);
impl ResolcCompilerOutput {
/// Whether the output contains a compiler error
pub fn has_error(&self) -> bool {
self.errors.iter().any(|err| err.severity.is_error())
}

/// Returns the output's source files and contracts separately, wrapped in helper types that
/// provide several helper methods
pub fn split(self) -> (SourceFiles, OutputContracts) {
(SourceFiles(self.sources), OutputContracts(self.contracts))
}

/// Retains only those files the given iterator yields
///
/// In other words, removes all contracts for files not included in the iterator
pub fn retain_files<'a, I>(&mut self, files: I)
where
I: IntoIterator<Item = &'a Path>,
{
// Note: use `to_lowercase` here because solc not necessarily emits the exact file name,
// e.g. `src/utils/upgradeProxy.sol` is emitted as `src/utils/UpgradeProxy.sol`
let files: HashSet<_> =
files.into_iter().map(|s| s.to_string_lossy().to_lowercase()).collect();
self.contracts.retain(|f, _| files.contains(&f.to_string_lossy().to_lowercase()));
self.sources.retain(|f, _| files.contains(&f.to_string_lossy().to_lowercase()));
}

pub fn merge(&mut self, other: Self) {
self.errors.extend(other.errors);
self.contracts.extend(other.contracts);
self.sources.extend(other.sources);
}

pub fn join_all(&mut self, root: impl AsRef<Path>) {
let root = root.as_ref();
self.contracts = std::mem::take(&mut self.contracts)
.into_iter()
.map(|(path, contracts)| (root.join(path), contracts))
.collect();
self.sources = std::mem::take(&mut self.sources)
.into_iter()
.map(|(path, source)| (root.join(path), source))
.collect();
}
}
Loading