Skip to content

Commit

Permalink
chore(committer): add struct for contract state leaf input (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
amosStarkware authored Aug 20, 2024
1 parent 7f4e7b3 commit df672bc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
18 changes: 9 additions & 9 deletions crates/starknet_committer/src/forest/filled_forest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::HashMap;

use starknet_patricia::hash::hash_trait::HashOutput;
use starknet_patricia::patricia_merkle_tree::filled_tree::tree::FilledTree;
use starknet_patricia::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications};
use starknet_patricia::patricia_merkle_tree::node_data::leaf::LeafModifications;
use starknet_patricia::patricia_merkle_tree::types::NodeIndex;
use starknet_patricia::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTreeImpl;
use starknet_patricia::storage::storage_trait::Storage;
Expand All @@ -12,7 +12,7 @@ use crate::block_committer::input::{ContractAddress, StarknetStorageValue};
use crate::forest::forest_errors::{ForestError, ForestResult};
use crate::forest::updated_skeleton_forest::UpdatedSkeletonForest;
use crate::hash_function::hash::ForestHashFunction;
use crate::patricia_merkle_tree::leaf::leaf_impl::ContractState;
use crate::patricia_merkle_tree::leaf::leaf_impl::{ContractState, ContractStateInput};
use crate::patricia_merkle_tree::types::{
ClassHash,
ClassesTrie,
Expand Down Expand Up @@ -118,7 +118,7 @@ impl FilledForest {
mut contract_address_to_storage_skeleton: HashMap<ContractAddress, UpdatedSkeletonTreeImpl>,
address_to_class_hash: &HashMap<ContractAddress, ClassHash>,
address_to_nonce: &HashMap<ContractAddress, Nonce>,
) -> ForestResult<HashMap<NodeIndex, <ContractState as Leaf>::Input>> {
) -> ForestResult<HashMap<NodeIndex, ContractStateInput>> {
let mut leaf_index_to_leaf_input = HashMap::new();
assert_eq!(
contract_address_to_storage_updates.len(),
Expand All @@ -138,19 +138,19 @@ impl FilledForest {
.ok_or(ForestError::MissingContractCurrentState(contract_address))?;
leaf_index_to_leaf_input.insert(
node_index,
(
node_index,
*(address_to_nonce
ContractStateInput {
leaf_index: node_index,
nonce: *(address_to_nonce
.get(&contract_address)
.unwrap_or(&original_contract_state.nonce)),
*(address_to_class_hash
class_hash: *(address_to_class_hash
.get(&contract_address)
.unwrap_or(&original_contract_state.class_hash)),
contract_address_to_storage_skeleton
updated_skeleton: contract_address_to_storage_skeleton
.remove(&contract_address)
.ok_or(ForestError::MissingUpdatedSkeleton(contract_address))?,
storage_updates,
),
},
);
}
Ok(leaf_index_to_leaf_input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,7 @@ impl Leaf for CompiledClassHash {
}

impl Leaf for ContractState {
type Input = (
NodeIndex,
Nonce,
ClassHash,
UpdatedSkeletonTreeImpl,
LeafModifications<StarknetStorageValue>,
);
type Input = ContractStateInput;
type Output = FilledTreeImpl<StarknetStorageValue>;

fn is_empty(&self) -> bool {
Expand All @@ -60,11 +54,12 @@ impl Leaf for ContractState {
}

async fn create(input: Self::Input) -> LeafResult<(Self, Self::Output)> {
let (leaf_index, nonce, class_hash, updated_skeleton, storage_modifications) = input;
let ContractStateInput { leaf_index, nonce, class_hash, updated_skeleton, storage_updates } =
input;

let storage_trie = FilledTreeImpl::<StarknetStorageValue>::create_with_existing_leaves::<
TreeHashFunctionImpl,
>(updated_skeleton, storage_modifications)
>(updated_skeleton, storage_updates)
.await
.map_err(|storage_error| {
LeafError::LeafComputationError(format!(
Expand All @@ -79,3 +74,11 @@ impl Leaf for ContractState {
))
}
}

pub struct ContractStateInput {
pub leaf_index: NodeIndex,
pub nonce: Nonce,
pub class_hash: ClassHash,
pub updated_skeleton: UpdatedSkeletonTreeImpl,
pub storage_updates: LeafModifications<StarknetStorageValue>,
}

0 comments on commit df672bc

Please sign in to comment.