Skip to content

Commit

Permalink
Rename associated type to NodeType
Browse files Browse the repository at this point in the history
  • Loading branch information
TheQuantumPhysicist committed Apr 18, 2024
1 parent 6f9c55f commit 4c47018
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 24 deletions.
6 changes: 3 additions & 3 deletions examples/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions examples/multi_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions examples/single_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 3 additions & 3 deletions src/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,16 @@ impl From<GenericArray<u8, typenum::U32>> 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()
Expand Down
6 changes: 3 additions & 3 deletions src/merkle/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
4 changes: 2 additions & 2 deletions src/merkle/proof/multi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ impl<'a, T, H> MultiProofNodes<'a, T, H> {
}
}

impl<'a, T: Clone, H: PairHasher<Type = T>> MultiProofNodes<'a, T, H> {
impl<'a, T: Clone, H: PairHasher<NodeType = T>> MultiProofNodes<'a, T, H> {
pub fn from_tree_leaves(
tree: &'a MerkleTree<T, H>,
leaves_indices: &[u32],
Expand Down Expand Up @@ -246,7 +246,7 @@ impl<T, H> MultiProofHashes<T, H> {
}
}

impl<T: Eq + Clone, H: PairHasher<Type = T>> MultiProofHashes<T, H> {
impl<T: Eq + Clone, H: PairHasher<NodeType = T>> MultiProofHashes<T, H> {
/// 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<u32, T> {
let mut result = input
Expand Down
8 changes: 4 additions & 4 deletions src/merkle/proof/single/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl<T, H> Clone for SingleProofNodes<'_, T, H> {
}
}

impl<'a, T: Clone, H: PairHasher<Type = T>> SingleProofNodes<'a, T, H> {
impl<'a, T: Clone, H: PairHasher<NodeType = T>> SingleProofNodes<'a, T, H> {
pub fn into_nodes(self) -> Vec<Node<'a, T, H>> {
self.branch
}
Expand All @@ -56,7 +56,7 @@ impl<'a, T: Clone, H: PairHasher<Type = T>> SingleProofNodes<'a, T, H> {
}
}

impl<'a, T: Clone, H: PairHasher<Type = T>> SingleProofNodes<'a, T, H> {
impl<'a, T: Clone, H: PairHasher<NodeType = T>> 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(
Expand Down Expand Up @@ -128,7 +128,7 @@ pub struct SingleProofHashes<T, H> {
_hasher: std::marker::PhantomData<H>,
}

impl<T: Eq, H: PairHasher<Type = T>> SingleProofHashes<T, H> {
impl<T: Eq, H: PairHasher<NodeType = T>> SingleProofHashes<T, H> {
pub fn into_hashes(self) -> Vec<T> {
self.branch
}
Expand All @@ -150,7 +150,7 @@ impl<T: Eq, H: PairHasher<Type = T>> SingleProofHashes<T, H> {
}
}

impl<T: Eq, H: PairHasher<Type = T>> SingleProofHashes<T, H> {
impl<T: Eq, H: PairHasher<NodeType = T>> SingleProofHashes<T, H> {
/// 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
Expand Down
8 changes: 5 additions & 3 deletions src/merkle/tree/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ impl<T: Clone, H> MerkleTree<T, H> {
}
}

impl<T: Clone, H: PairHasher<Type = T>> MerkleTree<T, H> {
impl<T: Clone, H: PairHasher<NodeType = T>> MerkleTree<T, H> {
fn create_tree_from_padded_leaves(
padded_leaves: impl IntoIterator<Item = T>,
) -> Result<Vec<T>, MerkleTreeFormError> {
Expand Down Expand Up @@ -226,7 +226,7 @@ impl<'a, T, H> Node<'a, T, H> {
}
}

impl<'a, T: Clone, H: PairHasher<Type = T>> Node<'a, T, H> {
impl<'a, T: Clone, H: PairHasher<NodeType = T>> 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")
Expand Down Expand Up @@ -276,7 +276,9 @@ impl<T: Debug, H> Debug for MerkleTreeNodeParentIterator<'_, T, H> {
}
}

impl<'a, T: Clone, H: PairHasher<Type = T>> Iterator for MerkleTreeNodeParentIterator<'a, T, H> {
impl<'a, T: Clone, H: PairHasher<NodeType = T>> Iterator
for MerkleTreeNodeParentIterator<'a, T, H>
{
type Item = Node<'a, T, H>;

fn next(&mut self) -> Option<Node<'a, T, H>> {
Expand Down

0 comments on commit 4c47018

Please sign in to comment.