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| 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.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 }
88
89pub 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"; fn try_init_env_vars() -> Result<(u32, U256), Box<dyn Error>> {
105 let seed_from_env = std::env::var("SEED").unwrap_or_else(|_| DEFAULT_SEED.to_string());
107 let seed: u32 = try_from_hex_to_u32(&seed_from_env)?;
108 let iterations_from_env =
110 std::env::var("ITERATIONS").unwrap_or_else(|_| DEFAULT_ITERATIONS.to_string());
111 let iterations = U256::from_str(&iterations_from_env)?;
112 Ok((seed, iterations))
113}
114
115fn try_from_hex_to_u32(hex: &str) -> Result<u32, Box<dyn Error>> {
116 let trimmed = hex.strip_prefix("0x").unwrap_or(hex);
117 u32::from_str_radix(trimmed, 16).map_err(|e| format!("Failed to parse hex: {e}").into())
118}
119
120fn insert_account_info(cache_db: &mut CacheDB<EmptyDB>, addr: Address, code: &str) {
121 let code = Bytes::from(hex::decode(code).unwrap());
122 let code_hash = hex::encode(keccak256(&code));
123 let account_info = AccountInfo::new(
124 U256::from(0),
125 0,
126 B256::from_str(&code_hash).unwrap(),
127 Bytecode::new_raw(code),
128 );
129 cache_db.insert_account_info(addr, account_info);
130}
131
132fn init_db() -> CacheDB<EmptyDB> {
133 let mut cache_db = CacheDB::new(EmptyDB::default());
134
135 insert_account_info(&mut cache_db, BURNTPIX_ADDRESS_ONE, BURNTPIX_BYTECODE_ONE);
136 insert_account_info(&mut cache_db, BURNTPIX_MAIN_ADDRESS, BURNTPIX_BYTECODE_TWO);
137 insert_account_info(&mut cache_db, BURNTPIX_ADDRESS_TWO, BURNTPIX_BYTECODE_THREE);
138 insert_account_info(
139 &mut cache_db,
140 BURNTPIX_ADDRESS_THREE,
141 BURNTPIX_BYTECODE_FOUR,
142 );
143
144 cache_db
145 .insert_account_storage(
146 BURNTPIX_MAIN_ADDRESS,
147 StorageKey::from(0),
148 StorageValue::from_be_bytes(*STORAGE_ZERO),
149 )
150 .unwrap();
151
152 cache_db
153 .insert_account_storage(
154 BURNTPIX_MAIN_ADDRESS,
155 StorageKey::from(1),
156 StorageValue::from_be_bytes(*STORAGE_ONE),
157 )
158 .unwrap();
159
160 cache_db
161 .insert_account_storage(
162 BURNTPIX_MAIN_ADDRESS,
163 StorageKey::from(2),
164 StorageValue::from_be_bytes(*STORAGE_TWO),
165 )
166 .unwrap();
167
168 cache_db
169}