From 4c47018c50d9dbe6bdb30425ec5378d30f75fb5a Mon Sep 17 00:00:00 2001 From: Samer Afach Date: Thu, 18 Apr 2024 12:32:29 +0400 Subject: [PATCH] Rename associated type to NodeType --- examples/basic.rs | 6 +++--- examples/multi_proof.rs | 6 +++--- examples/single_proof.rs | 6 +++--- src/internal.rs | 6 +++--- src/merkle/hasher.rs | 6 +++--- src/merkle/proof/multi/mod.rs | 4 ++-- src/merkle/proof/single/mod.rs | 8 ++++---- src/merkle/tree/mod.rs | 8 +++++--- 8 files changed, 26 insertions(+), 24 deletions(-) diff --git a/examples/basic.rs b/examples/basic.rs index ced81b1..4f7e78a 100644 --- a/examples/basic.rs +++ b/examples/basic.rs @@ -52,16 +52,16 @@ impl HashAlgo { // This is the important part, your hasher has to implement PairHasher impl PairHasher for HashAlgo { - type Type = TreeNode; + type NodeType = TreeNode; - fn hash_pair(left: &Self::Type, right: &Self::Type) -> Self::Type { + fn hash_pair(left: &Self::NodeType, right: &Self::NodeType) -> Self::NodeType { let mut h = Blake2bHasher::new(); Digest::update(&mut h, left); Digest::update(&mut h, right); h.finalize_reset().into() } - fn hash_single(data: &Self::Type) -> Self::Type { + fn hash_single(data: &Self::NodeType) -> Self::NodeType { let mut h = Blake2bHasher::new(); Digest::update(&mut h, data); h.finalize_reset().into() diff --git a/examples/multi_proof.rs b/examples/multi_proof.rs index 46408e8..963f4c0 100644 --- a/examples/multi_proof.rs +++ b/examples/multi_proof.rs @@ -59,16 +59,16 @@ impl HashAlgo { // This is the important part, your hasher has to implement PairHasher impl PairHasher for HashAlgo { - type Type = TreeNode; + type NodeType = TreeNode; - fn hash_pair(left: &Self::Type, right: &Self::Type) -> Self::Type { + fn hash_pair(left: &Self::NodeType, right: &Self::NodeType) -> Self::NodeType { let mut h = Blake2bHasher::new(); Digest::update(&mut h, left); Digest::update(&mut h, right); h.finalize_reset().into() } - fn hash_single(data: &Self::Type) -> Self::Type { + fn hash_single(data: &Self::NodeType) -> Self::NodeType { let mut h = Blake2bHasher::new(); Digest::update(&mut h, data); h.finalize_reset().into() diff --git a/examples/single_proof.rs b/examples/single_proof.rs index f5d7213..8342043 100644 --- a/examples/single_proof.rs +++ b/examples/single_proof.rs @@ -59,16 +59,16 @@ impl HashAlgo { // This is the important part, your hasher has to implement PairHasher impl PairHasher for HashAlgo { - type Type = TreeNode; + type NodeType = TreeNode; - fn hash_pair(left: &Self::Type, right: &Self::Type) -> Self::Type { + fn hash_pair(left: &Self::NodeType, right: &Self::NodeType) -> Self::NodeType { let mut h = Blake2bHasher::new(); Digest::update(&mut h, left); Digest::update(&mut h, right); h.finalize_reset().into() } - fn hash_single(data: &Self::Type) -> Self::Type { + fn hash_single(data: &Self::NodeType) -> Self::NodeType { let mut h = Blake2bHasher::new(); Digest::update(&mut h, data); h.finalize_reset().into() diff --git a/src/internal.rs b/src/internal.rs index d936077..8efa6bd 100644 --- a/src/internal.rs +++ b/src/internal.rs @@ -59,16 +59,16 @@ impl From> for HashedData { } impl PairHasher for HashAlgo { - type Type = HashedData; + type NodeType = HashedData; - fn hash_pair(left: &Self::Type, right: &Self::Type) -> Self::Type { + fn hash_pair(left: &Self::NodeType, right: &Self::NodeType) -> Self::NodeType { let mut h = Blake2bHasher::new(); Digest::update(&mut h, left); Digest::update(&mut h, right); h.finalize_reset().into() } - fn hash_single(data: &Self::Type) -> Self::Type { + fn hash_single(data: &Self::NodeType) -> Self::NodeType { let mut h = Blake2bHasher::new(); Digest::update(&mut h, data); h.finalize_reset().into() diff --git a/src/merkle/hasher.rs b/src/merkle/hasher.rs index 3b14c66..ab1d624 100644 --- a/src/merkle/hasher.rs +++ b/src/merkle/hasher.rs @@ -19,11 +19,11 @@ /// the node type, specifically. pub trait PairHasher: Sized + Clone { /// The node type in the merkle tree - type Type: Clone; + type NodeType: Clone; /// Hash a single node and return the hash value. - fn hash_single(data: &Self::Type) -> Self::Type; + fn hash_single(data: &Self::NodeType) -> Self::NodeType; /// Hash a pair of nodes and return the hash value. - fn hash_pair(left: &Self::Type, right: &Self::Type) -> Self::Type; + fn hash_pair(left: &Self::NodeType, right: &Self::NodeType) -> Self::NodeType; } diff --git a/src/merkle/proof/multi/mod.rs b/src/merkle/proof/multi/mod.rs index 6f1b53d..34e74d1 100644 --- a/src/merkle/proof/multi/mod.rs +++ b/src/merkle/proof/multi/mod.rs @@ -98,7 +98,7 @@ impl<'a, T, H> MultiProofNodes<'a, T, H> { } } -impl<'a, T: Clone, H: PairHasher> MultiProofNodes<'a, T, H> { +impl<'a, T: Clone, H: PairHasher> MultiProofNodes<'a, T, H> { pub fn from_tree_leaves( tree: &'a MerkleTree, leaves_indices: &[u32], @@ -246,7 +246,7 @@ impl MultiProofHashes { } } -impl> MultiProofHashes { +impl> MultiProofHashes { /// While verifying the multi-proof, we need to precalculate all the possible nodes that are required to build the root hash. fn calculate_missing_nodes(tree_size: TreeSize, input: BTreeMap<&u32, &T>) -> BTreeMap { let mut result = input diff --git a/src/merkle/proof/single/mod.rs b/src/merkle/proof/single/mod.rs index d71f565..b9a7116 100644 --- a/src/merkle/proof/single/mod.rs +++ b/src/merkle/proof/single/mod.rs @@ -42,7 +42,7 @@ impl Clone for SingleProofNodes<'_, T, H> { } } -impl<'a, T: Clone, H: PairHasher> SingleProofNodes<'a, T, H> { +impl<'a, T: Clone, H: PairHasher> SingleProofNodes<'a, T, H> { pub fn into_nodes(self) -> Vec> { self.branch } @@ -56,7 +56,7 @@ impl<'a, T: Clone, H: PairHasher> SingleProofNodes<'a, T, H> { } } -impl<'a, T: Clone, H: PairHasher> SingleProofNodes<'a, T, H> { +impl<'a, T: Clone, H: PairHasher> SingleProofNodes<'a, T, H> { /// Creates a proof for a leaf by its index in the lowest level (the tip). /// A proof doesn't contain the root. pub fn from_tree_leaf( @@ -128,7 +128,7 @@ pub struct SingleProofHashes { _hasher: std::marker::PhantomData, } -impl> SingleProofHashes { +impl> SingleProofHashes { pub fn into_hashes(self) -> Vec { self.branch } @@ -150,7 +150,7 @@ impl> SingleProofHashes { } } -impl> SingleProofHashes { +impl> SingleProofHashes { /// Verifies that the given leaf can produce the root's hash. pub fn verify(&self, leaf: T, root: T) -> ProofVerifyResult { // in case it's a single-node tree, we don't need to verify or hash anything diff --git a/src/merkle/tree/mod.rs b/src/merkle/tree/mod.rs index 875c200..4c08b5d 100644 --- a/src/merkle/tree/mod.rs +++ b/src/merkle/tree/mod.rs @@ -121,7 +121,7 @@ impl MerkleTree { } } -impl> MerkleTree { +impl> MerkleTree { fn create_tree_from_padded_leaves( padded_leaves: impl IntoIterator, ) -> Result, MerkleTreeFormError> { @@ -226,7 +226,7 @@ impl<'a, T, H> Node<'a, T, H> { } } -impl<'a, T: Clone, H: PairHasher> Node<'a, T, H> { +impl<'a, T: Clone, H: PairHasher> Node<'a, T, H> { pub fn into_position(self) -> NodePosition { NodePosition::from_abs_index(self.tree().total_node_count(), self.absolute_index) .expect("Should never fail since the index is transitively valid") @@ -276,7 +276,9 @@ impl Debug for MerkleTreeNodeParentIterator<'_, T, H> { } } -impl<'a, T: Clone, H: PairHasher> Iterator for MerkleTreeNodeParentIterator<'a, T, H> { +impl<'a, T: Clone, H: PairHasher> Iterator + for MerkleTreeNodeParentIterator<'a, T, H> +{ type Item = Node<'a, T, H>; fn next(&mut self) -> Option> {