revme/cmd/bench/
transfer.rs

1use context::{ContextTr, TxEnv};
2use criterion::Criterion;
3use database::{BenchmarkDB, BENCH_CALLER, BENCH_TARGET, BENCH_TARGET_BALANCE};
4use revm::{
5    bytecode::Bytecode,
6    context_interface::JournalTr,
7    primitives::{TxKind, U256},
8    Context, ExecuteEvm, MainBuilder, MainContext,
9};
10
11pub fn run(criterion: &mut Criterion) {
12    let mut evm = Context::mainnet()
13        .with_db(BenchmarkDB::new_bytecode(Bytecode::new()))
14        .modify_cfg_chained(|cfg| cfg.disable_nonce_check = true)
15        .build_mainnet();
16
17    let tx = TxEnv {
18        caller: BENCH_CALLER,
19        kind: TxKind::Call(BENCH_TARGET),
20        value: U256::from(1),
21        gas_price: 1,
22        gas_priority_fee: None,
23        ..Default::default()
24    };
25
26    evm.ctx.tx = tx.clone();
27
28    let mut i = 0;
29    criterion.bench_function("transfer", |b| {
30        b.iter(|| {
31            i += 1;
32            let _ = evm.transact(tx.clone()).unwrap();
33        })
34    });
35
36    let balance = evm
37        .journal_mut()
38        .load_account(BENCH_TARGET)
39        .unwrap()
40        .data
41        .info
42        .balance;
43
44    if balance != BENCH_TARGET_BALANCE + U256::from(i) {
45        panic!("balance of transfers is not correct");
46    }
47
48    // drop the journal
49    let _ = evm.finalize();
50
51    evm.modify_cfg(|cfg| cfg.disable_nonce_check = false);
52
53    criterion.bench_function("transfer_finalize", |b| {
54        b.iter(|| {
55            let _ = evm.replay().unwrap();
56        })
57    });
58}