Skip to content

PoC: Add initial Revive support(Take #2) #5

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

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ foundry-compilers = { path = "crates/compilers", version = "0.13.2" }
foundry-compilers-artifacts = { path = "crates/artifacts/artifacts", version = "0.13.2" }
foundry-compilers-artifacts-solc = { path = "crates/artifacts/solc", version = "0.13.2" }
foundry-compilers-artifacts-vyper = { path = "crates/artifacts/vyper", version = "0.13.2" }
foundry-compilers-artifacts-resolc = { path = "crates/artifacts/resolc", version = "0.13.2" }
foundry-compilers-core = { path = "crates/core", version = "0.13.2" }


alloy-json-abi = { version = "0.8", features = ["serde_json"] }
alloy-primitives = { version = "0.8", features = ["serde", "rand"] }
cfg-if = "1.0"
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"]
Expand Down
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;

pub use solc::*;
44 changes: 44 additions & 0 deletions crates/artifacts/resolc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
[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 }
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"]
77 changes: 77 additions & 0 deletions crates/artifacts/resolc/src/contract.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use std::collections::{BTreeMap, HashSet};

use alloy_json_abi::JsonAbi;
use foundry_compilers_artifacts_solc::{DevDoc, LosslessMetadata, StorageLayout, UserDoc};
use serde::{Deserialize, Serialize};

use crate::ResolcEVM;

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
#[serde(rename_all = "camelCase")]
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<ResolcEVM>,
/// The contract IR code.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ir: Option<String>,
/// 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 From<ResolcContract> for foundry_compilers_artifacts_solc::Contract {
fn from(contract: ResolcContract) -> Self {
let meta = match contract.metadata.as_ref() {
Some(meta) => match meta {
serde_json::Value::Object(map) => {
if let Some(meta) = map.get("solc_metadata") {
serde_json::from_value::<LosslessMetadata>(meta.clone()).ok()
} else {
None
}
}
_ => None,
},
None => Default::default(),
};

Self {
abi: contract.abi.or_else(Default::default),
evm: contract.evm.map(Into::into),
metadata: meta,
userdoc: contract.userdoc.unwrap_or_default(),
devdoc: contract.devdoc.unwrap_or_default(),
ir: contract.ir,
storage_layout: contract.storage_layout.unwrap_or_default(),
transient_storage_layout: Default::default(),
ewasm: None,
ir_optimized: contract.ir_optimized,
ir_optimized_ast: None,
}
}
}
127 changes: 127 additions & 0 deletions crates/artifacts/resolc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//! Resolc artifact types.

use std::{collections::BTreeMap, path::PathBuf};

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

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Default)]
pub struct ResolcCompilerOutput {
/// The file-contract hashmap.
#[serde(default)]
pub contracts: FileToContractsMap<ResolcContract>,
/// 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)]
#[serde(rename_all = "camelCase")]
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<ResolcBytecode>,
/// 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<ResolcBytecode>,
/// The contract function signatures.
#[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>,
}

/// The `solc --standard-json` output contract EVM deployed bytecode.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ResolcBytecode {
/// The bytecode object.
pub object: String,
}

impl ResolcBytecode {
/// A shortcut constructor.
pub fn new(object: String) -> Self {
Self { object }
}
}

impl From<ResolcBytecode> for Bytecode {
fn from(value: ResolcBytecode) -> Self {
Self {
function_debug_data: BTreeMap::new(),
object: BytecodeObject::Unlinked(value.object),
opcodes: None,
source_map: None,
generated_sources: vec![],
link_references: BTreeMap::new(),
}
}
}

impl From<ResolcEVM> for foundry_compilers_artifacts_solc::Evm {
fn from(evm: ResolcEVM) -> Self {
Self {
bytecode: evm.bytecode.clone().map(Into::into),
deployed_bytecode: Some(DeployedBytecode {
bytecode: evm.deployed_bytecode.or(evm.bytecode).map(Into::into),
immutable_references: BTreeMap::new(),
}),
method_identifiers: evm.method_identifiers,
assembly: evm.assembly_text,
legacy_assembly: evm.assembly,
gas_estimates: None,
}
}
}

pub type ResolcContracts = FileToContractsMap<ResolcContract>;
5 changes: 5 additions & 0 deletions crates/compilers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ svm = { workspace = true, optional = true }
svm-builds = { package = "svm-rs-builds", version = "0.5", default-features = false, optional = true }
sha2 = { version = "0.10", default-features = false, optional = true }


[dev-dependencies]
tracing-subscriber = { version = "0.3", default-features = false, features = [
"env-filter",
Expand All @@ -66,6 +67,10 @@ reqwest = "0.12"
tempfile = "3.9"
snapbox.workspace = true
foundry-compilers-core = { workspace = true, features = ["test-utils"] }
rstest = "0.24.0"
flate2 = "*"
tar = "0.4.44"
zip = "2.2.3"

[features]
default = ["rustls"]
Expand Down
1 change: 0 additions & 1 deletion crates/compilers/src/compile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,4 @@ pub mod many;

pub mod output;
pub use output::{contracts, info, sources};

pub mod project;
3 changes: 2 additions & 1 deletion crates/compilers/src/compilers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use std::{
};

pub mod multi;
pub mod resolc;
pub mod solc;
pub mod vyper;
pub use vyper::*;
Expand Down Expand Up @@ -67,7 +68,7 @@ impl fmt::Display for CompilerVersion {

/// Compilation settings including evm_version, output_selection, etc.
pub trait CompilerSettings:
Default + Serialize + DeserializeOwned + Clone + Debug + Send + Sync + 'static
Default + Serialize + DeserializeOwned + Clone + Debug + PartialEq + Send + Sync + 'static
{
/// We allow configuring settings restrictions which might optionally contain specific
/// requiremets for compiler configuration. e.g. min/max evm_version, optimizer runs
Expand Down
Loading
Loading