Skip to content

Commit c31ac25

Browse files
starknet_committer: remove code dup for single trie
1 parent 02b30b6 commit c31ac25

File tree

1 file changed

+62
-50
lines changed

1 file changed

+62
-50
lines changed

crates/starknet_committer/src/db/trie_traversal.rs

Lines changed: 62 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -434,29 +434,17 @@ where
434434
{
435435
let mut storage_tries = HashMap::new();
436436
for (address, updates) in actual_storage_updates {
437-
let sorted_leaf_indices = storage_tries_sorted_indices
438-
.get(address)
439-
.ok_or(ForestError::MissingSortedLeafIndices(*address))?;
440-
let contract_state = original_contracts_trie_leaves
441-
.get(&contract_address_into_node_index(address))
442-
.ok_or(ForestError::MissingContractCurrentState(*address))?;
443-
let config = OriginalSkeletonTrieConfig::new_for_classes_or_storage_trie(
444-
config.warn_on_trivial_modifications(),
445-
);
446-
447-
let original_skeleton = create_original_skeleton_tree::<Layout::DbLeaf, Layout>(
437+
let (address, original_skeleton) = create_storage_trie::<Layout>(
448438
storage,
449-
contract_state.storage_root_hash,
450-
*sorted_leaf_indices,
451-
&config,
452-
// TODO(Ariel): Change `LeafModifications` in `actual_storage_updates` to be an
453-
// iterator over borrowed data so that the conversion below is costless.
454-
&updates.iter().map(|(idx, value)| (*idx, Layout::DbLeaf::from(*value))).collect(),
455-
None,
456-
address,
439+
*address,
440+
updates,
441+
original_contracts_trie_leaves,
442+
storage_tries_sorted_indices,
443+
config.warn_on_trivial_modifications(),
457444
)
458445
.await?;
459-
storage_tries.insert(*address, original_skeleton);
446+
447+
storage_tries.insert(address, original_skeleton);
460448
}
461449
Ok(storage_tries)
462450
}
@@ -477,48 +465,72 @@ where
477465
let mut futures = FuturesUnordered::new();
478466
let mut storage_tries = HashMap::new();
479467

480-
let trie_config = OriginalSkeletonTrieConfig::new_for_classes_or_storage_trie(
481-
config.warn_on_trivial_modifications(),
482-
);
483468
for (address, updates) in actual_storage_updates {
484-
// Extract data needed for this contract.
485-
let sorted_leaf_indices = *storage_tries_sorted_indices
486-
.get(address)
487-
.ok_or(ForestError::MissingSortedLeafIndices(*address))?;
488-
let contract_state = original_contracts_trie_leaves
489-
.get(&contract_address_into_node_index(address))
490-
.ok_or(ForestError::MissingContractCurrentState(*address))?;
491-
let cloned_trie_config = trie_config.clone();
492-
493-
// TODO(Ariel): Change `LeafModifications` in `actual_storage_updates` to be an
494-
// iterator over borrowed data so that the conversion below is costless.
495-
let leaf_modifications: HashMap<
496-
NodeIndex,
497-
<Layout as NodeLayoutFor<StarknetStorageValue>>::DbLeaf,
498-
> = updates.iter().map(|(idx, value)| (*idx, Layout::DbLeaf::from(*value))).collect();
499-
500469
// Create the future - tokio will poll all futures concurrently.
501470
futures.push(async move {
502-
let previous_leaves = None;
503-
let original_skeleton = create_original_skeleton_tree::<Layout::DbLeaf, Layout>(
471+
create_storage_trie::<Layout>(
504472
storage,
505-
contract_state.storage_root_hash,
506-
sorted_leaf_indices,
507-
&cloned_trie_config,
508-
&leaf_modifications,
509-
previous_leaves,
510-
address,
473+
*address,
474+
updates,
475+
original_contracts_trie_leaves,
476+
storage_tries_sorted_indices,
477+
config.warn_on_trivial_modifications(),
511478
)
512-
.await?;
513-
Ok::<_, ForestError>((address, original_skeleton))
479+
.await
514480
});
515481
}
516482

517483
// Collect all results as they complete.
518484
while let Some(result) = futures.next().await {
519485
let (address, original_skeleton) = result?;
520-
storage_tries.insert(*address, original_skeleton);
486+
storage_tries.insert(address, original_skeleton);
521487
}
522488

523489
Ok(storage_tries)
524490
}
491+
492+
/// Helper function to create a storage trie for a single contract.
493+
async fn create_storage_trie<'a, Layout: NodeLayoutFor<StarknetStorageValue>>(
494+
storage: &impl Storage,
495+
address: ContractAddress,
496+
updates: &LeafModifications<StarknetStorageValue>,
497+
original_contracts_trie_leaves: &HashMap<NodeIndex, ContractState>,
498+
storage_tries_sorted_indices: &HashMap<ContractAddress, SortedLeafIndices<'a>>,
499+
warn_on_trivial_modifications: bool,
500+
) -> ForestResult<(ContractAddress, OriginalSkeletonTreeImpl<'a>)>
501+
where
502+
<Layout as NodeLayoutFor<StarknetStorageValue>>::DbLeaf:
503+
HasStaticPrefix<KeyContext = ContractAddress>,
504+
{
505+
// Extract data needed for this contract.
506+
let sorted_leaf_indices = *storage_tries_sorted_indices
507+
.get(&address)
508+
.ok_or(ForestError::MissingSortedLeafIndices(address))?;
509+
let contract_state = original_contracts_trie_leaves
510+
.get(&contract_address_into_node_index(&address))
511+
.ok_or(ForestError::MissingContractCurrentState(address))?;
512+
513+
let trie_config =
514+
OriginalSkeletonTrieConfig::new_for_classes_or_storage_trie(warn_on_trivial_modifications);
515+
516+
// TODO(Ariel): Change `LeafModifications` in `actual_storage_updates` to be an
517+
// iterator over borrowed data so that the conversion below is costless.
518+
let leaf_modifications: HashMap<
519+
NodeIndex,
520+
<Layout as NodeLayoutFor<StarknetStorageValue>>::DbLeaf,
521+
> = updates.iter().map(|(idx, value)| (*idx, Layout::DbLeaf::from(*value))).collect();
522+
523+
let previous_leaves = None;
524+
let original_skeleton = create_original_skeleton_tree::<Layout::DbLeaf, Layout>(
525+
storage,
526+
contract_state.storage_root_hash,
527+
sorted_leaf_indices,
528+
&trie_config,
529+
&leaf_modifications,
530+
previous_leaves,
531+
&address,
532+
)
533+
.await?;
534+
535+
Ok((address, original_skeleton))
536+
}

0 commit comments

Comments
 (0)