mirror of
https://github.com/pezkuwichain/revive-differential-tests.git
synced 2026-06-12 04:11:12 +00:00
Scaffold utility and library (#3)
Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com> Signed-off-by: xermicus <bigcyrill@hotmail.com>
This commit is contained in:
@@ -0,0 +1,122 @@
|
||||
//! The test driver handles the compilation and execution of the test cases.
|
||||
|
||||
use alloy::{
|
||||
primitives::{Address, map::HashMap},
|
||||
rpc::types::trace::geth::GethTrace,
|
||||
};
|
||||
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;
|
||||
|
||||
type Contracts<T> = HashMap<
|
||||
CompilerInput<<<T as Platform>::Compiler as SolidityCompiler>::Options>,
|
||||
SolcStandardJsonOutput,
|
||||
>;
|
||||
|
||||
pub struct State<'a, T: Platform> {
|
||||
config: &'a Arguments,
|
||||
contracts: Contracts<T>,
|
||||
deployed_contracts: HashMap<String, Address>,
|
||||
}
|
||||
|
||||
impl<'a, T> State<'a, T>
|
||||
where
|
||||
T: Platform,
|
||||
{
|
||||
pub fn new(config: &'a Arguments) -> Self {
|
||||
Self {
|
||||
config,
|
||||
contracts: Default::default(),
|
||||
deployed_contracts: Default::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn build_contracts(&mut self, mode: &SolcMode, metadata: &Metadata) -> anyhow::Result<()> {
|
||||
let Some(version) = mode.last_patch_version(&self.config.solc) else {
|
||||
anyhow::bail!("unsupported solc version: {:?}", mode.solc_version);
|
||||
};
|
||||
|
||||
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 output = compiler
|
||||
.solc_optimizer(mode.solc_optimize())
|
||||
.try_build(solc_path)?;
|
||||
|
||||
self.contracts.insert(output.input, output.output);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn execute_input(
|
||||
&mut self,
|
||||
input: &Input,
|
||||
node: &T::Blockchain,
|
||||
) -> anyhow::Result<GethTrace> {
|
||||
let receipt = node.execute_transaction(input.legacy_transaction(
|
||||
self.config.network_id,
|
||||
0,
|
||||
&self.deployed_contracts,
|
||||
)?)?;
|
||||
dbg!(&receipt);
|
||||
//node.trace_transaction(receipt)
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Driver<'a, Leader: Platform, Follower: Platform> {
|
||||
metadata: &'a Metadata,
|
||||
config: &'a Arguments,
|
||||
leader_node: &'a Leader::Blockchain,
|
||||
follower_node: &'a Follower::Blockchain,
|
||||
}
|
||||
|
||||
impl<'a, L, F> Driver<'a, L, F>
|
||||
where
|
||||
L: Platform,
|
||||
F: Platform,
|
||||
{
|
||||
pub fn new(
|
||||
metadata: &'a Metadata,
|
||||
config: &'a Arguments,
|
||||
leader_node: &'a L::Blockchain,
|
||||
follower_node: &'a F::Blockchain,
|
||||
) -> Driver<'a, L, F> {
|
||||
Self {
|
||||
metadata,
|
||||
config,
|
||||
leader_node,
|
||||
follower_node,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn execute(&mut self) -> anyhow::Result<()> {
|
||||
for mode in self.metadata.solc_modes() {
|
||||
let mut leader_state = State::<L>::new(self.config);
|
||||
leader_state.build_contracts(&mode, self.metadata)?;
|
||||
|
||||
let mut follower_state = State::<F>::new(self.config);
|
||||
follower_state.build_contracts(&mode, self.metadata)?;
|
||||
|
||||
for case in &self.metadata.cases {
|
||||
for input in &case.inputs {
|
||||
let _ = leader_state.execute_input(input, self.leader_node)?;
|
||||
let _ = follower_state.execute_input(input, self.follower_node)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
//! The revive differential testing core library.
|
||||
//!
|
||||
//! This crate defines the testing configuration and
|
||||
//! provides a helper utilty to execute tests.
|
||||
|
||||
use revive_dt_compiler::{SolidityCompiler, solc};
|
||||
use revive_dt_node::geth;
|
||||
use revive_dt_node_interaction::EthereumNode;
|
||||
|
||||
pub mod driver;
|
||||
|
||||
/// One platform can be tested differentially against another.
|
||||
///
|
||||
/// For this we need a blockchain node implementation and a compiler.
|
||||
pub trait Platform {
|
||||
type Blockchain: EthereumNode;
|
||||
type Compiler: SolidityCompiler;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Geth;
|
||||
|
||||
impl Platform for Geth {
|
||||
type Blockchain = geth::Instance;
|
||||
type Compiler = solc::Solc;
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Kitchensink;
|
||||
|
||||
impl Platform for Kitchensink {
|
||||
type Blockchain = geth::Instance;
|
||||
type Compiler = solc::Solc;
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
use std::{collections::HashMap, sync::LazyLock};
|
||||
|
||||
use clap::Parser;
|
||||
use rayon::{ThreadPoolBuilder, prelude::*};
|
||||
|
||||
use revive_dt_config::*;
|
||||
use revive_dt_core::{
|
||||
Geth,
|
||||
driver::{Driver, State},
|
||||
};
|
||||
use revive_dt_format::{corpus::Corpus, metadata::Metadata};
|
||||
use revive_dt_node::pool::NodePool;
|
||||
use temp_dir::TempDir;
|
||||
|
||||
static TEMP_DIR: LazyLock<TempDir> = LazyLock::new(|| TempDir::new().unwrap());
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let args = init_cli()?;
|
||||
|
||||
let corpora = collect_corpora(&args)?;
|
||||
|
||||
if let Some(platform) = &args.compile_only {
|
||||
for tests in corpora.values() {
|
||||
main_compile_only(&args, tests, platform)?;
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
for tests in corpora.values() {
|
||||
main_execute_differential(&args, tests)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn init_cli() -> anyhow::Result<Arguments> {
|
||||
env_logger::init();
|
||||
|
||||
let mut args = Arguments::parse();
|
||||
if args.corpus.is_empty() {
|
||||
anyhow::bail!("no test corpus specified");
|
||||
}
|
||||
if args.working_directory.is_none() {
|
||||
args.temp_dir = Some(&TEMP_DIR);
|
||||
}
|
||||
|
||||
ThreadPoolBuilder::new()
|
||||
.num_threads(args.workers)
|
||||
.build_global()
|
||||
.unwrap();
|
||||
|
||||
Ok(args)
|
||||
}
|
||||
|
||||
fn collect_corpora(args: &Arguments) -> anyhow::Result<HashMap<Corpus, Vec<Metadata>>> {
|
||||
let mut corpora = HashMap::new();
|
||||
|
||||
for path in &args.corpus {
|
||||
let corpus = Corpus::try_from_path(path)?;
|
||||
log::info!("found corpus: {}", path.display());
|
||||
let tests = corpus.enumerate_tests();
|
||||
log::info!("corpus '{}' contains {} tests", &corpus.name, tests.len());
|
||||
corpora.insert(corpus, tests);
|
||||
}
|
||||
|
||||
Ok(corpora)
|
||||
}
|
||||
|
||||
fn main_execute_differential(args: &Arguments, tests: &[Metadata]) -> anyhow::Result<()> {
|
||||
let leader_nodes = NodePool::new(args)?;
|
||||
let follower_nodes = NodePool::new(args)?;
|
||||
|
||||
tests.par_iter().for_each(|metadata| {
|
||||
let mut driver = match (&args.leader, &args.follower) {
|
||||
(TestingPlatform::Geth, TestingPlatform::Kitchensink) => Driver::<Geth, Geth>::new(
|
||||
metadata,
|
||||
args,
|
||||
leader_nodes.round_robbin(),
|
||||
follower_nodes.round_robbin(),
|
||||
),
|
||||
_ => unimplemented!(),
|
||||
};
|
||||
|
||||
match driver.execute() {
|
||||
Ok(build) => {
|
||||
log::info!(
|
||||
"metadata {} success",
|
||||
metadata.directory().as_ref().unwrap().display()
|
||||
);
|
||||
build
|
||||
}
|
||||
Err(error) => {
|
||||
log::warn!(
|
||||
"metadata {} failure: {error:?}",
|
||||
metadata.file_path.as_ref().unwrap().display()
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main_compile_only(
|
||||
config: &Arguments,
|
||||
tests: &[Metadata],
|
||||
platform: &TestingPlatform,
|
||||
) -> 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!(),
|
||||
};
|
||||
let _ = state.build_contracts(mode, metadata);
|
||||
}
|
||||
});
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user