revme/cmd/bench/
burntpix.rs

1pub mod static_data;
2
3use criterion::Criterion;
4use static_data::{
5    BURNTPIX_ADDRESS_ONE, BURNTPIX_ADDRESS_THREE, BURNTPIX_ADDRESS_TWO, BURNTPIX_BYTECODE_FOUR,
6    BURNTPIX_BYTECODE_ONE, BURNTPIX_BYTECODE_THREE, BURNTPIX_BYTECODE_TWO, BURNTPIX_MAIN_ADDRESS,
7    STORAGE_ONE, STORAGE_TWO, STORAGE_ZERO,
8};
9
10use alloy_sol_types::{sol, SolCall};
11use revm::{
12    context::TxEnv,
13    database::{CacheDB, BENCH_CALLER},
14    database_interface::EmptyDB,
15    primitives::{hex, keccak256, Address, Bytes, StorageKey, StorageValue, TxKind, B256, U256},
16    state::{AccountInfo, Bytecode},
17    Context, ExecuteEvm, MainBuilder, MainContext,
18};
19
20use std::{error::Error, fs::File, io::Write};
21
22use std::str::FromStr;
23
24sol! {
25    #[derive(Debug, PartialEq, Eq)]
26    interface IBURNTPIX {
27        function run( uint32 seed, uint256 iterations) returns (string);
28    }
29}
30
31pub fn run(criterion: &mut Criterion) {
32    let (seed, iterations) = try_init_env_vars().expect("Failed to parse env vars");
33
34    let run_call_data = IBURNTPIX::runCall { seed, iterations }.abi_encode();
35
36    let db = init_db();
37
38    let mut evm = Context::mainnet()
39        .with_db(db)
40        .modify_cfg_chained(|c| c.disable_nonce_check = true)
41        .build_mainnet();
42
43    let tx = TxEnv::builder()
44        .caller(BENCH_CALLER)
45        .kind(TxKind::Call(BURNTPIX_MAIN_ADDRESS))
46        .data(run_call_data.clone().into())
47        .gas_limit(u64::MAX)
48        .build()
49        .unwrap();
50
51    criterion.bench_function("burntpix", |b| {
52        b.iter_batched(
53            || tx.clone(),
54            |input| evm.transact_one(input).unwrap(),
55            criterion::BatchSize::SmallInput,
56        );
57    });
58
59    //Collects the data and uses it to generate the svg after running the benchmark
60    /*
61    let tx_result = evm.replay().unwrap();
62    let return_data = match tx_result.result {
63        context::result::ExecutionResult::Success {
64            output, gas_used, ..
65        } => {
66            println!("Gas used: {:?}", gas_used);
67            match output {
68                context::result::Output::Call(value) => value,
69                _ => unreachable!("Unexpected output type"),
70            }
71        }
72        _ => unreachable!("Execution failed: {:?}", tx_result),
73    };
74
75    // Remove returndata offset and length from output
76    let returndata_offset = 64;
77    let data = &return_data[returndata_offset..];
78
79    // Remove trailing zeros
80    let trimmed_data = data
81        .split_at(data.len() - data.iter().rev().filter(|&x| *x == 0).count())
82        .0;
83    let file_name = format!("{}_{}", seed, iterations);
84
85    svg(file_name, trimmed_data).expect("Failed to store svg");
86    */
87}
88
89/// Actually generates the svg
90pub fn svg(filename: String, svg_data: &[u8]) -> Result<(), Box<dyn Error>> {
91    let current_dir = std::env::current_dir()?;
92    let svg_dir = current_dir.join("burntpix").join("svgs");
93    std::fs::create_dir_all(&svg_dir)?;
94
95    let file_path = svg_dir.join(format!("{filename}.svg"));
96    let mut file = File::create(file_path)?;
97    file.write_all(svg_data)?;
98
99    Ok(())
100}
101
102const DEFAULT_SEED: &str = "0";
103const DEFAULT_ITERATIONS: &str = "0x4E20"; // 20_000 iterations
104fn try_init_env_vars() -> Result<(u32, U256), Box<dyn Error>> {
105    let seed_from_env = std::env::var("SEED").unwrap_or(DEFAULT_SEED.to_string());
106    let seed: u32 = try_from_hex_to_u32(&seed_from_env)?;
107    let iterations_from_env = std::env::var("ITERATIONS").unwrap_or(DEFAULT_ITERATIONS.to_string());
108    let iterations = U256::from_str(&iterations_from_env)?;
109    Ok((seed, iterations))
110}
111
112fn try_from_hex_to_u32(hex: &str) -> Result<u32, Box<dyn Error>> {
113    let trimmed = hex.strip_prefix("0x").unwrap_or(hex);
114    u32::from_str_radix(trimmed, 16).map_err(|e| format!("Failed to parse hex: {e}").into())
115}
116
117fn insert_account_info(cache_db: &mut CacheDB<EmptyDB>, addr: Address, code: &str) {
118    let code = Bytes::from(hex::decode(code).unwrap());
119    let code_hash = hex::encode(keccak256(&code));
120    let account_info = AccountInfo::new(
121        U256::from(0),
122        0,
123        B256::from_str(&code_hash).unwrap(),
124        Bytecode::new_raw(code),
125    );
126    cache_db.insert_account_info(addr, account_info);
127}
128
129fn init_db() -> CacheDB<EmptyDB> {
130    let mut cache_db = CacheDB::new(EmptyDB::default());
131
132    insert_account_info(&mut cache_db, BURNTPIX_ADDRESS_ONE, BURNTPIX_BYTECODE_ONE);
133    insert_account_info(&mut cache_db, BURNTPIX_MAIN_ADDRESS, BURNTPIX_BYTECODE_TWO);
134    insert_account_info(&mut cache_db, BURNTPIX_ADDRESS_TWO, BURNTPIX_BYTECODE_THREE);
135    insert_account_info(
136        &mut cache_db,
137        BURNTPIX_ADDRESS_THREE,
138        BURNTPIX_BYTECODE_FOUR,
139    );
140
141    cache_db
142        .insert_account_storage(
143            BURNTPIX_MAIN_ADDRESS,
144            StorageKey::from(0),
145            StorageValue::from_be_bytes(*STORAGE_ZERO),
146        )
147        .unwrap();
148
149    cache_db
150        .insert_account_storage(
151            BURNTPIX_MAIN_ADDRESS,
152            StorageKey::from(1),
153            StorageValue::from_be_bytes(*STORAGE_ONE),
154        )
155        .unwrap();
156
157    cache_db
158        .insert_account_storage(
159            BURNTPIX_MAIN_ADDRESS,
160            StorageKey::from(2),
161            StorageValue::from_be_bytes(*STORAGE_TWO),
162        )
163        .unwrap();
164
165    cache_db
166}