Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(blockifier): pass alias contract address #2674

Merged
merged 1 commit into from
Dec 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -170,7 +170,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
15 changes: 8 additions & 7 deletions crates/blockifier/src/state/stateful_compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ type AliasKey = StorageKey;
const INITIAL_AVAILABLE_ALIAS_HEX: &str = "0x80";
const INITIAL_AVAILABLE_ALIAS: Felt = Felt::from_hex_unchecked(INITIAL_AVAILABLE_ALIAS_HEX);

// The address of the alias contract.
const ALIAS_CONTRACT_ADDRESS: ContractAddress = ContractAddress(PatriciaKey::TWO);
// The storage key of the alias counter in the alias contract.
const ALIAS_COUNTER_STORAGE_KEY: StorageKey = StorageKey(PatriciaKey::ZERO);
// The maximal contract address for which aliases are not used and all keys are serialized as is,
Expand All @@ -36,6 +34,7 @@ const MIN_VALUE_FOR_ALIAS_ALLOC: 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 @@ -53,7 +52,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 contract_address in contract_addresses {
if let Some(storage_keys) = contract_address_to_sorted_storage_keys.get(&contract_address) {
for key in storage_keys {
Expand All @@ -73,23 +72,25 @@ struct AliasUpdater<'a, S: StateReader> {
state: &'a 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 S) -> StateResult<Self> {
fn new(state: &'a 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 @@ -118,7 +119,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()
}
}
33 changes: 19 additions & 14 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,12 +17,15 @@ 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,
value: Felt,
) {
storage.insert((ALIAS_CONTRACT_ADDRESS, key), value);
storage.insert((*ALIAS_CONTRACT_ADDRESS, key), value);
}

fn initial_state(n_existing_aliases: u8) -> CachedState<DictStateReader> {
Expand Down Expand Up @@ -73,7 +76,7 @@ fn test_alias_updater(
let 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(&state).unwrap();
let mut alias_contract_updater = AliasUpdater::new(&state, *ALIAS_CONTRACT_ADDRESS).unwrap();
for key in keys {
alias_contract_updater.insert_alias(&StorageKey::try_from(key).unwrap()).unwrap();
}
Expand Down Expand Up @@ -120,33 +123,34 @@ 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![
(
(ALIAS_CONTRACT_ADDRESS, ALIAS_COUNTER_STORAGE_KEY),
(*ALIAS_CONTRACT_ADDRESS, ALIAS_COUNTER_STORAGE_KEY),
INITIAL_AVAILABLE_ALIAS + Felt::from(6_u8)
),
((ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x200_u16)), INITIAL_AVAILABLE_ALIAS),
((*ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x200_u16)), INITIAL_AVAILABLE_ALIAS),
(
(ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x304_u16)),
(*ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x304_u16)),
INITIAL_AVAILABLE_ALIAS + Felt::ONE
),
(
(ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x307_u16)),
(*ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x307_u16)),
INITIAL_AVAILABLE_ALIAS + Felt::TWO
),
(
(ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x309_u16)),
(*ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x309_u16)),
INITIAL_AVAILABLE_ALIAS + Felt::THREE
),
(
(ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x201_u16)),
(*ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x201_u16)),
INITIAL_AVAILABLE_ALIAS + Felt::from(4_u8)
),
(
(ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x202_u16)),
(*ALIAS_CONTRACT_ADDRESS, StorageKey::from(0x202_u16)),
INITIAL_AVAILABLE_ALIAS + Felt::from(5_u8)
),
((ContractAddress::from(0x201_u16), StorageKey::from(0x304_u16)), Felt::THREE),
Expand All @@ -167,11 +171,12 @@ 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([(
(ALIAS_CONTRACT_ADDRESS, ALIAS_COUNTER_STORAGE_KEY),
(*ALIAS_CONTRACT_ADDRESS, ALIAS_COUNTER_STORAGE_KEY),
INITIAL_AVAILABLE_ALIAS,
)])
} else {
Expand Down
Loading