Skip to content

Commit

Permalink
use btreemap everywhere in genesis and state updates
Browse files Browse the repository at this point in the history
  • Loading branch information
kariy committed Sep 23, 2024
1 parent bbdc6c8 commit 6494abf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions crates/katana/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ anyhow.workspace = true
base64.workspace = true
derive_more.workspace = true
lazy_static.workspace = true
num-traits.workspace = true
rand = { workspace = true, features = [ "small_rng" ] }
serde.workspace = true
serde_json.workspace = true
Expand All @@ -24,9 +23,9 @@ thiserror.workspace = true
alloy-primitives.workspace = true
flate2 = { workspace = true, optional = true }
katana-cairo.workspace = true
num-bigint = "0.4.6"

[dev-dependencies]
num-traits.workspace = true
assert_matches.workspace = true
similar-asserts.workspace = true

Expand Down
14 changes: 7 additions & 7 deletions crates/katana/primitives/src/genesis/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! JSON representation of the genesis configuration. Used to deserialize the genesis configuration
//! from a JSON file.
use std::collections::{hash_map, BTreeMap, HashMap};
use std::collections::{btree_map, hash_map, BTreeMap, HashMap};
use std::fs::File;
use std::io::{
BufReader, {self},
Expand Down Expand Up @@ -267,9 +267,9 @@ pub struct GenesisJson {
pub fee_token: FeeTokenConfigJson,
pub universal_deployer: Option<UniversalDeployerConfigJson>,
#[serde(default)]
pub accounts: HashMap<ContractAddress, GenesisAccountJson>,
pub accounts: BTreeMap<ContractAddress, GenesisAccountJson>,
#[serde(default)]
pub contracts: HashMap<ContractAddress, GenesisContractJson>,
pub contracts: BTreeMap<ContractAddress, GenesisContractJson>,
}

impl GenesisJson {
Expand Down Expand Up @@ -319,7 +319,7 @@ impl TryFrom<GenesisJson> for Genesis {
fn try_from(value: GenesisJson) -> Result<Self, Self::Error> {
// a lookup table for classes that is assigned a name
let mut class_names: HashMap<String, Felt> = HashMap::new();
let mut classes: HashMap<ClassHash, GenesisClass> = HashMap::new();
let mut classes: BTreeMap<ClassHash, GenesisClass> = BTreeMap::new();

#[cfg(feature = "slot")]
// Merely a band aid fix for now.
Expand Down Expand Up @@ -521,7 +521,7 @@ impl TryFrom<GenesisJson> for Genesis {
None => {
// check that the default account class exists in the classes field before
// inserting it
if let hash_map::Entry::Vacant(e) =
if let btree_map::Entry::Vacant(e) =
classes.entry(DEFAULT_OZ_ACCOUNT_CONTRACT_CLASS_HASH)
{
// insert default account class to the classes map
Expand Down Expand Up @@ -877,7 +877,7 @@ mod tests {
let json = GenesisJson::load(path).unwrap();
let actual_genesis = Genesis::try_from(json).unwrap();

let expected_classes = HashMap::from([
let expected_classes = BTreeMap::from([
(
felt!("0x07b3e05f48f0c69e4a65ce5e076a66271a527aff2c34ce1083ec6e1526997a69"),
GenesisClass {
Expand Down Expand Up @@ -1116,7 +1116,7 @@ mod tests {
let genesis_json: GenesisJson = GenesisJson::from_str(json).unwrap();
let actual_genesis = Genesis::try_from(genesis_json).unwrap();

let classes = HashMap::from([
let classes = BTreeMap::from([
(
DEFAULT_LEGACY_UDC_CLASS_HASH,
GenesisClass {
Expand Down
10 changes: 5 additions & 5 deletions crates/katana/primitives/src/genesis/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ pub mod allocation;
pub mod constant;
pub mod json;

use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::sync::Arc;

Expand Down Expand Up @@ -98,7 +98,7 @@ pub struct Genesis {
/// The genesis block L1 gas prices.
pub gas_prices: GasPrices,
/// The classes to declare in the genesis block.
pub classes: HashMap<ClassHash, GenesisClass>,
pub classes: BTreeMap<ClassHash, GenesisClass>,
/// The fee token configuration.
pub fee_token: FeeTokenConfig,
/// The universal deployer (UDC) configuration.
Expand Down Expand Up @@ -262,7 +262,7 @@ impl Default for Genesis {
storage: None,
};

let classes = HashMap::from([
let classes = BTreeMap::from([
(
DEFAULT_LEGACY_ERC20_CONTRACT_CLASS_HASH,
GenesisClass {
Expand Down Expand Up @@ -315,7 +315,7 @@ impl Default for Genesis {

#[cfg(test)]
mod tests {
use std::str::FromStr;
use std::{collections::HashMap, str::FromStr};

use allocation::GenesisAccount;
use starknet::macros::felt;
Expand All @@ -326,7 +326,7 @@ mod tests {
fn genesis_block_and_state_updates() {
// setup initial states to test

let classes = HashMap::from([
let classes = BTreeMap::from([
(
DEFAULT_LEGACY_UDC_CLASS_HASH,
GenesisClass {
Expand Down
19 changes: 12 additions & 7 deletions crates/katana/primitives/src/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::collections::HashMap;
use std::collections::BTreeMap;
use std::collections::BTreeSet;

use crate::class::{ClassHash, CompiledClass, CompiledClassHash, FlattenedSierraClass};
use crate::contract::{ContractAddress, Nonce, StorageKey, StorageValue};
Expand All @@ -10,13 +11,17 @@ use crate::contract::{ContractAddress, Nonce, StorageKey, StorageValue};
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct StateUpdates {
/// A mapping of contract addresses to their updated nonces.
pub nonce_updates: HashMap<ContractAddress, Nonce>,
pub nonce_updates: BTreeMap<ContractAddress, Nonce>,
/// A mapping of contract addresses to their updated storage entries.
pub storage_updates: HashMap<ContractAddress, HashMap<StorageKey, StorageValue>>,
pub storage_updates: BTreeMap<ContractAddress, BTreeMap<StorageKey, StorageValue>>,
/// A mapping of contract addresses to their updated class hashes.
pub contract_updates: HashMap<ContractAddress, ClassHash>,
pub deployed_contracts: BTreeMap<ContractAddress, ClassHash>,
/// A mapping of newly declared class hashes to their compiled class hashes.
pub declared_classes: HashMap<ClassHash, CompiledClassHash>,
pub declared_classes: BTreeMap<ClassHash, CompiledClassHash>,
/// A mapping of newly declared legacy class hashes.
pub deprecated_declared_classes: BTreeSet<ClassHash>,
/// A mapping of replaced contract addresses to their new class hashes ie using `replace_class` syscall.
pub replaced_classes: BTreeMap<ContractAddress, ClassHash>,
}

/// State update with declared classes definition.
Expand All @@ -25,7 +30,7 @@ pub struct StateUpdatesWithDeclaredClasses {
/// State updates.
pub state_updates: StateUpdates,
/// A mapping of class hashes to their sierra classes definition.
pub declared_sierra_classes: HashMap<ClassHash, FlattenedSierraClass>,
pub declared_sierra_classes: BTreeMap<ClassHash, FlattenedSierraClass>,
/// A mapping of class hashes to their compiled classes definition.
pub declared_compiled_classes: HashMap<ClassHash, CompiledClass>,
pub declared_compiled_classes: BTreeMap<ClassHash, CompiledClass>,
}

0 comments on commit 6494abf

Please sign in to comment.