Added implementation for resolc trait (#12)

Implement the Solidity Compiler trait for resolc
This commit is contained in:
activecoder10
2025-05-08 12:09:02 +03:00
committed by GitHub
parent 8009f5880c
commit 38b42560ec
8 changed files with 115 additions and 12 deletions
Generated
+3
View File
@@ -2998,7 +2998,10 @@ name = "revive-dt-compiler"
version = "0.1.0"
dependencies = [
"anyhow",
"log",
"revive-common",
"revive-dt-config",
"revive-dt-solc-binaries",
"revive-solc-json-interface",
"semver 1.0.26",
"serde_json",
+3
View File
@@ -11,6 +11,9 @@ rust-version.workspace = true
[dependencies]
anyhow = { workspace = true }
revive-solc-json-interface = { workspace = true }
revive-dt-config = { workspace = true }
revive-dt-solc-binaries = { workspace = true }
revive-common = { workspace = true }
semver = { workspace = true }
serde_json = { workspace = true }
log = { workspace = true }
+4
View File
@@ -9,6 +9,8 @@ use std::{
path::{Path, PathBuf},
};
use revive_dt_config::Arguments;
use revive_common::EVMVersion;
use revive_solc_json_interface::{
SolcStandardJsonInput, SolcStandardJsonInputLanguage, SolcStandardJsonInputSettings,
@@ -33,6 +35,8 @@ pub trait SolidityCompiler {
) -> anyhow::Result<CompilerOutput<Self::Options>>;
fn new(solc_executable: PathBuf) -> Self;
fn get_compiler_executable(config: &Arguments, version: Version) -> anyhow::Result<PathBuf>;
}
/// The generic compilation input configuration.
+79 -2
View File
@@ -1,2 +1,79 @@
//! Implements the [crate::SolidityCompiler] trait with resolc for
//! compiling contracts to PVM bytecode.
//! Implements the [SolidityCompiler] trait with `resolc` for
//! compiling contracts to PolkaVM (PVM) bytecode.
use std::{
path::PathBuf,
process::{Command, Stdio},
};
use crate::{CompilerInput, CompilerOutput, SolidityCompiler};
use revive_dt_config::Arguments;
use revive_solc_json_interface::SolcStandardJsonOutput;
/// A wrapper around the `resolc` binary, emitting PVM-compatible bytecode.
pub struct Resolc {
/// Path to the `resolc` executable
resolc_path: PathBuf,
}
impl SolidityCompiler for Resolc {
type Options = Vec<String>;
fn build(
&self,
input: CompilerInput<Self::Options>,
) -> anyhow::Result<CompilerOutput<Self::Options>> {
let mut child = Command::new(&self.resolc_path)
.arg("--standard-json")
.args(&input.extra_options)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
let stdin_pipe = child.stdin.as_mut().expect("stdin must be piped");
serde_json::to_writer(stdin_pipe, &input.input)?;
let json_in = serde_json::to_string_pretty(&input.input)?;
let output = child.wait_with_output()?;
let stdout = output.stdout;
let stderr = output.stderr;
if !output.status.success() {
log::error!(
"resolc failed exit={} stderr={} JSON-in={} ",
output.status,
String::from_utf8_lossy(&stderr),
json_in,
);
}
let parsed: SolcStandardJsonOutput = serde_json::from_slice(&stdout).map_err(|e| {
anyhow::anyhow!(
"failed to parse resolc JSON output: {e}\nstderr: {}",
String::from_utf8_lossy(&stderr)
)
})?;
Ok(CompilerOutput {
input,
output: parsed,
})
}
fn new(resolc_path: PathBuf) -> Self {
Resolc { resolc_path }
}
fn get_compiler_executable(
config: &Arguments,
_version: semver::Version,
) -> anyhow::Result<PathBuf> {
if !config.resolc.as_os_str().is_empty() {
return Ok(config.resolc.clone());
}
Ok(PathBuf::from("resolc"))
}
}
+10
View File
@@ -7,6 +7,8 @@ use std::{
};
use crate::{CompilerInput, CompilerOutput, SolidityCompiler};
use revive_dt_config::Arguments;
use revive_dt_solc_binaries::download_solc;
pub struct Solc {
solc_path: PathBuf,
@@ -39,4 +41,12 @@ impl SolidityCompiler for Solc {
fn new(solc_path: PathBuf) -> Self {
Self { solc_path }
}
fn get_compiler_executable(
config: &Arguments,
version: semver::Version,
) -> anyhow::Result<PathBuf> {
let path = download_solc(config.directory(), version, config.wasm)?;
Ok(path)
}
}
+4 -3
View File
@@ -8,7 +8,6 @@ use revive_dt_compiler::{Compiler, CompilerInput, SolidityCompiler};
use revive_dt_config::Arguments;
use revive_dt_format::{input::Input, metadata::Metadata, mode::SolcMode};
use revive_dt_node_interaction::EthereumNode;
use revive_dt_solc_binaries::download_solc;
use revive_solc_json_interface::SolcStandardJsonOutput;
use crate::Platform;
@@ -43,16 +42,18 @@ where
let sources = metadata.contract_sources()?;
let base_path = metadata.directory()?.display().to_string();
let mut compiler = Compiler::<T::Compiler>::new().base_path(base_path.clone());
for (file, _contract) in sources.values() {
log::debug!("contract source {}", file.display());
compiler = compiler.with_source(file)?;
}
let solc_path = download_solc(self.config.directory(), version, self.config.wasm)?;
let compiler_path = T::Compiler::get_compiler_executable(self.config, version)?;
let output = compiler
.solc_optimizer(mode.solc_optimize())
.try_build(solc_path)?;
.try_build(compiler_path)?;
self.contracts.insert(output.input, output.output);
+2 -2
View File
@@ -3,7 +3,7 @@
//! This crate defines the testing configuration and
//! provides a helper utilty to execute tests.
use revive_dt_compiler::{SolidityCompiler, solc};
use revive_dt_compiler::{SolidityCompiler, revive_resolc, solc};
use revive_dt_node::geth;
use revive_dt_node_interaction::EthereumNode;
@@ -30,5 +30,5 @@ pub struct Kitchensink;
impl Platform for Kitchensink {
type Blockchain = geth::Instance;
type Compiler = solc::Solc;
type Compiler = revive_resolc::Resolc;
}
+10 -5
View File
@@ -5,7 +5,7 @@ use rayon::{ThreadPoolBuilder, prelude::*};
use revive_dt_config::*;
use revive_dt_core::{
Geth,
Geth, Kitchensink,
driver::{Driver, State},
};
use revive_dt_format::{corpus::Corpus, metadata::Metadata};
@@ -109,11 +109,16 @@ fn main_compile_only(
) -> anyhow::Result<()> {
tests.par_iter().for_each(|metadata| {
for mode in &metadata.solc_modes() {
let mut state = match platform {
TestingPlatform::Geth => State::<Geth>::new(config),
_ => todo!(),
match platform {
TestingPlatform::Geth => {
let mut state = State::<Geth>::new(config);
let _ = state.build_contracts(mode, metadata);
}
TestingPlatform::Kitchensink => {
let mut state = State::<Kitchensink>::new(config);
let _ = state.build_contracts(mode, metadata);
}
};
let _ = state.build_contracts(mode, metadata);
}
});