Skip to content

Commit

Permalink
jsontests: support multiple files (#214)
Browse files Browse the repository at this point in the history
  • Loading branch information
sorpaas authored Nov 15, 2023
1 parent 9d0a2eb commit 47e371a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 22 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,5 @@ jobs:
- name: Run tests
run: |
cargo run --release --verbose -p jsontests -- \
jsontests/res/ethtests/GeneralStateTests/stExample/add11.json
jsontests/res/ethtests/GeneralStateTests/stExample/add11.json \
jsontests/res/ethtests/GeneralStateTests/stSLoadTest/
2 changes: 2 additions & 0 deletions jsontests/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum Error {
EVM(#[from] evm::ExitError),
#[error("unsupported fork")]
UnsupportedFork,
#[error("non-utf8 filename")]
NonUtf8Filename,
#[error("test error")]
Test(#[from] TestError),
}
47 changes: 38 additions & 9 deletions jsontests/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,61 @@ use crate::error::Error;
use crate::types::*;
use clap::Parser;
use std::collections::BTreeMap;
use std::fs::File;
use std::fs::{self, File};
use std::io::BufReader;

#[derive(Parser)]
#[command(author, version, about, long_about = None)]
struct Cli {
filename: String,
filenames: Vec<String>,
}

fn main() -> Result<(), Error> {
let cli = Cli::parse();

fn run_file(filename: &str) -> Result<(), Error> {
let test_multi: BTreeMap<String, TestMulti> =
serde_json::from_reader(BufReader::new(File::open(cli.filename)?))?;
serde_json::from_reader(BufReader::new(File::open(filename)?))?;

for (test_name, test_multi) in test_multi {
let tests = test_multi.tests();

for test in tests {
match crate::run::run_test(&test_name, test) {
Ok(()) => println!("succeed"),
print!(
"{}/{}/{:?}/{}: ",
filename, test_name, test.fork, test.index
);
match crate::run::run_test(filename, &test_name, test) {
Ok(()) => println!("okay"),
Err(Error::UnsupportedFork) => println!("skipped"),
Err(err) => Err(err)?,
Err(err) => {
println!("err {:?}", err);
return Err(err);
}
}
}
}

Ok(())
}

fn run_single(filename: &str) -> Result<(), Error> {
if fs::metadata(&filename)?.is_dir() {
for filename in fs::read_dir(&filename)? {
let filepath = filename?.path();
let filename = filepath.to_str().ok_or(Error::NonUtf8Filename)?;
run_file(filename)?;
}
} else {
run_file(&filename)?;
}

Ok(())
}

fn main() -> Result<(), Error> {
let cli = Cli::parse();

for filename in cli.filenames {
run_single(&filename)?;
}

Ok(())
}
12 changes: 2 additions & 10 deletions jsontests/src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,7 @@ use evm::utils::u256_to_h256;
use primitive_types::U256;
use std::collections::{BTreeMap, BTreeSet};

pub fn run_test(test_name: &str, test: Test) -> Result<(), Error> {
println!(
"test name: {}, fork: {:?}, index: {}",
test_name, test.fork, test.index
);

pub fn run_test(_filename: &str, _test_name: &str, test: Test) -> Result<(), Error> {
let config = match test.fork {
Fork::Berlin => Config::berlin(),
_ => return Err(Error::UnsupportedFork),
Expand Down Expand Up @@ -67,7 +62,7 @@ pub fn run_test(test_name: &str, test: Test) -> Result<(), Error> {

let etable = Etable::runtime();
let invoker = Invoker::new(&config);
let result = invoker.transact_call(
let _result = invoker.transact_call(
test.transaction.sender,
test.transaction.to,
test.transaction.value,
Expand All @@ -81,9 +76,6 @@ pub fn run_test(test_name: &str, test: Test) -> Result<(), Error> {

let state_root = crate::hash::state_root(&backend);

println!("result: {:?}", result);
println!("state root: {:?}", state_root);

if state_root != test.post.hash {
return Err(TestError::StateMismatch.into());
}
Expand Down
14 changes: 12 additions & 2 deletions jsontests/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ impl TestMulti {
transaction: TestTransaction {
data: self.transaction.data[post_state.indexes.data].0.clone(),
gas_limit: self.transaction.gas_limit[post_state.indexes.gas],
gas_price: self.transaction.gas_price,
gas_price: self.transaction.gas_price.unwrap_or(U256::zero()),
nonce: self.transaction.nonce,
secret_key: self.transaction.secret_key,
sender: self.transaction.sender,
Expand Down Expand Up @@ -96,6 +96,14 @@ pub enum Fork {
London,
Merge,
Shanghai,
Byzantium,
Constantinople,
ConstantinopleFix,
EIP150,
EIP158,
Frontier,
Homestead,
Istanbul,
}

#[derive(Clone, Debug, Eq, PartialEq, Deserialize)]
Expand Down Expand Up @@ -126,7 +134,9 @@ pub struct TestPreState {
pub struct TestMultiTransaction {
pub data: Vec<HexBytes>,
pub gas_limit: Vec<U256>,
pub gas_price: U256,
pub gas_price: Option<U256>,
pub max_fee_per_gas: Option<U256>,
pub max_priority_fee_per_gas: Option<U256>,
pub nonce: U256,
pub secret_key: H256,
pub sender: H160,
Expand Down

0 comments on commit 47e371a

Please sign in to comment.