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

feat(eof): cli eof-validation #1622

Merged
merged 3 commits into from
Jul 16, 2024
Merged
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
63 changes: 59 additions & 4 deletions bins/revme/src/cmd/eofvalidation.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
use crate::cmd::Error;
use std::path::PathBuf;
mod test_suite;

pub use test_suite::{PragueTestResult, TestResult, TestSuite, TestUnit, TestVector};

use crate::{cmd::Error, dir_utils::find_all_json_tests};
use revm::{
interpreter::analysis::{validate_raw_eof, EofError},
primitives::Eof,
};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use structopt::StructOpt;

/// Statetest command
/// Eof validation command.
#[derive(StructOpt, Debug)]
pub struct Cmd {
/// Input path to the kzg trusted setup file.
/// Input path to eof validation test
#[structopt(required = true)]
path: PathBuf,
}
Expand All @@ -17,6 +26,52 @@ impl Cmd {
if !self.path.exists() {
return Err(Error::Custom("The specified path does not exist"));
}
run_test(&self.path);
Ok(())
}
}

pub fn run_test(path: &Path) {
let test_files = find_all_json_tests(path);
let mut test_sum = 0;
let mut passed_tests = 0;

#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
enum ErrorType {
FalsePositive,
Error(EofError),
}
let mut types_of_error: BTreeMap<ErrorType, usize> = BTreeMap::new();
for test_file in test_files {
let s = std::fs::read_to_string(test_file).unwrap();
let suite: TestSuite = serde_json::from_str(&s).unwrap();
for (name, test_unit) in suite.0 {
for (vector_name, test_vector) in test_unit.vectors {
test_sum += 1;
let res = validate_raw_eof(test_vector.code.clone());
if res.is_ok() != test_vector.results.prague.result {
let eof = Eof::decode(test_vector.code.clone());
println!(
"\nTest failed: {} - {}\nresult:{:?}\nrevm err_result:{:#?}\nbytes:{:?}\n,eof:{eof:#?}",
name,
vector_name,
test_vector.results.prague,
res.as_ref().err(),
test_vector.code
);
*types_of_error
.entry(
res.err()
.map(ErrorType::Error)
.unwrap_or(ErrorType::FalsePositive),
)
.or_default() += 1;
} else {
passed_tests += 1;
}
}
}
}
println!("Types of error: {:#?}", types_of_error);
println!("Passed tests: {}/{}", passed_tests, test_sum);
}
34 changes: 34 additions & 0 deletions bins/revme/src/cmd/eofvalidation/test_suite.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use revm::primitives::Bytes;
use serde::Deserialize;
use std::collections::BTreeMap;

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct TestSuite(pub BTreeMap<String, TestUnit>);

#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TestUnit {
#[serde(default, rename = "_info")]
pub info: Option<serde_json::Value>,
pub vectors: BTreeMap<String, TestVector>,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TestVector {
pub code: Bytes,
pub results: PragueTestResult,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PragueTestResult {
#[serde(rename = "Prague")]
pub prague: TestResult,
}

#[derive(Debug, PartialEq, Eq, Deserialize)]
pub struct TestResult {
pub result: bool,
pub exception: Option<String>,
}
11 changes: 11 additions & 0 deletions bins/revme/src/dir_utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use std::path::{Path, PathBuf};
use walkdir::{DirEntry, WalkDir};

pub fn find_all_json_tests(path: &Path) -> Vec<PathBuf> {
WalkDir::new(path)
.into_iter()
.filter_map(|e| e.ok())
.filter(|e| e.file_name().to_string_lossy().ends_with(".json"))
.map(DirEntry::into_path)
.collect::<Vec<PathBuf>>()
}
1 change: 1 addition & 0 deletions bins/revme/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod cmd;
pub mod dir_utils;
5 changes: 0 additions & 5 deletions crates/interpreter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ walkdir = "2.5"
serde_json = "1.0"
bincode = "1.3"

[[test]]
name = "eof"
path = "tests/eof.rs"
required-features = ["serde"]

[features]
default = ["std", "parse"]
std = ["serde?/std", "revm-primitives/std"]
Expand Down
Loading
Loading