Skip to content

Commit

Permalink
refactor: move starknet logic to a different crate
Browse files Browse the repository at this point in the history
  • Loading branch information
AvivYossef-starkware committed Jul 28, 2024
1 parent 1c5dedd commit 1b6dfbc
Show file tree
Hide file tree
Showing 45 changed files with 556 additions and 488 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# https://doc.rust-lang.org/cargo/reference/resolver.html#feature-resolver-version-2
resolver = "2"

members = ["crates/committer", "crates/committer_cli"]
members = ["crates/committer", "crates/committer_cli", "crates/starknet-committer"]

[workspace.package]
version = "0.1.0-rc.0"
Expand Down
3 changes: 2 additions & 1 deletion crates/committer/src/felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ pub struct Felt(pub StarknetTypesFelt);
macro_rules! impl_from_hex_for_felt_wrapper {
($wrapper:ty) => {
impl $wrapper {
pub(crate) fn from_hex(hex_string: &str) -> Result<Self, FromStrError> {
#[allow(dead_code)]
pub fn from_hex(hex_string: &str) -> Result<Self, FromStrError> {
Ok(Self(Felt::from_hex(hex_string)?))
}
}
Expand Down
2 changes: 0 additions & 2 deletions crates/committer/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
pub mod block_committer;
pub mod felt;
pub mod forest_errors;
pub mod hash;
pub mod patricia_merkle_tree;
pub mod storage;
1 change: 0 additions & 1 deletion crates/committer/src/patricia_merkle_tree/filled_tree.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pub mod errors;
pub mod forest;
pub mod node;
pub mod node_serde;
pub mod tree;
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
use tokio::task::JoinError;

use crate::block_committer::input::StarknetStorageValue;
use crate::patricia_merkle_tree::filled_tree::node::CompiledClassHash;

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

Expand Down Expand Up @@ -32,7 +30,3 @@ pub enum FilledTreeError<L: Leaf> {
#[error(transparent)]
JoinError(#[from] JoinError),
}

pub type StorageTrieError = FilledTreeError<StarknetStorageValue>;
pub type ClassesTrieError = FilledTreeError<CompiledClassHash>;
pub type ContractsTrieError = FilledTreeError<ContractState>;
22 changes: 1 addition & 21 deletions crates/committer/src/patricia_merkle_tree/filled_tree/node.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,7 @@
use starknet_types_core::felt::FromStrError;

use crate::felt::Felt;
use crate::hash::hash_trait::HashOutput;
use crate::impl_from_hex_for_felt_wrapper;
use crate::patricia_merkle_tree::node_data::inner_node::NodeData;
use crate::patricia_merkle_tree::node_data::leaf::Leaf;

// TODO(Nimrod, 1/6/2024): Use the ClassHash defined in starknet-types-core when available.

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

impl_from_hex_for_felt_wrapper!(ClassHash);
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Nonce(pub Felt);

impl_from_hex_for_felt_wrapper!(Nonce);

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

impl_from_hex_for_felt_wrapper!(CompiledClassHash);

use crate::hash::hash_trait::HashOutput;
#[derive(Clone, Debug, PartialEq, Eq)]
/// A node in a Patricia-Merkle tree which was modified during an update.
pub struct FilledNode<L: Leaf> {
Expand Down
16 changes: 4 additions & 12 deletions crates/committer/src/patricia_merkle_tree/filled_tree/tree.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
use std::collections::HashMap;
use std::future::Future;
use std::sync::Arc;
use std::sync::Mutex;

use async_recursion::async_recursion;

use crate::block_committer::input::ContractAddress;
use crate::block_committer::input::StarknetStorageValue;
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::filled_tree::errors::FilledTreeError;
use crate::patricia_merkle_tree::filled_tree::node::CompiledClassHash;
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
use crate::patricia_merkle_tree::node_data::inner_node::BinaryData;
use crate::patricia_merkle_tree::node_data::inner_node::EdgeData;
use crate::patricia_merkle_tree::node_data::inner_node::NodeData;
use crate::patricia_merkle_tree::node_data::leaf::ContractState;
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
use crate::patricia_merkle_tree::node_data::leaf::LeafModifications;
use crate::patricia_merkle_tree::types::NodeIndex;
Expand All @@ -32,12 +29,12 @@ pub(crate) type FilledTreeResult<T, L> = Result<T, FilledTreeError<L>>;
/// Consider a Patricia-Merkle Tree which has been updated with new leaves.
/// FilledTree consists of all nodes which were modified in the update, including their updated
/// data and hashes.
pub(crate) trait FilledTree<L: Leaf>: Sized {
pub trait FilledTree<L: Leaf>: Sized {
/// Computes and returns the filled tree.
async fn create<'a, TH: TreeHashFunction<L> + 'static>(
fn create<'a, TH: TreeHashFunction<L> + 'static>(
updated_skeleton: Arc<impl UpdatedSkeletonTree<'a> + 'static>,
leaf_modifications: Arc<LeafModifications<L>>,
) -> FilledTreeResult<Self, L>;
) -> impl Future<Output = FilledTreeResult<Self, L>> +Send;

/// Serializes the current state of the tree into a hashmap,
/// where each key-value pair corresponds
Expand All @@ -53,11 +50,6 @@ pub struct FilledTreeImpl<L: Leaf> {
pub root_hash: HashOutput,
}

pub type StorageTrie = FilledTreeImpl<StarknetStorageValue>;
pub type ClassesTrie = FilledTreeImpl<CompiledClassHash>;
pub type ContractsTrie = FilledTreeImpl<ContractState>;
pub type StorageTrieMap = HashMap<ContractAddress, StorageTrie>;

impl<L: Leaf + 'static> FilledTreeImpl<L> {
fn initialize_with_placeholders<'a>(
updated_skeleton: &Arc<impl UpdatedSkeletonTree<'a>>,
Expand Down
1 change: 0 additions & 1 deletion crates/committer/src/patricia_merkle_tree/node_data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod errors;
pub mod inner_node;
pub mod leaf;
pub mod leaf_serde;
50 changes: 0 additions & 50 deletions crates/committer/src/patricia_merkle_tree/node_data/leaf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ use std::fmt::Debug;
use std::future::Future;
use std::sync::Arc;

use crate::block_committer::input::StarknetStorageValue;
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, Deserializable};
Expand Down Expand Up @@ -40,53 +37,6 @@ pub trait Leaf: Clone + Sync + Send + DBObject + Deserializable + Default + Debu
}
}

#[derive(Clone, Debug, Default, Eq, PartialEq)]
pub struct ContractState {
pub nonce: Nonce,
pub storage_root_hash: HashOutput,
pub class_hash: ClassHash,
}

impl Leaf for StarknetStorageValue {
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)
}
}

impl Leaf for CompiledClassHash {
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)
}
}

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

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

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum SkeletonLeaf {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,5 @@ pub mod config;
pub mod create_tree;
pub mod errors;
pub mod node;
pub mod skeleton_forest;
pub mod tree;
pub mod utils;
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use crate::block_committer::input::StarknetStorageValue;
use crate::patricia_merkle_tree::filled_tree::node::CompiledClassHash;
use crate::patricia_merkle_tree::node_data::leaf::{ContractState, Leaf, LeafModifications};
use crate::patricia_merkle_tree::original_skeleton_tree::errors::OriginalSkeletonTreeError;
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
use crate::patricia_merkle_tree::original_skeleton_tree::tree::OriginalSkeletonTreeResult;
use crate::patricia_merkle_tree::types::NodeIndex;

Expand Down Expand Up @@ -59,29 +56,3 @@ macro_rules! generate_trie_config {
}
};
}

generate_trie_config!(OriginalSkeletonStorageTrieConfig, StarknetStorageValue);

generate_trie_config!(OriginalSkeletonClassesTrieConfig, CompiledClassHash);

pub(crate) struct OriginalSkeletonContractsTrieConfig;

impl OriginalSkeletonTreeConfig<ContractState> for OriginalSkeletonContractsTrieConfig {
fn compare_modified_leaves(&self) -> bool {
false
}

fn compare_leaf(
&self,
_index: &NodeIndex,
_previous_leaf: &ContractState,
) -> OriginalSkeletonTreeResult<bool> {
Ok(false)
}
}

impl OriginalSkeletonContractsTrieConfig {
pub(crate) fn new() -> Self {
Self
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,7 @@ impl<'a> OriginalSkeletonTreeImpl<'a> {
Ok(skeleton_tree)
}

#[allow(dead_code)]
pub(crate) fn create_and_get_previous_leaves_impl<L: Leaf>(
storage: &impl Storage,
root_hash: HashOutput,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::patricia_merkle_tree::node_data::inner_node::PathToBottom;

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
/// A node in the structure of a Patricia-Merkle tree, before the update.
pub(crate) enum OriginalSkeletonNode {
pub enum OriginalSkeletonNode {
Binary,
Edge(PathToBottom),
// Represents a root of a subtree where none of it's descendants has changed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ use crate::patricia_merkle_tree::original_skeleton_tree::node::OriginalSkeletonN
use crate::patricia_merkle_tree::types::{NodeIndex, SortedLeafIndices};
use crate::storage::storage_trait::Storage;

pub(crate) type OriginalSkeletonNodeMap = HashMap<NodeIndex, OriginalSkeletonNode>;
pub(crate) type OriginalSkeletonTreeResult<T> = Result<T, OriginalSkeletonTreeError>;
pub type OriginalSkeletonNodeMap = HashMap<NodeIndex, OriginalSkeletonNode>;
pub type OriginalSkeletonTreeResult<T> = Result<T, OriginalSkeletonTreeError>;

/// Consider a Patricia-Merkle Tree which should be updated with new leaves.
/// This trait represents the structure of the subtree which will be modified in the
/// update. It also contains the hashes (for edge siblings - also the edge data) of the unmodified
/// nodes on the Merkle paths from the updated leaves to the root.
pub(crate) trait OriginalSkeletonTree<'a>: Sized {
pub trait OriginalSkeletonTree<'a>: Sized {
fn create<L: Leaf>(
storage: &impl Storage,
root_hash: HashOutput,
Expand All @@ -27,6 +27,7 @@ pub(crate) trait OriginalSkeletonTree<'a>: Sized {

fn get_nodes_mut(&mut self) -> &mut OriginalSkeletonNodeMap;

#[allow(dead_code)]
fn create_and_get_previous_leaves<L: Leaf>(
storage: &impl Storage,
root_hash: HashOutput,
Expand All @@ -40,7 +41,7 @@ pub(crate) trait OriginalSkeletonTree<'a>: Sized {

// TODO(Dori, 1/7/2024): Make this a tuple struct.
#[derive(Debug, PartialEq)]
pub(crate) struct OriginalSkeletonTreeImpl<'a> {
pub struct OriginalSkeletonTreeImpl<'a> {
pub(crate) nodes: HashMap<NodeIndex, OriginalSkeletonNode>,
pub(crate) sorted_leaf_indices: SortedLeafIndices<'a>,
}
Expand Down
24 changes: 6 additions & 18 deletions crates/committer/src/patricia_merkle_tree/types.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::block_committer::input::{ContractAddress, StarknetStorageKey};
use crate::felt::Felt;
use crate::patricia_merkle_tree::errors::TypesError;
use crate::patricia_merkle_tree::filled_tree::node::ClassHash;
use crate::patricia_merkle_tree::node_data::inner_node::{EdgePathLength, PathToBottom};

use ethnum::U256;
Expand Down Expand Up @@ -133,22 +131,12 @@ impl NodeIndex {
.expect("Illegal PathToBottom")
}

pub(crate) fn from_starknet_storage_key(key: &StarknetStorageKey) -> Self {
Self::from_leaf_felt(&key.0)
}

pub(crate) fn from_contract_address(address: &ContractAddress) -> Self {
Self::from_leaf_felt(&address.0)
}

pub(crate) fn from_class_hash(class_hash: &ClassHash) -> Self {
Self::from_leaf_felt(&class_hash.0)
}

fn from_leaf_felt(felt: &Felt) -> Self {
#[allow(dead_code)]
pub fn from_leaf_felt(felt: &Felt) -> Self {
Self::FIRST_LEAF + Self::from_felt_value(felt)
}

#[allow(dead_code)]
fn from_felt_value(felt: &Felt) -> Self {
Self(U256::from(felt))
}
Expand Down Expand Up @@ -225,18 +213,18 @@ impl TryFrom<NodeIndex> for Felt {
}

#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub(crate) struct SortedLeafIndices<'a>(&'a [NodeIndex]);
pub struct SortedLeafIndices<'a>(&'a [NodeIndex]);

impl<'a> SortedLeafIndices<'a> {
/// Creates a new instance by sorting the given indices.
// TODO(Nimrod, 1/8/2024): Remove duplicates from the given indices.
pub(crate) fn new(indices: &'a mut [NodeIndex]) -> Self {
pub fn new(indices: &'a mut [NodeIndex]) -> Self {
indices.sort();
Self(indices)
}

/// Returns a subslice of the indices stored at self, at the range [leftmost_idx, rightmost_idx).
pub(crate) fn subslice(&self, leftmost_idx: usize, rightmost_idx: usize) -> Self {
pub fn subslice(&self, leftmost_idx: usize, rightmost_idx: usize) -> Self {
Self(&self.0[leftmost_idx..rightmost_idx])
}

Expand Down
Loading

0 comments on commit 1b6dfbc

Please sign in to comment.