From f036d8cb52159c7de3db24114e53caf7ea3192f5 Mon Sep 17 00:00:00 2001 From: Aner Ben Efraim Date: Tue, 9 Apr 2024 22:42:30 +0300 Subject: [PATCH] build: add get_root_hash for filled tree --- Cargo.toml | 1 + crates/committer/Cargo.toml | 2 +- crates/committer/src/hash/types.rs | 1 + crates/committer/src/patricia_merkle_tree/errors.rs | 5 +++-- .../committer/src/patricia_merkle_tree/filled_node.rs | 2 +- .../committer/src/patricia_merkle_tree/filled_tree.rs | 11 +++++++++-- crates/committer/src/types.rs | 2 +- 7 files changed, 17 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1b369bbc..7af53a3b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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"] } diff --git a/crates/committer/Cargo.toml b/crates/committer/Cargo.toml index 89e19185..4e0faa63 100644 --- a/crates/committer/Cargo.toml +++ b/crates/committer/Cargo.toml @@ -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" diff --git a/crates/committer/src/hash/types.rs b/crates/committer/src/hash/types.rs index f80bf707..f64744b1 100644 --- a/crates/committer/src/hash/types.rs +++ b/crates/committer/src/hash/types.rs @@ -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)] diff --git a/crates/committer/src/patricia_merkle_tree/errors.rs b/crates/committer/src/patricia_merkle_tree/errors.rs index 7f4cdd89..bb0ef7d3 100644 --- a/crates/committer/src/patricia_merkle_tree/errors.rs +++ b/crates/committer/src/patricia_merkle_tree/errors.rs @@ -7,5 +7,6 @@ pub(crate) enum UpdatedSkeletonTreeError { MissingNode, } -#[derive(Debug)] -pub(crate) enum FilledTreeError {} +pub(crate) enum FilledTreeError { + MissingRoot, +} diff --git a/crates/committer/src/patricia_merkle_tree/filled_node.rs b/crates/committer/src/patricia_merkle_tree/filled_node.rs index 6cd72000..e82d2693 100644 --- a/crates/committer/src/patricia_merkle_tree/filled_node.rs +++ b/crates/committer/src/patricia_merkle_tree/filled_node.rs @@ -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 { - hash: HashOutput, + pub(crate) hash: HashOutput, data: NodeData, } diff --git a/crates/committer/src/patricia_merkle_tree/filled_tree.rs b/crates/committer/src/patricia_merkle_tree/filled_tree.rs index 612b5ddd..e01a238e 100644 --- a/crates/committer/src/patricia_merkle_tree/filled_tree.rs +++ b/crates/committer/src/patricia_merkle_tree/filled_tree.rs @@ -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. @@ -29,4 +29,11 @@ impl FilledTreeImpl { fn get_all_nodes(&self) -> &HashMap>> { &self.tree_map } + + fn get_root_hash(&self) -> Result { + match self.tree_map.get(&NodeIndex::root_index()) { + Some(root_node) => Ok(root_node.lock().expect("Lock poisoned.").hash), + None => Err(FilledTreeError::MissingRoot), + } + } } diff --git a/crates/committer/src/types.rs b/crates/committer/src/types.rs index 2f28a3bb..6f1de664 100644 --- a/crates/committer/src/types.rs +++ b/crates/committer/src/types.rs @@ -11,7 +11,7 @@ impl From for Felt { impl From for Felt { fn from(value: u128) -> Self { - Self(StarknetTypesFelt::from(value)) + Self(value.into()) } }