Skip to content

Commit

Permalink
Fix invalid trait implementations (#96)
Browse files Browse the repository at this point in the history
* Fix invalid trait implementations

* Simplify with derive_where
  • Loading branch information
Dzejkop authored Oct 3, 2024
1 parent 5747d00 commit 6532dd0
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 32 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
10 changes: 7 additions & 3 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,7 +34,10 @@ use self::storage_ops::{sparse_fill_partial_subtree, StorageOps};
/// Leaves are 0 indexed
/// 0 1 2 3 4 5 6 7
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[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 Down Expand Up @@ -477,7 +481,7 @@ mod tests {

pub fn debug_tree<H, S>(tree: &CascadingMerkleTree<H, S>)
where
H: Hasher + std::fmt::Debug,
H: Hasher,
<H as Hasher>::Hash: Debug + Copy,
S: GenericStorage<H::Hash> + std::fmt::Debug,
{
Expand All @@ -487,7 +491,7 @@ mod tests {

pub fn debug_storage<H, S>(storage: &S)
where
H: Hasher + std::fmt::Debug,
H: Hasher,
<H as Hasher>::Hash: Debug + Copy,
S: std::ops::Deref<Target = [<H as Hasher>::Hash]> + std::fmt::Debug,
{
Expand Down
6 changes: 5 additions & 1 deletion crates/trees/src/imt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +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(Clone, PartialEq, Eq, Debug)]
#[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 Down
33 changes: 5 additions & 28 deletions crates/trees/src/proof.rs
Original file line number Diff line number Diff line change
@@ -1,31 +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(Clone)]
#[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> 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 @@ -82,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 6532dd0

Please sign in to comment.