Skip to content

Commit 247fbd5

Browse files
starknet_committer,starknet_os,apollo_committer: relocate commitment infos to starknet_committer
1 parent e273e9a commit 247fbd5

8 files changed

Lines changed: 149 additions & 124 deletions

File tree

crates/apollo_batcher/src/commitment_manager/commitment_manager_impl.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,6 @@ impl<S: StateCommitterTrait> CommitmentManager<S> {
551551
COMMITMENT_MANAGER_REVERT_BLOCK_LATENCY.increment(task_duration);
552552
COMMITMENT_MANAGER_REVERT_BLOCK_COUNT.increment(1);
553553
}
554-
#[cfg(feature = "os_input")]
555554
CommitterRequestLabelValue::ReadPathsAndCommitBlock => {
556555
debug!(
557556
"Read paths and commit block latency for block {height}: {task_duration} \

crates/apollo_batcher/src/commitment_manager/types.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,6 @@ impl TaskTimer {
110110
CommitterRequestLabelValue::CommitBlock => {
111111
self.commit.insert(height, instant);
112112
}
113-
#[cfg(feature = "os_input")]
114113
CommitterRequestLabelValue::ReadPathsAndCommitBlock => {
115114
self.commit.insert(height, instant);
116115
}
@@ -128,7 +127,6 @@ impl TaskTimer {
128127
) -> Option<u128> {
129128
let map = match task {
130129
CommitterRequestLabelValue::CommitBlock => &mut self.commit,
131-
#[cfg(feature = "os_input")]
132130
CommitterRequestLabelValue::ReadPathsAndCommitBlock => &mut self.commit,
133131
CommitterRequestLabelValue::RevertBlock => &mut self.revert,
134132
};

crates/apollo_committer/src/committer.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -684,8 +684,12 @@ fn update_metrics(
684684
n_writes,
685685
durations,
686686
modifications_counts,
687+
// `fetched_witnesses_count` exists whenever `starknet_committer/os_input` is enabled, which
688+
// can happen independently of this crate's `os_input` (e.g. via `apollo_committer_types`).
689+
// `..` tolerates that mismatch; the field is still bound and used under our own `os_input`.
687690
#[cfg(feature = "os_input")]
688691
fetched_witnesses_count,
692+
..
689693
}: &BlockMeasurement,
690694
) {
691695
BLOCKS_COMMITTED.increment(1);

crates/apollo_committer_types/src/communication.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use async_trait::async_trait;
1313
#[cfg(any(feature = "testing", test))]
1414
use mockall::automock;
1515
use serde::{Deserialize, Serialize};
16-
use strum::{AsRefStr, EnumDiscriminants, EnumIter, IntoStaticStr, VariantNames};
16+
use strum::{AsRefStr, EnumIter, IntoStaticStr, VariantNames};
1717

1818
use crate::committer_types::{
1919
CommitBlockRequest,
@@ -55,19 +55,38 @@ pub trait CommitterClient: Send + Sync {
5555
) -> CommitterClientResult<ReadPathsAndCommitBlockResponse>;
5656
}
5757

58-
#[derive(Serialize, Deserialize, Clone, AsRefStr, EnumDiscriminants)]
59-
#[strum_discriminants(
60-
name(CommitterRequestLabelValue),
61-
derive(IntoStaticStr, EnumIter, VariantNames),
62-
strum(serialize_all = "snake_case")
63-
)]
58+
#[derive(Serialize, Deserialize, Clone, AsRefStr)]
6459
pub enum CommitterRequest {
6560
CommitBlock(CommitBlockRequest),
6661
RevertBlock(RevertBlockRequest),
6762
#[cfg(feature = "os_input")]
6863
ReadPathsAndCommitBlock(ReadPathsAndCommitBlockRequest),
6964
}
7065

66+
/// Payload-free discriminants of [`CommitterRequest`], used as a metric label. Independent of
67+
/// `os_input` on purpose: the label set is identical in every build, while the request variant and
68+
/// its payload stay feature-gated. Hand-written rather than derived via `EnumDiscriminants` so the
69+
/// `os_input`-only request variant does not gate the label out — otherwise every consumer would
70+
/// have to re-gate on its own `os_input`, which diverges across crates under `--all-features`.
71+
#[derive(Clone, Copy, Debug, PartialEq, Eq, IntoStaticStr, EnumIter, VariantNames)]
72+
#[strum(serialize_all = "snake_case")]
73+
pub enum CommitterRequestLabelValue {
74+
CommitBlock,
75+
RevertBlock,
76+
ReadPathsAndCommitBlock,
77+
}
78+
79+
impl From<&CommitterRequest> for CommitterRequestLabelValue {
80+
fn from(request: &CommitterRequest) -> Self {
81+
match request {
82+
CommitterRequest::CommitBlock(_) => Self::CommitBlock,
83+
CommitterRequest::RevertBlock(_) => Self::RevertBlock,
84+
#[cfg(feature = "os_input")]
85+
CommitterRequest::ReadPathsAndCommitBlock(_) => Self::ReadPathsAndCommitBlock,
86+
}
87+
}
88+
}
89+
7190
impl_debug_for_infra_requests_and_responses!(CommitterRequest);
7291
impl_labeled_request!(CommitterRequest, CommitterRequestLabelValue);
7392
impl PrioritizedRequest for CommitterRequest {}

crates/starknet_committer/src/patricia_merkle_tree/types.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use starknet_patricia::impl_from_hex_for_felt_wrapper;
77
use starknet_patricia::patricia_merkle_tree::filled_tree::tree::FilledTreeImpl;
88
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::PreimageMap;
99
use starknet_patricia::patricia_merkle_tree::node_data::leaf::SkeletonLeaf;
10-
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
10+
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SubTreeHeight};
1111
use starknet_types_core::felt::{Felt, FromStrError};
1212

1313
use crate::block_committer::input::{try_node_index_into_contract_address, StarknetStorageValue};
@@ -57,6 +57,40 @@ pub struct StarknetForestProofs {
5757
pub contracts_trie_storage_proofs: HashMap<ContractAddress, PreimageMap>,
5858
}
5959

60+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
61+
#[serde(deny_unknown_fields)]
62+
pub struct CommitmentInfo {
63+
pub previous_root: HashOutput,
64+
pub updated_root: HashOutput,
65+
pub tree_height: SubTreeHeight,
66+
// TODO(Dori, 1/8/2025): The value type here should probably be more specific (NodeData<L> for
67+
// L: Leaf). This poses a problem in deserialization, as a serialized edge node and a
68+
// serialized contract state leaf are both currently vectors of 3 field elements; as the
69+
// semantics of the values are unimportant for the OS commitments, we make do with a vector
70+
// of field elements as values for now.
71+
pub commitment_facts: HashMap<HashOutput, Vec<Felt>>,
72+
}
73+
74+
#[cfg(any(feature = "testing", test))]
75+
impl Default for CommitmentInfo {
76+
fn default() -> CommitmentInfo {
77+
CommitmentInfo {
78+
previous_root: HashOutput::default(),
79+
updated_root: HashOutput::default(),
80+
tree_height: SubTreeHeight::ACTUAL_HEIGHT,
81+
commitment_facts: HashMap::default(),
82+
}
83+
}
84+
}
85+
86+
/// Contains all commitment information for a block's state trees.
87+
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
88+
pub struct StateCommitmentInfos {
89+
pub contracts_trie_commitment_info: CommitmentInfo,
90+
pub classes_trie_commitment_info: CommitmentInfo,
91+
pub storage_tries_commitment_infos: HashMap<ContractAddress, CommitmentInfo>,
92+
}
93+
6094
impl StarknetForestProofs {
6195
pub fn build<Layout>(
6296
classes_trie_proof: PreimageMap,

crates/starknet_os/src/commitment_infos.rs

Lines changed: 76 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -11,50 +11,18 @@ use starknet_committer::db::facts_db::create_facts_tree::get_leaves;
1111
use starknet_committer::patricia_merkle_tree::leaf::leaf_impl::ContractState;
1212
use starknet_committer::patricia_merkle_tree::tree::fetch_previous_and_new_patricia_paths;
1313
use starknet_committer::patricia_merkle_tree::types::RootHashes;
14+
// `CommitmentInfo` and `StateCommitmentInfos` were relocated to `starknet_committer` so that
15+
// lower layers (e.g. `apollo_storage`) can store them without depending on `starknet_os`. They
16+
// are re-exported here to keep this module's public API stable for OS consumers.
17+
pub use starknet_committer::patricia_merkle_tree::types::{CommitmentInfo, StateCommitmentInfos};
1418
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::flatten_preimages;
1519
use starknet_patricia::patricia_merkle_tree::original_skeleton_tree::errors::OriginalSkeletonTreeError;
1620
use starknet_patricia::patricia_merkle_tree::traversal::TraversalError;
1721
use starknet_patricia::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices, SubTreeHeight};
1822
use starknet_patricia_storage::db_object::EmptyKeyContext;
1923
use starknet_patricia_storage::map_storage::MapStorage;
20-
use starknet_types_core::felt::Felt;
2124
use thiserror::Error;
2225

23-
#[cfg_attr(feature = "deserialize", derive(serde::Deserialize))]
24-
#[cfg_attr(feature = "deserialize", serde(deny_unknown_fields))]
25-
#[derive(Debug)]
26-
pub struct CommitmentInfo {
27-
pub previous_root: HashOutput,
28-
pub updated_root: HashOutput,
29-
pub tree_height: SubTreeHeight,
30-
// TODO(Dori, 1/8/2025): The value type here should probably be more specific (NodeData<L> for
31-
// L: Leaf). This poses a problem in deserialization, as a serialized edge node and a
32-
// serialized contract state leaf are both currently vectors of 3 field elements; as the
33-
// semantics of the values are unimportant for the OS commitments, we make do with a vector
34-
// of field elements as values for now.
35-
pub commitment_facts: HashMap<HashOutput, Vec<Felt>>,
36-
}
37-
38-
#[cfg(any(feature = "testing", test))]
39-
impl Default for CommitmentInfo {
40-
fn default() -> CommitmentInfo {
41-
CommitmentInfo {
42-
previous_root: HashOutput::default(),
43-
updated_root: HashOutput::default(),
44-
tree_height: SubTreeHeight::ACTUAL_HEIGHT,
45-
commitment_facts: HashMap::default(),
46-
}
47-
}
48-
}
49-
50-
// TODO(Aviv): Use this struct in `OsBlockInput`
51-
/// Contains all commitment information for a block's state trees.
52-
pub struct StateCommitmentInfos {
53-
pub contracts_trie_commitment_info: CommitmentInfo,
54-
pub classes_trie_commitment_info: CommitmentInfo,
55-
pub storage_tries_commitment_infos: HashMap<ContractAddress, CommitmentInfo>,
56-
}
57-
5826
/// Error type for commitment infos creation.
5927
#[derive(Debug, Error)]
6028
pub enum CommitmentInfosError {
@@ -66,83 +34,82 @@ pub enum CommitmentInfosError {
6634
FetchStorageProofs(#[from] TraversalError),
6735
}
6836

69-
impl StateCommitmentInfos {
70-
/// Creates the commitment infos for the OS from previous and new state roots and the
71-
/// keys that were read during execution.
72-
pub async fn new(
73-
previous_state_roots: &StateRoots,
74-
new_state_roots: &StateRoots,
75-
commitments: &mut MapStorage,
76-
accessed_keys: &AccessedKeys,
77-
) -> Result<Self, CommitmentInfosError> {
78-
let addresses: Vec<ContractAddress> =
79-
accessed_keys.accessed_contracts.iter().copied().collect();
80-
81-
let previous_storage_roots = get_storage_roots(
82-
&addresses,
83-
previous_state_roots.contracts_trie_root_hash,
84-
commitments,
85-
)
86-
.await?;
87-
let new_storage_roots =
88-
get_storage_roots(&addresses, new_state_roots.contracts_trie_root_hash, commitments)
89-
.await?;
37+
/// Creates the commitment infos for the OS from previous and new state roots and the
38+
/// keys that were read during execution.
39+
// TODO(ItamarS): Temporary — to be deleted once the committer builds `StateCommitmentInfos` from
40+
// its own storage; tests against that new committer API will be added then. Kept here (as a free
41+
// function rather than an inherent method) because the struct now lives in `starknet_committer`
42+
// and the orphan rule forbids an inherent impl on a foreign type.
43+
pub async fn build_state_commitment_infos(
44+
previous_state_roots: &StateRoots,
45+
new_state_roots: &StateRoots,
46+
commitments: &mut MapStorage,
47+
accessed_keys: &AccessedKeys,
48+
) -> Result<StateCommitmentInfos, CommitmentInfosError> {
49+
let addresses: Vec<ContractAddress> =
50+
accessed_keys.accessed_contracts.iter().copied().collect();
9051

91-
let storage_proofs = fetch_previous_and_new_patricia_paths(
92-
commitments,
93-
RootHashes {
94-
previous_root_hash: previous_state_roots.classes_trie_root_hash,
95-
new_root_hash: new_state_roots.classes_trie_root_hash,
96-
},
97-
RootHashes {
98-
previous_root_hash: previous_state_roots.contracts_trie_root_hash,
99-
new_root_hash: new_state_roots.contracts_trie_root_hash,
100-
},
101-
accessed_keys,
102-
)
103-
.await?;
52+
let previous_storage_roots =
53+
get_storage_roots(&addresses, previous_state_roots.contracts_trie_root_hash, commitments)
54+
.await?;
55+
let new_storage_roots =
56+
get_storage_roots(&addresses, new_state_roots.contracts_trie_root_hash, commitments)
57+
.await?;
10458

105-
let contracts_trie_commitment_info = CommitmentInfo {
106-
previous_root: previous_state_roots.contracts_trie_root_hash,
107-
updated_root: new_state_roots.contracts_trie_root_hash,
108-
tree_height: SubTreeHeight::ACTUAL_HEIGHT,
109-
commitment_facts: flatten_preimages(&storage_proofs.contracts_trie_proof.nodes),
110-
};
111-
let classes_trie_commitment_info = CommitmentInfo {
112-
previous_root: previous_state_roots.classes_trie_root_hash,
113-
updated_root: new_state_roots.classes_trie_root_hash,
114-
tree_height: SubTreeHeight::ACTUAL_HEIGHT,
115-
commitment_facts: flatten_preimages(&storage_proofs.classes_trie_proof),
116-
};
117-
let storage_tries_commitment_infos = previous_storage_roots
118-
.iter()
119-
.map(|(address, previous_root_hash)| {
120-
// Not all contracts in `previous_storage_roots` have storage proofs. For
121-
// example, a contract that only had its nonce changed.
122-
let storage_proof = flatten_preimages(
123-
storage_proofs
124-
.contracts_trie_storage_proofs
125-
.get(address)
126-
.unwrap_or(&HashMap::new()),
127-
);
128-
(
129-
*address,
130-
CommitmentInfo {
131-
previous_root: *previous_root_hash,
132-
updated_root: new_storage_roots[address],
133-
tree_height: SubTreeHeight::ACTUAL_HEIGHT,
134-
commitment_facts: storage_proof,
135-
},
136-
)
137-
})
138-
.collect();
59+
let storage_proofs = fetch_previous_and_new_patricia_paths(
60+
commitments,
61+
RootHashes {
62+
previous_root_hash: previous_state_roots.classes_trie_root_hash,
63+
new_root_hash: new_state_roots.classes_trie_root_hash,
64+
},
65+
RootHashes {
66+
previous_root_hash: previous_state_roots.contracts_trie_root_hash,
67+
new_root_hash: new_state_roots.contracts_trie_root_hash,
68+
},
69+
accessed_keys,
70+
)
71+
.await?;
13972

140-
Ok(Self {
141-
contracts_trie_commitment_info,
142-
classes_trie_commitment_info,
143-
storage_tries_commitment_infos,
73+
let contracts_trie_commitment_info = CommitmentInfo {
74+
previous_root: previous_state_roots.contracts_trie_root_hash,
75+
updated_root: new_state_roots.contracts_trie_root_hash,
76+
tree_height: SubTreeHeight::ACTUAL_HEIGHT,
77+
commitment_facts: flatten_preimages(&storage_proofs.contracts_trie_proof.nodes),
78+
};
79+
let classes_trie_commitment_info = CommitmentInfo {
80+
previous_root: previous_state_roots.classes_trie_root_hash,
81+
updated_root: new_state_roots.classes_trie_root_hash,
82+
tree_height: SubTreeHeight::ACTUAL_HEIGHT,
83+
commitment_facts: flatten_preimages(&storage_proofs.classes_trie_proof),
84+
};
85+
let storage_tries_commitment_infos = previous_storage_roots
86+
.iter()
87+
.map(|(address, previous_root_hash)| {
88+
// Not all contracts in `previous_storage_roots` have storage proofs. For
89+
// example, a contract that only had its nonce changed.
90+
let storage_proof = flatten_preimages(
91+
storage_proofs
92+
.contracts_trie_storage_proofs
93+
.get(address)
94+
.unwrap_or(&HashMap::new()),
95+
);
96+
(
97+
*address,
98+
CommitmentInfo {
99+
previous_root: *previous_root_hash,
100+
updated_root: new_storage_roots[address],
101+
tree_height: SubTreeHeight::ACTUAL_HEIGHT,
102+
commitment_facts: storage_proof,
103+
},
104+
)
144105
})
145-
}
106+
.collect();
107+
108+
Ok(StateCommitmentInfos {
109+
contracts_trie_commitment_info,
110+
classes_trie_commitment_info,
111+
storage_tries_commitment_infos,
112+
})
146113
}
147114

148115
/// Fetches the storage root hash of each contract from the contracts trie at the given root.

crates/starknet_os_flow_tests/src/test_manager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ use starknet_committer::block_committer::input::{
6565
};
6666
use starknet_committer::db::facts_db::FactsDb;
6767
use starknet_committer::db::forest_trait::StorageInitializer;
68-
use starknet_os::commitment_infos::StateCommitmentInfos;
68+
use starknet_os::commitment_infos::build_state_commitment_infos;
6969
use starknet_os::hints::hint_implementation::state_diff_encryption::utils::compute_public_keys;
7070
use starknet_os::io::os_input::{OsBlockInput, OsHints, OsHintsConfig, StarknetOsInput};
7171
use starknet_os::io::os_output::{MessageToL2, OsStateDiff, StarknetOsRunnerOutput};
@@ -940,7 +940,7 @@ impl<S: FlowTestState> TestBuilder<S> {
940940
.expect("Failed to commit state diff.");
941941
map_storage = db.consume_storage();
942942

943-
let commitment_infos = StateCommitmentInfos::new(
943+
let commitment_infos = build_state_commitment_infos(
944944
&previous_state_roots,
945945
&new_state_roots,
946946
&mut map_storage,

crates/starknet_transaction_prover/src/running/storage_proofs.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ use serde::{Deserialize, Serialize};
88
use starknet_api::block::BlockNumber;
99
use starknet_api::core::{ClassHash, ContractAddress, Nonce};
1010
use starknet_api::hash::{HashOutput, StateRoots};
11-
use starknet_os::commitment_infos::{CommitmentInfo, StateCommitmentInfos};
11+
use starknet_os::commitment_infos::{
12+
build_state_commitment_infos,
13+
CommitmentInfo,
14+
StateCommitmentInfos,
15+
};
1216
use starknet_patricia::patricia_merkle_tree::node_data::inner_node::{
1317
flatten_preimages,
1418
Preimage,
@@ -348,7 +352,7 @@ impl RpcStorageProofsProvider {
348352

349353
// TODO(Aviv): Try to undertand if we can create classes trie commitment info
350354
// without the compiled class hashes.
351-
let mut commitment_infos = StateCommitmentInfos::new(
355+
let mut commitment_infos = build_state_commitment_infos(
352356
&previous_state_roots,
353357
&new_roots,
354358
&mut map_storage,

0 commit comments

Comments
 (0)