Skip to content

Commit

Permalink
move evm runner script to revme subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
charles-cooper committed Feb 29, 2024
1 parent a6d8657 commit 566c32f
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 48 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 0 additions & 48 deletions bins/revm-test/src/bin/evm.rs

This file was deleted.

2 changes: 2 additions & 0 deletions bins/revme/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ version = "0.2.2"

[dependencies]
hash-db = "0.15"
hex = "0.4"
hashbrown = "0.14"
indicatif = "0.17"
microbench = "0.5"
plain_hasher = "0.2"
revm = { path = "../../crates/revm", version = "6.1.0", default-features = false, features = [
"ethersdb",
Expand Down
6 changes: 6 additions & 0 deletions bins/revme/src/cmd.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod format_kzg_setup;
pub mod statetest;
pub mod evmrunner;

use structopt::{clap::AppSettings, StructOpt};

Expand All @@ -13,6 +14,8 @@ pub enum MainCmd {
about = "Format kzg settings from a trusted setup file (.txt) into binary format (.bin)"
)]
FormatKzgSetup(format_kzg_setup::Cmd),
#[structopt(about = "Run evm script directly")]
Evm(evmrunner::Cmd),
}

#[derive(Debug, thiserror::Error)]
Expand All @@ -21,13 +24,16 @@ pub enum Error {
Statetest(#[from] statetest::Error),
#[error(transparent)]
KzgErrors(#[from] format_kzg_setup::KzgErrors),
#[error(transparent)]
EvmRunnerErrors(#[from] evmrunner::Errors),
}

impl MainCmd {
pub fn run(&self) -> Result<(), Error> {
match self {
Self::Statetest(cmd) => cmd.run().map_err(Into::into),
Self::FormatKzgSetup(cmd) => cmd.run().map_err(Into::into),
Self::Evm(cmd) => cmd.run().map_err(Into::into),
}
}
}
89 changes: 89 additions & 0 deletions bins/revme/src/cmd/evmrunner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use revm::{
db::BenchmarkDB,
primitives::{Bytecode, TransactTo},
Evm,
};
use std::time::Duration;
use std::fs;
use std::path::PathBuf;
use core::fmt::Display;
use structopt::StructOpt;

extern crate alloc;

#[derive(Debug)]
pub enum Errors {
PathNotExists,
InvalidFile,
EVMError,
}

impl std::error::Error for Errors {}

impl Display for Errors {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Errors::PathNotExists => write!(f, "The specified path does not exist"),
Errors::InvalidFile => write!(f, "Invalid EVM script"),
Errors::EVMError => write!(f, "VM error"),
}
}
}

/// EvmRunner command
#[derive(StructOpt, Debug)]
pub struct Cmd {
/// Path to file containing the evm script.
#[structopt(required = true)]
path: PathBuf,
/// Run in benchmarking mode
#[structopt(long)]
bench: bool,
}

impl Cmd {
/// Run statetest command.
pub fn run(&self) -> Result<(), Errors> {
// check if path exists.
if !self.path.exists() {
return Err(Errors::PathNotExists);
}


let contents = fs::read_to_string(&self.path).map_err(|_| Errors::InvalidFile)?;
let contents_str = contents.to_string();
let bytecode = hex::decode(contents_str.trim()).map_err(|_| Errors::InvalidFile)?;

let zero_address = "0x0000000000000000000000000000000000000000";

// BenchmarkDB is dummy state that implements Database trait.
// the bytecode is deployed at zero address.
let mut evm = Evm::builder()
.with_db(BenchmarkDB::new_bytecode(Bytecode::new_raw(bytecode.into())))
.modify_tx_env(|tx| {
// execution globals block hash/gas_limit/coinbase/timestamp..
tx.caller = "0x0000000000000000000000000000000000000001"
.parse()
.unwrap();
tx.transact_to = TransactTo::Call(
zero_address
.parse()
.unwrap(),
);
})
.build();

if self.bench {
// Microbenchmark
let bench_options = microbench::Options::default().time(Duration::from_secs(3));

microbench::bench(&bench_options, "Run bytecode", || {
let _ = evm.transact().unwrap();
});
} else {
evm.transact().map_err(|_| Errors::EVMError)?;
// TODO: print the result
}
Ok(())
}
}

0 comments on commit 566c32f

Please sign in to comment.