pub mod static_data;
use static_data::{
BURNTPIX_ADDRESS_ONE, BURNTPIX_ADDRESS_THREE, BURNTPIX_ADDRESS_TWO, BURNTPIX_BYTECODE_FOUR,
BURNTPIX_BYTECODE_ONE, BURNTPIX_BYTECODE_THREE, BURNTPIX_BYTECODE_TWO, BURNTPIX_MAIN_ADDRESS,
STORAGE_ONE, STORAGE_TWO, STORAGE_ZERO,
};
use alloy_sol_macro::sol;
use alloy_sol_types::SolCall;
use database::CacheDB;
use revm::{
context_interface::result::{ExecutionResult, Output},
database_interface::EmptyDB,
handler::EthHandler,
primitives::{address, hex, keccak256, Address, Bytes, TxKind, B256, U256},
state::{AccountInfo, Bytecode},
Context, MainEvm,
};
use std::fs::File;
use std::{error::Error, time::Instant};
use std::{io::Write, str::FromStr};
sol! {
#[derive(Debug, PartialEq, Eq)]
interface IBURNTPIX {
function run( uint32 seed, uint256 iterations) returns (string);
}
}
pub fn run() {
let (seed, iterations) = try_init_env_vars().expect("Failed to parse env vars");
let run_call_data = IBURNTPIX::runCall { seed, iterations }.abi_encode();
let db = init_db();
let context = Context::builder().with_db(db).modify_tx_chained(|tx| {
tx.caller = address!("1000000000000000000000000000000000000000");
tx.transact_to = TxKind::Call(BURNTPIX_MAIN_ADDRESS);
tx.data = run_call_data.clone().into();
});
let mut evm = MainEvm::new(context, EthHandler::default());
let started = Instant::now();
let tx_result = evm.transact().unwrap().result;
let return_data = match tx_result {
ExecutionResult::Success {
output, gas_used, ..
} => {
println!("Gas used: {:?}", gas_used);
println!("Time elapsed: {:?}", started.elapsed());
match output {
Output::Call(value) => value,
_ => unreachable!("Unexpected output type"),
}
}
_ => unreachable!("Execution failed: {:?}", tx_result),
};
let returndata_offset = 64;
let data = &return_data[returndata_offset..];
let trimmed_data = data
.split_at(data.len() - data.iter().rev().filter(|&x| *x == 0).count())
.0;
let file_name = format!("{}_{}", seed, iterations);
svg(file_name, trimmed_data).expect("Failed to store svg");
}
fn svg(filename: String, svg_data: &[u8]) -> Result<(), Box<dyn Error>> {
let current_dir = std::env::current_dir()?;
let svg_dir = current_dir.join("burntpix").join("svgs");
std::fs::create_dir_all(&svg_dir)?;
let file_path = svg_dir.join(format!("{}.svg", filename));
let mut file = File::create(file_path)?;
file.write_all(svg_data)?;
Ok(())
}
const DEFAULT_SEED: &str = "0";
const DEFAULT_ITERATIONS: &str = "0x7A120";
fn try_init_env_vars() -> Result<(u32, U256), Box<dyn Error>> {
let seed_from_env = std::env::var("SEED").unwrap_or(DEFAULT_SEED.to_string());
let seed: u32 = try_from_hex_to_u32(&seed_from_env)?;
let iterations_from_env = std::env::var("ITERATIONS").unwrap_or(DEFAULT_ITERATIONS.to_string());
let iterations = U256::from_str(&iterations_from_env)?;
Ok((seed, iterations))
}
fn try_from_hex_to_u32(hex: &str) -> Result<u32, Box<dyn Error>> {
let trimmed = hex.strip_prefix("0x").unwrap_or(hex);
u32::from_str_radix(trimmed, 16).map_err(|e| format!("Failed to parse hex: {}", e).into())
}
fn insert_account_info(cache_db: &mut CacheDB<EmptyDB>, addr: Address, code: Bytes) {
let code_hash = hex::encode(keccak256(&code));
let account_info = AccountInfo::new(
U256::from(0),
0,
B256::from_str(&code_hash).unwrap(),
Bytecode::new_raw(code),
);
cache_db.insert_account_info(addr, account_info);
}
fn init_db() -> CacheDB<EmptyDB> {
let mut cache_db = CacheDB::new(EmptyDB::default());
insert_account_info(
&mut cache_db,
BURNTPIX_ADDRESS_ONE,
BURNTPIX_BYTECODE_ONE.clone(),
);
insert_account_info(
&mut cache_db,
BURNTPIX_MAIN_ADDRESS,
BURNTPIX_BYTECODE_TWO.clone(),
);
insert_account_info(
&mut cache_db,
BURNTPIX_ADDRESS_TWO,
BURNTPIX_BYTECODE_THREE.clone(),
);
insert_account_info(
&mut cache_db,
BURNTPIX_ADDRESS_THREE,
BURNTPIX_BYTECODE_FOUR.clone(),
);
cache_db
.insert_account_storage(
BURNTPIX_MAIN_ADDRESS,
U256::from(0),
U256::from_be_bytes(*STORAGE_ZERO),
)
.unwrap();
cache_db
.insert_account_storage(
BURNTPIX_MAIN_ADDRESS,
U256::from(1),
U256::from_be_bytes(*STORAGE_ONE),
)
.unwrap();
cache_db
.insert_account_storage(
BURNTPIX_MAIN_ADDRESS,
U256::from(2),
U256::from_be_bytes(*STORAGE_TWO),
)
.unwrap();
cache_db
}