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

fix(katana): separate deprecated declared class #2903

Merged
merged 4 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 17 additions & 14 deletions crates/katana/executor/src/implementation/blockifier/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};
use std::num::NonZeroU128;
use std::sync::Arc;

Expand Down Expand Up @@ -432,9 +432,21 @@ pub(super) fn state_update_from_cached_state<S: StateDb>(
katana_primitives::class::ContractClass,
> = BTreeMap::new();

for class_hash in state_diff.compiled_class_hashes.keys() {
let mut declared_classes = BTreeMap::new();
let mut deprecated_declared_classes = BTreeSet::new();

// TODO: Legacy class shouldn't have a compiled class hash. This is a hack we added
// in our fork of `blockifier. Check if it's possible to remove it now.
for (class_hash, compiled_hash) in state_diff.compiled_class_hashes {
let hash = class_hash.0;
let class = state.class(hash).unwrap().expect("must exist if declared");

if class.is_legacy() {
deprecated_declared_classes.insert(hash);
} else {
declared_classes.insert(hash, compiled_hash.0);
}

declared_contract_classes.insert(hash, class);
}

Expand Down Expand Up @@ -470,24 +482,15 @@ pub(super) fn state_update_from_cached_state<S: StateDb>(
katana_primitives::class::ClassHash,
>>();

let declared_classes =
state_diff
.compiled_class_hashes
.into_iter()
.map(|(key, value)| (key.0, value.0))
.collect::<BTreeMap<
katana_primitives::class::ClassHash,
katana_primitives::class::CompiledClassHash,
>>();

StateUpdatesWithClasses {
classes: declared_contract_classes,
state_updates: StateUpdates {
nonce_updates,
storage_updates,
deployed_contracts,
declared_classes,
..Default::default()
deployed_contracts,
deprecated_declared_classes,
replaced_classes: BTreeMap::default(),
},
}
}
Expand Down
9 changes: 2 additions & 7 deletions crates/katana/primitives/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use crate::genesis::allocation::{DevAllocationsGenerator, GenesisAllocation};
use crate::genesis::constant::{
get_fee_token_balance_base_storage_address, DEFAULT_ACCOUNT_CLASS_PUBKEY_STORAGE_SLOT,
DEFAULT_ETH_FEE_TOKEN_ADDRESS, DEFAULT_LEGACY_ERC20_CLASS, DEFAULT_LEGACY_ERC20_CLASS_HASH,
DEFAULT_LEGACY_UDC_CLASS, DEFAULT_LEGACY_UDC_CLASS_HASH,
DEFAULT_LEGACY_UDC_COMPILED_CLASS_HASH, DEFAULT_PREFUNDED_ACCOUNT_BALANCE,
DEFAULT_LEGACY_UDC_CLASS, DEFAULT_LEGACY_UDC_CLASS_HASH, DEFAULT_PREFUNDED_ACCOUNT_BALANCE,
DEFAULT_STRK_FEE_TOKEN_ADDRESS, DEFAULT_UDC_ADDRESS, ERC20_DECIMAL_STORAGE_SLOT,
ERC20_NAME_STORAGE_SLOT, ERC20_SYMBOL_STORAGE_SLOT, ERC20_TOTAL_SUPPLY_STORAGE_SLOT,
};
Expand Down Expand Up @@ -233,11 +232,7 @@ fn add_default_udc(states: &mut StateUpdatesWithClasses) {
.entry(DEFAULT_LEGACY_UDC_CLASS_HASH)
.or_insert_with(|| DEFAULT_LEGACY_UDC_CLASS.clone());

states
.state_updates
.declared_classes
.entry(DEFAULT_LEGACY_UDC_CLASS_HASH)
.or_insert(DEFAULT_LEGACY_UDC_COMPILED_CLASS_HASH);
states.state_updates.deprecated_declared_classes.insert(DEFAULT_LEGACY_UDC_CLASS_HASH);

// deploy UDC contract
states
Expand Down
6 changes: 5 additions & 1 deletion crates/katana/rpc/rpc-types/src/state_update.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use katana_primitives::class::ClassHash;
use serde::{Deserialize, Serialize};
use starknet::core::types::{
ContractStorageDiffItem, DeclaredClassItem, DeployedContractItem, NonceUpdate, StorageEntry,
Expand Down Expand Up @@ -49,6 +50,9 @@
.map(|(addr, nonce)| NonceUpdate { nonce, contract_address: addr.into() })
.collect();

let deprecated_declared_classes: Vec<ClassHash> =
value.deprecated_declared_classes.into_iter().collect();

Check warning on line 55 in crates/katana/rpc/rpc-types/src/state_update.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/rpc/rpc-types/src/state_update.rs#L53-L55

Added lines #L53 - L55 were not covered by tests
let declared_classes: Vec<DeclaredClassItem> = value
.declared_classes
.into_iter()
Expand Down Expand Up @@ -81,8 +85,8 @@
storage_diffs,
declared_classes,
deployed_contracts,
deprecated_declared_classes,

Check warning on line 88 in crates/katana/rpc/rpc-types/src/state_update.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/rpc/rpc-types/src/state_update.rs#L88

Added line #L88 was not covered by tests
replaced_classes: Default::default(),
deprecated_declared_classes: Default::default(),
})
}
}
Expand Down
36 changes: 21 additions & 15 deletions crates/katana/storage/provider/src/providers/db/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub mod state;
pub mod trie;

use std::collections::BTreeMap;
use std::collections::{BTreeMap, BTreeSet};
use std::fmt::Debug;
use std::ops::{Range, RangeInclusive};

Expand Down Expand Up @@ -301,20 +301,24 @@
Ok((contract_address, class_hash))
})?;

let declared_classes = dup_entries::<
Db,
tables::ClassDeclarations,
BTreeMap<ClassHash, CompiledClassHash>,
_,
>(&db_tx, block_num, |entry| {
let (_, class_hash) = entry?;

let compiled_hash = db_tx
.get::<tables::CompiledClassHashes>(class_hash)?
.ok_or(ProviderError::MissingCompiledClassHash(class_hash))?;
let mut declared_classes = BTreeMap::new();
let mut deprecated_declared_classes = BTreeSet::new();

Ok((class_hash, compiled_hash))
})?;
if let Some(block_entries) =
db_tx.cursor_dup::<tables::ClassDeclarations>()?.walk_dup(Some(block_num), None)?
{
for entry in block_entries {
let (_, class_hash) = entry?;
match db_tx.get::<tables::CompiledClassHashes>(class_hash)? {
Some(compiled_hash) => {
declared_classes.insert(class_hash, compiled_hash);
}
None => {
deprecated_declared_classes.insert(class_hash);
}

Check warning on line 318 in crates/katana/storage/provider/src/providers/db/mod.rs

View check run for this annotation

Codecov / codecov/patch

crates/katana/storage/provider/src/providers/db/mod.rs#L316-L318

Added lines #L316 - L318 were not covered by tests
}
}
}

let storage_updates = {
let entries = dup_entries::<
Expand All @@ -337,12 +341,14 @@
};

db_tx.commit()?;

Ok(Some(StateUpdates {
nonce_updates,
storage_updates,
deployed_contracts,
declared_classes,
..Default::default()
deprecated_declared_classes,
replaced_classes: BTreeMap::default(),
}))
} else {
Ok(None)
Expand Down
Loading