Skip to content

Commit

Permalink
set coinbase and base_fee in v0 storage (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 authored Feb 6, 2024
1 parent 97f50f1 commit 2aa521d
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 5 deletions.
2 changes: 1 addition & 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,7 @@ 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) -> StateResult<()> {
fn setup_state(&mut self, _base_fee: U256) -> StateResult<()> {
panic!("Not implemented, use features flag \"v0\" or \"v1\"")
}

Expand Down
28 changes: 27 additions & 1 deletion crates/ef-testing/src/evm_sequencer/evm_state/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,38 @@ use super::Evm;
use crate::evm_sequencer::account::{AccountType, KakarotAccount};
use crate::evm_sequencer::constants::ETH_FEE_TOKEN_ADDRESS;
use crate::evm_sequencer::sequencer::KakarotSequencer;
use crate::evm_sequencer::types::felt::FeltSequencer;
use crate::evm_sequencer::utils::{felt_to_bytes, split_u256, to_broadcasted_starknet_transaction};
use crate::starknet_storage;

impl Evm for KakarotSequencer {
/// Sets up the evm state (coinbase, block number, etc.)
fn setup_state(&mut self) -> StateResult<()> {
fn setup_state(&mut self, base_fee: U256) -> StateResult<()> {
let kakarot_address = self.environment.kakarot_address;
let coinbase_address: FeltSequencer = (*self.address()).try_into().unwrap(); // infallible

// Set the coinbase address.
self.state_mut().set_storage_at(
kakarot_address,
get_storage_var_address("coinbase", &[]),
coinbase_address.into(),
);

// 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", &[]);
self.state_mut()
.set_storage_at(kakarot_address, base_address, StarkFelt::from(low_fee));
self.state_mut().set_storage_at(
kakarot_address,
next_storage_key(&base_address)?,
StarkFelt::from(high_fee),
);

Ok(())
}

Expand Down
4 changes: 2 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,7 @@ use crate::{

impl Evm for KakarotSequencer {
/// Sets up the evm state (coinbase, block number, etc.)
fn setup_state(&mut self) -> StateResult<()> {
fn setup_state(&mut self, _base_fee: U256) -> StateResult<()> {
let coinbase_address = *self.address();
let coinbase = KakarotAccount::new(&coinbase_address, &Bytes::default(), U256::ZERO, &[])?;
self.setup_account(coinbase)?;
Expand Down Expand Up @@ -429,7 +429,7 @@ mod tests {
transaction.signature = signature;

// When
sequencer.setup_state().unwrap();
sequencer.setup_state(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
7 changes: 6 additions & 1 deletion crates/ef-testing/src/models/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,11 @@ impl Case for BlockchainTestCase {

let coinbase_address = maybe_block_header.map(|b| b.coinbase).unwrap_or_default();

let base_fee = maybe_block_header
.and_then(|block_header| block_header.base_fee_per_gas)
.map(|base_fee| base_fee.0)
.unwrap_or_default();

let block_number = maybe_block_header.map(|b| b.number.0).unwrap_or_default();
let block_number = TryInto::<u64>::try_into(block_number).unwrap_or_default();

Expand All @@ -243,7 +248,7 @@ impl Case for BlockchainTestCase {
block_timestamp,
);

sequencer.setup_state()?;
sequencer.setup_state(base_fee)?;

self.handle_pre_state(&mut sequencer)?;

Expand Down

0 comments on commit 2aa521d

Please sign in to comment.