1use clap::Parser;
2use revm::{
3 bytecode::{Bytecode, BytecodeDecodeError},
4 context::TxEnv,
5 database::{BenchmarkDB, BENCH_CALLER, BENCH_TARGET},
6 inspector::{inspectors::TracerEip3155, InspectEvm},
7 primitives::{hex, TxKind},
8 Context, Database, ExecuteEvm, MainBuilder, MainContext,
9};
10use std::{borrow::Cow, fs, io::Error as IoError, path::PathBuf, time::Instant};
11
12#[derive(Debug, thiserror::Error)]
13pub enum Errors {
14 #[error("The specified path does not exist")]
15 PathNotExists,
16 #[error("Invalid bytecode")]
17 InvalidBytecode,
18 #[error("Invalid input")]
19 InvalidInput,
20 #[error("EVM Error")]
21 EVMError,
22 #[error(transparent)]
23 Io(#[from] IoError),
24 #[error(transparent)]
25 BytecodeDecodeError(#[from] BytecodeDecodeError),
26}
27
28#[derive(Parser, Debug)]
32pub struct Cmd {
33 #[arg(required_unless_present = "path")]
35 bytecode: Option<String>,
36 #[arg(long)]
40 path: Option<PathBuf>,
41
42 #[arg(long)]
44 bench: bool,
45
46 #[arg(long, default_value = "")]
48 input: String,
49 #[arg(long, default_value = "1000000000")]
51 gas_limit: u64,
52
53 #[arg(long)]
55 state: bool,
56 #[arg(long)]
58 trace: bool,
59}
60
61impl Cmd {
62 pub fn run(&self) -> Result<(), Errors> {
64 let bytecode_str: Cow<'_, str> = if let Some(path) = &self.path {
65 if !path.exists() {
67 return Err(Errors::PathNotExists);
68 }
69 fs::read_to_string(path)?.into()
70 } else if let Some(bytecode) = &self.bytecode {
71 bytecode.as_str().into()
72 } else {
73 unreachable!()
74 };
75
76 let bytecode = hex::decode(bytecode_str.trim().trim_start_matches("0x"))
77 .map_err(|_| Errors::InvalidBytecode)?;
78 let input = hex::decode(self.input.trim().trim_start_matches("0x"))
79 .map_err(|_| Errors::InvalidInput)?
80 .into();
81
82 let mut db = BenchmarkDB::new_bytecode(Bytecode::new_raw_checked(bytecode.into())?);
83
84 let nonce = db
85 .basic(BENCH_CALLER)
86 .unwrap()
87 .map_or(0, |account| account.nonce);
88
89 let mut evm = Context::mainnet()
92 .with_db(db)
93 .build_mainnet_with_inspector(TracerEip3155::new(Box::new(std::io::stdout())));
94
95 let tx = TxEnv::builder()
96 .caller(BENCH_CALLER)
97 .kind(TxKind::Call(BENCH_TARGET))
98 .data(input)
99 .nonce(nonce)
100 .gas_limit(self.gas_limit)
101 .build()
102 .unwrap();
103
104 if self.bench {
105 let mut criterion = criterion::Criterion::default()
106 .warm_up_time(std::time::Duration::from_millis(300))
107 .measurement_time(std::time::Duration::from_secs(2))
108 .without_plots();
109 let mut criterion_group = criterion.benchmark_group("revme");
110 criterion_group.bench_function("evm", |b| {
111 b.iter_batched(
112 || tx.clone(),
113 |input| evm.transact(input).unwrap(),
114 criterion::BatchSize::SmallInput,
115 );
116 });
117 criterion_group.finish();
118
119 return Ok(());
120 }
121
122 let time = Instant::now();
123 let r = if self.trace {
124 evm.inspect_tx(tx)
125 } else {
126 evm.transact(tx)
127 }
128 .map_err(|_| Errors::EVMError)?;
129 let time = time.elapsed();
130
131 println!("Result: {:#?}", r.result);
132 if self.state {
133 println!("State: {:#?}", r.state);
134 }
135
136 println!("Elapsed: {time:?}");
137 Ok(())
138 }
139}