revme/cmd/bench/
burntpix.rs1pub 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| {
41 c.disable_nonce_check = true;
42 c.tx_gas_limit_cap = Some(u64::MAX);
43 })
44 .build_mainnet();
45
46 let tx = TxEnv::builder()
47 .caller(BENCH_CALLER)
48 .kind(TxKind::Call(BURNTPIX_MAIN_ADDRESS))
49 .data(run_call_data.into())
50 .gas_limit(u64::MAX)
51 .build()
52 .unwrap();
53
54 criterion.bench_function("burntpix", |b| {
55 b.iter_batched(
56 || tx.clone(),
57 |input| evm.transact_one(input).unwrap(),
58 criterion::BatchSize::SmallInput,
59 );
60 });
61
62 }
91
92pub fn svg(filename: String, svg_data: &[u8]) -> Result<(), Box<dyn Error>> {
94 let current_dir = std::env::current_dir()?;
95 let svg_dir = current_dir.join("burntpix").join("svgs");
96 std::fs::create_dir_all(&svg_dir)?;
97
98 let file_path = svg_dir.join(format!("{filename}.svg"));
99 let mut file = File::create(file_path)?;
100 file.write_all(svg_data)?;
101
102 Ok(())
103}
104
105const DEFAULT_SEED: &str = "0";
106const DEFAULT_ITERATIONS: &str = "0x4E20"; fn try_init_env_vars() -> Result<(u32, U256), Box<dyn Error>> {
108 let seed_from_env = std::env::var("SEED").unwrap_or_else(|_| DEFAULT_SEED.to_string());
110 let seed: u32 = try_from_hex_to_u32(&seed_from_env)?;
111 let iterations_from_env =
113 std::env::var("ITERATIONS").unwrap_or_else(|_| DEFAULT_ITERATIONS.to_string());
114 let iterations = U256::from_str(&iterations_from_env)?;
115 Ok((seed, iterations))
116}
117
118fn try_from_hex_to_u32(hex: &str) -> Result<u32, Box<dyn Error>> {
119 let trimmed = hex.strip_prefix("0x").unwrap_or(hex);
120 u32::from_str_radix(trimmed, 16).map_err(|e| format!("Failed to parse hex: {e}").into())
121}
122
123fn insert_account_info(cache_db: &mut CacheDB<EmptyDB>, addr: Address, code: &str) {
124 let code = Bytes::from(hex::decode(code).unwrap());
125 let code_hash = hex::encode(keccak256(&code));
126 let bytecode = Bytecode::new_raw(code);
127 let _ = bytecode.legacy_jump_table();
128 let account_info = AccountInfo::new(
129 U256::from(0),
130 0,
131 B256::from_str(&code_hash).unwrap(),
132 bytecode,
133 );
134 cache_db.insert_account_info(addr, account_info);
135}
136
137fn init_db() -> CacheDB<EmptyDB> {
138 let mut cache_db = CacheDB::new(EmptyDB::default());
139
140 insert_account_info(&mut cache_db, BURNTPIX_ADDRESS_ONE, BURNTPIX_BYTECODE_ONE);
141 insert_account_info(&mut cache_db, BURNTPIX_MAIN_ADDRESS, BURNTPIX_BYTECODE_TWO);
142 insert_account_info(&mut cache_db, BURNTPIX_ADDRESS_TWO, BURNTPIX_BYTECODE_THREE);
143 insert_account_info(
144 &mut cache_db,
145 BURNTPIX_ADDRESS_THREE,
146 BURNTPIX_BYTECODE_FOUR,
147 );
148
149 cache_db
150 .insert_account_storage(
151 BURNTPIX_MAIN_ADDRESS,
152 StorageKey::from(0),
153 StorageValue::from_be_bytes(*STORAGE_ZERO),
154 )
155 .unwrap();
156
157 cache_db
158 .insert_account_storage(
159 BURNTPIX_MAIN_ADDRESS,
160 StorageKey::from(1),
161 StorageValue::from_be_bytes(*STORAGE_ONE),
162 )
163 .unwrap();
164
165 cache_db
166 .insert_account_storage(
167 BURNTPIX_MAIN_ADDRESS,
168 StorageKey::from(2),
169 StorageValue::from_be_bytes(*STORAGE_TWO),
170 )
171 .unwrap();
172
173 cache_db
174}