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 account_info = AccountInfo::new(
127 U256::from(0),
128 0,
129 B256::from_str(&code_hash).unwrap(),
130 Bytecode::new_raw(code),
131 );
132 cache_db.insert_account_info(addr, account_info);
133}
134
135fn init_db() -> CacheDB<EmptyDB> {
136 let mut cache_db = CacheDB::new(EmptyDB::default());
137
138 insert_account_info(&mut cache_db, BURNTPIX_ADDRESS_ONE, BURNTPIX_BYTECODE_ONE);
139 insert_account_info(&mut cache_db, BURNTPIX_MAIN_ADDRESS, BURNTPIX_BYTECODE_TWO);
140 insert_account_info(&mut cache_db, BURNTPIX_ADDRESS_TWO, BURNTPIX_BYTECODE_THREE);
141 insert_account_info(
142 &mut cache_db,
143 BURNTPIX_ADDRESS_THREE,
144 BURNTPIX_BYTECODE_FOUR,
145 );
146
147 cache_db
148 .insert_account_storage(
149 BURNTPIX_MAIN_ADDRESS,
150 StorageKey::from(0),
151 StorageValue::from_be_bytes(*STORAGE_ZERO),
152 )
153 .unwrap();
154
155 cache_db
156 .insert_account_storage(
157 BURNTPIX_MAIN_ADDRESS,
158 StorageKey::from(1),
159 StorageValue::from_be_bytes(*STORAGE_ONE),
160 )
161 .unwrap();
162
163 cache_db
164 .insert_account_storage(
165 BURNTPIX_MAIN_ADDRESS,
166 StorageKey::from(2),
167 StorageValue::from_be_bytes(*STORAGE_TWO),
168 )
169 .unwrap();
170
171 cache_db
172}