Skip to content

Commit 00eddd6

Browse files
starknet_committer: flush siblings into storage
1 parent 14998f9 commit 00eddd6

File tree

4 files changed

+40
-26
lines changed

4 files changed

+40
-26
lines changed

crates/starknet_committer/src/db/facts_db/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ impl<S: Storage> ForestReader for FactsDb<S> {
113113
config: ReaderConfig,
114114
) -> ForestResult<(OriginalSkeletonForest<'a>, HashMap<NodeIndex, ContractState>)> {
115115
read_forest::<S, FactsNodeLayout>(
116-
&self.storage,
116+
&mut self.storage,
117117
roots,
118118
storage_updates,
119119
classes_updates,

crates/starknet_committer/src/db/forest_trait.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ pub trait ForestReader {
8787

8888
/// Helper function containing layout-common read logic.
8989
pub(crate) async fn read_forest<'a, S, Layout>(
90-
storage: &S,
90+
storage: &mut S,
9191
roots: StateRoots,
9292
storage_updates: &'a HashMap<ContractAddress, LeafModifications<StarknetStorageValue>>,
9393
classes_updates: &'a LeafModifications<CompiledClassHash>,
@@ -98,22 +98,22 @@ where
9898
S: Storage,
9999
Layout: DbLayout,
100100
{
101-
let (contracts_trie, original_contracts_trie_leaves) =
101+
let (contracts_trie, original_contracts_trie_leaves, contracts_trie_siblings) =
102102
create_contracts_trie::<Layout::NodeLayout>(
103103
storage,
104104
roots.contracts_trie_root_hash,
105105
forest_sorted_indices.contracts_trie_sorted_indices,
106106
)
107107
.await?;
108-
let (storage_tries, _storage_tries_siblings) = create_storage_tries::<Layout::NodeLayout>(
108+
let (storage_tries, storage_tries_siblings) = create_storage_tries::<Layout::NodeLayout>(
109109
storage,
110110
storage_updates,
111111
&original_contracts_trie_leaves,
112112
&config,
113113
&forest_sorted_indices.storage_tries_sorted_indices,
114114
)
115115
.await?;
116-
let classes_trie = create_classes_trie::<Layout::NodeLayout>(
116+
let (classes_trie, classes_trie_siblings) = create_classes_trie::<Layout::NodeLayout>(
117117
storage,
118118
classes_updates,
119119
roots.classes_trie_root_hash,
@@ -122,6 +122,15 @@ where
122122
)
123123
.await?;
124124

125+
// After all siblings are collected - flush them to storage.
126+
let all_siblings = contracts_trie_siblings
127+
.into_iter()
128+
.chain(storage_tries_siblings.into_iter())
129+
.chain(classes_trie_siblings.into_iter())
130+
.collect();
131+
132+
storage.flush_to_cache(all_siblings)?;
133+
125134
Ok((
126135
OriginalSkeletonForest { classes_trie, contracts_trie, storage_tries },
127136
original_contracts_trie_leaves,

crates/starknet_committer/src/db/index_db/db.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<S: Storage> ForestReader for IndexDb<S> {
183183
config: ReaderConfig,
184184
) -> ForestResult<(OriginalSkeletonForest<'a>, HashMap<NodeIndex, ContractState>)> {
185185
read_forest::<S, IndexNodeLayout>(
186-
&self.storage,
186+
&mut self.storage,
187187
roots,
188188
storage_updates,
189189
classes_updates,

crates/starknet_committer/src/db/trie_traversal.rs

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -393,29 +393,30 @@ pub async fn create_contracts_trie<'a, Layout: NodeLayoutFor<ContractState>>(
393393
storage: &impl Storage,
394394
contracts_trie_root_hash: HashOutput,
395395
contracts_trie_sorted_indices: SortedLeafIndices<'a>,
396-
) -> ForestResult<(OriginalSkeletonTreeImpl<'a>, HashMap<NodeIndex, ContractState>)>
396+
) -> ForestResult<(OriginalSkeletonTreeImpl<'a>, HashMap<NodeIndex, ContractState>, DbHashMap)>
397397
where
398398
<Layout as NodeLayoutFor<ContractState>>::DbLeaf: HasStaticPrefix<KeyContext = EmptyKeyContext>,
399399
{
400400
let config = OriginalSkeletonTrieConfig::new_for_contracts_trie();
401401

402402
let mut leaves = HashMap::new();
403+
let mut siblings_map = DbHashMap::new();
403404
let skeleton_tree = create_original_skeleton_tree::<Layout::DbLeaf, Layout>(
404405
storage,
405406
contracts_trie_root_hash,
406407
contracts_trie_sorted_indices,
407408
&config,
408409
&HashMap::new(),
409410
Some(&mut leaves),
410-
None,
411+
Some(&mut siblings_map),
411412
&EmptyKeyContext,
412413
)
413414
.await?;
414415

415416
let leaves: HashMap<NodeIndex, ContractState> =
416417
leaves.into_iter().map(|(idx, leaf)| (idx, leaf.into())).collect();
417418

418-
Ok((skeleton_tree, leaves))
419+
Ok((skeleton_tree, leaves, siblings_map))
419420
}
420421

421422
pub async fn create_classes_trie<'a, Layout: NodeLayoutFor<CompiledClassHash>>(
@@ -424,31 +425,35 @@ pub async fn create_classes_trie<'a, Layout: NodeLayoutFor<CompiledClassHash>>(
424425
classes_trie_root_hash: HashOutput,
425426
config: &ReaderConfig,
426427
contracts_trie_sorted_indices: SortedLeafIndices<'a>,
427-
) -> ForestResult<OriginalSkeletonTreeImpl<'a>>
428+
) -> ForestResult<(OriginalSkeletonTreeImpl<'a>, DbHashMap)>
428429
where
429430
<Layout as NodeLayoutFor<CompiledClassHash>>::DbLeaf:
430431
HasStaticPrefix<KeyContext = EmptyKeyContext>,
431432
{
432433
let config = OriginalSkeletonTrieConfig::new_for_classes_or_storage_trie(
433434
config.warn_on_trivial_modifications(),
434435
);
436+
let mut siblings_map = DbHashMap::new();
435437

436-
Ok(create_original_skeleton_tree::<Layout::DbLeaf, Layout>(
437-
storage,
438-
classes_trie_root_hash,
439-
contracts_trie_sorted_indices,
440-
&config,
441-
// TODO(Ariel): Change `actual_classes_updates` to be an iterator over borrowed data so
442-
// that the conversion below is costless.
443-
&actual_classes_updates
444-
.iter()
445-
.map(|(idx, value)| (*idx, Layout::DbLeaf::from(*value)))
446-
.collect(),
447-
None,
448-
None,
449-
&EmptyKeyContext,
450-
)
451-
.await?)
438+
Ok((
439+
create_original_skeleton_tree::<Layout::DbLeaf, Layout>(
440+
storage,
441+
classes_trie_root_hash,
442+
contracts_trie_sorted_indices,
443+
&config,
444+
// TODO(Ariel): Change `actual_classes_updates` to be an iterator over borrowed data so
445+
// that the conversion below is costless.
446+
&actual_classes_updates
447+
.iter()
448+
.map(|(idx, value)| (*idx, Layout::DbLeaf::from(*value)))
449+
.collect(),
450+
None,
451+
Some(&mut siblings_map),
452+
&EmptyKeyContext,
453+
)
454+
.await?,
455+
siblings_map,
456+
))
452457
}
453458

454459
async fn create_storage_tries_sequentially<'a, Layout: NodeLayoutFor<StarknetStorageValue>>(

0 commit comments

Comments
 (0)