Skip to main content

revme/cmd/bench/
transfer.rs

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