Skip to content

feat: move leaf fetching logic to leaf data trait #160

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

Merged
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use tokio::task::JoinError;

use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
use crate::patricia_merkle_tree::node_data::errors::LeafError;
use crate::patricia_merkle_tree::updated_skeleton_tree::errors::UpdatedSkeletonTreeError;
use crate::patricia_merkle_tree::{node_data::leaf::LeafData, types::NodeIndex};

Expand All @@ -13,8 +14,8 @@ pub enum FilledTreeError<L: LeafData> {
index: NodeIndex,
existing_value: Box<FilledNode<L>>,
},
#[error("Missing modification data at index {0:?}.")]
MissingDataForUpdate(NodeIndex),
#[error(transparent)]
Leaf(#[from] LeafError),
#[error("Missing node at index {0:?}.")]
MissingNode(NodeIndex),
#[error("Missing root.")]
Expand Down
9 changes: 4 additions & 5 deletions crates/committer/src/patricia_merkle_tree/filled_tree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,11 @@ impl FilledTreeImpl {
UpdatedSkeletonNode::Sibling(hash_result)
| UpdatedSkeletonNode::UnmodifiedBottom(hash_result) => Ok(*hash_result),
UpdatedSkeletonNode::Leaf => {
let leaf_data = leaf_modifications
.get(&index)
.ok_or(FilledTreeError::<LeafDataImpl>::MissingDataForUpdate(index))?
.clone();
let leaf_data = LeafDataImpl::create(&index, leaf_modifications).await?;
if leaf_data.is_empty() {
return Err(FilledTreeError::DeletedLeafInSkeleton(index));
return Err(FilledTreeError::<LeafDataImpl>::DeletedLeafInSkeleton(
index,
));
}
let node_data = NodeData::Leaf(leaf_data);
let hash_value = TH::compute_node_hash(&node_data);
Expand Down
9 changes: 9 additions & 0 deletions crates/committer/src/patricia_merkle_tree/node_data/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt::Debug;
use thiserror::Error;

use crate::patricia_merkle_tree::node_data::inner_node::EdgePathLength;
use crate::patricia_merkle_tree::types::NodeIndex;

#[derive(Debug, Error)]
pub enum PathToBottomError {
Expand All @@ -11,3 +12,11 @@ pub enum PathToBottomError {
n_edges: EdgePathLength,
},
}

#[derive(Debug, Error)]
pub enum LeafError {
#[error("Missing modification data at index {0:?}.")]
MissingLeafModificationData(NodeIndex),
}

pub type LeafResult<T> = Result<T, LeafError>;
24 changes: 24 additions & 0 deletions crates/committer/src/patricia_merkle_tree/node_data/leaf.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,29 @@
use std::collections::HashMap;
use std::future::Future;
use std::sync::Arc;

use crate::felt::Felt;
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::filled_tree::node::{ClassHash, CompiledClassHash, Nonce};
use crate::patricia_merkle_tree::node_data::errors::{LeafError, LeafResult};
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::storage::db_object::DBObject;
use strum_macros::{EnumDiscriminants, EnumIter};

pub trait LeafData: Clone + Sync + Send + DBObject {
/// Returns true if leaf is empty.
fn is_empty(&self) -> bool;

/// Creates a leaf.
// Use explicit desugaring of `async fn` to allow adding trait bounds to the return type, see
// https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html#async-fn-in-public-traits
// for details.
fn create(
index: &NodeIndex,
leaf_modifications: Arc<LeafModifications<LeafDataImpl>>,
) -> impl Future<Output = LeafResult<Self>>;
}

#[allow(dead_code)]
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ContractState {
Expand Down Expand Up @@ -41,6 +54,17 @@ impl LeafData for LeafDataImpl {
}
}
}

async fn create(
index: &NodeIndex,
leaf_modifications: Arc<LeafModifications<LeafDataImpl>>,
) -> LeafResult<Self> {
let leaf_data = leaf_modifications
.get(index)
.ok_or(LeafError::MissingLeafModificationData(*index))?
.clone();
Ok(leaf_data)
}
}

#[allow(dead_code)]
Expand Down
Loading