revme/cmd/bench/
burntpix.rs1pub mod static_data;
2
3use context::TxEnv;
4use criterion::Criterion;
5use primitives::{StorageKey, StorageValue};
6use static_data::{
7 BURNTPIX_ADDRESS_ONE, BURNTPIX_ADDRESS_THREE, BURNTPIX_ADDRESS_TWO, BURNTPIX_BYTECODE_FOUR,
8 BURNTPIX_BYTECODE_ONE, BURNTPIX_BYTECODE_THREE, BURNTPIX_BYTECODE_TWO, BURNTPIX_MAIN_ADDRESS,
9 STORAGE_ONE, STORAGE_TWO, STORAGE_ZERO,
10};
11
12use alloy_sol_types::{sol, SolCall};
13use database::{CacheDB, BENCH_CALLER};
14use revm::{
15 database_interface::EmptyDB,
16 primitives::{hex, keccak256, Address, Bytes, TxKind, B256, U256},
17 state::{AccountInfo, Bytecode},
18 Context, ExecuteEvm, MainBuilder, MainContext,
19};
20
21use std::{error::Error, fs::File, io::Write};
22
23use std::str::FromStr;
24
25sol! {
26 #[derive(Debug, PartialEq, Eq)]
27 interface IBURNTPIX {
28 function run( uint32 seed, uint256 iterations) returns (string);
29 }
30}
31
32pub fn run(criterion: &mut Criterion) {
33 let (seed, iterations) = try_init_env_vars().expect("Failed to parse env vars");
34
35 let run_call_data = IBURNTPIX::runCall { seed, iterations }.abi_encode();
36
37 let db = init_db();
38
39 let mut evm = Context::mainnet()
40 .with_db(db)
41 .modify_cfg_chained(|c| c.disable_nonce_check = true)
42 .build_mainnet();
43
44 let tx = TxEnv::builder()
45 .caller(BENCH_CALLER)
46 .kind(TxKind::Call(BURNTPIX_MAIN_ADDRESS))
47 .data(run_call_data.clone().into())
48 .gas_limit(u64::MAX)
49 .build()
50 .unwrap();
51
52 criterion.bench_function("burntpix", |b| {
53 b.iter_batched(
54 || {
55 tx.clone()
57 },
58 |input| {
59 evm.transact_one(input).unwrap();
60 },
61 criterion::BatchSize::SmallInput,
62 );
63 });
64
65 }
94
95pub fn svg(filename: String, svg_data: &[u8]) -> Result<(), Box<dyn Error>> {
97 let current_dir = std::env::current_dir()?;
98 let svg_dir = current_dir.join("burntpix").join("svgs");
99 std::fs::create_dir_all(&svg_dir)?;
100
101 let file_path = svg_dir.join(format!("{filename}.svg"));
102 let mut file = File::create(file_path)?;
103 file.write_all(svg_data)?;
104
105 Ok(())
106}
107
108const DEFAULT_SEED: &str = "0";
109const DEFAULT_ITERATIONS: &str = "0x4E20"; fn try_init_env_vars() -> Result<(u32, U256), Box<dyn Error>> {
111 let seed_from_env = std::env::var("SEED").unwrap_or(DEFAULT_SEED.to_string());
112 let seed: u32 = try_from_hex_to_u32(&seed_from_env)?;
113 let iterations_from_env = std::env::var("ITERATIONS").unwrap_or(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 StorageValue::from(2),
167 StorageKey::from_be_bytes(*STORAGE_TWO),
168 )
169 .unwrap();
170
171 cache_db
172}