Skip to content

Commit

Permalink
feat: set prevrandao and block gaslimit (#670)
Browse files Browse the repository at this point in the history
* feat: set randao and block_gaslimit in runner

* use mixhash for prevrandao

* use split_u256

* chore: fmt

* chore: normalize block_gas_limit naming

* fix: use u128 part from block gas limit

* make clippy happy
  • Loading branch information
enitrat authored Mar 5, 2024
1 parent 4e35d11 commit 1d5caf0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 13 deletions.
7 changes: 6 additions & 1 deletion crates/ef-testing/src/evm_sequencer/evm_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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\"")
}

Expand Down
43 changes: 34 additions & 9 deletions crates/ef-testing/src/evm_sequencer/evm_state/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(())
}

Expand Down
11 changes: 9 additions & 2 deletions crates/ef-testing/src/evm_sequencer/evm_state/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion crates/ef-testing/src/models/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u64>::try_into(block_number).unwrap_or_default();
Expand All @@ -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)?;

Expand Down

0 comments on commit 1d5caf0

Please sign in to comment.