mirror of
https://github.com/tlsnotary/rs-merkle.git
synced 2026-01-08 22:38:07 -05:00
fix clippy warnings
This commit is contained in:
@@ -53,15 +53,15 @@ pub trait Hasher: Clone {
|
||||
/// if the left node doesn't have a sibling it is concatenated to itself and
|
||||
/// then hashed instead of just being propagated to the next level.
|
||||
fn concat_and_hash(left: &Self::Hash, right: Option<&Self::Hash>) -> Self::Hash {
|
||||
let mut concatenated: Vec<u8> = left.clone().into();
|
||||
let mut concatenated: Vec<u8> = (*left).into();
|
||||
|
||||
match right {
|
||||
Some(right_node) => {
|
||||
let mut right_node_clone: Vec<u8> = right_node.clone().into();
|
||||
let mut right_node_clone: Vec<u8> = (*right_node).into();
|
||||
concatenated.append(&mut right_node_clone);
|
||||
Self::hash(&concatenated)
|
||||
}
|
||||
None => left.clone(),
|
||||
None => *left,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -122,7 +122,7 @@ impl<T: Hasher> MerkleProof<T> {
|
||||
// TODO: remove the unwrap!
|
||||
let partial_tree = PartialTree::<T>::build(proof_layers, tree_depth).unwrap();
|
||||
|
||||
return partial_tree.root().unwrap().clone();
|
||||
*partial_tree.root().unwrap()
|
||||
}
|
||||
|
||||
/// Calculates the root and serializes it into a hex string
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::partial_tree::PartialTree;
|
||||
use crate::utils::indices::{parent_indices, proof_indices_by_layers};
|
||||
use crate::utils::indices;
|
||||
use crate::{utils, Hasher, MerkleProof};
|
||||
|
||||
/// [`MerkleTree`] is a Merkle Tree that is well suited for both basic and advanced usage.
|
||||
@@ -98,6 +98,12 @@ pub struct MerkleTree<T: Hasher> {
|
||||
uncommitted_leaves: Vec<T::Hash>,
|
||||
}
|
||||
|
||||
impl<T: Hasher> Default for MerkleTree<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hasher> MerkleTree<T> {
|
||||
/// Creates a new instance of Merkle Tree. Requires specifying the hash algorithm.
|
||||
///
|
||||
@@ -174,7 +180,7 @@ impl<T: Hasher> MerkleTree<T> {
|
||||
for index in helper_indices {
|
||||
match tree_layer.get(index) {
|
||||
Some(hash) => {
|
||||
helpers_layer.push((index, hash.clone()));
|
||||
helpers_layer.push((index, *hash));
|
||||
}
|
||||
// This means that there's no right sibling to the current index, thus
|
||||
// we don't need to include anything in the proof for that index
|
||||
@@ -183,7 +189,7 @@ impl<T: Hasher> MerkleTree<T> {
|
||||
}
|
||||
|
||||
helper_nodes.push(helpers_layer);
|
||||
current_layer_indices = parent_indices(¤t_layer_indices);
|
||||
current_layer_indices = indices::parent_indices(¤t_layer_indices);
|
||||
}
|
||||
|
||||
helper_nodes
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use crate::error::{Error, ErrorKind};
|
||||
use crate::error::Error;
|
||||
use crate::{utils, Hasher};
|
||||
|
||||
type PartialTreeLayer<H> = Vec<(usize, H)>;
|
||||
|
||||
/// Partial tree represents a part of the original tree that is enough to calculate the root.
|
||||
/// Used in to extract the root in a merkle proof, to apply diff to a tree or to merge
|
||||
/// multiple trees into one
|
||||
@@ -9,6 +11,12 @@ pub struct PartialTree<T: Hasher> {
|
||||
layers: Vec<Vec<(usize, T::Hash)>>,
|
||||
}
|
||||
|
||||
impl<T: Hasher> Default for PartialTree<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: Hasher> PartialTree<T> {
|
||||
/// Takes leaves (item hashes) as an argument and build a Merkle Tree from them.
|
||||
/// Since it's a partial tree, hashes must be accompanied by their index in the original tree.
|
||||
@@ -35,7 +43,7 @@ impl<T: Hasher> PartialTree<T> {
|
||||
fn build_tree(
|
||||
mut partial_layers: Vec<Vec<(usize, T::Hash)>>,
|
||||
full_tree_depth: usize,
|
||||
) -> Result<Vec<Vec<(usize, T::Hash)>>, Error> {
|
||||
) -> Result<Vec<PartialTreeLayer<T::Hash>>, Error> {
|
||||
let mut partial_tree: Vec<Vec<(usize, T::Hash)>> = Vec::new();
|
||||
let mut current_layer = Vec::new();
|
||||
|
||||
@@ -67,7 +75,7 @@ impl<T: Hasher> PartialTree<T> {
|
||||
match nodes.get(i * 2) {
|
||||
// Populate `current_layer` back for the next iteration
|
||||
Some(left_node) => current_layer.push((
|
||||
parent_node_index.clone(),
|
||||
*parent_node_index,
|
||||
T::concat_and_hash(left_node, nodes.get(i * 2 + 1)),
|
||||
)),
|
||||
None => return Err(Error::not_enough_helper_nodes()),
|
||||
@@ -92,15 +100,7 @@ impl<T: Hasher> PartialTree<T> {
|
||||
|
||||
pub fn contains(&self, layer_index: usize, node_index: usize) -> bool {
|
||||
match self.layers().get(layer_index) {
|
||||
Some(layer) => {
|
||||
match layer
|
||||
.iter()
|
||||
.position(|(index, _)| index.clone() == node_index)
|
||||
{
|
||||
Some(_) => true,
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
Some(layer) => layer.iter().any(|(index, _)| *index == node_index),
|
||||
None => false,
|
||||
}
|
||||
}
|
||||
@@ -126,7 +126,7 @@ impl<T: Hasher> PartialTree<T> {
|
||||
if let Some(self_layer) = self.layers().get(layer_index) {
|
||||
let mut filtered_layer: Vec<(usize, T::Hash)> = self_layer
|
||||
.iter()
|
||||
.filter(|(node_index, _)| !other.contains(layer_index, node_index.clone()))
|
||||
.filter(|(node_index, _)| !other.contains(layer_index, *node_index))
|
||||
.cloned()
|
||||
.collect();
|
||||
|
||||
|
||||
@@ -1,11 +1,6 @@
|
||||
use crate::utils;
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct LayerInfo {
|
||||
index: usize,
|
||||
leaves_count: usize,
|
||||
}
|
||||
|
||||
pub fn is_left_index(index: usize) -> bool {
|
||||
index % 2 == 0
|
||||
}
|
||||
@@ -27,7 +22,7 @@ pub fn parent_index(index: usize) -> usize {
|
||||
if is_left_index(index) {
|
||||
return index / 2;
|
||||
}
|
||||
return get_sibling_index(index) / 2;
|
||||
get_sibling_index(index) / 2
|
||||
}
|
||||
|
||||
pub fn parent_indices(indices: &[usize]) -> Vec<usize> {
|
||||
@@ -44,10 +39,6 @@ pub fn tree_depth(leaves_count: usize) -> usize {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn max_leaves_count_at_depth(depth: usize) -> usize {
|
||||
return (2 as u32).pow(depth as u32) as usize;
|
||||
}
|
||||
|
||||
pub fn uneven_layers(tree_leaves_count: usize) -> HashMap<usize, usize> {
|
||||
let mut leaves_count = tree_leaves_count;
|
||||
let depth = tree_depth(tree_leaves_count);
|
||||
@@ -57,12 +48,12 @@ pub fn uneven_layers(tree_leaves_count: usize) -> HashMap<usize, usize> {
|
||||
for index in 0..depth {
|
||||
let uneven_layer = leaves_count % 2 != 0;
|
||||
if uneven_layer {
|
||||
uneven_layers.insert(index, leaves_count.clone());
|
||||
uneven_layers.insert(index, leaves_count);
|
||||
}
|
||||
leaves_count = div_ceil(leaves_count, 2);
|
||||
}
|
||||
|
||||
return uneven_layers;
|
||||
uneven_layers
|
||||
}
|
||||
|
||||
/// Returns layered proof indices
|
||||
|
||||
Reference in New Issue
Block a user