Skip to content

Commit

Permalink
feat(starknet_os,starknet_patricia,starknet_committer): deserializabl…
Browse files Browse the repository at this point in the history
…e commitment info
  • Loading branch information
dorimedini-starkware committed Feb 5, 2025
1 parent 1819f6e commit 26bbea3
Show file tree
Hide file tree
Showing 12 changed files with 54 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,7 @@ starknet_mempool_p2p = { path = "crates/starknet_mempool_p2p", version = "0.0.0"
starknet_mempool_p2p_types = { path = "crates/starknet_mempool_p2p_types", version = "0.0.0" }
starknet_mempool_types = { path = "crates/starknet_mempool_types", version = "0.0.0" }
starknet_monitoring_endpoint = { path = "crates/starknet_monitoring_endpoint", version = "0.0.0" }
starknet_os = { path = "crates/starknet_os", version = "0.0.0" }
starknet_patricia = { path = "crates/starknet_patricia", version = "0.0.0" }
starknet_sequencer_infra = { path = "crates/starknet_sequencer_infra", version = "0.0.0" }
starknet_sequencer_metrics = { path = "crates/starknet_sequencer_metrics", version = "0.0.0" }
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_committer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ repository.workspace = true
license.workspace = true
description = "Computes and manages Starknet state."

[features]
deserialize = ["serde", "starknet_patricia/deserialize"]

[dependencies]
hex.workspace = true
pretty_assertions.workspace = true
rstest.workspace = true
serde = { workspace = true, optional = true, features = ["derive"] }
serde_json.workspace = true
starknet-types-core = { workspace = true, features = ["hash"] }
starknet_patricia = { workspace = true, features = ["testing"] }
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_committer/src/block_committer/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ impl From<&StarknetStorageKey> for NodeIndex {
}
}

#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Default, Debug, Eq, PartialEq)]
pub struct StarknetStorageValue(pub Felt);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::block_committer::input::StarknetStorageValue;
use crate::hash_function::hash::TreeHashFunctionImpl;
use crate::patricia_merkle_tree::types::{ClassHash, CompiledClassHash, Nonce};

#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ContractState {
pub nonce: Nonce,
Expand Down
3 changes: 3 additions & 0 deletions crates/starknet_committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use crate::block_committer::input::{ContractAddress, StarknetStorageValue};
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;

// TODO(Nimrod, 1/6/2024): Use the ClassHash defined in starknet-types-core when available.
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct ClassHash(pub Felt);

Expand All @@ -20,11 +21,13 @@ impl From<&ClassHash> for NodeIndex {
}

impl_from_hex_for_felt_wrapper!(ClassHash);
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Nonce(pub Felt);

impl_from_hex_for_felt_wrapper!(Nonce);

#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct CompiledClassHash(pub Felt);

Expand Down
4 changes: 4 additions & 0 deletions crates/starknet_os/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ license-file.workspace = true
description = "The Starknet OS."

[features]
deserialize = ["starknet_committer/deserialize", "starknet_patricia/deserialize"]
testing = ["dep:strum", "dep:strum_macros"]

[dependencies]
blockifier.workspace = true
cairo-vm.workspace = true
indoc.workspace = true
serde.workspace = true
starknet-types-core.workspace = true
starknet_api.workspace = true
starknet_committer.workspace = true
starknet_patricia.workspace = true
strum = { workspace = true, optional = true }
strum_macros = { workspace = true, optional = true }
thiserror.workspace = true
Expand Down
25 changes: 24 additions & 1 deletion crates/starknet_os/src/io/os_input.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,27 @@
use std::collections::HashMap;

use starknet_committer::block_committer::input::StarknetStorageValue;
use starknet_committer::patricia_merkle_tree::leaf::leaf_impl::ContractState;
use starknet_committer::patricia_merkle_tree::types::CompiledClassHash;
use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::NodeData;
use starknet_patricia::patricia_merkle_tree::node_data::leaf::Leaf;
use starknet_patricia::patricia_merkle_tree::types::SubTreeHeight;

#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
pub struct CommitmentInfo<L: Leaf> {
_previous_root: HashOutput,
_updated_root: HashOutput,
_tree_height: SubTreeHeight,
_commitment_facts: HashMap<HashOutput, NodeData<L>>,
}

/// All input needed to initialize the execution helper.
// TODO(Dori): Add all fields needed to compute commitments, initialize a CachedState and other data
// required by the execution helper.
pub struct StarknetOsInput {}
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
pub struct StarknetOsInput {
_contract_commitments: CommitmentInfo<ContractState>,
_storage_commitments: CommitmentInfo<StarknetStorageValue>,
_class_commitments: CommitmentInfo<CompiledClassHash>,
}
1 change: 1 addition & 0 deletions crates/starknet_patricia/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ description = "Library for computing and updating Patricia trees."
workspace = true

[features]
deserialize = ["ethnum/serde"]
testing = []

[dev-dependencies]
Expand Down
1 change: 1 addition & 0 deletions crates/starknet_patricia/src/hash/hash_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use starknet_types_core::felt::FromStrError;
use crate::felt::Felt;
use crate::impl_from_hex_for_felt_wrapper;

#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct HashOutput(pub Felt);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::patricia_merkle_tree::types::{NodeIndex, SubTreeHeight};
pub mod inner_node_test;

#[derive(Clone, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[cfg_attr(any(test, feature = "testing"), derive(strum_macros::EnumDiscriminants))]
#[cfg_attr(any(test, feature = "testing"), strum_discriminants(derive(strum_macros::EnumIter)))]
// A Patricia-Merkle tree node's data, i.e., the pre-image of its hash.
Expand All @@ -20,13 +21,15 @@ pub enum NodeData<L: Leaf> {
Leaf(L),
}

#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct BinaryData {
pub left_hash: HashOutput,
pub right_hash: HashOutput,
}

// Wraps a U256. Maximal possible value is the longest path in a tree of height 251 (2 ^ 251 - 1).
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct EdgePath(pub U256);

Expand Down Expand Up @@ -63,6 +66,7 @@ impl From<&EdgePath> for U256 {
path.0
}
}
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(
Clone, Copy, Debug, Default, PartialOrd, derive_more::Add, derive_more::Sub, PartialEq, Eq, Hash,
)]
Expand Down Expand Up @@ -94,6 +98,7 @@ impl From<EdgePathLength> for Felt {
}

#[allow(clippy::manual_non_exhaustive)]
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct PathToBottom {
pub path: EdgePath,
Expand All @@ -116,6 +121,7 @@ impl PathToBottom {
}
}

#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct EdgeData {
pub bottom_hash: HashOutput,
Expand Down
2 changes: 2 additions & 0 deletions crates/starknet_patricia/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::patricia_merkle_tree::node_data::inner_node::{EdgePathLength, PathToB
#[path = "types_test.rs"]
pub mod types_test;

#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(Clone, Copy, Debug, Eq, PartialEq, derive_more::Sub, derive_more::Display)]
pub struct SubTreeHeight(pub(crate) u8);

Expand All @@ -28,6 +29,7 @@ impl From<SubTreeHeight> for u8 {
}
}

#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
#[derive(
Clone, Copy, Debug, PartialEq, Eq, Hash, derive_more::BitAnd, derive_more::Sub, PartialOrd, Ord,
)]
Expand Down

0 comments on commit 26bbea3

Please sign in to comment.