Skip to content

Commit

Permalink
build: impl mock leaf
Browse files Browse the repository at this point in the history
build: mock hash
  • Loading branch information
AvivYossef-starkware committed Jul 17, 2024
1 parent 0b1ec9d commit 6d9b95d
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
61 changes: 60 additions & 1 deletion crates/committer/src/patricia_merkle_tree/internal_test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,76 @@
use std::sync::Arc;

use crate::felt::Felt;
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::external_test_utils::get_random_u256;

use super::node_data::errors::LeafResult;
use super::node_data::leaf::{Leaf, LeafModifications};
use super::updated_skeleton_tree::hash_function::HashFunction;
use crate::generate_trie_config;
use crate::patricia_merkle_tree::node_data::inner_node::{EdgePathLength, PathToBottom};
use crate::patricia_merkle_tree::node_data::leaf::SkeletonLeaf;
use crate::patricia_merkle_tree::original_skeleton_tree::config::OriginalSkeletonTreeConfig;
use crate::patricia_merkle_tree::original_skeleton_tree::errors::OriginalSkeletonTreeError;
use crate::patricia_merkle_tree::original_skeleton_tree::node::OriginalSkeletonNode;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeResult;
use crate::patricia_merkle_tree::types::{NodeIndex, SubTreeHeight};
use crate::patricia_merkle_tree::updated_skeleton_tree::node::UpdatedSkeletonNode;
use crate::patricia_merkle_tree::updated_skeleton_tree::tree::UpdatedSkeletonTreeImpl;

use crate::storage::db_object::{DBObject, Deserializable};
use crate::storage::storage_trait::StorageValue;
use ethnum::U256;
use rand::rngs::ThreadRng;
use rstest::{fixture, rstest};

#[derive(Debug, PartialEq, Clone, Copy, Default, Eq)]
pub(crate) struct MockLeaf(pub(crate) Felt);

impl DBObject for MockLeaf {
fn serialize(&self) -> StorageValue {
StorageValue(self.0.to_bytes_be().to_vec())
}

fn get_prefix(&self) -> Vec<u8> {
vec![0]
}
}

impl Deserializable for MockLeaf {
fn deserialize(
value: &StorageValue,
) -> Result<Self, crate::storage::errors::DeserializationError> {
Ok(Self(Felt::from_bytes_be_slice(&value.0)))
}

fn prefix() -> Vec<u8> {
vec![0]
}
}

impl Leaf for MockLeaf {
fn is_empty(&self) -> bool {
self.0 == Felt::ZERO
}

async fn create(
index: &NodeIndex,
leaf_modifications: Arc<LeafModifications<Self>>,
) -> LeafResult<Self> {
Self::from_modifications(index, leaf_modifications)
}
}

generate_trie_config!(OriginalSkeletonMockTrieConfig, MockLeaf);

struct MockHashFunction;

impl HashFunction for MockHashFunction {
fn hash(left: &Felt, right: &Felt) -> HashOutput {
HashOutput(*left + *right)
}
}

impl From<u8> for SkeletonLeaf {
fn from(value: u8) -> Self {
Self::from(Felt::from(value))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pub(crate) trait OriginalSkeletonTreeConfig<L: Leaf> {
) -> OriginalSkeletonTreeResult<bool>;
}

#[macro_export]
macro_rules! generate_trie_config {
($struct_name:ident, $leaf_type:ty) => {
pub(crate) struct $struct_name<'a> {
Expand All @@ -27,6 +28,7 @@ macro_rules! generate_trie_config {
}

impl<'a> $struct_name<'a> {
#[allow(dead_code)]
pub(crate) fn new(
modifications: &'a LeafModifications<$leaf_type>,
compare_modified_leaves: bool,
Expand Down

0 comments on commit 6d9b95d

Please sign in to comment.