Skip to content

Commit

Permalink
some refactor around Felt (#727)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Jul 18, 2024
1 parent 0915e15 commit c839f61
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 33 deletions.
4 changes: 2 additions & 2 deletions crates/ef-testing/src/evm_sequencer/account/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use crate::{
/// - The short string `ByteArray`.
fn prepare_bytearray_storage(code: &Bytes) -> Vec<(StorageKey, Felt)> {
let bytecode_base_address = get_storage_var_address(ACCOUNT_BYTECODE, &[]);
let mut bytearray = vec![(bytecode_base_address, Felt::from(code.len() as u128))];
let mut bytearray = vec![(bytecode_base_address, Felt::from(code.len()))];

let bytecode_storage: Vec<_> = pack_byte_array_to_starkfelt_array(code)
.enumerate()
Expand Down Expand Up @@ -113,7 +113,7 @@ mod tests {

// Then
let expected_result = vec![
(bytecode_base_address, Felt::from(code.len() as u128)),
(bytecode_base_address, Felt::from(code.len())),
(
offset_storage_key(
inner_byte_array_pointer(*bytecode_base_address.0.key(), Felt::ZERO)
Expand Down
23 changes: 11 additions & 12 deletions crates/ef-testing/src/evm_sequencer/evm_state/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,11 @@ impl Evm for KakarotSequencer {
let [low_fee, high_fee] = split_u256(base_fee);
let basefee_address = get_storage_var_address(KAKAROT_BASE_FEE, &[]);
self.state_mut()
.set_storage_at(kakarot_address, basefee_address, Felt::from(low_fee))?;
.set_storage_at(kakarot_address, basefee_address, low_fee.into())?;
self.state_mut().set_storage_at(
kakarot_address,
next_storage_key(&basefee_address)?,
Felt::from(high_fee),
high_fee.into(),
)?;

// Set the previous randao.
Expand All @@ -58,22 +58,21 @@ impl Evm for KakarotSequencer {
self.state_mut().set_storage_at(
kakarot_address,
prev_randao_address,
Felt::from(low_prev_randao),
low_prev_randao.into(),
)?;
self.state_mut().set_storage_at(
kakarot_address,
next_storage_key(&prev_randao_address)?,
Felt::from(high_prev_randao),
high_prev_randao.into(),
)?;

// Set the block gas limit, considering it fits in a felt.
let [block_gas_limit, _] = split_u256(block_gas_limit);
let block_gas_limit = Felt::from(block_gas_limit);
let block_gas_limit_address = get_storage_var_address(KAKAROT_BLOCK_GAS_LIMIT, &[]);
self.state_mut().set_storage_at(
kakarot_address,
block_gas_limit_address,
block_gas_limit,
block_gas_limit.into(),
)?;

Ok(())
Expand Down Expand Up @@ -131,8 +130,8 @@ impl Evm for KakarotSequencer {
let balance_low_key = get_fee_token_var_address(starknet_address);
let balance_high_key = next_storage_key(&balance_low_key)?;
storage.append(&mut vec![
(balance_low_key, Felt::from(balance_values[0])),
(balance_high_key, Felt::from(balance_values[1])),
(balance_low_key, balance_values[0].into()),
(balance_high_key, balance_values[1].into()),
]);

// Initialize the allowance storage var.
Expand All @@ -145,8 +144,8 @@ impl Evm for KakarotSequencer {
);
let allowance_key_high = next_storage_key(&allowance_key_low)?;
storage.append(&mut vec![
(allowance_key_low, Felt::from(u128::MAX)),
(allowance_key_high, Felt::from(u128::MAX)),
(allowance_key_low, u128::MAX.into()),
(allowance_key_high, u128::MAX.into()),
]);

// Write all the storage vars to the sequencer state.
Expand Down Expand Up @@ -376,9 +375,9 @@ mod tests {
.state_mut()
.get_storage_at(
contract_starknet_address,
get_storage_var_address(ACCOUNT_STORAGE, &[Felt::from(0u8), Felt::from(0u8)]),
get_storage_var_address(ACCOUNT_STORAGE, &[Felt::ZERO, Felt::ZERO]),
)
.unwrap();
assert_eq!(storage, Felt::from(1u8));
assert_eq!(storage, Felt::ONE);
}
}
19 changes: 9 additions & 10 deletions crates/ef-testing/src/evm_sequencer/evm_state/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ impl Evm for KakarotSequencer {
let [low_fee, high_fee] = split_u256(base_fee);
let basefee_address = get_storage_var_address(KAKAROT_BASE_FEE, &[]);
self.state_mut()
.set_storage_at(kakarot_address, basefee_address, Felt::from(low_fee))?;
.set_storage_at(kakarot_address, basefee_address, low_fee.into())?;
self.state_mut().set_storage_at(
kakarot_address,
next_storage_key(&basefee_address)?,
Felt::from(high_fee),
high_fee.into(),
)?;

// Set the previous randao.
Expand All @@ -71,22 +71,21 @@ impl Evm for KakarotSequencer {
self.state_mut().set_storage_at(
kakarot_address,
prev_randao_address,
Felt::from(low_prev_randao),
low_prev_randao.into(),
)?;
self.state_mut().set_storage_at(
kakarot_address,
next_storage_key(&prev_randao_address)?,
Felt::from(high_prev_randao),
high_prev_randao.into(),
)?;

// Set the block gas limit, considering it fits in a felt.
let [block_gas_limit, _] = split_u256(block_gas_limit);
let block_gas_limit = Felt::from(block_gas_limit);
let block_gas_limit_address = get_storage_var_address(KAKAROT_BLOCK_GAS_LIMIT, &[]);
self.state_mut().set_storage_at(
kakarot_address,
block_gas_limit_address,
block_gas_limit,
block_gas_limit.into(),
)?;

Ok(())
Expand Down Expand Up @@ -139,8 +138,8 @@ impl Evm for KakarotSequencer {
let balance_key_low = get_fee_token_var_address(starknet_address);
let balance_key_high = next_storage_key(&balance_key_low)?;
storage.append(&mut vec![
(balance_key_low, Felt::from(balance_values[0])),
(balance_key_high, Felt::from(balance_values[1])),
(balance_key_low, balance_values[0].into()),
(balance_key_high, balance_values[1].into()),
]);

// Initialize the allowance storage var.
Expand All @@ -150,8 +149,8 @@ impl Evm for KakarotSequencer {
);
let allowance_key_high = next_storage_key(&allowance_key_low)?;
storage.append(&mut vec![
(allowance_key_low, Felt::from(u128::MAX)),
(allowance_key_high, Felt::from(u128::MAX)),
(allowance_key_low, u128::MAX.into()),
(allowance_key_high, u128::MAX.into()),
]);

// Write all the storage vars to the sequencer state.
Expand Down
4 changes: 1 addition & 3 deletions crates/ef-testing/src/evm_sequencer/sequencer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ lazy_static! {

use std::ops::{Deref, DerefMut};

use crate::evm_sequencer::types::felt::FeltSequencer;
use blockifier::blockifier::block::{BlockInfo, GasPrices};
use blockifier::context::ChainInfo;
use blockifier::context::{BlockContext, FeeTokenAddresses};
Expand Down Expand Up @@ -91,7 +92,6 @@ impl KakarotSequencer {
) -> Self {
let kakarot_address = *environment.kakarot_address.0.key();
let coinbase_constructor_args = {
use crate::evm_sequencer::types::felt::FeltSequencer;
let evm_address: FeltSequencer = coinbase_address.try_into().unwrap(); // infallible
vec![kakarot_address, evm_address.into()]
};
Expand Down Expand Up @@ -149,7 +149,6 @@ impl KakarotSequencer {

pub fn chain_id(&self) -> u64 {
// Safety: chain_id is always 8 bytes.
// let chain_id = &self.block_context().chain_info().chain_id.0.as_bytes()[..8];
let chain_id = self.block_context().chain_info().chain_id.to_string();
let chain_id = &chain_id.as_bytes()[..8];
u64::from_be_bytes(chain_id.try_into().unwrap())
Expand All @@ -160,7 +159,6 @@ impl KakarotSequencer {
let base_class_hash = self.environment.base_account_class_hash.0;

let constructor_args = {
use crate::evm_sequencer::types::felt::FeltSequencer;
let evm_address: FeltSequencer = (*evm_address).try_into().unwrap(); // infallible
vec![kakarot_address, evm_address.into()]
};
Expand Down
8 changes: 4 additions & 4 deletions crates/sequencer/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ pub mod test_constants {
pub static ref TEST_ACCOUNT: ContractAddress = ContractAddress(*TWO_PATRICIA);
pub static ref TEST_STORAGE_KEY: StorageKey = StorageKey(*ONE_PATRICIA);
pub static ref TEST_NONCE: Nonce = Nonce(*ONE_FELT);
pub static ref SENDER_ADDRESS: Felt = Felt::from(2u8);
pub static ref SENDER_ADDRESS: Felt = Felt::TWO;
pub static ref SEQUENCER_ADDRESS: ContractAddress = Felt::from(1234u16).try_into().unwrap();
pub static ref ETH_FEE_TOKEN_ADDRESS: ContractAddress = Felt::from(12345u16).try_into().unwrap();
pub static ref STRK_FEE_TOKEN_ADDRESS: ContractAddress = Felt::from(123456u32).try_into().unwrap();

pub static ref ZERO_FELT: Felt = Felt::from(0u8);
pub static ref ONE_FELT: Felt = Felt::from(1u8);
pub static ref TWO_FELT: Felt = Felt::from(2u8);
pub static ref ZERO_FELT: Felt = Felt::ZERO;
pub static ref ONE_FELT: Felt = Felt::ONE;
pub static ref TWO_FELT: Felt = Felt::TWO;
pub static ref ONE_PATRICIA: PatriciaKey = (*ONE_FELT).try_into().unwrap();
pub static ref TWO_PATRICIA: PatriciaKey = (*TWO_FELT).try_into().unwrap();
pub static ref ONE_CLASS_HASH: ClassHash = ClassHash(*ONE_FELT);
Expand Down
4 changes: 2 additions & 2 deletions crates/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ mod tests {
.set_storage_at(
*ETH_FEE_TOKEN_ADDRESS,
get_storage_var_address("ERC20_balances", &[address]),
Felt::from(u128::MAX),
u128::MAX.into(),
)
.unwrap_or_else(|_| panic!("failed to fund account {}", address));
}
Expand Down Expand Up @@ -241,7 +241,7 @@ mod tests {
sequencer.execute(transaction).unwrap();

// Then
let expected = Felt::from(1u8);
let expected = Felt::ONE;
let actual = (&mut sequencer.state)
.get_storage_at(*TEST_CONTRACT, get_storage_var_address("counter", &[]))
.unwrap();
Expand Down

0 comments on commit c839f61

Please sign in to comment.