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,starknet_api): const patricia keys #2751

Merged
merged 1 commit into from
Dec 18, 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
16 changes: 7 additions & 9 deletions crates/blockifier/src/state/stateful_compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ type AliasKey = StorageKey;
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()));
const ALIAS_CONTRACT_ADDRESS: ContractAddress = ContractAddress(PatriciaKey::TWO);
// 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()));
const ALIAS_COUNTER_STORAGE_KEY: StorageKey = StorageKey(PatriciaKey::ZERO);
// The minimal value for a key to be allocated an alias. Smaller keys are serialized as is (their
// alias is identical to the key).
static MIN_VALUE_FOR_ALIAS_ALLOC: LazyLock<PatriciaKey> =
Expand All @@ -39,7 +37,7 @@ struct AliasUpdater<'a, S: StateReader> {
impl<'a, S: StateReader> AliasUpdater<'a, S> {
fn new(state: &'a CachedState<S>) -> 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(),
Expand All @@ -50,7 +48,7 @@ impl<'a, S: StateReader> AliasUpdater<'a, S> {
/// 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(ALIAS_CONTRACT_ADDRESS, *alias_key)? == Felt::ZERO
&& !self.new_aliases.contains_key(alias_key)
{
let alias_to_allocate = match self.next_free_alias {
Expand All @@ -68,18 +66,18 @@ impl<'a, S: StateReader> AliasUpdater<'a, S> {
fn finalize_updates(mut self) -> HashMap<StorageEntry, Felt> {
match self.next_free_alias {
None => {
self.new_aliases.insert(*ALIAS_COUNTER_STORAGE_KEY, INITIAL_AVAILABLE_ALIAS);
self.new_aliases.insert(ALIAS_COUNTER_STORAGE_KEY, INITIAL_AVAILABLE_ALIAS);
}
Some(alias) => {
if !self.new_aliases.is_empty() {
self.new_aliases.insert(*ALIAS_COUNTER_STORAGE_KEY, alias);
self.new_aliases.insert(ALIAS_COUNTER_STORAGE_KEY, alias);
}
}
}

self.new_aliases
.into_iter()
.map(|(key, alias)| ((*ALIAS_CONTRACT_ADDRESS, key), alias))
.map(|(key, alias)| ((ALIAS_CONTRACT_ADDRESS, key), alias))
.collect()
}
}
6 changes: 3 additions & 3 deletions crates/blockifier/src/state/stateful_compression_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ fn insert_to_alias_contract(
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 All @@ -27,7 +27,7 @@ fn initial_state(n_existing_aliases: u8) -> CachedState<DictStateReader> {
let high_alias_key = INITIAL_AVAILABLE_ALIAS * Felt::TWO;
insert_to_alias_contract(
&mut state_reader.storage_view,
*ALIAS_COUNTER_STORAGE_KEY,
ALIAS_COUNTER_STORAGE_KEY,
INITIAL_AVAILABLE_ALIAS + Felt::from(n_existing_aliases),
);
for i in 0..n_existing_aliases {
Expand Down Expand Up @@ -89,7 +89,7 @@ fn test_alias_updater(
if !expected_alias_keys.is_empty() || n_existing_aliases == 0 {
insert_to_alias_contract(
&mut expected_storage_diff,
*ALIAS_COUNTER_STORAGE_KEY,
ALIAS_COUNTER_STORAGE_KEY,
expected_next_alias,
);
}
Expand Down
4 changes: 4 additions & 0 deletions crates/starknet_api/src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,10 @@ pub const PATRICIA_KEY_UPPER_BOUND: &str =
"0x800000000000000000000000000000000000000000000000000000000000000";

impl PatriciaKey {
pub const ZERO: Self = Self(StarkHash::ZERO);
pub const ONE: Self = Self(StarkHash::ONE);
pub const TWO: Self = Self(StarkHash::TWO);

pub fn key(&self) -> &StarkHash {
&self.0
}
Expand Down
Loading