diff --git a/crates/ef-testing/src/evm_sequencer/evm_state/mod.rs b/crates/ef-testing/src/evm_sequencer/evm_state/mod.rs index f637d19e..e211c0cc 100644 --- a/crates/ef-testing/src/evm_sequencer/evm_state/mod.rs +++ b/crates/ef-testing/src/evm_sequencer/evm_state/mod.rs @@ -17,7 +17,12 @@ use super::account::KakarotAccount; pub trait Evm { // TODO enforce using a marker type that you can only proceed // with execution if the state is initialized. - fn setup_state(&mut self, _base_fee: U256) -> StateResult<()> { + fn setup_state( + &mut self, + _base_fee: U256, + _prev_randao: U256, + _block_gas_limit: U256, + ) -> StateResult<()> { panic!("Not implemented, use features flag \"v0\" or \"v1\"") } diff --git a/crates/ef-testing/src/evm_sequencer/evm_state/v0.rs b/crates/ef-testing/src/evm_sequencer/evm_state/v0.rs index 761f9e77..c5243d58 100644 --- a/crates/ef-testing/src/evm_sequencer/evm_state/v0.rs +++ b/crates/ef-testing/src/evm_sequencer/evm_state/v0.rs @@ -21,7 +21,12 @@ use crate::starknet_storage; impl Evm for KakarotSequencer { /// Sets up the evm state (coinbase, block number, etc.) - fn setup_state(&mut self, base_fee: U256) -> StateResult<()> { + fn setup_state( + &mut self, + base_fee: U256, + prev_randao: U256, + block_gas_limit: U256, + ) -> StateResult<()> { let kakarot_address = self.environment.kakarot_address; let coinbase_address: FeltSequencer = (*self.address()).try_into().unwrap(); // infallible @@ -33,20 +38,40 @@ impl Evm for KakarotSequencer { ); // Set the base fee. - let low_fee = base_fee & U256::from(u128::MAX); - let low_fee: u128 = low_fee.try_into().unwrap(); // safe unwrap <= U128::MAX. - let high_fee = base_fee >> U256::from(128); - let high_fee: u128 = high_fee.try_into().unwrap(); // safe unwrap <= U128::MAX. - - let base_address = get_storage_var_address("base_fee", &[]); + let [low_fee, high_fee] = split_u256(base_fee); + let basefee_address = get_storage_var_address("base_fee", &[]); self.state_mut() - .set_storage_at(kakarot_address, base_address, StarkFelt::from(low_fee)); + .set_storage_at(kakarot_address, basefee_address, StarkFelt::from(low_fee)); self.state_mut().set_storage_at( kakarot_address, - next_storage_key(&base_address)?, + next_storage_key(&basefee_address)?, StarkFelt::from(high_fee), ); + // Set the previous randao. + let [low_prev_randao, high_prev_randao] = split_u256(prev_randao); + let prev_randao_address = get_storage_var_address("prev_randao", &[]); + self.state_mut().set_storage_at( + kakarot_address, + prev_randao_address, + StarkFelt::from(low_prev_randao), + ); + self.state_mut().set_storage_at( + kakarot_address, + next_storage_key(&prev_randao_address)?, + StarkFelt::from(high_prev_randao), + ); + + // Set the block gas limit, considering it fits in a felt. + let [block_gas_limit, _] = split_u256(block_gas_limit); + let block_gas_limit = StarkFelt::from(block_gas_limit); + let block_gas_limit_address = get_storage_var_address("block_gas_limit", &[]); + self.state_mut().set_storage_at( + kakarot_address, + block_gas_limit_address, + StarkFelt::from(block_gas_limit), + ); + Ok(()) } diff --git a/crates/ef-testing/src/evm_sequencer/evm_state/v1.rs b/crates/ef-testing/src/evm_sequencer/evm_state/v1.rs index 8f5e8cad..f98c3ee0 100644 --- a/crates/ef-testing/src/evm_sequencer/evm_state/v1.rs +++ b/crates/ef-testing/src/evm_sequencer/evm_state/v1.rs @@ -38,7 +38,12 @@ use crate::{ impl Evm for KakarotSequencer { /// Sets up the evm state (coinbase, block number, etc.) - fn setup_state(&mut self, _base_fee: U256) -> StateResult<()> { + fn setup_state( + &mut self, + _base_fee: U256, + _prev_randao: U256, + _block_gas_limit: U256, + ) -> StateResult<()> { let coinbase_address = *self.address(); let coinbase = KakarotAccount::new(&coinbase_address, &Bytes::default(), U256::ZERO, &[], true)?; @@ -431,7 +436,9 @@ mod tests { transaction.signature = signature; // When - sequencer.setup_state(U256::ZERO).unwrap(); + sequencer + .setup_state(U256::ZERO, U256::ZERO, U256::ZERO) + .unwrap(); let bytecode = Bytes::from(vec![ 0x60, 0x01, 0x60, 0x00, 0x55, 0x60, 0x02, 0x60, 0x00, 0x53, 0x60, 0x01, 0x60, 0x00, 0xf3, diff --git a/crates/ef-testing/src/models/case.rs b/crates/ef-testing/src/models/case.rs index 02c21fa3..25a00dd2 100644 --- a/crates/ef-testing/src/models/case.rs +++ b/crates/ef-testing/src/models/case.rs @@ -242,6 +242,12 @@ impl Case for BlockchainTestCase { let base_fee = maybe_block_header .and_then(|block_header| block_header.base_fee_per_gas) .unwrap_or_default(); + let prev_randao = maybe_block_header + .map(|block_header| block_header.mix_hash) + .unwrap_or_default(); + let block_gas_limit = maybe_block_header + .map(|block_header| block_header.gas_limit) + .unwrap_or_default(); let block_number = maybe_block_header.map(|b| b.number).unwrap_or_default(); let block_number = TryInto::::try_into(block_number).unwrap_or_default(); @@ -264,7 +270,7 @@ impl Case for BlockchainTestCase { block_timestamp, ); - sequencer.setup_state(base_fee)?; + sequencer.setup_state(base_fee, prev_randao.into(), block_gas_limit)?; self.handle_pre_state(&mut sequencer)?;