diff --git a/Cargo.toml b/Cargo.toml index 015dedb..8a513d3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/crates/trees/Cargo.toml b/crates/trees/Cargo.toml index ddc1224..34c8b56 100644 --- a/crates/trees/Cargo.toml +++ b/crates/trees/Cargo.toml @@ -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 diff --git a/crates/trees/src/cascading/mod.rs b/crates/trees/src/cascading/mod.rs index 6d504a3..9e36f4f 100644 --- a/crates/trees/src/cascading/mod.rs +++ b/crates/trees/src/cascading/mod.rs @@ -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}; @@ -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; ::Hash: Clone, S: Clone)] +#[derive_where(PartialEq; ::Hash: PartialEq, S: PartialEq)] +#[derive_where(Eq; ::Hash: Eq, S: Eq)] +#[derive_where(Debug; ::Hash: Debug, S: Debug)] pub struct CascadingMerkleTree::Hash>> where H: Hasher, @@ -45,64 +50,6 @@ where _marker: std::marker::PhantomData, } -impl Debug for CascadingMerkleTree -where - H: 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 Clone for CascadingMerkleTree -where - H: 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 PartialEq for CascadingMerkleTree -where - H: 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 Eq for CascadingMerkleTree -where - H: Hasher, - ::Hash: Eq, - S: Eq, -{ -} - impl CascadingMerkleTree where H: Hasher, diff --git a/crates/trees/src/imt/mod.rs b/crates/trees/src/imt/mod.rs index c3535d7..20a2c92 100644 --- a/crates/trees/src/imt/mod.rs +++ b/crates/trees/src/imt/mod.rs @@ -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; ::Hash: Clone)] +#[derive_where(PartialEq; ::Hash: PartialEq)] +#[derive_where(Eq; ::Hash: Eq)] +#[derive_where(Debug; ::Hash: Debug)] pub struct MerkleTree where H: Hasher, @@ -23,51 +28,6 @@ where nodes: Vec, } -impl Clone for MerkleTree -where - H: Hasher, - ::Hash: Clone, -{ - fn clone(&self) -> Self { - Self { - depth: self.depth, - empty: self.empty.clone(), - nodes: self.nodes.clone(), - } - } -} - -impl PartialEq for MerkleTree -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 Eq for MerkleTree -where - H: Hasher, - H::Hash: Eq, -{ -} - -impl Debug for MerkleTree -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 { diff --git a/crates/trees/src/proof.rs b/crates/trees/src/proof.rs index e2e23d0..54ce7e3 100644 --- a/crates/trees/src/proof.rs +++ b/crates/trees/src/proof.rs @@ -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; ::Hash: Clone)] +#[derive_where(PartialEq; ::Hash: PartialEq)] +#[derive_where(Eq; ::Hash: Eq)] +#[derive_where(Debug; ::Hash: Debug)] pub struct Proof(pub Vec>) where H: Hasher; -impl PartialEq for Proof -where - H: Hasher, - H::Hash: PartialEq, -{ - fn eq(&self, other: &Self) -> bool { - self.0 == other.0 - } -} - -impl Clone for Proof -where - H: Hasher, - ::Hash: Clone, -{ - fn clone(&self) -> Self { - Self(self.0.clone()) - } -} - -impl Eq for Proof -where - H: Hasher, - H::Hash: Eq, -{ -} - /// Element of a Merkle proof #[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)] pub enum Branch { @@ -91,13 +69,3 @@ impl Debug for Branch { } } } - -impl Debug for Proof -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() - } -}