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

refactor: extract artifacts to a separate crate #142

Merged
merged 22 commits into from
Jun 14, 2024
124 changes: 23 additions & 101 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[package]
name = "foundry-compilers"
[workspace]
members = ["crates/artifacts/*", "crates/core", "crates/project"]
resolver = "2"

[workspace.package]
authors = ["Foundry Maintainers"]
version = "0.7.0"
rust-version = "1.65"
Expand All @@ -13,110 +16,29 @@ keywords = ["foundry", "solidity", "solc", "ethereum", "ethers"]
edition = "2021"
exclude = [".github/", "scripts/", "test-data/"]

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[package.metadata.playground]
all-features = true

[dependencies]
[workspace.dependencies]
foundry-compilers-artifacts-solc = { path = "crates/artifacts/solc", version = "0.7.0" }
foundry-compilers-artifacts-vyper = { path = "crates/artifacts/vyper", version = "0.7.0" }
foundry-compilers-artifacts = { path = "crates/artifacts/artifacts", version = "0.7.0" }
foundry-compilers-core = { path = "crates/core", version = "0.7.0" }
serde = { version = "1", features = ["derive", "rc"] }
semver = { version = "1.0", features = ["serde"] }
serde_json = "1.0"
alloy-primitives = { version = "0.7", features = ["serde", "rand"] }
alloy-json-abi = { version = "0.7", features = ["serde_json"] }

solang-parser = { version = "=0.3.3", default-features = false }

cfg-if = "1.0.0"
dirs = "5.0"
dunce = "1.0"
md-5 = "0.10"
memmap2 = "0.9"
once_cell = "1.19"
path-slash = "0.2"
tracing = "0.1"
rayon = "1.8"
thiserror = "1"
regex = "1.10"
semver = { version = "1.0", features = ["serde"] }
serde = { version = "1", features = ["derive", "rc"] }
serde_json = "1.0"
thiserror = "1.0"
tracing = "0.1"
walkdir = "2.4"
path-slash = "0.2"
md-5 = "0.10"
yansi = "1.0.1"

# async
futures-util = { version = "0.3", optional = true }
tokio = { version = "1.35", features = ["rt-multi-thread"], optional = true }

# project-util
tempfile = { version = "3.9", optional = true }
fs_extra = { version = "1.3", optional = true }
rand = { version = "0.8", optional = true }

# svm
home = "0.5"
svm = { package = "svm-rs", version = "0.5", default-features = false, 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 }
itertools = "0.13"
auto_impl = "1"

winnow = "0.6"
dyn-clone = "1"
derivative = "2.2"

[dev-dependencies]
alloy-primitives = { version = "0.7", features = ["serde", "rand"] }
criterion = { version = "0.5", features = ["async_tokio"] }
once_cell = "1.19"
svm = { package = "svm-rs", version = "0.5", default-features = false }
solang-parser = { version = "=0.3.3", default-features = false }
pretty_assertions = "1"
rand = "0.8"
serde_path_to_error = "0.1"
tempfile = "3.9"
tokio = { version = "1.35", features = ["rt-multi-thread", "macros"] }
tracing-subscriber = { version = "0.3", default-features = false, features = [
"env-filter",
"fmt",
] }
reqwest = "0.12"
fd-lock = "4.0.0"

[[bench]]
name = "compile_many"
required-features = ["svm-solc"]
harness = false

[[bench]]
name = "read_all"
required-features = ["project-util", "svm-solc"]
harness = false

[[test]]
name = "project"
path = "tests/project.rs"
required-features = ["full", "project-util", "test-utils"]

[[test]]
name = "mocked"
path = "tests/mocked.rs"
required-features = ["full", "project-util"]

[features]
default = ["rustls"]
test-utils = []

full = ["async", "svm-solc"]

# Adds extra `async` methods using `tokio` to some types.
async = [
"dep:futures-util",
"dep:tokio",
"tokio/fs",
"tokio/process",
"tokio/io-util",
]
# Enables `svm` to auto-detect and manage `solc` builds.
svm-solc = ["dep:svm", "dep:svm-builds", "dep:sha2", "dep:tokio"]
# Utilities for creating and testing project workspaces.
project-util = ["dep:tempfile", "dep:fs_extra", "dep:rand", "svm-solc"]

rustls = ["svm?/rustls"]
openssl = ["svm?/openssl"]
# async
futures-util = "0.3"
tokio = { version = "1.35", features = ["rt-multi-thread"] }
2 changes: 1 addition & 1 deletion benches/compile_many.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ fn compile_many_benchmark(c: &mut Criterion) {

fn load_compiler_inputs() -> Vec<SolcInput> {
let mut inputs = Vec::new();
for file in std::fs::read_dir(Path::new(&env!("CARGO_MANIFEST_DIR")).join("test-data/in"))
for file in std::fs::read_dir(Path::new(&env!("CARGO_MANIFEST_DIR")).join("../../test-data/in"))
.unwrap()
.take(5)
{
Expand Down
26 changes: 26 additions & 0 deletions crates/artifacts/artifacts/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "foundry-compilers-artifacts"
description = "Rust bindings for compilers 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

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[package.metadata.playground]
all-features = true

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

[features]
async = ["foundry-compilers-artifacts-solc/async"]
7 changes: 7 additions & 0 deletions crates/artifacts/artifacts/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Meta crate reexporting all artifacts types.

#![cfg_attr(not(test), warn(unused_crate_dependencies))]

pub use foundry_compilers_artifacts_solc as solc;
pub use foundry_compilers_artifacts_vyper as vyper;
pub use solc::*;
48 changes: 48 additions & 0 deletions crates/artifacts/solc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
[package]
name = "foundry-compilers-artifacts-solc"
description = "Rust bindings for Solc 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

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[package.metadata.playground]
all-features = true

[dependencies]
foundry-compilers-core.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"
pretty_assertions.workspace = true
foundry-compilers-core = { workspace = true, features = ["test-utils"] }

[features]
async = ["dep:tokio", "futures-util", "tokio/fs"]
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Bindings for solc's `ast` output field

use crate::artifacts::serde_helpers;
use crate::serde_helpers;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{collections::BTreeMap, fmt, fmt::Write, str::FromStr};

Expand Down Expand Up @@ -213,7 +213,7 @@ mod tests {

#[test]
fn can_parse_ast() {
let ast = include_str!("../../../test-data/ast/ast-erc4626.json");
let ast = include_str!("../../../../../test-data/ast/ast-erc4626.json");
let _ast: Ast = serde_json::from_str(ast).unwrap();
}
}
File renamed without changes.
File renamed without changes.
40 changes: 21 additions & 19 deletions src/artifacts/ast/mod.rs → crates/artifacts/solc/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ pub use lowfidelity::{Ast, Node, NodeType, SourceLocation as LowFidelitySourceLo
/// The Yul AST is embedded into the Solidity AST for inline assembly blocks.
pub mod yul;

use crate::artifacts::serde_helpers;
use crate::serde_helpers;
use macros::{ast_node, expr_node, node_group, stmt_node};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
Expand Down Expand Up @@ -1093,24 +1093,26 @@ mod tests {

#[test]
fn can_parse_ast() {
fs::read_dir(PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("test-data").join("ast"))
.unwrap()
.for_each(|path| {
let path = path.unwrap().path();
let path_str = path.to_string_lossy();

let input = fs::read_to_string(&path).unwrap();
let deserializer = &mut serde_json::Deserializer::from_str(&input);
let result: Result<SourceUnit, _> = serde_path_to_error::deserialize(deserializer);
match result {
Err(e) => {
println!("... {path_str} fail: {e}");
panic!();
}
Ok(_) => {
println!("... {path_str} ok");
}
fs::read_dir(
PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../../test-data").join("ast"),
)
.unwrap()
.for_each(|path| {
let path = path.unwrap().path();
let path_str = path.to_string_lossy();

let input = fs::read_to_string(&path).unwrap();
let deserializer = &mut serde_json::Deserializer::from_str(&input);
let result: Result<SourceUnit, _> = serde_path_to_error::deserialize(deserializer);
match result {
Err(e) => {
println!("... {path_str} fail: {e}");
panic!();
}
})
Ok(_) => {
println!("... {path_str} ok");
}
}
})
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::{macros::node_group, misc::SourceLocation};
use crate::artifacts::serde_helpers;
use crate::serde_helpers;
use serde::{Deserialize, Serialize};

node_group! {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Bytecode related types.

use crate::{
artifacts::{serde_helpers, FunctionDebugData, GeneratedSource, Offsets},
serde_helpers,
sourcemap::{self, SourceMap, SyntaxError},
utils,
FunctionDebugData, GeneratedSource, Offsets,
};
use alloy_primitives::{hex, Address, Bytes};
use foundry_compilers_core::utils;
use serde::{Deserialize, Serialize, Serializer};
use std::collections::BTreeMap;

Expand Down Expand Up @@ -498,7 +499,7 @@ impl From<CompactDeployedBytecode> for DeployedBytecode {

#[cfg(test)]
mod tests {
use crate::{artifacts::ContractBytecode, ConfigurableContractArtifact};
use crate::{ConfigurableContractArtifact, ContractBytecode};

#[test]
fn test_empty_bytecode() {
Expand Down
Loading