@@ -11,50 +11,18 @@ use starknet_committer::db::facts_db::create_facts_tree::get_leaves;
1111use starknet_committer:: patricia_merkle_tree:: leaf:: leaf_impl:: ContractState ;
1212use starknet_committer:: patricia_merkle_tree:: tree:: fetch_previous_and_new_patricia_paths;
1313use 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 } ;
1418use starknet_patricia:: patricia_merkle_tree:: node_data:: inner_node:: flatten_preimages;
1519use starknet_patricia:: patricia_merkle_tree:: original_skeleton_tree:: errors:: OriginalSkeletonTreeError ;
1620use starknet_patricia:: patricia_merkle_tree:: traversal:: TraversalError ;
1721use starknet_patricia:: patricia_merkle_tree:: types:: { NodeIndex , SortedLeafIndices , SubTreeHeight } ;
1822use starknet_patricia_storage:: db_object:: EmptyKeyContext ;
1923use starknet_patricia_storage:: map_storage:: MapStorage ;
20- use starknet_types_core:: felt:: Felt ;
2124use 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 ) ]
6028pub 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.
0 commit comments