Skip to content

Commit

Permalink
fix blockifier API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
greged93 committed Nov 28, 2023
1 parent cd747e0 commit 87dfc81
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 27 deletions.
7 changes: 4 additions & 3 deletions crates/ef-testing/src/evm_sequencer/account.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use blockifier::abi::abi_utils::{get_storage_var_address, get_uint256_storage_var_addresses};
use blockifier::abi::{abi_utils::get_storage_var_address, sierra_types::next_storage_key};
use reth_primitives::{Address, Bytes};
use revm_primitives::U256;
use starknet_api::{
Expand Down Expand Up @@ -109,8 +109,9 @@ impl KakarotAccount {
.flat_map(|(k, v)| {
let keys = split_u256(*k).map(Into::into);
let values = split_u256(*v).map(Into::<StarkFelt>::into);
let keys = get_uint256_storage_var_addresses("storage_", &keys).unwrap(); // safe unwrap: all vars are ASCII
vec![(keys.0, values[0]), (keys.1, values[1])]
let low_key = get_storage_var_address("storage_", &keys);
let high_key = next_storage_key(&low_key).unwrap(); // can fail only if low is the max key
vec![(low_key, values[0]), (high_key, values[1])]
})
.collect();
storage.append(&mut evm_storage_storage);
Expand Down
42 changes: 19 additions & 23 deletions crates/ef-testing/src/evm_sequencer/evm_state.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
use blockifier::abi::abi_utils::{
get_erc20_balance_var_addresses, get_storage_var_address, get_uint256_storage_var_addresses,
};
use blockifier::abi::abi_utils::{get_fee_token_var_address, get_storage_var_address};
use blockifier::abi::sierra_types::next_storage_key;
use blockifier::state::state_api::{State, StateReader, StateResult};
use reth_primitives::{Address, Bytes};
use revm_primitives::U256;
use starknet::core::types::FieldElement;
use starknet_api::hash::StarkFelt;
use starknet_api::state::StorageKey;

use super::account::{AccountType, KakarotAccount};
use super::constants::{
Expand Down Expand Up @@ -80,26 +78,23 @@ impl EvmState for KakarotSequencer {
let mut storage = vec![];

// Initialize the balance storage var.
let balance_keys = get_erc20_balance_var_addresses(&starknet_address.try_into()?)?;
let balance_keys: [StorageKey; 2] = balance_keys.into();
let balance_storage = &mut balance_keys
.into_iter()
.zip(balance_values)
.map(|(k, v)| (k, StarkFelt::from(v)))
.collect();
storage.append(balance_storage);
let balance_low_key = get_fee_token_var_address(&starknet_address.try_into()?);
let balance_high_key = next_storage_key(&balance_low_key)?;
storage.append(&mut vec![
(balance_low_key, StarkFelt::from(balance_values[0])),
(balance_high_key, StarkFelt::from(balance_values[1])),
]);

// Initialize the allowance storage var.
let allowance_keys = get_uint256_storage_var_addresses(
let allowance_key_low = get_storage_var_address(
"ERC20_allowances",
&[starknet_address.into(), *KAKAROT_ADDRESS.0.key()],
)?;
let allowance_keys: [StorageKey; 2] = allowance_keys.into();
let allowance_storage = &mut allowance_keys
.into_iter()
.map(|k| (k, StarkFelt::from(u128::MAX)))
.collect();
storage.append(allowance_storage);
);
let allowance_key_high = next_storage_key(&allowance_key_low)?;
storage.append(&mut vec![
(allowance_key_low, StarkFelt::from(u128::MAX)),
(allowance_key_high, StarkFelt::from(u128::MAX)),
]);

// Write all the storage vars to the sequencer state.
for (k, v) in storage {
Expand All @@ -111,12 +106,13 @@ impl EvmState for KakarotSequencer {
/// Returns the storage value at the given key evm storage key.
fn get_storage_at(&mut self, evm_address: &Address, key: U256) -> StateResult<U256> {
let keys = split_u256(key).map(Into::into);
let keys = get_uint256_storage_var_addresses("storage_", &keys).unwrap(); // safe unwrap: all vars are ASCII
let key_low = get_storage_var_address("storage_", &keys);
let key_high = next_storage_key(&key_low)?;

let starknet_address = compute_starknet_address(evm_address);

let low = (&mut self.0.state).get_storage_at(starknet_address.try_into()?, keys.0)?;
let high = (&mut self.0.state).get_storage_at(starknet_address.try_into()?, keys.1)?;
let low = (&mut self.0.state).get_storage_at(starknet_address.try_into()?, key_low)?;
let high = (&mut self.0.state).get_storage_at(starknet_address.try_into()?, key_high)?;

let low = U256::from_be_bytes(Into::<FieldElement>::into(low).to_bytes_be());
let high = U256::from_be_bytes(Into::<FieldElement>::into(high).to_bytes_be());
Expand Down
2 changes: 1 addition & 1 deletion crates/ef-testing/src/evm_sequencer/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ pub(crate) fn split_bytecode_to_starkfelt(bytecode: &Bytes) -> Vec<StarkFelt> {
pub(crate) fn split_u256(value: U256) -> [u128; 2] {
[
(value & U256::from(u128::MAX)).try_into().unwrap(), // safe unwrap <= U128::MAX.
(value >> 128).try_into().unwrap(), // safe unwrap <= U128::MAX.
(value >> U256::from(128)).try_into().unwrap(), // safe unwrap <= U128::MAX.
]
}

Expand Down
1 change: 1 addition & 0 deletions crates/sequencer/src/sequencer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ mod tests {
signature: TransactionSignature(vec![]),
nonce: Nonce(*ZERO_FELT),
}),
only_query: false,
tx_hash: TransactionHash(*ZERO_FELT),
}))
}
Expand Down
1 change: 1 addition & 0 deletions crates/sequencer/src/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ impl BroadcastedTransactionWrapper {
.collect(),
)),
}),
only_query: false,
tx_hash: TransactionHash(Into::<StarkHash>::into(Into::<StarkFelt>::into(
compute_transaction_hash(
invoke.sender_address,
Expand Down

0 comments on commit 87dfc81

Please sign in to comment.