Skip to content

Commit

Permalink
refactor: move starknet logic to a different crate (#154)
Browse files Browse the repository at this point in the history
python side
starkware-industries/starkware#35643

<!-- Reviewable:start -->
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/starkware-libs/sequencer/154)
<!-- Reviewable:end -->
  • Loading branch information
AvivYossef-starkware authored Aug 7, 2024
1 parent a40cb09 commit 8134c27
Show file tree
Hide file tree
Showing 61 changed files with 695 additions and 611 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/committer_ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ on:
- 'Cargo.lock'
- 'crates/committer/**'
- 'crates/committer_cli/**'
- 'crates/starknet_committer/**'

pull_request:
types:
Expand All @@ -27,6 +28,7 @@ on:
- 'Cargo.lock'
- 'crates/committer/**'
- 'crates/committer_cli/**'
- 'crates/starknet_committer/**'

jobs:
run-regression-tests:
Expand Down
16 changes: 16 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ members = [
"crates/sequencing/papyrus_consensus",
"crates/starknet_api",
"crates/starknet_client",
"crates/starknet_committer",
"crates/starknet_sierra_compile",
"crates/task_executor",
"crates/tests-integration",
Expand Down
2 changes: 1 addition & 1 deletion crates/committer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ version = "0.1.0-rc.0"
edition.workspace = true
repository.workspace = true
license-file.workspace = true
description = "Computes and manages Starknet state."
description = "Library for computing and updating Patricia trees."

[lints]
workspace = true
Expand Down
8 changes: 4 additions & 4 deletions crates/committer/src/felt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ 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> {
pub fn from_hex(hex_string: &str) -> Result<Self, FromStrError> {
Ok(Self(Felt::from_hex(hex_string)?))
}
}
Expand Down Expand Up @@ -76,11 +76,11 @@ impl fmt::Debug for Felt {
impl Felt {
pub const ZERO: Felt = Felt(StarknetTypesFelt::ZERO);
#[allow(dead_code)]
pub(crate) const ONE: Felt = Felt(StarknetTypesFelt::ONE);
pub const ONE: Felt = Felt(StarknetTypesFelt::ONE);
#[allow(dead_code)]
pub(crate) const TWO: Felt = Felt(StarknetTypesFelt::TWO);
pub const TWO: Felt = Felt(StarknetTypesFelt::TWO);
#[allow(dead_code)]
pub(crate) const THREE: Felt = Felt(StarknetTypesFelt::THREE);
pub const THREE: Felt = Felt(StarknetTypesFelt::THREE);
pub const MAX: Felt = Felt(StarknetTypesFelt::MAX);

pub fn from_bytes_be_slice(bytes: &[u8]) -> Self {
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;
114 changes: 113 additions & 1 deletion crates/committer/src/patricia_merkle_tree/external_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ use rand::Rng;
use serde_json::json;

use super::filled_tree::tree::{FilledTree, FilledTreeImpl};
use super::node_data::inner_node::{EdgePathLength, PathToBottom};
use super::node_data::leaf::{Leaf, LeafModifications, SkeletonLeaf};
use super::original_skeleton_tree::config::OriginalSkeletonTreeConfig;
use super::original_skeleton_tree::node::OriginalSkeletonNode;
use super::original_skeleton_tree::tree::{OriginalSkeletonTree, OriginalSkeletonTreeImpl};
use super::types::{NodeIndex, SortedLeafIndices};
use super::types::{NodeIndex, SortedLeafIndices, SubTreeHeight};
use super::updated_skeleton_tree::hash_function::TreeHashFunction;
use super::updated_skeleton_tree::tree::{UpdatedSkeletonTree, UpdatedSkeletonTreeImpl};
use crate::felt::Felt;
use crate::hash::hash_trait::HashOutput;
use crate::patricia_merkle_tree::errors::TypesError;
use crate::storage::map_storage::MapStorage;
use crate::storage::storage_trait::{create_db_key, StarknetPrefix, StorageKey, StorageValue};

impl TryFrom<&U256> for Felt {
type Error = TypesError<U256>;
Expand Down Expand Up @@ -127,3 +130,112 @@ pub async fn single_tree_flow_test<L: Leaf + 'static, TH: TreeHashFunction<L> +
result_map.insert("storage_changes", json_storage);
serde_json::to_string(&result_map).expect("serialization failed")
}

pub fn create_32_bytes_entry(simple_val: u128) -> [u8; 32] {
U256::from(simple_val).to_be_bytes()
}

fn create_patricia_key(val: u128) -> StorageKey {
create_db_key(StarknetPrefix::InnerNode.to_storage_prefix(), &U256::from(val).to_be_bytes())
}

fn create_binary_val(left: u128, right: u128) -> StorageValue {
StorageValue(
(create_32_bytes_entry(left).into_iter().chain(create_32_bytes_entry(right))).collect(),
)
}

fn create_edge_val(hash: u128, path: u128, length: u8) -> StorageValue {
StorageValue(
create_32_bytes_entry(hash)
.into_iter()
.chain(create_32_bytes_entry(path))
.chain([length])
.collect(),
)
}

pub fn create_binary_entry(left: u128, right: u128) -> (StorageKey, StorageValue) {
(create_patricia_key(left + right), create_binary_val(left, right))
}

pub fn create_edge_entry(hash: u128, path: u128, length: u8) -> (StorageKey, StorageValue) {
(create_patricia_key(hash + path + u128::from(length)), create_edge_val(hash, path, length))
}

pub fn create_binary_skeleton_node(idx: u128) -> (NodeIndex, OriginalSkeletonNode) {
(NodeIndex::from(idx), OriginalSkeletonNode::Binary)
}

pub fn create_edge_skeleton_node(
idx: u128,
path: u128,
length: u8,
) -> (NodeIndex, OriginalSkeletonNode) {
(
NodeIndex::from(idx),
OriginalSkeletonNode::Edge(
PathToBottom::new(path.into(), EdgePathLength::new(length).unwrap()).unwrap(),
),
)
}

pub fn create_unmodified_subtree_skeleton_node(
idx: u128,
hash_output: u128,
) -> (NodeIndex, OriginalSkeletonNode) {
(
NodeIndex::from(idx),
OriginalSkeletonNode::UnmodifiedSubTree(HashOutput(Felt::from(hash_output))),
)
}

pub fn create_root_edge_entry(
old_root: u128,
subtree_height: SubTreeHeight,
) -> (StorageKey, StorageValue) {
// Assumes path is 0.
let length = SubTreeHeight::ACTUAL_HEIGHT.0 - subtree_height.0;
let new_root = old_root + u128::from(length);
let key = create_db_key(
StarknetPrefix::InnerNode.to_storage_prefix(),
&Felt::from(new_root).to_bytes_be(),
);
let value = StorageValue(
Felt::from(old_root)
.to_bytes_be()
.into_iter()
.chain(Felt::from(0_u128).to_bytes_be())
.chain([length])
.collect(),
);
(key, value)
}

pub fn create_expected_skeleton_nodes(
nodes: Vec<(NodeIndex, OriginalSkeletonNode)>,
height: u8,
) -> HashMap<NodeIndex, OriginalSkeletonNode> {
let subtree_height = SubTreeHeight::new(height);
nodes
.into_iter()
.map(|(node_index, node)| (NodeIndex::from_subtree_index(node_index, subtree_height), node))
.chain([(
NodeIndex::ROOT,
OriginalSkeletonNode::Edge(
PathToBottom::new(0.into(), EdgePathLength::new(251 - height).unwrap()).unwrap(),
),
)])
.collect()
}

impl NodeIndex {
/// Assumes self represents an index in a smaller tree height. Returns a node index represents
/// the same index in the starknet state tree as if the smaller tree was 'planted' at the lowest
/// leftmost node from the root.
pub fn from_subtree_index(subtree_index: Self, subtree_height: SubTreeHeight) -> Self {
let height_diff = SubTreeHeight::ACTUAL_HEIGHT.0 - subtree_height.0;
let offset = (NodeIndex::ROOT << height_diff) - 1.into();
subtree_index + (offset << (subtree_index.bit_length() - 1))
}
}
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,9 +1,8 @@
use tokio::task::JoinError;

use crate::block_committer::input::StarknetStorageValue;
use crate::patricia_merkle_tree::filled_tree::node::{CompiledClassHash, FilledNode};
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, Leaf};
use crate::patricia_merkle_tree::node_data::leaf::Leaf;
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::patricia_merkle_tree::updated_skeleton_tree::errors::UpdatedSkeletonTreeError;

Expand All @@ -28,7 +27,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>;
27 changes: 0 additions & 27 deletions crates/committer/src/patricia_merkle_tree/filled_tree/node.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,6 @@
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;
use crate::patricia_merkle_tree::types::NodeIndex;

// 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<&ClassHash> for NodeIndex {
fn from(class_hash: &ClassHash) -> NodeIndex {
NodeIndex::from_leaf_felt(&class_hash.0)
}
}

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);

#[derive(Clone, Debug, PartialEq, Eq)]
/// A node in a Patricia-Merkle tree which was modified during an update.
Expand Down
12 changes: 3 additions & 9 deletions crates/committer/src/patricia_merkle_tree/filled_tree/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use std::sync::{Arc, Mutex};

use async_recursion::async_recursion;

use crate::block_committer::input::{ContractAddress, 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, FilledNode};
use crate::patricia_merkle_tree::filled_tree::node::FilledNode;
use crate::patricia_merkle_tree::node_data::inner_node::{BinaryData, EdgeData, NodeData};
use crate::patricia_merkle_tree::node_data::leaf::{ContractState, Leaf, LeafModifications};
use crate::patricia_merkle_tree::node_data::leaf::{Leaf, LeafModifications};
use crate::patricia_merkle_tree::types::NodeIndex;
use crate::patricia_merkle_tree::updated_skeleton_tree::hash_function::TreeHashFunction;
use crate::patricia_merkle_tree::updated_skeleton_tree::node::UpdatedSkeletonNode;
Expand All @@ -25,7 +24,7 @@ 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.
fn create<'a, TH: TreeHashFunction<L> + 'static>(
updated_skeleton: Arc<impl UpdatedSkeletonTree<'a> + 'static>,
Expand All @@ -46,11 +45,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
11 changes: 0 additions & 11 deletions crates/committer/src/patricia_merkle_tree/internal_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,6 @@ pub(crate) fn random() -> ThreadRng {
rand::thread_rng()
}

impl NodeIndex {
/// Assumes self represents an index in a smaller tree height. Returns a node index represents
/// the same index in the starknet state tree as if the smaller tree was 'planted' at the lowest
/// leftmost node from the root.
pub(crate) fn from_subtree_index(subtree_index: Self, subtree_height: SubTreeHeight) -> Self {
let height_diff = SubTreeHeight::ACTUAL_HEIGHT.0 - subtree_height.0;
let offset = (NodeIndex::ROOT << height_diff) - 1.into();
subtree_index + (offset << (subtree_index.bit_length() - 1))
}
}

pub(crate) fn small_tree_index_to_full(index: U256, height: SubTreeHeight) -> NodeIndex {
NodeIndex::from_subtree_index(NodeIndex::new(index), height)
}
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;
Loading

0 comments on commit 8134c27

Please sign in to comment.