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

fect(revme): EmptyDb Blockhash string, json-outcome flag, set prevrandao in statetest #994

Merged
merged 3 commits into from
Jan 19, 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
11 changes: 6 additions & 5 deletions bins/revme/src/cmd/statetest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,13 @@ pub struct Cmd {
#[structopt(short = "s", long)]
single_thread: bool,
/// Output results in JSON format.
/// It will stop second run of evm on failure.
#[structopt(long)]
json: bool,
/// Output outcome in JSON format. If json is true, this is implied.
/// It will stop second run of evm on failure.
#[structopt(short = "o", long)]
json_outcome: bool,
}

impl Cmd {
Expand All @@ -32,11 +37,7 @@ impl Cmd {
for path in &self.path {
println!("\nRunning tests in {}...", path.display());
let test_files = find_all_json_tests(path);
run(
test_files,
if self.json { true } else { self.single_thread },
self.json,
)?
run(test_files, self.single_thread, self.json, self.json_outcome)?
}
Ok(())
}
Expand Down
25 changes: 0 additions & 25 deletions bins/revme/src/cmd/statetest/cmd.rs

This file was deleted.

36 changes: 27 additions & 9 deletions bins/revme/src/cmd/statetest/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ fn check_evm_execution<EXT>(
test_name: &str,
exec_result: &EVMResultGeneric<ExecutionResult, Infallible>,
evm: &Evm<'_, EXT, &mut State<EmptyDB>>,
is_json_trace: bool,
print_json_outcome: bool,
) -> Result<(), TestError> {
let logs_root = log_rlp_hash(&exec_result.as_ref().map(|r| r.logs()).unwrap_or_default());
let state_root = state_merkle_trie_root(evm.context.evm.db.cache.trie_account());

let print_json_output = |error: Option<String>| {
if is_json_trace {
if print_json_outcome {
let json = json!({
"stateRoot": state_root,
"logsRoot": logs_root,
Expand Down Expand Up @@ -215,6 +215,7 @@ pub fn execute_test_suite(
path: &Path,
elapsed: &Arc<Mutex<Duration>>,
trace: bool,
print_json_outcome: bool,
) -> Result<(), TestError> {
if skip_test(path) {
return Ok(());
Expand Down Expand Up @@ -252,7 +253,7 @@ pub fn execute_test_suite(
env.block.basefee = unit.env.current_base_fee.unwrap_or_default();
env.block.difficulty = unit.env.current_difficulty;
// after the Merge prevrandao replaces mix_hash field in block and replaced difficulty opcode in EVM.
env.block.prevrandao = Some(unit.env.current_difficulty.to_be_bytes().into());
env.block.prevrandao = unit.env.current_random;
// EIP-4844
if let (Some(parent_blob_gas_used), Some(parent_excess_blob_gas)) = (
unit.env.parent_blob_gas_used,
Expand Down Expand Up @@ -361,9 +362,14 @@ pub fn execute_test_suite(
.build();
let res = evm.transact_commit();

let Err(e) =
check_evm_execution(&test, unit.out.as_ref(), &name, &res, &evm, trace)
else {
let Err(e) = check_evm_execution(
&test,
unit.out.as_ref(),
&name,
&res,
&evm,
print_json_outcome,
) else {
continue;
};
// reset external context
Expand All @@ -372,8 +378,14 @@ pub fn execute_test_suite(
let res = evm.transact_commit();

// dump state and traces if test failed
let output =
check_evm_execution(&test, unit.out.as_ref(), &name, &res, &evm, trace);
let output = check_evm_execution(
&test,
unit.out.as_ref(),
&name,
&res,
&evm,
print_json_outcome,
);
let Err(e) = output else {
continue;
};
Expand Down Expand Up @@ -428,8 +440,14 @@ pub fn run(
test_files: Vec<PathBuf>,
mut single_thread: bool,
trace: bool,
mut print_outcome: bool,
) -> Result<(), TestError> {
// trace implies print_outcome
if trace {
print_outcome = true;
}
// print_outcome or trace implies single_thread
if print_outcome {
single_thread = true;
}
let n_files = test_files.len();
Expand Down Expand Up @@ -471,7 +489,7 @@ pub fn run(
(prev_idx, test_path)
};

if let Err(err) = execute_test_suite(&test_path, &elapsed, trace) {
if let Err(err) = execute_test_suite(&test_path, &elapsed, trace, print_outcome) {
endjob.store(true, Ordering::SeqCst);
return Err(err);
}
Expand Down
34 changes: 33 additions & 1 deletion crates/revm/src/db/emptydb.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use alloc::string::ToString;
use core::{convert::Infallible, fmt, marker::PhantomData};
use revm_interpreter::primitives::{
db::{Database, DatabaseRef},
Expand Down Expand Up @@ -102,6 +103,37 @@ impl<E> DatabaseRef for EmptyDBTyped<E> {

#[inline]
fn block_hash_ref(&self, number: U256) -> Result<B256, Self::Error> {
Ok(keccak256(number.to_be_bytes::<{ U256::BYTES }>()))
Ok(keccak256(number.to_string().as_bytes()))
}
}

#[cfg(test)]
mod tests {
use super::*;
use crate::primitives::b256;

#[test]
fn conform_block_hash_calculation() {
let db = EmptyDB::new();
assert_eq!(
db.block_hash_ref(U256::from(0)),
Ok(b256!(
"044852b2a670ade5407e78fb2863c51de9fcb96542a07186fe3aeda6bb8a116d"
))
);

assert_eq!(
db.block_hash_ref(U256::from(1)),
Ok(b256!(
"c89efdaa54c0f20c7adf612882df0950f5a951637e0307cdcb4c672f298b8bc6"
))
);

assert_eq!(
db.block_hash_ref(U256::from(100)),
Ok(b256!(
"8c18210df0d9514f2d2e5d8ca7c100978219ee80d3968ad850ab5ead208287b3"
))
);
}
}
6 changes: 3 additions & 3 deletions crates/revm/src/db/states/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,9 @@ mod tests {

let test_number = BLOCK_HASH_HISTORY as u64 + 2;

let block1_hash = keccak256(U256::from(1).to_be_bytes::<{ U256::BYTES }>());
let block2_hash = keccak256(U256::from(2).to_be_bytes::<{ U256::BYTES }>());
let block_test_hash = keccak256(U256::from(test_number).to_be_bytes::<{ U256::BYTES }>());
let block1_hash = keccak256(U256::from(1).to_string().as_bytes());
let block2_hash = keccak256(U256::from(2).to_string().as_bytes());
let block_test_hash = keccak256(U256::from(test_number).to_string().as_bytes());

assert_eq!(
state.block_hashes,
Expand Down
Loading