Compare commits

..

3 Commits

Author SHA1 Message Date
Omar Abdulla c1f367b850 Format comments 2025-08-13 16:15:30 +03:00
Omar Abdulla 0797cd99fc Reorder the metadata struct 2025-08-13 15:26:06 +03:00
Omar Abdulla 5bd61d9651 Implement various needed features and improvements 2025-08-13 15:10:52 +03:00
10 changed files with 215 additions and 348 deletions
@@ -1,38 +0,0 @@
//! Implements a cached file system that allows for files to be read once into memory and then when
//! they're requested to be read again they will be returned from the cache.
use std::{
collections::HashMap,
path::{Path, PathBuf},
sync::{Arc, LazyLock},
};
use anyhow::Result;
use tokio::sync::RwLock;
#[allow(clippy::type_complexity)]
static CACHE: LazyLock<Arc<RwLock<HashMap<PathBuf, Vec<u8>>>>> = LazyLock::new(Default::default);
pub struct CachedFileSystem;
impl CachedFileSystem {
pub async fn read(path: impl AsRef<Path>) -> Result<Vec<u8>> {
let cache_read_lock = CACHE.read().await;
match cache_read_lock.get(path.as_ref()) {
Some(entry) => Ok(entry.clone()),
None => {
drop(cache_read_lock);
let content = std::fs::read(&path)?;
let mut cache_write_lock = CACHE.write().await;
cache_write_lock.insert(path.as_ref().to_path_buf(), content.clone());
Ok(content)
}
}
}
pub async fn read_to_string(path: impl AsRef<Path>) -> Result<String> {
let content = Self::read(path).await?;
String::from_utf8(content).map_err(Into::into)
}
}
-2
View File
@@ -1,5 +1,3 @@
mod cached_file_system;
mod clear_dir; mod clear_dir;
pub use cached_file_system::*;
pub use clear_dir::*; pub use clear_dir::*;
+6 -28
View File
@@ -5,6 +5,7 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
fs::read_to_string,
hash::Hash, hash::Hash,
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@@ -15,7 +16,7 @@ use semver::Version;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use revive_common::EVMVersion; use revive_common::EVMVersion;
use revive_dt_common::{fs::CachedFileSystem, types::VersionOrRequirement}; use revive_dt_common::types::VersionOrRequirement;
use revive_dt_config::Arguments; use revive_dt_config::Arguments;
pub mod revive_js; pub mod revive_js;
@@ -54,7 +55,6 @@ pub struct CompilerInput {
pub base_path: Option<PathBuf>, pub base_path: Option<PathBuf>,
pub sources: HashMap<PathBuf, String>, pub sources: HashMap<PathBuf, String>,
pub libraries: HashMap<PathBuf, HashMap<String, Address>>, pub libraries: HashMap<PathBuf, HashMap<String, Address>>,
pub revert_string_handling: Option<RevertString>,
} }
/// The generic compilation output configuration. /// The generic compilation output configuration.
@@ -91,7 +91,6 @@ where
base_path: Default::default(), base_path: Default::default(),
sources: Default::default(), sources: Default::default(),
libraries: Default::default(), libraries: Default::default(),
revert_string_handling: Default::default(),
}, },
additional_options: T::Options::default(), additional_options: T::Options::default(),
} }
@@ -122,11 +121,10 @@ where
self self
} }
pub async fn with_source(mut self, path: impl AsRef<Path>) -> anyhow::Result<Self> { pub fn with_source(mut self, path: impl AsRef<Path>) -> anyhow::Result<Self> {
self.input.sources.insert( self.input
path.as_ref().to_path_buf(), .sources
CachedFileSystem::read_to_string(path.as_ref()).await?, .insert(path.as_ref().to_path_buf(), read_to_string(path.as_ref())?);
);
Ok(self) Ok(self)
} }
@@ -144,14 +142,6 @@ where
self self
} }
pub fn with_revert_string_handling(
mut self,
revert_string_handling: impl Into<Option<RevertString>>,
) -> Self {
self.input.revert_string_handling = revert_string_handling.into();
self
}
pub fn with_additional_options(mut self, options: impl Into<T::Options>) -> Self { pub fn with_additional_options(mut self, options: impl Into<T::Options>) -> Self {
self.additional_options = options.into(); self.additional_options = options.into();
self self
@@ -170,15 +160,3 @@ where
self.input.clone() self.input.clone()
} }
} }
/// Defines how the compiler should handle revert strings.
#[derive(
Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
pub enum RevertString {
#[default]
Default,
Debug,
Strip,
VerboseDebug,
}
-3
View File
@@ -47,9 +47,6 @@ impl SolidityCompiler for Resolc {
base_path, base_path,
sources, sources,
libraries, libraries,
// TODO: this is currently not being handled since there is no way to pass it into
// resolc. So, we need to go back to this later once it's supported.
revert_string_handling: _,
}: CompilerInput, }: CompilerInput,
additional_options: Self::Options, additional_options: Self::Options,
) -> anyhow::Result<CompilerOutput> { ) -> anyhow::Result<CompilerOutput> {
-10
View File
@@ -42,7 +42,6 @@ impl SolidityCompiler for Solc {
base_path, base_path,
sources, sources,
libraries, libraries,
revert_string_handling,
}: CompilerInput, }: CompilerInput,
_: Self::Options, _: Self::Options,
) -> anyhow::Result<CompilerOutput> { ) -> anyhow::Result<CompilerOutput> {
@@ -88,15 +87,6 @@ impl SolidityCompiler for Solc {
}) })
.collect(), .collect(),
}, },
debug: revert_string_handling.map(|revert_string_handling| DebuggingSettings {
revert_strings: match revert_string_handling {
crate::RevertString::Default => Some(RevertStrings::Default),
crate::RevertString::Debug => Some(RevertStrings::Debug),
crate::RevertString::Strip => Some(RevertStrings::Strip),
crate::RevertString::VerboseDebug => Some(RevertStrings::VerboseDebug),
},
debug_info: Default::default(),
}),
..Default::default() ..Default::default()
}, },
}; };
+2 -18
View File
@@ -96,19 +96,10 @@ pub struct Arguments {
#[arg(long, default_value = "1")] #[arg(long, default_value = "1")]
pub number_of_nodes: usize, pub number_of_nodes: usize,
/// Determines the amount of tokio worker threads that will will be used. /// Determines the amount of threads that will will be used.
#[arg( #[arg(long, default_value = "12")]
long,
default_value_t = std::thread::available_parallelism()
.map(|n| n.get())
.unwrap_or(1)
)]
pub number_of_threads: usize, pub number_of_threads: usize,
/// Determines the amount of concurrent tasks that will be spawned to run tests. Defaults to 10 x the number of nodes.
#[arg(long)]
pub number_concurrent_tasks: Option<usize>,
/// Extract problems back to the test corpus. /// Extract problems back to the test corpus.
#[arg(short, long = "extract-problems")] #[arg(short, long = "extract-problems")]
pub extract_problems: bool, pub extract_problems: bool,
@@ -143,13 +134,6 @@ impl Arguments {
panic!("should have a workdir configured") panic!("should have a workdir configured")
} }
/// Return the number of concurrent tasks to run. This is provided via the
/// `--number-concurrent-tasks` argument, and otherwise defaults to --number-of-nodes * 20.
pub fn number_of_concurrent_tasks(&self) -> usize {
self.number_concurrent_tasks
.unwrap_or(20 * self.number_of_nodes)
}
/// Try to parse `self.account` into a [PrivateKeySigner], /// Try to parse `self.account` into a [PrivateKeySigner],
/// panicing on error. /// panicing on error.
pub fn wallet(&self) -> EthereumWallet { pub fn wallet(&self) -> EthereumWallet {
+183 -192
View File
@@ -1,6 +1,6 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
path::{Path, PathBuf}, path::Path,
sync::{Arc, LazyLock}, sync::{Arc, LazyLock},
time::Instant, time::Instant,
}; };
@@ -18,7 +18,7 @@ use revive_dt_common::iterators::FilesWithExtensionIterator;
use revive_dt_node_interaction::EthereumNode; use revive_dt_node_interaction::EthereumNode;
use semver::Version; use semver::Version;
use temp_dir::TempDir; use temp_dir::TempDir;
use tokio::sync::{Mutex, RwLock, mpsc}; use tokio::sync::{Mutex, RwLock};
use tracing::{Instrument, Level}; use tracing::{Instrument, Level};
use tracing_subscriber::{EnvFilter, FmtSubscriber}; use tracing_subscriber::{EnvFilter, FmtSubscriber};
@@ -41,33 +41,20 @@ use revive_dt_report::reporter::{Report, Span};
static TEMP_DIR: LazyLock<TempDir> = LazyLock::new(|| TempDir::new().unwrap()); static TEMP_DIR: LazyLock<TempDir> = LazyLock::new(|| TempDir::new().unwrap());
type CompilationCache = Arc< type CompilationCache<'a> = Arc<
RwLock< RwLock<
HashMap< HashMap<
(PathBuf, SolcMode, TestingPlatform), (&'a Path, SolcMode, TestingPlatform),
Arc<Mutex<Option<Arc<(Version, CompilerOutput)>>>>, Arc<Mutex<Option<Arc<(Version, CompilerOutput)>>>>,
>, >,
>, >,
>; >;
/// this represents a single "test"; a mode, path and collection of cases.
#[derive(Clone)]
struct Test {
metadata: Metadata,
path: PathBuf,
mode: SolcMode,
case_idx: usize,
case: Case,
}
/// This represents the results that we gather from running test cases.
type CaseResult = Result<usize, anyhow::Error>;
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let args = init_cli()?; let args = init_cli()?;
let body = async { let body = async {
for (corpus, tests) in collect_corpora(&args).await? { for (corpus, tests) in collect_corpora(&args)? {
let span = Span::new(corpus, args.clone())?; let span = Span::new(corpus, args.clone())?;
match &args.compile_only { match &args.compile_only {
Some(platform) => compile_corpus(&args, &tests, platform, span).await, Some(platform) => compile_corpus(&args, &tests, platform, span).await,
@@ -117,13 +104,13 @@ fn init_cli() -> anyhow::Result<Arguments> {
Ok(args) Ok(args)
} }
async fn collect_corpora(args: &Arguments) -> anyhow::Result<HashMap<Corpus, Vec<MetadataFile>>> { fn collect_corpora(args: &Arguments) -> anyhow::Result<HashMap<Corpus, Vec<MetadataFile>>> {
let mut corpora = HashMap::new(); let mut corpora = HashMap::new();
for path in &args.corpus { for path in &args.corpus {
let corpus = Corpus::try_from_path(path)?; let corpus = Corpus::try_from_path(path)?;
tracing::info!("found corpus: {}", path.display()); tracing::info!("found corpus: {}", path.display());
let tests = corpus.enumerate_tests().await; let tests = corpus.enumerate_tests();
tracing::info!("corpus '{}' contains {} tests", &corpus.name, tests.len()); tracing::info!("corpus '{}' contains {} tests", &corpus.name, tests.len());
corpora.insert(corpus, tests); corpora.insert(corpus, tests);
} }
@@ -133,7 +120,7 @@ async fn collect_corpora(args: &Arguments) -> anyhow::Result<HashMap<Corpus, Vec
async fn run_driver<L, F>( async fn run_driver<L, F>(
args: &Arguments, args: &Arguments,
metadata_files: &[MetadataFile], tests: &[MetadataFile],
span: Span, span: Span,
) -> anyhow::Result<()> ) -> anyhow::Result<()>
where where
@@ -142,25 +129,10 @@ where
L::Blockchain: revive_dt_node::Node + Send + Sync + 'static, L::Blockchain: revive_dt_node::Node + Send + Sync + 'static,
F::Blockchain: revive_dt_node::Node + Send + Sync + 'static, F::Blockchain: revive_dt_node::Node + Send + Sync + 'static,
{ {
let (report_tx, report_rx) = mpsc::unbounded_channel::<(Test, CaseResult)>(); let leader_nodes = NodePool::<L::Blockchain>::new(args)?;
let follower_nodes = NodePool::<F::Blockchain>::new(args)?;
let tests = prepare_tests::<L, F>(metadata_files); let test_cases = tests
let driver_task = start_driver_task::<L, F>(args, tests, span, report_tx).await?;
let status_reporter_task = start_reporter_task(report_rx);
tokio::join!(status_reporter_task, driver_task);
Ok(())
}
fn prepare_tests<L, F>(metadata_files: &[MetadataFile]) -> impl Iterator<Item = Test>
where
L: Platform,
F: Platform,
L::Blockchain: revive_dt_node::Node + Send + Sync + 'static,
F::Blockchain: revive_dt_node::Node + Send + Sync + 'static,
{
metadata_files
.iter() .iter()
.flat_map( .flat_map(
|MetadataFile { |MetadataFile {
@@ -226,159 +198,188 @@ where
} }
None => true, None => true,
}) })
.map(|(metadata_file_path, metadata, case_idx, case, solc_mode)| { .collect::<Vec<_>>();
Test {
metadata: metadata.clone(), let metadata_case_status = Arc::new(RwLock::new(test_cases.iter().fold(
path: metadata_file_path.to_path_buf(), HashMap::<_, HashMap<_, _>>::new(),
mode: solc_mode, |mut map, (path, _, case_idx, case, solc_mode)| {
case_idx, map.entry((path.to_path_buf(), solc_mode.clone()))
case: case.clone(), .or_default()
.insert((CaseIdx::new(*case_idx), case.name.clone()), None::<bool>);
map
},
)));
let status_reporter_task = {
let metadata_case_status = metadata_case_status.clone();
let start = Instant::now();
async move {
const GREEN: &str = "\x1B[32m";
const RED: &str = "\x1B[31m";
const RESET: &str = "\x1B[0m";
let mut entries_to_delete = Vec::new();
let mut number_of_successes = 0;
let mut number_of_failures = 0;
loop {
let metadata_case_status_read = metadata_case_status.read().await;
if metadata_case_status_read.is_empty() {
break;
}
for ((metadata_file_path, solc_mode), case_status) in
metadata_case_status_read.iter()
{
if case_status.values().any(|value| value.is_none()) {
continue;
}
let contains_failures = case_status
.values()
.any(|value| value.is_some_and(|value| !value));
if !contains_failures {
eprintln!(
"{}Succeeded:{} {} - {:?}",
GREEN,
RESET,
metadata_file_path.display(),
solc_mode
)
} else {
eprintln!(
"{}Failed:{} {} - {:?}",
RED,
RESET,
metadata_file_path.display(),
solc_mode
)
};
number_of_successes += case_status
.values()
.filter(|value| value.is_some_and(|value| value))
.count();
number_of_failures += case_status
.values()
.filter(|value| value.is_some_and(|value| !value))
.count();
let mut case_status = case_status
.iter()
.map(|((case_idx, case_name), case_status)| {
(case_idx.into_inner(), case_name, case_status.unwrap())
})
.collect::<Vec<_>>();
case_status.sort_by(|a, b| a.0.cmp(&b.0));
for (case_idx, case_name, case_status) in case_status.into_iter() {
if case_status {
eprintln!(
" {GREEN}Case Succeeded:{RESET} {} - Case Idx: {case_idx}",
case_name
.as_ref()
.map(|string| string.as_str())
.unwrap_or("Unnamed case")
)
} else {
eprintln!(
" {RED}Case Failed:{RESET} {} - Case Idx: {case_idx}",
case_name
.as_ref()
.map(|string| string.as_str())
.unwrap_or("Unnamed case")
)
};
}
eprintln!();
entries_to_delete.push((metadata_file_path.clone(), solc_mode.clone()));
}
drop(metadata_case_status_read);
let mut metadata_case_status_write = metadata_case_status.write().await;
for entry in entries_to_delete.drain(..) {
metadata_case_status_write.remove(&entry);
}
tokio::time::sleep(std::time::Duration::from_secs(3)).await;
} }
})
}
async fn start_driver_task<L, F>( let elapsed = start.elapsed();
args: &Arguments, eprintln!(
tests: impl Iterator<Item = Test>, "{GREEN}{}{RESET} cases succeeded, {RED}{}{RESET} cases failed in {} seconds",
span: Span, number_of_successes,
report_tx: mpsc::UnboundedSender<(Test, CaseResult)>, number_of_failures,
) -> anyhow::Result<impl Future<Output = ()>> elapsed.as_secs()
where );
L: Platform, }
F: Platform, };
L::Blockchain: revive_dt_node::Node + Send + Sync + 'static,
F::Blockchain: revive_dt_node::Node + Send + Sync + 'static,
{
let leader_nodes = Arc::new(NodePool::<L::Blockchain>::new(args).await?);
let follower_nodes = Arc::new(NodePool::<F::Blockchain>::new(args).await?);
let compilation_cache = Arc::new(RwLock::new(HashMap::new())); let compilation_cache = Arc::new(RwLock::new(HashMap::new()));
let number_concurrent_tasks = args.number_of_concurrent_tasks(); let driver_task = futures::stream::iter(test_cases).for_each_concurrent(
None,
Ok(futures::stream::iter(tests).for_each_concurrent( |(metadata_file_path, metadata, case_idx, case, solc_mode)| {
// We want to limit the concurrent tasks here because:
//
// 1. We don't want to overwhelm the nodes with too many requests, leading to responses timing out.
// 2. We don't want to open too many files at once, leading to the OS running out of file descriptors.
//
// By default, we allow maximum of 10 ongoing requests per node in order to limit (1), and assume that
// this number will automatically be low enough to address (2). The user can override this.
Some(number_concurrent_tasks),
move |test| {
let leader_nodes = leader_nodes.clone();
let follower_nodes = follower_nodes.clone();
let compilation_cache = compilation_cache.clone(); let compilation_cache = compilation_cache.clone();
let report_tx = report_tx.clone(); let leader_node = leader_nodes.round_robbin();
let follower_node = follower_nodes.round_robbin();
let tracing_span = tracing::span!(
Level::INFO,
"Running driver",
metadata_file_path = %metadata_file_path.display(),
case_idx = case_idx,
solc_mode = ?solc_mode,
);
let metadata_case_status = metadata_case_status.clone();
async move { async move {
let leader_node = leader_nodes.round_robbin();
let follower_node = follower_nodes.round_robbin();
let tracing_span = tracing::span!(
Level::INFO,
"Running driver",
metadata_file_path = %test.path.display(),
case_idx = ?test.case_idx,
solc_mode = ?test.mode,
);
let result = handle_case_driver::<L, F>( let result = handle_case_driver::<L, F>(
&test.path, metadata_file_path.as_path(),
&test.metadata, metadata,
test.case_idx.into(), case_idx.into(),
&test.case, case,
test.mode.clone(), solc_mode.clone(),
args, args,
compilation_cache.clone(), compilation_cache.clone(),
leader_node, leader_node,
follower_node, follower_node,
span, span,
) )
.instrument(tracing_span)
.await; .await;
let mut metadata_case_status = metadata_case_status.write().await;
report_tx match result {
.send((test, result)) Ok(inputs_executed) => {
.expect("Failed to send report"); tracing::info!(inputs_executed, "Execution succeeded");
metadata_case_status
.entry((metadata_file_path.clone(), solc_mode))
.or_default()
.insert((CaseIdx::new(case_idx), case.name.clone()), Some(true));
}
Err(error) => {
metadata_case_status
.entry((metadata_file_path.clone(), solc_mode))
.or_default()
.insert((CaseIdx::new(case_idx), case.name.clone()), Some(false));
tracing::error!(%error, "Execution failed")
}
}
tracing::info!("Execution completed");
} }
.instrument(tracing_span)
}, },
))
}
async fn start_reporter_task(mut report_rx: mpsc::UnboundedReceiver<(Test, CaseResult)>) {
let start = Instant::now();
const GREEN: &str = "\x1B[32m";
const RED: &str = "\x1B[31m";
const COLOUR_RESET: &str = "\x1B[0m";
const BOLD: &str = "\x1B[1m";
const BOLD_RESET: &str = "\x1B[22m";
let mut number_of_successes = 0;
let mut number_of_failures = 0;
let mut failures = vec![];
// Wait for reports to come from our test runner. When the channel closes, this ends.
while let Some((test, case_result)) = report_rx.recv().await {
let case_name = test.case.name.as_deref().unwrap_or("unnamed_case");
let case_idx = test.case_idx;
let test_path = test.path.display();
let test_mode = test.mode.clone();
match case_result {
Ok(_inputs) => {
number_of_successes += 1;
eprintln!(
"{GREEN}Case Succeeded:{COLOUR_RESET} {test_path} -> {case_name}:{case_idx} (mode: {test_mode:?})"
);
}
Err(err) => {
number_of_failures += 1;
eprintln!(
"{RED}Case Failed:{COLOUR_RESET} {test_path} -> {case_name}:{case_idx} (mode: {test_mode:?})"
);
failures.push((test, err));
}
}
}
eprintln!();
let elapsed = start.elapsed();
// Now, log the failures with more complete errors at the bottom, like `cargo test` does, so
// that we don't have to scroll through the entire output to find them.
if !failures.is_empty() {
eprintln!("{BOLD}Failures:{BOLD_RESET}\n");
for failure in failures {
let (test, err) = failure;
let case_name = test.case.name.as_deref().unwrap_or("unnamed_case");
let case_idx = test.case_idx;
let test_path = test.path.display();
let test_mode = test.mode.clone();
eprintln!(
"---- {RED}Case Failed:{COLOUR_RESET} {test_path} -> {case_name}:{case_idx} (mode: {test_mode:?}) ----\n\n{err}\n"
);
}
}
// Summary at the end.
eprintln!(
"{} cases: {GREEN}{number_of_successes}{COLOUR_RESET} cases succeeded, {RED}{number_of_failures}{COLOUR_RESET} cases failed in {} seconds",
number_of_successes + number_of_failures,
elapsed.as_secs()
); );
tokio::join!(status_reporter_task, driver_task);
Ok(())
} }
#[allow(clippy::too_many_arguments)] #[allow(clippy::too_many_arguments)]
async fn handle_case_driver<L, F>( async fn handle_case_driver<'a, L, F>(
metadata_file_path: &Path, metadata_file_path: &'a Path,
metadata: &Metadata, metadata: &'a Metadata,
case_idx: CaseIdx, case_idx: CaseIdx,
case: &Case, case: &Case,
mode: SolcMode, mode: SolcMode,
config: &Arguments, config: &Arguments,
compilation_cache: CompilationCache, compilation_cache: CompilationCache<'a>,
leader_node: &L::Blockchain, leader_node: &L::Blockchain,
follower_node: &F::Blockchain, follower_node: &F::Blockchain,
_: Span, _: Span,
@@ -519,9 +520,11 @@ where
); );
let Some(leader_library_address) = leader_receipt.contract_address else { let Some(leader_library_address) = leader_receipt.contract_address else {
tracing::error!("Contract deployment transaction didn't return an address");
anyhow::bail!("Contract deployment didn't return an address"); anyhow::bail!("Contract deployment didn't return an address");
}; };
let Some(follower_library_address) = follower_receipt.contract_address else { let Some(follower_library_address) = follower_receipt.contract_address else {
tracing::error!("Contract deployment transaction didn't return an address");
anyhow::bail!("Contract deployment didn't return an address"); anyhow::bail!("Contract deployment didn't return an address");
}; };
@@ -551,16 +554,8 @@ where
.any(|(code, _)| !code.chars().all(|char| char.is_ascii_hexdigit())); .any(|(code, _)| !code.chars().all(|char| char.is_ascii_hexdigit()));
let (leader_compiled_contracts, follower_compiled_contracts) = let (leader_compiled_contracts, follower_compiled_contracts) =
if metadata_file_contains_libraries && compiled_contracts_require_linking { if metadata_file_contains_libraries && compiled_contracts_require_linking {
let leader_key = ( let leader_key = (metadata_file_path, mode.clone(), L::config_id());
metadata_file_path.to_path_buf(), let follower_key = (metadata_file_path, mode.clone(), L::config_id());
mode.clone(),
L::config_id(),
);
let follower_key = (
metadata_file_path.to_path_buf(),
mode.clone(),
F::config_id(),
);
{ {
let mut cache = compilation_cache.write().await; let mut cache = compilation_cache.write().await;
cache.remove(&leader_key); cache.remove(&leader_key);
@@ -614,19 +609,15 @@ where
driver.execute().await driver.execute().await
} }
async fn get_or_build_contracts<P: Platform>( async fn get_or_build_contracts<'a, P: Platform>(
metadata: &Metadata, metadata: &'a Metadata,
metadata_file_path: &Path, metadata_file_path: &'a Path,
mode: SolcMode, mode: SolcMode,
config: &Arguments, config: &Arguments,
compilation_cache: CompilationCache, compilation_cache: CompilationCache<'a>,
deployed_libraries: &HashMap<ContractInstance, (Address, JsonAbi)>, deployed_libraries: &HashMap<ContractInstance, (Address, JsonAbi)>,
) -> anyhow::Result<Arc<(Version, CompilerOutput)>> { ) -> anyhow::Result<Arc<(Version, CompilerOutput)>> {
let key = ( let key = (metadata_file_path, mode.clone(), P::config_id());
metadata_file_path.to_path_buf(),
mode.clone(),
P::config_id(),
);
if let Some(compilation_artifact) = compilation_cache.read().await.get(&key).cloned() { if let Some(compilation_artifact) = compilation_cache.read().await.get(&key).cloned() {
let mut compilation_artifact = compilation_artifact.lock().await; let mut compilation_artifact = compilation_artifact.lock().await;
match *compilation_artifact { match *compilation_artifact {
@@ -693,12 +684,12 @@ async fn compile_contracts<P: Platform>(
"Compiling contracts" "Compiling contracts"
); );
let mut compiler = Compiler::<P::Compiler>::new() let compiler = Compiler::<P::Compiler>::new()
.with_allow_path(metadata.directory()?) .with_allow_path(metadata.directory()?)
.with_optimization(mode.solc_optimize()); .with_optimization(mode.solc_optimize());
for path in metadata.files_to_compile()? { let mut compiler = metadata
compiler = compiler.with_source(path).await?; .files_to_compile()?
} .try_fold(compiler, |compiler, path| compiler.with_source(&path))?;
for (library_instance, (library_address, _)) in deployed_libraries.iter() { for (library_instance, (library_address, _)) in deployed_libraries.iter() {
let library_ident = &metadata let library_ident = &metadata
.contracts .contracts
+6 -6
View File
@@ -39,9 +39,9 @@ impl Corpus {
} }
/// Scan the corpus base directory and return all tests found. /// Scan the corpus base directory and return all tests found.
pub async fn enumerate_tests(&self) -> Vec<MetadataFile> { pub fn enumerate_tests(&self) -> Vec<MetadataFile> {
let mut tests = Vec::new(); let mut tests = Vec::new();
collect_metadata(&self.path, &mut tests).await; collect_metadata(&self.path, &mut tests);
tests tests
} }
} }
@@ -52,7 +52,7 @@ impl Corpus {
/// Found tests are inserted into `tests`. /// Found tests are inserted into `tests`.
/// ///
/// `path` is expected to be a directory. /// `path` is expected to be a directory.
pub async fn collect_metadata(path: &Path, tests: &mut Vec<MetadataFile>) { pub fn collect_metadata(path: &Path, tests: &mut Vec<MetadataFile>) {
if path.is_dir() { if path.is_dir() {
let dir_entry = match std::fs::read_dir(path) { let dir_entry = match std::fs::read_dir(path) {
Ok(dir_entry) => dir_entry, Ok(dir_entry) => dir_entry,
@@ -73,12 +73,12 @@ pub async fn collect_metadata(path: &Path, tests: &mut Vec<MetadataFile>) {
let path = entry.path(); let path = entry.path();
if path.is_dir() { if path.is_dir() {
Box::pin(collect_metadata(&path, tests)).await; collect_metadata(&path, tests);
continue; continue;
} }
if path.is_file() { if path.is_file() {
if let Some(metadata) = MetadataFile::try_from_file(&path).await { if let Some(metadata) = MetadataFile::try_from_file(&path) {
tests.push(metadata) tests.push(metadata)
} }
} }
@@ -89,7 +89,7 @@ pub async fn collect_metadata(path: &Path, tests: &mut Vec<MetadataFile>) {
return; return;
}; };
if extension.eq_ignore_ascii_case("sol") || extension.eq_ignore_ascii_case("json") { if extension.eq_ignore_ascii_case("sol") || extension.eq_ignore_ascii_case("json") {
if let Some(metadata) = MetadataFile::try_from_file(path).await { if let Some(metadata) = MetadataFile::try_from_file(path) {
tests.push(metadata) tests.push(metadata)
} }
} else { } else {
+12 -46
View File
@@ -2,6 +2,7 @@ use std::{
cmp::Ordering, cmp::Ordering,
collections::BTreeMap, collections::BTreeMap,
fmt::Display, fmt::Display,
fs::{File, read_to_string},
ops::Deref, ops::Deref,
path::{Path, PathBuf}, path::{Path, PathBuf},
str::FromStr, str::FromStr,
@@ -10,9 +11,7 @@ use std::{
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use revive_common::EVMVersion; use revive_common::EVMVersion;
use revive_dt_common::{ use revive_dt_common::{iterators::FilesWithExtensionIterator, macros::define_wrapper_type};
fs::CachedFileSystem, iterators::FilesWithExtensionIterator, macros::define_wrapper_type,
};
use crate::{ use crate::{
case::Case, case::Case,
@@ -30,8 +29,8 @@ pub struct MetadataFile {
} }
impl MetadataFile { impl MetadataFile {
pub async fn try_from_file(path: &Path) -> Option<Self> { pub fn try_from_file(path: &Path) -> Option<Self> {
Metadata::try_from_file(path).await.map(|metadata| Self { Metadata::try_from_file(path).map(|metadata| Self {
path: path.to_owned(), path: path.to_owned(),
content: metadata, content: metadata,
}) })
@@ -76,12 +75,6 @@ pub struct Metadata {
/// be run of the evm version of the nodes match the evm version specified here. /// be run of the evm version of the nodes match the evm version specified here.
#[serde(skip_serializing_if = "Option::is_none")] #[serde(skip_serializing_if = "Option::is_none")]
pub required_evm_version: Option<EvmVersionRequirement>, pub required_evm_version: Option<EvmVersionRequirement>,
/// A set of compilation directives that will be passed to the compiler whenever the contracts for
/// the test are being compiled. Note that this differs from the [`Mode`]s in that a [`Mode`] is
/// just a filter for when a test can run whereas this is an instruction to the compiler.
#[serde(skip_serializing_if = "Option::is_none")]
pub compiler_directives: Option<CompilationDirectives>,
} }
impl Metadata { impl Metadata {
@@ -152,7 +145,7 @@ impl Metadata {
/// ///
/// # Panics /// # Panics
/// Expects the supplied `path` to be a file. /// Expects the supplied `path` to be a file.
pub async fn try_from_file(path: &Path) -> Option<Self> { pub fn try_from_file(path: &Path) -> Option<Self> {
assert!(path.is_file(), "not a file: {}", path.display()); assert!(path.is_file(), "not a file: {}", path.display());
let Some(file_extension) = path.extension() else { let Some(file_extension) = path.extension() else {
@@ -161,20 +154,19 @@ impl Metadata {
}; };
if file_extension == METADATA_FILE_EXTENSION { if file_extension == METADATA_FILE_EXTENSION {
return Self::try_from_json(path).await; return Self::try_from_json(path);
} }
if file_extension == SOLIDITY_CASE_FILE_EXTENSION { if file_extension == SOLIDITY_CASE_FILE_EXTENSION {
return Self::try_from_solidity(path).await; return Self::try_from_solidity(path);
} }
tracing::debug!("ignoring invalid corpus file: {}", path.display()); tracing::debug!("ignoring invalid corpus file: {}", path.display());
None None
} }
async fn try_from_json(path: &Path) -> Option<Self> { fn try_from_json(path: &Path) -> Option<Self> {
let content = CachedFileSystem::read(path) let file = File::open(path)
.await
.inspect_err(|error| { .inspect_err(|error| {
tracing::error!( tracing::error!(
"opening JSON test metadata file '{}' error: {error}", "opening JSON test metadata file '{}' error: {error}",
@@ -183,7 +175,7 @@ impl Metadata {
}) })
.ok()?; .ok()?;
match serde_json::from_slice::<Metadata>(content.as_slice()) { match serde_json::from_reader::<_, Metadata>(file) {
Ok(mut metadata) => { Ok(mut metadata) => {
metadata.file_path = Some(path.to_path_buf()); metadata.file_path = Some(path.to_path_buf());
Some(metadata) Some(metadata)
@@ -198,9 +190,8 @@ impl Metadata {
} }
} }
async fn try_from_solidity(path: &Path) -> Option<Self> { fn try_from_solidity(path: &Path) -> Option<Self> {
let spec = CachedFileSystem::read_to_string(path) let spec = read_to_string(path)
.await
.inspect_err(|error| { .inspect_err(|error| {
tracing::error!( tracing::error!(
"opening JSON test metadata file '{}' error: {error}", "opening JSON test metadata file '{}' error: {error}",
@@ -499,31 +490,6 @@ impl From<EvmVersionRequirement> for String {
} }
} }
/// A set of compilation directives that will be passed to the compiler whenever the contracts for
/// the test are being compiled. Note that this differs from the [`Mode`]s in that a [`Mode`] is
/// just a filter for when a test can run whereas this is an instruction to the compiler.
/// Defines how the compiler should handle revert strings.
#[derive(
Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
pub struct CompilationDirectives {
/// Defines how the revert strings should be handled.
pub revert_string_handling: Option<RevertString>,
}
/// Defines how the compiler should handle revert strings.
#[derive(
Clone, Debug, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default, Serialize, Deserialize,
)]
#[serde(rename_all = "camelCase")]
pub enum RevertString {
#[default]
Default,
Debug,
Strip,
VerboseDebug,
}
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use super::*; use super::*;
+6 -5
View File
@@ -1,12 +1,12 @@
//! This crate implements concurrent handling of testing node. //! This crate implements concurrent handling of testing node.
use std::{ use std::{
fs::read_to_string,
sync::atomic::{AtomicUsize, Ordering}, sync::atomic::{AtomicUsize, Ordering},
thread, thread,
}; };
use anyhow::Context; use anyhow::Context;
use revive_dt_common::fs::CachedFileSystem;
use revive_dt_config::Arguments; use revive_dt_config::Arguments;
use crate::Node; use crate::Node;
@@ -23,11 +23,12 @@ where
T: Node + Send + 'static, T: Node + Send + 'static,
{ {
/// Create a new Pool. This will start as many nodes as there are workers in `config`. /// Create a new Pool. This will start as many nodes as there are workers in `config`.
pub async fn new(config: &Arguments) -> anyhow::Result<Self> { pub fn new(config: &Arguments) -> anyhow::Result<Self> {
let nodes = config.number_of_nodes; let nodes = config.number_of_nodes;
let genesis = CachedFileSystem::read_to_string(&config.genesis_file) let genesis = read_to_string(&config.genesis_file).context(format!(
.await "can not read genesis file: {}",
.context("Failed to read genesis file")?; config.genesis_file.display()
))?;
let mut handles = Vec::with_capacity(nodes); let mut handles = Vec::with_capacity(nodes);
for _ in 0..nodes { for _ in 0..nodes {