Skip to content

Commit

Permalink
Simplify with derive_where
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Oct 3, 2024
1 parent 14aafbc commit ca90c3a
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 140 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ bincode = "1.3.3"
bytemuck = "1.18"
color-eyre = "0.6"
criterion = { version = "0.5", features = ["async_tokio", "html_reports"] }
derive-where = "1"
ethabi = "18.0.0"
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
hex = "0.4.0"
Expand Down
1 change: 1 addition & 0 deletions crates/trees/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ storage.workspace = true
# 3rd Party
bytemuck.workspace = true
color-eyre.workspace = true
derive-where.workspace = true
hex.workspace = true
hex-literal.workspace = true
itertools.workspace = true
Expand Down
63 changes: 5 additions & 58 deletions crates/trees/src/cascading/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::fmt::Debug;

use bytemuck::Pod;
use color_eyre::eyre::{ensure, Result};
use derive_where::derive_where;
use hasher::Hasher;

use crate::proof::{Branch, Proof};
Expand Down Expand Up @@ -33,6 +34,10 @@ use self::storage_ops::{sparse_fill_partial_subtree, StorageOps};
/// Leaves are 0 indexed
/// 0 1 2 3 4 5 6 7
/// ```
#[derive_where(Clone; <H as Hasher>::Hash: Clone, S: Clone)]
#[derive_where(PartialEq; <H as Hasher>::Hash: PartialEq, S: PartialEq)]
#[derive_where(Eq; <H as Hasher>::Hash: Eq, S: Eq)]
#[derive_where(Debug; <H as Hasher>::Hash: Debug, S: Debug)]
pub struct CascadingMerkleTree<H, S = Vec<<H as Hasher>::Hash>>
where
H: Hasher,
Expand All @@ -45,64 +50,6 @@ where
_marker: std::marker::PhantomData<H>,
}

impl<H, S> Debug for CascadingMerkleTree<H, S>
where
H: Hasher,
<H as Hasher>::Hash: Debug,
S: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("CascadingMerkleTree")
.field("depth", &self.depth)
.field("root", &self.root)
.field("empty_value", &self.empty_value)
.field("sparse_column", &self.sparse_column)
.field("storage", &self.storage)
.finish()
}
}

impl<H, S> Clone for CascadingMerkleTree<H, S>
where
H: Hasher,
<H as Hasher>::Hash: Clone,
S: Clone,
{
fn clone(&self) -> Self {
Self {
depth: self.depth,
root: self.root.clone(),
empty_value: self.empty_value.clone(),
sparse_column: self.sparse_column.clone(),
storage: self.storage.clone(),
_marker: std::marker::PhantomData,
}
}
}

impl<H, S> PartialEq for CascadingMerkleTree<H, S>
where
H: Hasher,
<H as Hasher>::Hash: PartialEq,
S: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.depth == other.depth
&& self.root == other.root
&& self.empty_value == other.empty_value
&& self.sparse_column == other.sparse_column
&& self.storage == other.storage
}
}

impl<H, S> Eq for CascadingMerkleTree<H, S>
where
H: Hasher,
<H as Hasher>::Hash: Eq,
S: Eq,
{
}

impl<H, S> CascadingMerkleTree<H, S>
where
H: Hasher,
Expand Down
50 changes: 5 additions & 45 deletions crates/trees/src/imt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ use std::fmt::Debug;
use std::iter::{once, repeat, successors};

use bytemuck::Pod;
use derive_where::derive_where;
use hasher::Hasher;

use crate::proof::{Branch, Proof};

/// Merkle tree with all leaf and intermediate hashes stored
#[derive_where(Clone; <H as Hasher>::Hash: Clone)]
#[derive_where(PartialEq; <H as Hasher>::Hash: PartialEq)]
#[derive_where(Eq; <H as Hasher>::Hash: Eq)]
#[derive_where(Debug; <H as Hasher>::Hash: Debug)]
pub struct MerkleTree<H>
where
H: Hasher,
Expand All @@ -23,51 +28,6 @@ where
nodes: Vec<H::Hash>,
}

impl<H> Clone for MerkleTree<H>
where
H: Hasher,
<H as Hasher>::Hash: Clone,
{
fn clone(&self) -> Self {
Self {
depth: self.depth,
empty: self.empty.clone(),
nodes: self.nodes.clone(),
}
}
}

impl<H> PartialEq for MerkleTree<H>
where
H: Hasher,
H::Hash: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.depth.eq(&other.depth) && self.empty.eq(&other.empty) && self.nodes.eq(&other.nodes)
}
}

impl<H> Eq for MerkleTree<H>
where
H: Hasher,
H::Hash: Eq,
{
}

impl<H> Debug for MerkleTree<H>
where
H: Hasher,
H::Hash: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("MerkleTree")
.field("depth", &self.depth)
.field("empty", &self.empty)
.field("nodes", &self.nodes)
.finish()
}
}

/// For a given node index, return the parent node index
/// Returns None if there is no parent (root node)
const fn parent(index: usize) -> Option<usize> {
Expand Down
42 changes: 5 additions & 37 deletions crates/trees/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,18 @@
use std::fmt::Debug;

use derive_where::derive_where;
use hasher::Hasher;
use serde::{Deserialize, Serialize};

/// Merkle proof path, bottom to top.
#[derive_where(Clone; <H as Hasher>::Hash: Clone)]
#[derive_where(PartialEq; <H as Hasher>::Hash: PartialEq)]
#[derive_where(Eq; <H as Hasher>::Hash: Eq)]
#[derive_where(Debug; <H as Hasher>::Hash: Debug)]
pub struct Proof<H>(pub Vec<Branch<H::Hash>>)
where
H: Hasher;

impl<H> PartialEq for Proof<H>
where
H: Hasher,
H::Hash: PartialEq,
{
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}

impl<H> Clone for Proof<H>
where
H: Hasher,
<H as Hasher>::Hash: Clone,
{
fn clone(&self) -> Self {
Self(self.0.clone())
}
}

impl<H> Eq for Proof<H>
where
H: Hasher,
H::Hash: Eq,
{
}

/// Element of a Merkle proof
#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Branch<T> {
Expand Down Expand Up @@ -91,13 +69,3 @@ impl<T: Debug> Debug for Branch<T> {
}
}
}

impl<H> Debug for Proof<H>
where
H: Hasher,
H::Hash: Debug,
{
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Proof").field(&self.0).finish()
}
}

0 comments on commit ca90c3a

Please sign in to comment.