Skip to content

Commit

Permalink
feat(blockifier): pass alias contract address
Browse files Browse the repository at this point in the history
  • Loading branch information
yoavGrs committed Dec 17, 2024
1 parent 2879a07 commit 4f43df7
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
9 changes: 8 additions & 1 deletion crates/blockifier/src/blockifier/transaction_executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,14 @@ impl<S: StateReader> TransactionExecutor<S> {
log::debug!("Final block weights: {:?}.", self.bouncer.get_accumulated_weights());
let mut block_state = self.block_state.take().expect(BLOCK_STATE_ACCESS_ERR);
let state_diff = if self.block_context.versioned_constants.enable_stateful_compression {
state_diff_with_alias_allocation(&mut block_state)?
state_diff_with_alias_allocation(
&mut block_state,
self.block_context
.versioned_constants
.os_constants
.os_contract_addresses
.alias_contract_address(),
)?
} else {
block_state.to_state_diff()?.state_maps
};
Expand Down
19 changes: 11 additions & 8 deletions crates/blockifier/src/state/stateful_compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ type AliasKey = StorageKey;
// The initial alias available for allocation.
const INITIAL_AVAILABLE_ALIAS: Felt = Felt::from_hex_unchecked("0x80");

// The address of the alias contract.
static ALIAS_CONTRACT_ADDRESS: LazyLock<ContractAddress> =
LazyLock::new(|| ContractAddress(PatriciaKey::try_from(Felt::TWO).unwrap()));
// The storage key of the alias counter in the alias contract.
static ALIAS_COUNTER_STORAGE_KEY: LazyLock<StorageKey> =
LazyLock::new(|| StorageKey(PatriciaKey::try_from(Felt::ZERO).unwrap()));
Expand All @@ -38,6 +35,7 @@ static MIN_VALUE_FOR_ALIAS_ALLOC: LazyLock<PatriciaKey> =
/// storage keys (in ascending order) and for the address itself.
pub fn state_diff_with_alias_allocation<S: StateReader>(
state: &mut CachedState<S>,
alias_contract_address: ContractAddress,
) -> StateResult<StateMaps> {
let mut state_diff = state.to_state_diff()?.state_maps;

Expand All @@ -52,7 +50,7 @@ pub fn state_diff_with_alias_allocation<S: StateReader>(
}

// Iterate over the addresses and the storage keys and update the aliases.
let mut alias_updater = AliasUpdater::new(state)?;
let mut alias_updater = AliasUpdater::new(state, alias_contract_address)?;
for address in addresses {
if let Some(storage_keys) = sorted_storage_keys.get(&address) {
for key in storage_keys {
Expand All @@ -72,23 +70,28 @@ struct AliasUpdater<'a, S: StateReader> {
state: &'a CachedState<S>,
new_aliases: HashMap<AliasKey, Alias>,
next_free_alias: Option<Alias>,
alias_contract_address: ContractAddress,
}

impl<'a, S: StateReader> AliasUpdater<'a, S> {
fn new(state: &'a CachedState<S>) -> StateResult<Self> {
fn new(
state: &'a CachedState<S>,
alias_contract_address: ContractAddress,
) -> StateResult<Self> {
let stored_counter =
state.get_storage_at(*ALIAS_CONTRACT_ADDRESS, *ALIAS_COUNTER_STORAGE_KEY)?;
state.get_storage_at(alias_contract_address, *ALIAS_COUNTER_STORAGE_KEY)?;
Ok(Self {
state,
new_aliases: HashMap::new(),
next_free_alias: if stored_counter == Felt::ZERO { None } else { Some(stored_counter) },
alias_contract_address,
})
}

/// Inserts the alias key to the updates if it's not already aliased.
fn insert_alias(&mut self, alias_key: &AliasKey) -> StateResult<()> {
if alias_key.0 >= *MIN_VALUE_FOR_ALIAS_ALLOC
&& self.state.get_storage_at(*ALIAS_CONTRACT_ADDRESS, *alias_key)? == Felt::ZERO
&& self.state.get_storage_at(self.alias_contract_address, *alias_key)? == Felt::ZERO
&& !self.new_aliases.contains_key(alias_key)
{
let alias_to_allocate = match self.next_free_alias {
Expand Down Expand Up @@ -117,7 +120,7 @@ impl<'a, S: StateReader> AliasUpdater<'a, S> {

self.new_aliases
.into_iter()
.map(|(key, alias)| ((*ALIAS_CONTRACT_ADDRESS, key), alias))
.map(|(key, alias)| ((self.alias_contract_address, key), alias))
.collect()
}
}
16 changes: 11 additions & 5 deletions crates/blockifier/src/state/stateful_compression_test.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::collections::HashMap;
use std::sync::LazyLock;

use rstest::rstest;
use starknet_api::core::{ClassHash, ContractAddress};
use starknet_api::core::{ClassHash, ContractAddress, PatriciaKey};
use starknet_api::state::StorageKey;
use starknet_types_core::felt::Felt;

use super::{
state_diff_with_alias_allocation,
AliasUpdater,
ALIAS_CONTRACT_ADDRESS,
ALIAS_COUNTER_STORAGE_KEY,
INITIAL_AVAILABLE_ALIAS,
MAX_NON_COMPRESSED_CONTRACT_ADDRESS,
Expand All @@ -17,6 +17,9 @@ use crate::state::cached_state::{CachedState, StorageEntry};
use crate::state::state_api::{State, StateReader};
use crate::test_utils::dict_state_reader::DictStateReader;

static ALIAS_CONTRACT_ADDRESS: LazyLock<ContractAddress> =
LazyLock::new(|| ContractAddress(PatriciaKey::try_from(Felt::TWO).unwrap()));

fn insert_to_alias_contract(
storage: &mut HashMap<StorageEntry, Felt>,
key: StorageKey,
Expand Down Expand Up @@ -73,7 +76,8 @@ fn test_alias_updater(
let mut state = initial_state(n_existing_aliases);

// Insert the keys into the alias contract updater and finalize the updates.
let mut alias_contract_updater = AliasUpdater::new(&mut state).unwrap();
let mut alias_contract_updater =
AliasUpdater::new(&mut state, *ALIAS_CONTRACT_ADDRESS).unwrap();
for key in keys {
alias_contract_updater.insert_alias(&StorageKey::try_from(key).unwrap()).unwrap();
}
Expand Down Expand Up @@ -124,7 +128,8 @@ fn test_iterate_aliases() {
state.set_class_hash_at(ContractAddress::from(0x202_u16), ClassHash(Felt::ONE)).unwrap();
state.increment_nonce(ContractAddress::from(0x200_u16)).unwrap();

let storage_diff = state_diff_with_alias_allocation(&mut state).unwrap().storage;
let storage_diff =
state_diff_with_alias_allocation(&mut state, *ALIAS_CONTRACT_ADDRESS).unwrap().storage;
assert_eq!(
storage_diff,
vec![
Expand Down Expand Up @@ -171,7 +176,8 @@ fn test_read_only_state(#[values(0, 2)] n_existing_aliases: u8) {
.unwrap();
state.get_nonce_at(ContractAddress::from(0x201_u16)).unwrap();
state.get_class_hash_at(ContractAddress::from(0x202_u16)).unwrap();
let storage_diff = state_diff_with_alias_allocation(&mut state).unwrap().storage;
let storage_diff =
state_diff_with_alias_allocation(&mut state, *ALIAS_CONTRACT_ADDRESS).unwrap().storage;

let expected_storage_diff = if n_existing_aliases == 0 {
HashMap::from([(
Expand Down

0 comments on commit 4f43df7

Please sign in to comment.