Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: add get_root_hash for filled tree #31

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ license = "Apache-2.0"
license-file = "LICENSE"

[workspace.dependencies]
derive_more = "0.99.17"
pretty_assertions = "1.2.1"
rstest = "0.17.0"
starknet-types-core = { version = "0.0.11", features = ["hash"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/committer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ pretty_assertions.workspace = true
rstest.workspace = true

[dependencies]
derive_more.workspace = true
starknet-types-core.workspace = true
derive_more = "0.99.17"
1 change: 1 addition & 0 deletions crates/committer/src/hash/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::types::Felt;
pub(crate) struct HashInputPair(pub Felt, pub Felt);

#[allow(dead_code)]
#[derive(Clone, Copy)]
pub(crate) struct HashOutput(pub Felt);

#[allow(dead_code)]
Expand Down
5 changes: 3 additions & 2 deletions crates/committer/src/patricia_merkle_tree/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ pub(crate) enum UpdatedSkeletonTreeError {
MissingNode,
}

#[derive(Debug)]
pub(crate) enum FilledTreeError {}
pub(crate) enum FilledTreeError {
MissingRoot,
}
2 changes: 1 addition & 1 deletion crates/committer/src/patricia_merkle_tree/filled_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub(crate) struct Nonce(pub Felt);
#[allow(dead_code)]
/// A node in a Patricia-Merkle tree which was modified during an update.
pub(crate) struct FilledNode<L: LeafDataTrait> {
hash: HashOutput,
pub(crate) hash: HashOutput,
data: NodeData<L>,
}

Expand Down
11 changes: 9 additions & 2 deletions crates/committer/src/patricia_merkle_tree/filled_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ use std::collections::HashMap;
use std::collections::HashSet;
use std::sync::Mutex;

use crate::hash::types::HashOutput;
use crate::patricia_merkle_tree::errors::FilledTreeError;
use crate::patricia_merkle_tree::filled_node::FilledNode;
use crate::patricia_merkle_tree::types::LeafDataTrait;
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::patricia_merkle_tree::types::{LeafDataTrait, NodeIndex};
use crate::storage::storage_trait::Storage;

/// Consider a Patricia-Merkle Tree which has been updated with new leaves.
Expand All @@ -29,4 +29,11 @@ impl<L: LeafDataTrait> FilledTreeImpl<L> {
fn get_all_nodes(&self) -> &HashMap<NodeIndex, Mutex<FilledNode<L>>> {
&self.tree_map
}

fn get_root_hash(&self) -> Result<HashOutput, FilledTreeError> {
match self.tree_map.get(&NodeIndex::root_index()) {
Some(root_node) => Ok(root_node.lock().expect("Lock poisoned.").hash),
None => Err(FilledTreeError::MissingRoot),
}
}
}
2 changes: 1 addition & 1 deletion crates/committer/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ impl From<StarknetTypesFelt> for Felt {

impl From<u128> for Felt {
fn from(value: u128) -> Self {
Self(StarknetTypesFelt::from(value))
Self(value.into())
}
}

Expand Down
Loading