Skip to content

Commit

Permalink
cleanup some logic around Felt (#726)
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger authored Jul 16, 2024
1 parent a38a32e commit 0915e15
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 68 deletions.
7 changes: 3 additions & 4 deletions crates/ef-testing/src/evm_sequencer/account/v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use blockifier::abi::{abi_utils::get_storage_var_address, sierra_types::next_sto
use reth_primitives::{Address, U256};
use revm_interpreter::analysis::to_analysed;
use revm_primitives::{Bytecode, Bytes};
use starknet_api::core::PatriciaKey;
use starknet_api::{core::Nonce, state::StorageKey, StarknetApiError};
use starknet_crypto::Felt;

Expand Down Expand Up @@ -65,9 +64,9 @@ impl KakarotAccount {
let jumdpests_storage_address = *jumdpests_storage_address.0.key();
valid_jumpdests.into_iter().for_each(|index| {
storage.push((
StorageKey(
PatriciaKey::try_from(jumdpests_storage_address + Felt::from(index)).unwrap(),
),
(jumdpests_storage_address + Felt::from(index))
.try_into()
.unwrap(),
Felt::ONE,
))
});
Expand Down
19 changes: 4 additions & 15 deletions crates/ef-testing/src/evm_sequencer/account/v1.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use blockifier::abi::abi_utils::get_storage_var_address;
use reth_primitives::{Address, Bytes, U256};
use starknet::core::types::Felt;
use starknet_api::{
core::{Nonce, PatriciaKey},
state::StorageKey,
StarknetApiError,
};
use starknet_api::{core::Nonce, state::StorageKey, StarknetApiError};

use super::KakarotAccount;
use super::{inner_byte_array_pointer, pack_byte_array_to_starkfelt_array};
Expand Down Expand Up @@ -39,10 +35,7 @@ fn prepare_bytearray_storage(code: &Bytes) -> Vec<(StorageKey, Felt)> {
let index = index / 256;
let key = inner_byte_array_pointer(*bytecode_base_address.0.key(), index.into());
(
offset_storage_key(
StorageKey(PatriciaKey::try_from(key).unwrap()),
offset as i64,
),
offset_storage_key(key.try_into().unwrap(), offset as i64),
b,
)
})
Expand Down Expand Up @@ -123,13 +116,9 @@ mod tests {
(bytecode_base_address, Felt::from(code.len() as u128)),
(
offset_storage_key(
StorageKey(
PatriciaKey::try_from(inner_byte_array_pointer(
*bytecode_base_address.0.key(),
Felt::ZERO,
))
inner_byte_array_pointer(*bytecode_base_address.0.key(), Felt::ZERO)
.try_into()
.unwrap(),
),
0,
),
Felt::from(0x0102030405u64),
Expand Down
26 changes: 8 additions & 18 deletions crates/ef-testing/src/evm_sequencer/evm_state/v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use num_integer::Integer;
use reth_primitives::{Address, Bytes, TransactionSigned, U256};
use sequencer::{execution::Execution as _, transaction::BroadcastedTransactionWrapper};
use starknet::core::types::BroadcastedTransaction;
use starknet_api::{
core::{PatriciaKey, L2_ADDRESS_UPPER_BOUND},
state::StorageKey,
};
use starknet_api::{core::L2_ADDRESS_UPPER_BOUND, state::StorageKey};
use starknet_crypto::{poseidon_hash_many, Felt};

use super::Evm;
Expand Down Expand Up @@ -229,10 +226,7 @@ impl Evm for KakarotSequencer {
let offset = chunk_index % 256;
let storage_pointer =
inner_byte_array_pointer(*bytecode_base_address.0.key(), Felt::from(index));
let key = offset_storage_key(
StorageKey(PatriciaKey::try_from(storage_pointer).unwrap()),
offset as i64,
);
let key = offset_storage_key(storage_pointer.try_into().unwrap(), offset as i64);
let code = self.state_mut().get_storage_at(starknet_address, key)?;
bytecode.append(&mut code.to_bytes_be()[1..].to_vec());
}
Expand All @@ -244,10 +238,8 @@ impl Evm for KakarotSequencer {
*bytecode_base_address.0.key(),
storage_chunk_index.into(),
);
let key = offset_storage_key(
StorageKey(PatriciaKey::try_from(storage_pointer).unwrap()),
offset_in_chunk as i64,
);
let key =
offset_storage_key(storage_pointer.try_into().unwrap(), offset_in_chunk as i64);

let pending_word = self.state_mut().get_storage_at(starknet_address, key)?;
bytecode
Expand Down Expand Up @@ -321,12 +313,12 @@ pub(crate) fn compute_storage_base_address(storage_var_name: &str, keys: &[Felt]
let key: Felt = poseidon_hash_many(&data);
let key_floored = key.mod_floor(&L2_ADDRESS_UPPER_BOUND);

StorageKey(PatriciaKey::try_from(key_floored).unwrap()) // infallible
key_floored.try_into().unwrap() // infallible
}

pub(crate) fn offset_storage_key(key: StorageKey, offset: i64) -> StorageKey {
let base_address = *key.0.key() + Felt::from(offset);
StorageKey(PatriciaKey::try_from(base_address).unwrap()) // infallible
base_address.try_into().unwrap() // infallible
}

#[cfg(test)]
Expand All @@ -349,16 +341,14 @@ mod tests {
#[test]
fn test_offset_storage_base_address() {
// Given
let base_address =
StorageKey(PatriciaKey::try_from(Felt::from(0x0102030405060708u64)).unwrap());
let base_address = StorageKey(Felt::from(0x0102030405060708u64).try_into().unwrap());
let offset = -1;

// When
let result = offset_storage_key(base_address, offset);

// Then
let expected =
StorageKey(PatriciaKey::try_from(Felt::from(0x0102030405060707u64)).unwrap());
let expected = StorageKey(Felt::from(0x0102030405060707u64).try_into().unwrap());
assert!(result == expected);
}

Expand Down
7 changes: 2 additions & 5 deletions crates/ef-testing/src/evm_sequencer/types/felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@ use std::convert::Infallible;

use reth_primitives::Address;
use starknet::core::types::Felt;
use starknet_api::{
core::{ContractAddress, PatriciaKey},
StarknetApiError,
};
use starknet_api::{core::ContractAddress, StarknetApiError};

/// A wrapper around a Felt in order to facilitate conversion.
#[derive(Debug, Clone, Copy)]
Expand Down Expand Up @@ -37,6 +34,6 @@ impl TryFrom<FeltSequencer> for ContractAddress {

fn try_from(felt: FeltSequencer) -> Result<Self, Self::Error> {
let felt: Felt = felt.into();
Ok(Self(TryInto::<PatriciaKey>::try_into(felt)?))
Ok(Self(felt.try_into()?))
}
}
10 changes: 5 additions & 5 deletions crates/sequencer/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ pub mod test_constants {
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 SEQUENCER_ADDRESS: ContractAddress = ContractAddress(TryInto::<PatriciaKey>::try_into(Felt::from(1234u16)).unwrap());
pub static ref ETH_FEE_TOKEN_ADDRESS: ContractAddress = ContractAddress(TryInto::<PatriciaKey>::try_into(Felt::from(12345u16)).unwrap());
pub static ref STRK_FEE_TOKEN_ADDRESS: ContractAddress = ContractAddress(TryInto::<PatriciaKey>::try_into(Felt::from(123456u32)).unwrap());
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 ONE_PATRICIA: PatriciaKey = TryInto::<PatriciaKey>::try_into(*ONE_FELT).unwrap();
pub static ref TWO_PATRICIA: PatriciaKey = TryInto::<PatriciaKey>::try_into(*TWO_FELT).unwrap();
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);
pub static ref TWO_CLASS_HASH: ClassHash = ClassHash(*TWO_FELT);
pub static ref ONE_COMPILED_CLASS_HASH: CompiledClassHash = CompiledClassHash(*ONE_FELT);
Expand Down
31 changes: 10 additions & 21 deletions crates/sequencer/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use blockifier::transaction::{
};
use starknet::core::crypto::compute_hash_on_elements;
use starknet::core::types::{BroadcastedInvokeTransaction, BroadcastedTransaction, Felt};
use starknet_api::core::{ContractAddress, Nonce, PatriciaKey};
use starknet_api::hash::StarkHash;
use starknet_api::core::Nonce;
use starknet_api::transaction::InvokeTransaction;
use starknet_api::transaction::{
Calldata, Fee, InvokeTransactionV1, TransactionHash, TransactionSignature,
Expand Down Expand Up @@ -40,18 +39,10 @@ impl BroadcastedTransactionWrapper {
tx: InvokeTransaction::V1(InvokeTransactionV1 {
max_fee: Fee(invoke_v1.max_fee.to_biguint().try_into()?),
signature: TransactionSignature(
invoke_v1
.signature
.into_iter()
.map(Into::<Felt>::into)
.collect(),
invoke_v1.signature.into_iter().map(Into::into).collect(),
),
nonce: Nonce(invoke_v1.nonce),
sender_address: ContractAddress(TryInto::<PatriciaKey>::try_into(
Into::<StarkHash>::into(Into::<Felt>::into(
invoke_v1.sender_address,
)),
)?),
sender_address: invoke_v1.sender_address.try_into()?,
calldata: Calldata(Arc::new(
invoke_v1
.calldata
Expand All @@ -61,15 +52,13 @@ impl BroadcastedTransactionWrapper {
)),
}),
only_query: false,
tx_hash: TransactionHash(Into::<StarkHash>::into(Into::<Felt>::into(
compute_transaction_hash(
invoke_v1.sender_address,
&invoke_v1.calldata,
invoke_v1.max_fee,
chain_id,
invoke_v1.nonce,
),
))),
tx_hash: TransactionHash(compute_transaction_hash(
invoke_v1.sender_address,
&invoke_v1.calldata,
invoke_v1.max_fee,
chain_id,
invoke_v1.nonce,
)),
}),
))
}
Expand Down

0 comments on commit 0915e15

Please sign in to comment.