Skip to content

Commit

Permalink
build: updated API
Browse files Browse the repository at this point in the history
  • Loading branch information
amosStarkware committed Mar 30, 2024
1 parent 641c399 commit 94edaba
Show file tree
Hide file tree
Showing 11 changed files with 1,915 additions and 134 deletions.
1,888 changes: 1,804 additions & 84 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion crates/committer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ workspace = true
pretty_assertions.workspace = true

[dependencies]
pathfinder-crypto = { git = "https://github.com/eqlabs/pathfinder.git"}
starknet-types-core = "0.0.11"
starknet_api = "0.12.0-dev.0"
1 change: 1 addition & 0 deletions crates/committer/src/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod types;
17 changes: 17 additions & 0 deletions crates/committer/src/hash/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use crate::types::Felt;

#[allow(dead_code)]
pub(crate) struct HashInputPair(pub Felt, pub Felt);

#[allow(dead_code)]
pub(crate) struct HashOutput(pub Felt);

#[allow(dead_code)]
impl HashOutput {
pub(crate) const ZERO: HashOutput = HashOutput(Felt::ZERO);
}

pub(crate) trait HashFunction {
/// Computes the hash of given input.
async fn compute_hash(i: HashInputPair) -> HashOutput;
}
30 changes: 2 additions & 28 deletions crates/committer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,3 @@
use pathfinder_crypto::Felt;
use patricia_merkle_tree::{
hash::{HashInput, HashOutput},
node::NodeIndex,
};

pub mod hash;
pub mod patricia_merkle_tree;

// TODO(Dori, 3/3/2024): Delete this dummy code once rust allows it.
pub fn dummy() -> Felt {
let felt = Felt::from_u64(0_u64);
let hash_input = HashInput(felt, felt, felt);
let hash_output = HashOutput(felt);
let node_index = NodeIndex(felt);
// Rust requires that every field will be used.
hash_input.0 + hash_input.1 + hash_input.2 + hash_output.0 + node_index.0
}

#[cfg(test)]
pub mod test {
use super::dummy;
use pathfinder_crypto::Felt;
use pretty_assertions::assert_eq;

#[test]
fn test_dummy() {
assert_eq!(dummy(), Felt::from_u64(0_u64));
}
}
pub mod types;
2 changes: 1 addition & 1 deletion crates/committer/src/patricia_merkle_tree.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod hash;
pub mod errors;
pub mod node;
pub mod tree;
3 changes: 3 additions & 0 deletions crates/committer/src/patricia_merkle_tree/errors.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// TODO(Amos, 01/04/2024): Add error types.
#[derive(Debug)]
pub enum NodeHashComputationError {}
10 changes: 0 additions & 10 deletions crates/committer/src/patricia_merkle_tree/hash.rs

This file was deleted.

75 changes: 71 additions & 4 deletions crates/committer/src/patricia_merkle_tree/node.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,75 @@
use pathfinder_crypto::Felt;
use starknet_api::core::{ClassHash, Nonce};

use super::hash::HashFunction;
use crate::types::Felt;

use crate::hash::types::{HashFunction, HashOutput};

use super::errors::NodeHashComputationError;

// TODO(Amos, 01/05/2024): Implement types for NodeIndex, EdgePath, EdgePathLength
#[allow(dead_code)]
pub(crate) struct NodeIndex(pub Felt);

// TODO(Amos, 26/3/2024): Define trait's methods.
pub(crate) trait Node<H: HashFunction> {}
#[allow(dead_code)]
pub(crate) struct EdgePath(pub Felt);

#[allow(dead_code)]
pub(crate) struct EdgePathLength(pub Felt);

#[allow(dead_code)]
pub(crate) enum NodeValue<LeafVal> {
Binary(Option<BinaryValue>),
Edge {
bottom_value: Option<BottomValue>,
path_to_bottom: PathToBottom,
},
Leaf(LeafVal),
Sibling,
}

#[allow(dead_code)]
pub(crate) enum BinaryValue {
Left(HashOutput),
Right(HashOutput),
}

#[allow(dead_code)]
pub(crate) enum BottomValue {
BottomBinaryValue(BinaryValue),
BottomLeafValue(LeafValue),
}

#[allow(dead_code)]
pub(crate) struct PathToBottom {
pub path: EdgePath,
pub length: EdgePathLength,
}

#[allow(dead_code)]
pub(crate) enum LeafValue {
StorageValue(Felt),
CompiledClassHash(Felt),
StateTreeValue {
class_hash: ClassHash,
contract_state_root_hash: Felt,
nonce: Nonce,
},
}

pub(crate) trait Node<H: HashFunction, LeafVal> {
/// Computes and sets sub-tree's hashes and values, if possible.
/// If successful or if hash and value of node are already set - returns hash of node.
fn compute_and_set_subtree_hashes_and_values(
&mut self,
) -> Result<HashOutput, NodeHashComputationError>;

/// Computes and returns hash of node, if value is set.
/// If node is None - returns hash of empty tree.
fn compute_hash(node: Option<&Self>) -> Result<HashOutput, NodeHashComputationError>;

/// Returns node's value. If value was not set some fields may be empty.
fn get_value(&self) -> NodeValue<LeafVal>;

/// Returns node's hash if it was set.
fn get_hash(&self) -> Option<HashOutput>;
}
17 changes: 11 additions & 6 deletions crates/committer/src/patricia_merkle_tree/tree.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
use crate::patricia_merkle_tree::node::Node;
use std::iter::Map;

use super::{hash::HashFunction, node::NodeIndex};
use crate::patricia_merkle_tree::node::{Node, NodeIndex};

pub(crate) trait Tree<H: HashFunction, N: Node<H>> {
/// Returns the node with given full (Merkle) index, if it exists.
fn get_node(full_index: NodeIndex) -> Option<N>;
use crate::hash::types::HashFunction;

pub(crate) trait Tree<H: HashFunction, LeafVal, N: Node<H, LeafVal>> {
/// Returns the root if the tree is not empty.
fn get_root() -> Option<N>;
fn get_root(&self) -> Option<&mut N>;

/// Updates tree in place with given leaves, and returns all modified (and new) nodes.
fn update_and_get_modified_nodes(
&mut self,
index_to_updated_leaf: Map<NodeIndex, &N>,
) -> Map<NodeIndex, &N>;
}
3 changes: 3 additions & 0 deletions crates/committer/src/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
use starknet_types_core::felt::Felt as StarknetTypesFelt;

pub(crate) type Felt = StarknetTypesFelt;

0 comments on commit 94edaba

Please sign in to comment.