Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Add bytemuck::Pod to Hasher::Hash and remove unsafe code. #53

Merged
merged 5 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ once_cell = "1.8"
proptest = { version = "1.0", optional = true }
rand = "0.8.4"
rayon = "1.5.1"
ruint = { version = "=1.11.0", features = ["serde", "num-bigint", "ark-ff"] }
ruint = { version = "=1.11.0", features = [
"serde",
"num-bigint",
"ark-ff",
"bytemuck",
] }
semaphore-depth-config = { path = "crates/semaphore-depth-config" }
serde = "1.0"
sha2 = "0.10.1"
Expand All @@ -83,6 +88,7 @@ bincode = "1.3.3"
# Use the same `ethers-core` version as ark-circom
# TODO: Remove
ethers-core = { git = "https://github.com/gakonst/ethers-rs", default-features = false }
bytemuck = "1.13.1"

[dev-dependencies]
bincode = "1.3.3"
Expand Down
23 changes: 3 additions & 20 deletions src/lazy_merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@
/// Creates a new memory mapped file specified by path and creates a tree
/// with dense prefix of the given depth with initial values
#[must_use]
pub fn new_mmapped_with_dense_prefix_with_init_values(
depth: usize,
prefix_depth: usize,
empty_value: &H::Hash,
initial_values: &[H::Hash],
file_path: &str,
) -> Result<LazyMerkleTree<H, Canonical>, DenseMMapError> {

Check warning on line 92 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

docs for function returning `Result` missing `# Errors` section

warning: docs for function returning `Result` missing `# Errors` section --> src/lazy_merkle_tree.rs:86:5 | 86 | / pub fn new_mmapped_with_dense_prefix_with_init_values( 87 | | depth: usize, 88 | | prefix_depth: usize, 89 | | empty_value: &H::Hash, 90 | | initial_values: &[H::Hash], 91 | | file_path: &str, 92 | | ) -> Result<LazyMerkleTree<H, Canonical>, DenseMMapError> { | |_____________________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc

Check warning on line 92 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

this function has an empty `#[must_use]` attribute, but returns a type already marked as `#[must_use]`

warning: this function has an empty `#[must_use]` attribute, but returns a type already marked as `#[must_use]` --> src/lazy_merkle_tree.rs:86:5 | 86 | / pub fn new_mmapped_with_dense_prefix_with_init_values( 87 | | depth: usize, 88 | | prefix_depth: usize, 89 | | empty_value: &H::Hash, 90 | | initial_values: &[H::Hash], 91 | | file_path: &str, 92 | | ) -> Result<LazyMerkleTree<H, Canonical>, DenseMMapError> { | |_____________________________________________________________^ | = help: either add some descriptive text or remove the attribute = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#double_must_use = note: `#[warn(clippy::double_must_use)]` implied by `#[warn(clippy::all)]`
Ok(LazyMerkleTree {
tree: AnyTree::new_mmapped_with_dense_prefix_with_init_values(
depth,
Expand Down Expand Up @@ -245,7 +245,7 @@
while current_depth < depth {
result = SparseTree::new(
result,
EmptyTree::new(current_depth, empty_value.clone()).into(),

Check warning on line 248 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:248:47 | 248 | EmptyTree::new(current_depth, empty_value.clone()).into(), | ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
)
.into();
current_depth += 1;
Expand All @@ -255,14 +255,14 @@

fn new_with_dense_prefix(depth: usize, prefix_depth: usize, empty_value: &H::Hash) -> Self {
assert!(depth >= prefix_depth);
let mut result: Self = EmptyTree::new(prefix_depth, empty_value.clone())

Check warning on line 258 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:258:61 | 258 | let mut result: Self = EmptyTree::new(prefix_depth, empty_value.clone()) | ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
.alloc_dense()
.into();
let mut current_depth = prefix_depth;
while current_depth < depth {
result = SparseTree::new(
result,
EmptyTree::new(current_depth, empty_value.clone()).into(),

Check warning on line 265 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:265:47 | 265 | EmptyTree::new(current_depth, empty_value.clone()).into(), | ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
)
.into();
current_depth += 1;
Expand All @@ -285,7 +285,7 @@
while current_depth < depth {
result = SparseTree::new(
result,
EmptyTree::new(current_depth, empty_value.clone()).into(),

Check warning on line 288 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:288:47 | 288 | EmptyTree::new(current_depth, empty_value.clone()).into(), | ^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
)
.into();
current_depth += 1;
Expand All @@ -307,7 +307,7 @@
while current_depth < depth {
result = SparseTree::new(
result,
EmptyTree::new(current_depth, empty_leaf.clone()).into(),

Check warning on line 310 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:310:47 | 310 | EmptyTree::new(current_depth, empty_leaf.clone()).into(), | ^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_leaf` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
)
.into();
current_depth += 1;
Expand Down Expand Up @@ -454,7 +454,7 @@

fn write_proof(&self, index: usize, path: &mut Vec<Branch<H>>) {
for depth in (1..=self.depth).rev() {
let val = self.empty_tree_values[depth - 1].clone();

Check warning on line 457 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:457:23 | 457 | let val = self.empty_tree_values[depth - 1].clone(); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.empty_tree_values[depth - 1]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
let branch = if get_turn_at_depth(index, depth) == Turn::Left {
Branch::Left(val)
} else {
Expand Down Expand Up @@ -498,7 +498,7 @@
.flat_map(|(depth, value)| repeat(value).take(1 << depth));
let padded_values = once(&self.empty_tree_values[0])
.chain(values)
.cloned()

Check warning on line 501 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

used `cloned` where `copied` could be used instead

warning: used `cloned` where `copied` could be used instead --> src/lazy_merkle_tree.rs:501:14 | 501 | .cloned() | ^^^^^^ help: try: `copied` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cloned_instead_of_copied
.collect();
DenseTree {
depth: self.depth,
Expand All @@ -509,11 +509,11 @@

#[must_use]
fn root(&self) -> H::Hash {
self.empty_tree_values[self.depth].clone()

Check warning on line 512 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:512:9 | 512 | self.empty_tree_values[self.depth].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.empty_tree_values[self.depth]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}

fn get_leaf(&self) -> H::Hash {
self.empty_tree_values[0].clone()

Check warning on line 516 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:516:9 | 516 | self.empty_tree_values[0].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.empty_tree_values[0]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}
}

Expand Down Expand Up @@ -559,8 +559,8 @@
fn from(children: Children<H>) -> Self {
assert_eq!(children.left.depth(), children.right.depth());
let (depth, root) = {
let left = children.left.clone();

Check warning on line 562 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant clone

warning: redundant clone --> src/lazy_merkle_tree.rs:562:37 | 562 | let left = children.left.clone(); | ^^^^^^^^ help: remove this | note: cloned value is neither consumed nor mutated --> src/lazy_merkle_tree.rs:562:24 | 562 | let left = children.left.clone(); | ^^^^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone note: the lint level is defined here --> src/lib.rs:2:55 | 2 | #![warn(clippy::all, clippy::pedantic, clippy::cargo, clippy::nursery)] | ^^^^^^^^^^^^^^^ = note: `#[warn(clippy::redundant_clone)]` implied by `#[warn(clippy::nursery)]`
let right = children.right.clone();

Check warning on line 563 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

redundant clone

warning: redundant clone --> src/lazy_merkle_tree.rs:563:39 | 563 | let right = children.right.clone(); | ^^^^^^^^ help: remove this | note: cloned value is neither consumed nor mutated --> src/lazy_merkle_tree.rs:563:25 | 563 | let right = children.right.clone(); | ^^^^^^^^^^^^^^^^^^^^^^ = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_clone
let depth = left.depth() + 1;
let root = H::hash_node(&left.root(), &right.root());
(depth, root)
Expand All @@ -577,7 +577,7 @@
fn clone(&self) -> Self {
Self {
depth: self.depth,
root: self.root.clone(),

Check warning on line 580 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:580:23 | 580 | root: self.root.clone(), | ^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.root` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
children: self.children.clone(),
}
}
Expand Down Expand Up @@ -623,7 +623,7 @@
) -> Self {
let Some(children) = &self.children else {
// no children – this is a leaf
return Self::new_leaf(value.clone());

Check warning on line 626 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:626:35 | 626 | return Self::new_leaf(value.clone()); | ^^^^^^^^^^^^^ help: try dereferencing it: `*value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
};

let next_index = clear_turn_at_depth(index, self.depth);
Expand All @@ -649,12 +649,12 @@
}

fn root(&self) -> H::Hash {
self.root.clone()

Check warning on line 652 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:652:9 | 652 | self.root.clone() | ^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.root` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}

fn get_leaf(&self, index: usize) -> H::Hash {
self.children.as_ref().map_or_else(
|| self.root.clone(),

Check warning on line 657 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:657:16 | 657 | || self.root.clone(), | ^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.root` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
|children| {
let next_index = clear_turn_at_depth(index, self.depth);
if get_turn_at_depth(index, self.depth) == Turn::Left {
Expand Down Expand Up @@ -690,7 +690,7 @@
let first_leaf_index = 1 << depth;
let storage_size = 1 << (depth + 1);
assert!(values.len() <= leaf_count);
let mut storage = vec![empty_leaf.clone(); storage_size];

Check warning on line 693 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:693:32 | 693 | let mut storage = vec![empty_leaf.clone(); storage_size]; | ^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*empty_leaf` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
storage[first_leaf_index..(first_leaf_index + values.len())].clone_from_slice(values);
for i in (1..first_leaf_index).rev() {
let left = &storage[2 * i];
Expand All @@ -708,7 +708,7 @@
where
F: FnOnce(DenseTreeRef<H>) -> R,
{
let guard = self.storage.lock().expect("lock poisoned, terminating");

Check warning on line 711 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/lazy_merkle_tree.rs:711:13 | 710 | / { 711 | | let guard = self.storage.lock().expect("lock poisoned, terminating"); | | ^^^^^ 712 | | let r = DenseTreeRef { 713 | | depth: self.depth, ... | 718 | | fun(r) 719 | | } | |_____- temporary `guard` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening = note: `#[warn(clippy::significant_drop_tightening)]` implied by `#[warn(clippy::nursery)]` help: merge the temporary construction with its single usage | 711 ~ 712 + let r = self.storage.lock().expect("lock poisoned, terminating").; | help: remove separated single usage | 712 - let r = DenseTreeRef { 713 - depth: self.depth, 714 - root_index: self.root_index, 715 - storage: &guard, 716 - locked_storage: &self.storage, 717 - }; 712 + |
let r = DenseTreeRef {
depth: self.depth,
root_index: self.root_index,
Expand All @@ -725,7 +725,7 @@
fn get_leaf(&self, index: usize) -> H::Hash {
self.with_ref(|r| {
let leaf_index_in_dense_tree = index + (self.root_index << self.depth);
r.storage[leaf_index_in_dense_tree].clone()

Check warning on line 728 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:728:13 | 728 | r.storage[leaf_index_in_dense_tree].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `r.storage[leaf_index_in_dense_tree]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
})
}

Expand All @@ -744,9 +744,9 @@
}

fn update_with_mutation(&self, index: usize, value: &H::Hash) {
let mut storage = self.storage.lock().expect("lock poisoned, terminating");

Check warning on line 747 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/lazy_merkle_tree.rs:747:17 | 746 | fn update_with_mutation(&self, index: usize, value: &H::Hash) { | ___________________________________________________________________- 747 | | let mut storage = self.storage.lock().expect("lock poisoned, terminating"); | | ^^^^^^^ 748 | | let leaf_index_in_dense_tree = index + (self.root_index << self.depth); 749 | | storage[leaf_index_in_dense_tree] = value.clone(); ... | 756 | | } 757 | | } | |_____- temporary `storage` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening help: drop the temporary after the end of its last usage | 754 ~ storage[current] = H::hash_node(left, right); 755 + drop(storage); |
let leaf_index_in_dense_tree = index + (self.root_index << self.depth);
storage[leaf_index_in_dense_tree] = value.clone();

Check warning on line 749 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:749:45 | 749 | storage[leaf_index_in_dense_tree] = value.clone(); | ^^^^^^^^^^^^^ help: try dereferencing it: `*value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
let mut current = leaf_index_in_dense_tree / 2;
while current > 0 {
let left = &storage[2 * current];
Expand All @@ -757,7 +757,7 @@
}

fn root(&self) -> H::Hash {
self.storage.lock().unwrap()[self.root_index].clone()

Check warning on line 760 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:760:9 | 760 | self.storage.lock().unwrap()[self.root_index].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.storage.lock().unwrap()[self.root_index]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}
}

Expand Down Expand Up @@ -786,7 +786,7 @@

impl<'a, H: Hasher> DenseTreeRef<'a, H> {
fn root(&self) -> H::Hash {
self.storage[self.root_index].clone()

Check warning on line 789 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:789:9 | 789 | self.storage[self.root_index].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.storage[self.root_index]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}

const fn left(&self) -> DenseTreeRef<H> {
Expand Down Expand Up @@ -823,7 +823,7 @@

fn update(&self, index: usize, hash: &H::Hash) -> SparseTree<H> {
if self.depth == 0 {
return SparseTree::new_leaf(hash.clone());

Check warning on line 826 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:826:41 | 826 | return SparseTree::new_leaf(hash.clone()); | ^^^^^^^^^^^^ help: try dereferencing it: `*hash` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}
let next_index = clear_turn_at_depth(index, self.depth);
if get_turn_at_depth(index, self.depth) == Turn::Left {
Expand Down Expand Up @@ -945,7 +945,7 @@
where
F: FnOnce(DenseTreeMMapRef<H>) -> R,
{
let guard = self.storage.lock().expect("lock poisoned, terminating");

Check warning on line 948 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/lazy_merkle_tree.rs:948:13 | 947 | / { 948 | | let guard = self.storage.lock().expect("lock poisoned, terminating"); | | ^^^^^ 949 | | let r = DenseTreeMMapRef { 950 | | depth: self.depth, ... | 955 | | fun(r) 956 | | } | |_____- temporary `guard` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening help: merge the temporary construction with its single usage | 948 ~ 949 + let r = self.storage.lock().expect("lock poisoned, terminating").; | help: remove separated single usage | 949 - let r = DenseTreeMMapRef { 950 - depth: self.depth, 951 - root_index: self.root_index, 952 - storage: &guard, 953 - locked_storage: &self.storage, 954 - }; 949 + |
let r = DenseTreeMMapRef {
depth: self.depth,
root_index: self.root_index,
Expand All @@ -962,7 +962,7 @@
fn get_leaf(&self, index: usize) -> H::Hash {
self.with_ref(|r| {
let leaf_index_in_dense_tree = index + (self.root_index << self.depth);
r.storage[leaf_index_in_dense_tree].clone()

Check warning on line 965 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:965:13 | 965 | r.storage[leaf_index_in_dense_tree].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `r.storage[leaf_index_in_dense_tree]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
})
}

Expand All @@ -981,9 +981,9 @@
}

fn update_with_mutation(&self, index: usize, value: &H::Hash) {
let mut storage = self.storage.lock().expect("lock poisoned, terminating");

Check warning on line 984 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

temporary with significant `Drop` can be early dropped

warning: temporary with significant `Drop` can be early dropped --> src/lazy_merkle_tree.rs:984:17 | 983 | fn update_with_mutation(&self, index: usize, value: &H::Hash) { | ___________________________________________________________________- 984 | | let mut storage = self.storage.lock().expect("lock poisoned, terminating"); | | ^^^^^^^ 985 | | let leaf_index_in_dense_tree = index + (self.root_index << self.depth); 986 | | storage[leaf_index_in_dense_tree] = value.clone(); ... | 993 | | } 994 | | } | |_____- temporary `storage` is currently being dropped at the end of its contained scope | = note: this might lead to unnecessary resource contention = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#significant_drop_tightening help: drop the temporary after the end of its last usage | 991 ~ storage[current] = H::hash_node(left, right); 992 + drop(storage); |
let leaf_index_in_dense_tree = index + (self.root_index << self.depth);
storage[leaf_index_in_dense_tree] = value.clone();

Check warning on line 986 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:986:45 | 986 | storage[leaf_index_in_dense_tree] = value.clone(); | ^^^^^^^^^^^^^ help: try dereferencing it: `*value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
let mut current = leaf_index_in_dense_tree / 2;
while current > 0 {
let left = &storage[2 * current];
Expand All @@ -994,7 +994,7 @@
}

fn root(&self) -> H::Hash {
self.storage.lock().expect("lock poisoned")[self.root_index].clone()

Check warning on line 997 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:997:9 | 997 | self.storage.lock().expect("lock poisoned")[self.root_index].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.storage.lock().expect("lock poisoned")[self.root_index]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}
}

Expand Down Expand Up @@ -1023,7 +1023,7 @@

impl<'a, H: Hasher> DenseTreeMMapRef<'a, H> {
fn root(&self) -> H::Hash {
self.storage[self.root_index].clone()

Check warning on line 1026 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:1026:9 | 1026 | self.storage[self.root_index].clone() | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.storage[self.root_index]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}

const fn left(&self) -> DenseTreeMMapRef<H> {
Expand Down Expand Up @@ -1060,7 +1060,7 @@

fn update(&self, index: usize, hash: &H::Hash) -> SparseTree<H> {
if self.depth == 0 {
return SparseTree::new_leaf(hash.clone());

Check warning on line 1063 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:1063:41 | 1063 | return SparseTree::new_leaf(hash.clone()); | ^^^^^^^^^^^^ help: try dereferencing it: `*hash` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
}
let next_index = clear_turn_at_depth(index, self.depth);
if get_turn_at_depth(index, self.depth) == Turn::Left {
Expand Down Expand Up @@ -1119,21 +1119,8 @@
storage_size: usize,
) -> Result<Self, DenseMMapError> {
let size_of_val = std::mem::size_of_val(initial_value);
let initial_vals: Vec<H::Hash> = vec![initial_value.clone(); storage_size];

Check warning on line 1122 in src/lazy_merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/lazy_merkle_tree.rs:1122:47 | 1122 | let initial_vals: Vec<H::Hash> = vec![initial_value.clone(); storage_size]; | ^^^^^^^^^^^^^^^^^^^^^ help: try dereferencing it: `*initial_value` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy

// cast Hash pointer to u8 pointer
let ptr = initial_vals.as_ptr().cast::<u8>();

let size_of_buffer: usize = storage_size * size_of_val;

let buf: &[u8] = unsafe {
// moving pointer by u8 for storage_size * size of hash would get us the full
// buffer
std::slice::from_raw_parts(ptr, size_of_buffer)
};

// assure that buffer is correct length
assert_eq!(buf.len(), size_of_buffer);
let buf: &[u8] = bytemuck::cast_slice(&initial_vals);

let file_size: u64 = storage_size as u64 * size_of_val as u64;

Expand Down Expand Up @@ -1218,17 +1205,13 @@
type Target = [H::Hash];

fn deref(&self) -> &Self::Target {
let bytes: &[u8] = &self.mmap;
let ptr = bytes.as_ptr().cast::<H::Hash>();
unsafe { std::slice::from_raw_parts(ptr, bytes.len() / std::mem::size_of::<H::Hash>()) }
bytemuck::cast_slice(self.mmap.as_slice())
}
}

impl<H: Hasher> DerefMut for MmapMutWrapper<H> {
fn deref_mut(&mut self) -> &mut Self::Target {
let bytes: &mut [u8] = self.mmap.as_mut_slice();
let ptr = bytes.as_mut_ptr().cast::<H::Hash>();
unsafe { std::slice::from_raw_parts_mut(ptr, bytes.len() / std::mem::size_of::<H::Hash>()) }
bytemuck::cast_slice_mut(self.mmap.as_mut_slice())
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/merkle_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! * Disk based storage backend (using mmaped files should be easy)

use crate::Field;
use bytemuck::Pod;
use serde::{Deserialize, Serialize};
use std::{
fmt::Debug,
Expand All @@ -14,7 +15,7 @@
/// Hash types, values and algorithms for a Merkle tree
pub trait Hasher {
/// Type of the leaf and node hashes
type Hash: Clone + Eq + Serialize + Debug;
type Hash: Clone + Eq + Serialize + Debug + Pod;

/// Compute the hash of an intermediate node
fn hash_node(left: &Self::Hash, right: &Self::Hash) -> Self::Hash;
Expand Down Expand Up @@ -87,7 +88,7 @@
.rev()
.enumerate()
.flat_map(|(depth, hash)| repeat(hash).take(1 << depth))
.cloned()

Check warning on line 91 in src/merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

used `cloned` where `copied` could be used instead

warning: used `cloned` where `copied` could be used instead --> src/merkle_tree.rs:91:14 | 91 | .cloned() | ^^^^^^ help: try: `copied` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#cloned_instead_of_copied note: the lint level is defined here --> src/lib.rs:2:22 | 2 | #![warn(clippy::all, clippy::pedantic, clippy::cargo, clippy::nursery)] | ^^^^^^^^^^^^^^^^ = note: `#[warn(clippy::cloned_instead_of_copied)]` implied by `#[warn(clippy::pedantic)]`
.collect::<Vec<_>>();
debug_assert!(nodes.len() == (1 << depth) - 1);

Expand All @@ -108,7 +109,7 @@

#[must_use]
pub fn root(&self) -> H::Hash {
self.nodes[0].clone()

Check warning on line 112 in src/merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/merkle_tree.rs:112:9 | 112 | self.nodes[0].clone() | ^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.nodes[0]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy note: the lint level is defined here --> src/lib.rs:2:9 | 2 | #![warn(clippy::all, clippy::pedantic, clippy::cargo, clippy::nursery)] | ^^^^^^^^^^^ = note: `#[warn(clippy::clone_on_copy)]` implied by `#[warn(clippy::all)]`
}

pub fn set(&mut self, leaf: usize, hash: H::Hash) {
Expand Down Expand Up @@ -149,8 +150,8 @@
while let Some(parent) = parent(index) {
// Add proof for node at index to parent
path.push(match index & 1 {
1 => Branch::Left(self.nodes[index + 1].clone()),

Check warning on line 153 in src/merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/merkle_tree.rs:153:35 | 153 | 1 => Branch::Left(self.nodes[index + 1].clone()), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.nodes[index + 1]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
0 => Branch::Right(self.nodes[index - 1].clone()),

Check warning on line 154 in src/merkle_tree.rs

View workflow job for this annotation

GitHub Actions / clippy

using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait

warning: using `clone` on type `<H as Hasher>::Hash` which implements the `Copy` trait --> src/merkle_tree.rs:154:36 | 154 | 0 => Branch::Right(self.nodes[index - 1].clone()), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try removing the `clone` call: `self.nodes[index - 1]` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#clone_on_copy
_ => unreachable!(),
});
index = parent;
Expand Down
Loading