From 697f4dbc8eda2128d39973bf712574367583dad7 Mon Sep 17 00:00:00 2001 From: Sergey Timoshin Date: Mon, 23 Dec 2024 17:04:42 +0000 Subject: [PATCH 1/7] chore: add GitHub Actions workflows and configuration for linting, testing, and dependency updates --- .github/actionlint.yaml | 11 ++++++ .github/dependabot.yml | 18 ++++++++++ .github/workflows/ci-lint.yml | 15 +++++++++ .github/workflows/js.yml | 62 ++++++++++++++++++++++++++++++++++ .github/workflows/rust.yml | 63 +++++++++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 .github/actionlint.yaml create mode 100644 .github/dependabot.yml create mode 100644 .github/workflows/ci-lint.yml create mode 100644 .github/workflows/js.yml create mode 100644 .github/workflows/rust.yml diff --git a/.github/actionlint.yaml b/.github/actionlint.yaml new file mode 100644 index 0000000..3f55237 --- /dev/null +++ b/.github/actionlint.yaml @@ -0,0 +1,11 @@ +self-hosted-runner: + # Labels of self-hosted runner in array of strings. + labels: + - buildjet-2vcpu-ubuntu-2204 + - buildjet-4vcpu-ubuntu-2204 + - buildjet-8vcpu-ubuntu-2204 + - buildjet-16vcpu-ubuntu-2204 +# Configuration variables in array of strings defined in your repository or +# organization. `null` means disabling configuration variables check. +# Empty array means no configuration variable is allowed. +config-variables: null diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000..b8bade9 --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,18 @@ +# Please see the documentation for all configuration options: +# https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file + +version: 2 +updates: + - package-ecosystem: "cargo" + directory: "/" + schedule: + interval: "weekly" + - package-ecosystem: "github-actions" + directory: "/" + schedule: + interval: "weekly" + # pnpm is handled by the "npm" package ecosystem + - package-ecosystem: "npm" + directory: "/" + schedule: + interval: "weekly" diff --git a/.github/workflows/ci-lint.yml b/.github/workflows/ci-lint.yml new file mode 100644 index 0000000..0bc7914 --- /dev/null +++ b/.github/workflows/ci-lint.yml @@ -0,0 +1,15 @@ +name: Lint GitHub Actions workflows +on: [push, pull_request] + +jobs: + actionlint: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Download actionlint + id: get_actionlint + run: bash <(curl https://raw.githubusercontent.com/rhysd/actionlint/main/scripts/download-actionlint.bash) + shell: bash + - name: Check workflow files + run: ${{ steps.get_actionlint.outputs.executable }} -color + shell: bash \ No newline at end of file diff --git a/.github/workflows/js.yml b/.github/workflows/js.yml new file mode 100644 index 0000000..cd0a57d --- /dev/null +++ b/.github/workflows/js.yml @@ -0,0 +1,62 @@ +on: + push: + branches: + - main + pull_request: + branches: + - "*" + types: + - opened + - synchronize + - reopened + - ready_for_review + +name: js-tests + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + stateless-js: + name: stateless-js + if: github.event.pull_request.draft == false + runs-on: ubuntu-latest + + steps: + - name: Checkout sources + uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + targets: wasm32-unknown-unknown + + - name: Install wasm-pack + run: curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh + + - name: Setup Node.js + uses: actions/setup-node@v4 + with: + node-version: '22' + + - name: Install pnpm + uses: pnpm/action-setup@v4 + with: + version: 9 + + - name: Cache dependencies + uses: actions/cache@v4 + with: + path: | + ~/.cargo + **/target + **/node_modules + key: ${{ runner.os }}-deps-${{ hashFiles('**/Cargo.lock', '**/pnpm-lock.yaml') }} + + - name: Run hasher.rs tests + working-directory: hasher.rs + run: | + pnpm install + pnpm build + pnpm test \ No newline at end of file diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml new file mode 100644 index 0000000..19045ff --- /dev/null +++ b/.github/workflows/rust.yml @@ -0,0 +1,63 @@ +on: + push: + branches: + - main + paths: + - ".cargo/**" + - "**/*.rs" + - "**/Cargo.*" + - ".github/workflows/rust.yml" + pull_request: + branches: + - "*" + paths: + - "**/*.rs" + - "**/Cargo.*" + - ".github/workflows/rust.yml" + + types: + - opened + - synchronize + - reopened + - ready_for_review + +name: rust + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + test: + if: github.event.pull_request.draft == false + runs-on: ubuntu-latest + steps: + - name: Checkout sources + uses: actions/checkout@v4 + + - name: Install Rust + uses: dtolnay/rust-toolchain@stable + with: + components: rustfmt, clippy + + - name: Cache dependencies + uses: actions/cache@v4 + with: + path: | + ~/.cargo + **/target + key: ${{ runner.os }}-rust-${{ hashFiles('**/Cargo.lock') }} + + - name: Run cargo fmt + run: cargo fmt --all -- --check + + - name: Run clippy + run: cargo clippy --all-targets -- -D warnings + + - name: Run tests + run: | + for dir in concurrent bounded-vec hash-set indexed; do + cd $dir + cargo test --all-targets + cd .. + done \ No newline at end of file From 6134dac2ce73eeff693e2392fb54ab19d013f061 Mon Sep 17 00:00:00 2001 From: Sergey Timoshin Date: Mon, 23 Dec 2024 17:06:00 +0000 Subject: [PATCH 2/7] chore: rename js job in GitHub Actions workflow for clarity --- .github/workflows/js.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/js.yml b/.github/workflows/js.yml index cd0a57d..53179cd 100644 --- a/.github/workflows/js.yml +++ b/.github/workflows/js.yml @@ -18,8 +18,8 @@ concurrency: cancel-in-progress: true jobs: - stateless-js: - name: stateless-js + js: + name: js tests if: github.event.pull_request.draft == false runs-on: ubuntu-latest From 9e0503a19fbdd4c04c400a5a3ecf45f1071e217e Mon Sep 17 00:00:00 2001 From: Sergey Timoshin Date: Mon, 23 Dec 2024 17:10:02 +0000 Subject: [PATCH 3/7] chore: consolidate formatting, linting, and testing steps in GitHub Actions workflow --- .github/workflows/rust.yml | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 19045ff..86fa6e8 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -48,16 +48,12 @@ jobs: **/target key: ${{ runner.os }}-rust-${{ hashFiles('**/Cargo.lock') }} - - name: Run cargo fmt - run: cargo fmt --all -- --check - - - name: Run clippy - run: cargo clippy --all-targets -- -D warnings - - - name: Run tests + - name: Check formatting, lint & run tests run: | for dir in concurrent bounded-vec hash-set indexed; do cd $dir + cargo fmt -- --check + cargo clippy --all-targets -- -D warnings cargo test --all-targets cd .. done \ No newline at end of file From 70f35ec3afea7c2860af79b693fca34e569c3ce5 Mon Sep 17 00:00:00 2001 From: ananas-block Date: Tue, 24 Dec 2024 09:30:05 +0000 Subject: [PATCH 4/7] chore: add lint script --- .github/workflows/rust.yml | 12 +++--------- scripts/lint.sh | 11 +++++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) create mode 100644 scripts/lint.sh diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 86fa6e8..2914c46 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -5,7 +5,7 @@ on: paths: - ".cargo/**" - "**/*.rs" - - "**/Cargo.*" + - "**/Cargo.*" - ".github/workflows/rust.yml" pull_request: branches: @@ -47,13 +47,7 @@ jobs: ~/.cargo **/target key: ${{ runner.os }}-rust-${{ hashFiles('**/Cargo.lock') }} - + - name: Check formatting, lint & run tests run: | - for dir in concurrent bounded-vec hash-set indexed; do - cd $dir - cargo fmt -- --check - cargo clippy --all-targets -- -D warnings - cargo test --all-targets - cd .. - done \ No newline at end of file + ./scripts/lint.sh diff --git a/scripts/lint.sh b/scripts/lint.sh new file mode 100644 index 0000000..0f38685 --- /dev/null +++ b/scripts/lint.sh @@ -0,0 +1,11 @@ +#!/usr/bin/env bash + +set -e + +for dir in concurrent bounded-vec hash-set indexed; do + cd $dir + cargo fmt -- --check + cargo clippy --all-targets -- -D warnings + cargo test --all-targets + cd .. +done \ No newline at end of file From 80d634afa4da6f788dee8b29ed3b7da400c11938 Mon Sep 17 00:00:00 2001 From: ananas-block Date: Tue, 24 Dec 2024 09:30:58 +0000 Subject: [PATCH 5/7] chore: removed ignored test to print test data --- indexed/tests/tests.rs | 223 ----------------------------------------- 1 file changed, 223 deletions(-) diff --git a/indexed/tests/tests.rs b/indexed/tests/tests.rs index 5962201..c4b93c4 100644 --- a/indexed/tests/tests.rs +++ b/indexed/tests/tests.rs @@ -674,229 +674,6 @@ pub fn functional_non_inclusion_test() { .unwrap(); } -// /** -// * -// * Range Hash (value, next_index, next_value) -> need next value not next value index -// * Update of a range: -// * 1. Find the low element, low element points to the next hight element -// * 2. update low element with H (low_value, new_inserted_value_index, new_inserted_value) -// * 3. append the tree with H(new_inserted_value,index_of_next_value, next_value) -// * -// */ -// /// This test is generating a situation where the low element has to be patched. -// /// Scenario: -// /// 1. two parties start with the initialized indexing array -// /// 2. both parties compute their values with the empty indexed Merkle tree state -// /// 3. party one inserts first -// /// 4. party two needs to patch the low element because the low element has changed -// /// 5. party two inserts -// Commented because the test is not working -// TODO: figure out address Merkle tree changelog -// #[test] -// pub fn functional_changelog_test() { -// let address_1 = 30_u32.to_biguint().unwrap(); -// let address_2 = 10_u32.to_biguint().unwrap(); -// cargo test -- --nocapture print_test_data -#[test] -#[ignore = "only used to generate test data"] -pub fn print_test_data() { - let mut relayer_indexing_array = IndexedArray::::default(); - relayer_indexing_array.init().unwrap(); - let mut relayer_merkle_tree = - reference::IndexedMerkleTree::::new(4, 0).unwrap(); - relayer_merkle_tree.init().unwrap(); - let root = relayer_merkle_tree.root(); - let root_bn = BigUint::from_bytes_be(&root); - println!("root {:?}", root_bn); - println!("indexed mt inited root {:?}", relayer_merkle_tree.root()); - - let address1 = 30_u32.to_biguint().unwrap(); - - let test_address: BigUint = BigUint::from_bytes_be(&[ - 171, 159, 63, 33, 62, 94, 156, 27, 61, 216, 203, 164, 91, 229, 110, 16, 230, 124, 129, 133, - 222, 159, 99, 235, 50, 181, 94, 203, 105, 23, 82, - ]); - - let non_inclusion_proof_0 = relayer_merkle_tree - .get_non_inclusion_proof(&test_address, &relayer_indexing_array) - .unwrap(); - - println!("non inclusion proof init {:?}", non_inclusion_proof_0); - - relayer_merkle_tree - .append(&address1, &mut relayer_indexing_array) - .unwrap(); - - println!( - "indexed mt with one append {:?}", - relayer_merkle_tree.root() - ); - let root_bn = BigUint::from_bytes_be(&relayer_merkle_tree.root()); - println!("indexed mt with one append {:?}", root_bn); - - let proof = relayer_merkle_tree.get_proof_of_leaf(2, true).unwrap(); - - let leaf = relayer_merkle_tree.merkle_tree.get_leaf(2).unwrap(); - let leaf_bn = BigUint::from_bytes_be(&leaf); - println!("(30) leaf_hash[2] = {:?}", leaf_bn); - - let subtrees = relayer_merkle_tree.merkle_tree.get_subtrees(); - for subtree in subtrees { - let subtree_bn = BigUint::from_bytes_be(&subtree); - println!("subtree = {:?}", subtree_bn); - } - - let res = relayer_merkle_tree.merkle_tree.verify(&leaf, &proof, 2); - println!("verify leaf 2 {:?}", res); - - println!( - "indexed array state element 0 {:?}", - relayer_indexing_array.get(0).unwrap() - ); - println!( - "indexed array state element 1 {:?}", - relayer_indexing_array.get(1).unwrap() - ); - println!( - "indexed array state element 2 {:?}", - relayer_indexing_array.get(2).unwrap() - ); - - let address2 = 42_u32.to_biguint().unwrap(); - - let non_inclusion_proof = relayer_merkle_tree - .get_non_inclusion_proof(&address2, &relayer_indexing_array) - .unwrap(); - println!("non inclusion proof address 2 {:?}", non_inclusion_proof); - - relayer_merkle_tree - .append(&address2, &mut relayer_indexing_array) - .unwrap(); - - println!( - "indexed mt with two appends {:?}", - relayer_merkle_tree.root() - ); - let root_bn = BigUint::from_bytes_be(&relayer_merkle_tree.root()); - println!("indexed mt with two appends {:?}", root_bn); - - println!( - "indexed array state element 0 {:?}", - relayer_indexing_array.get(0).unwrap() - ); - println!( - "indexed array state element 1 {:?}", - relayer_indexing_array.get(1).unwrap() - ); - println!( - "indexed array state element 2 {:?}", - relayer_indexing_array.get(2).unwrap() - ); - println!( - "indexed array state element 3 {:?}", - relayer_indexing_array.get(3).unwrap() - ); - - let address3 = 12_u32.to_biguint().unwrap(); - - let non_inclusion_proof = relayer_merkle_tree - .get_non_inclusion_proof(&address3, &relayer_indexing_array) - .unwrap(); - - relayer_merkle_tree - .append(&address3, &mut relayer_indexing_array) - .unwrap(); - - println!( - "indexed mt with three appends {:?}", - relayer_merkle_tree.root() - ); - let root_bn = BigUint::from_bytes_be(&relayer_merkle_tree.root()); - println!("indexed mt with three appends {:?}", root_bn); - - println!("non inclusion proof address 3 {:?}", non_inclusion_proof); - println!( - "indexed array state element 0 {:?}", - relayer_indexing_array.get(0).unwrap() - ); - println!( - "indexed array state element 1 {:?}", - relayer_indexing_array.get(1).unwrap() - ); - println!( - "indexed array state element 2 {:?}", - relayer_indexing_array.get(2).unwrap() - ); - println!( - "indexed array state element 3 {:?}", - relayer_indexing_array.get(3).unwrap() - ); - println!( - "indexed array state element 4 {:?}", - relayer_indexing_array.get(4).unwrap() - ); - - // // indexed array: - // // element: 0 - // // value: 0 - // // next_value: 30 - // // index: 0 - // // element: 1 - // // value: 30 - // // next_value: 0 - // // index: 1 - // // merkle tree: - // // leaf index: 0 = H(0, 1, 30) //Hash(value, next_index, next_value) - // // leaf index: 1 = H(30, 0, 0) - // let indexed_array_element_0 = relayer_indexing_array.get(0).unwrap(); - // assert_eq!(indexed_array_element_0.value, 0_u32.to_biguint().unwrap()); - // assert_eq!(indexed_array_element_0.next_index, 1); - // assert_eq!(indexed_array_element_0.index, 0); - // let indexed_array_element_1 = relayer_indexing_array.get(1).unwrap(); - // assert_eq!(indexed_array_element_1.value, 30_u32.to_biguint().unwrap()); - // assert_eq!(indexed_array_element_1.next_index, 0); - // assert_eq!(indexed_array_element_1.index, 1); - - // let leaf_0 = relayer_merkle_tree.merkle_tree.leaf(0); - // let leaf_1 = relayer_merkle_tree.merkle_tree.leaf(1); - // assert_eq!( - // leaf_0, - // Poseidon::hashv(&[ - // &0_u32.to_biguint().unwrap().to_bytes_be(), - // &1_u32.to_biguint().unwrap().to_bytes_be(), - // &30_u32.to_biguint().unwrap().to_bytes_be() - // ]) - // .unwrap() - // ); - // assert_eq!( - // leaf_1, - // Poseidon::hashv(&[ - // &30_u32.to_biguint().unwrap().to_bytes_be(), - // &0_u32.to_biguint().unwrap().to_bytes_be(), - // &0_u32.to_biguint().unwrap().to_bytes_be() - // ]) - // .unwrap() - // ); - - // let non_inclusion_proof = relayer_merkle_tree - // .get_non_inclusion_proof(&10_u32.to_biguint().unwrap(), &relayer_indexing_array) - // .unwrap(); - // assert_eq!(non_inclusion_proof.root, relayer_merkle_tree.root()); - // assert_eq!( - // non_inclusion_proof.value, - // bigint_to_be_bytes_array::<32>(&10_u32.to_biguint().unwrap()).unwrap() - // ); - // assert_eq!(non_inclusion_proof.leaf_lower_range_value, [0; 32]); - // assert_eq!( - // non_inclusion_proof.leaf_higher_range_value, - // bigint_to_be_bytes_array::<32>(&30_u32.to_biguint().unwrap()).unwrap() - // ); - // assert_eq!(non_inclusion_proof.leaf_index, 0); - - // relayer_merkle_tree - // .verify_non_inclusion_proof(&non_inclusion_proof) - // .unwrap(); -} /// Performs conflicting Merkle tree updates where: /// /// 1. Party one inserts 30. From 44cfa79d4736f95c185f8cd42ae3fff71edd46b7 Mon Sep 17 00:00:00 2001 From: Sergey Timoshin Date: Tue, 24 Dec 2024 09:36:30 +0000 Subject: [PATCH 6/7] chmod +x scripts/lint.sh --- scripts/lint.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 scripts/lint.sh diff --git a/scripts/lint.sh b/scripts/lint.sh old mode 100644 new mode 100755 From 5b159b4b30195f8eee64ac69f7367d1b54e978e7 Mon Sep 17 00:00:00 2001 From: ananas-block Date: Wed, 25 Dec 2024 09:03:32 +0000 Subject: [PATCH 7/7] fix: lint errors --- bounded-vec/src/lib.rs | 6 +-- bounded-vec/src/offset/copy.rs | 4 +- concurrent/src/lib.rs | 1 + concurrent/src/zero_copy.rs | 8 ++-- concurrent/tests/tests.rs | 34 ++++++++--------- hash-set/src/lib.rs | 67 +++++++++++++++------------------- hash-set/src/zero_copy.rs | 17 ++++----- indexed/src/array.rs | 2 +- indexed/src/zero_copy.rs | 12 +++--- indexed/tests/tests.rs | 15 ++++---- 10 files changed, 77 insertions(+), 89 deletions(-) diff --git a/bounded-vec/src/lib.rs b/bounded-vec/src/lib.rs index 7f7c673..6b67eab 100644 --- a/bounded-vec/src/lib.rs +++ b/bounded-vec/src/lib.rs @@ -1270,7 +1270,7 @@ mod test { } for element in vec.iter_mut() { - *element = *element * 2; + *element *= 2; } for (i, element) in vec.iter().enumerate() { @@ -1492,13 +1492,13 @@ mod test { #[test] fn test_cyclic_bounded_vec_first() { - let mut vec = CyclicBoundedVec::with_capacity(500); + let mut vec = CyclicBoundedVec::::with_capacity(500); assert!(vec.first().is_none()); for i in 0..1000 { vec.push(i); - assert_eq!(vec.first(), Some(&((i as u64).saturating_sub(499)))); + assert_eq!(vec.first(), Some(&((i).saturating_sub(499)))); } } diff --git a/bounded-vec/src/offset/copy.rs b/bounded-vec/src/offset/copy.rs index acc94a7..2a3024a 100644 --- a/bounded-vec/src/offset/copy.rs +++ b/bounded-vec/src/offset/copy.rs @@ -214,7 +214,7 @@ mod test { assert_eq!(offset, 0); let vec: BoundedVec = read_bounded_vec_at(&buf, &mut offset, &metadata); for (i, element) in vec.iter().enumerate() { - assert_eq!(i as i64, -(*element as i64)); + assert_eq!(i as i64, -*element); } assert_eq!(offset, 256); @@ -223,7 +223,7 @@ mod test { assert_eq!(offset, 256); let vec: BoundedVec = read_bounded_vec_at(&buf, &mut offset, &metadata); for (i, element) in vec.iter().enumerate() { - assert_eq!(i as u64, *element as u64); + assert_eq!(i as u64, *element); } assert_eq!(offset, 512); diff --git a/concurrent/src/lib.rs b/concurrent/src/lib.rs index 0ab48d1..6f9c1c6 100644 --- a/concurrent/src/lib.rs +++ b/concurrent/src/lib.rs @@ -644,6 +644,7 @@ where fn update_canopy(&mut self, first_changelog_index: usize, num_leaves: usize) { for i in 0..num_leaves { + println!("canopy: {:?}", self.canopy); let changelog_index = (first_changelog_index + i) % self.changelog.capacity(); for (i, path_node) in self.changelog[changelog_index] .path diff --git a/concurrent/src/zero_copy.rs b/concurrent/src/zero_copy.rs index a2e1103..c728a57 100644 --- a/concurrent/src/zero_copy.rs +++ b/concurrent/src/zero_copy.rs @@ -128,7 +128,7 @@ where } } -impl<'a, H, const HEIGHT: usize> Deref for ConcurrentMerkleTreeZeroCopy<'a, H, HEIGHT> +impl Deref for ConcurrentMerkleTreeZeroCopy<'_, H, HEIGHT> where H: Hasher, { @@ -139,7 +139,7 @@ where } } -impl<'a, H, const HEIGHT: usize> Drop for ConcurrentMerkleTreeZeroCopy<'a, H, HEIGHT> +impl Drop for ConcurrentMerkleTreeZeroCopy<'_, H, HEIGHT> where H: Hasher, { @@ -244,7 +244,7 @@ where } } -impl<'a, H, const HEIGHT: usize> Deref for ConcurrentMerkleTreeZeroCopyMut<'a, H, HEIGHT> +impl Deref for ConcurrentMerkleTreeZeroCopyMut<'_, H, HEIGHT> where H: Hasher, { @@ -254,7 +254,7 @@ where &self.0.merkle_tree } } -impl<'a, H, const HEIGHT: usize> DerefMut for ConcurrentMerkleTreeZeroCopyMut<'a, H, HEIGHT> +impl DerefMut for ConcurrentMerkleTreeZeroCopyMut<'_, H, HEIGHT> where H: Hasher, { diff --git a/concurrent/tests/tests.rs b/concurrent/tests/tests.rs index c1ce8b1..f409dec 100644 --- a/concurrent/tests/tests.rs +++ b/concurrent/tests/tests.rs @@ -289,7 +289,7 @@ fn invalid_updates( let res = merkle_tree.update( changelog_index, &invalid_old_leaf, - &new_leaf, + new_leaf, 0, &mut proof_clone, ); @@ -392,9 +392,8 @@ where ]; let mut expected_canopy = Vec::new(); - for canopy_level in 0..CANOPY { - println!("canopy_level: {canopy_level}"); - expected_canopy.extend_from_slice(&canopy_levels[canopy_level]); + for canopy_level in canopy_levels[..CANOPY].iter() { + expected_canopy.extend_from_slice(canopy_level); } assert_eq!(merkle_tree.changelog_index(), 4 % CHANGELOG); @@ -468,8 +467,8 @@ where ][..], ]; let mut expected_canopy = Vec::new(); - for canopy_level in 0..CANOPY { - expected_canopy.extend_from_slice(&canopy_levels[canopy_level]); + for canopy_level in canopy_levels[..CANOPY].iter() { + expected_canopy.extend_from_slice(canopy_level); } assert_eq!(merkle_tree.changelog_index(), 5 % CHANGELOG); @@ -542,8 +541,8 @@ where ][..], ]; let mut expected_canopy = Vec::new(); - for canopy_level in 0..CANOPY { - expected_canopy.extend_from_slice(&canopy_levels[canopy_level]); + for canopy_level in canopy_levels[..CANOPY].iter() { + expected_canopy.extend_from_slice(canopy_level); } assert_eq!(merkle_tree.changelog_index(), 6 % CHANGELOG); @@ -615,8 +614,8 @@ where ][..], ]; let mut expected_canopy = Vec::new(); - for canopy_level in 0..CANOPY { - expected_canopy.extend_from_slice(&canopy_levels[canopy_level]); + for canopy_level in canopy_levels[..CANOPY].iter() { + expected_canopy.extend_from_slice(canopy_level); } assert_eq!(merkle_tree.changelog_index(), 7 % CHANGELOG); @@ -688,8 +687,8 @@ where ][..], ]; let mut expected_canopy = Vec::new(); - for canopy_level in 0..CANOPY { - expected_canopy.extend_from_slice(&canopy_levels[canopy_level]); + for canopy_level in canopy_levels[..CANOPY].iter() { + expected_canopy.extend_from_slice(canopy_level); } assert_eq!(merkle_tree.changelog_index(), 8 % CHANGELOG); @@ -1395,7 +1394,7 @@ async fn test_spl_compat() { .update(changelog_index, &old_leaf, &new_leaf, 0, &mut proof) .unwrap(); spl_concurrent_mt - .set_leaf(root, old_leaf, new_leaf, proof.as_slice(), 0 as u32) + .set_leaf(root, old_leaf, new_leaf, proof.as_slice(), 0_u32) .unwrap(); reference_tree.update(&new_leaf, 0).unwrap(); @@ -2170,8 +2169,7 @@ pub fn test_100_nullify_mt() { let leaf_cell = queue.get_unmarked_bucket(queue_index).unwrap().unwrap(); let leaf_index = crank_merkle_tree .get_leaf_index(&leaf_cell.value_bytes()) - .unwrap() - .clone(); + .unwrap(); let mut proof = crank_merkle_tree .get_proof_of_leaf(leaf_index, false) @@ -2397,7 +2395,7 @@ fn test_subtree_updates() { spl_concurrent_mt.initialize().unwrap(); con_mt.init().unwrap(); assert_eq!(ref_mt.root(), con_mt.root()); - for (_, leaf) in LEAVES_WITH_NULLIFICATIONS.iter().enumerate() { + for leaf in LEAVES_WITH_NULLIFICATIONS.iter() { match leaf.1 { Some(index) => { let change_log_index = con_mt.changelog_index(); @@ -3377,7 +3375,7 @@ where tree.append(&leaf_0).unwrap(); tree.append(&leaf_1).unwrap(); tree.append(&leaf_2).unwrap(); - let old_canopy = tree.canopy.as_slice()[0].clone(); + let old_canopy = tree.canopy.as_slice()[0]; let new_leaf_0 = [1; 32]; let mut leaf_0_proof = BoundedVec::with_capacity(2); @@ -3390,7 +3388,7 @@ where &mut leaf_0_proof, ) .unwrap(); - let new_canopy = tree.canopy.as_slice()[0].clone(); + let new_canopy = tree.canopy.as_slice()[0]; assert_ne!(old_canopy, new_canopy); diff --git a/hash-set/src/lib.rs b/hash-set/src/lib.rs index c7cbbb3..9c74f0b 100644 --- a/hash-set/src/lib.rs +++ b/hash-set/src/lib.rs @@ -598,10 +598,10 @@ mod test { sequence_number: None, }; // It should be always valid, no matter the sequence number. - assert_eq!(cell.is_valid(0), true); + assert!(cell.is_valid(0)); for _ in 0..100 { let seq: usize = rng.gen(); - assert_eq!(cell.is_valid(seq), true); + assert!(cell.is_valid(seq)); } let cell = HashSetCell { @@ -610,10 +610,10 @@ mod test { }; // Sequence numbers up to 2400 should succeed. for i in 0..2400 { - assert_eq!(cell.is_valid(i), true); + assert!(cell.is_valid(i)); } for i in 2400..10000 { - assert_eq!(cell.is_valid(i), false); + assert!(!cell.is_valid(i)); } } @@ -630,7 +630,7 @@ mod test { hs.mark_with_sequence_number(index_1_1, 1).unwrap(); // Check if element exists in the set. - assert_eq!(hs.contains(&element_1_1, Some(1)).unwrap(), true); + assert!(hs.contains(&element_1_1, Some(1)).unwrap()); // Try inserting the same element, even though we didn't reach the // threshold. assert!(matches!( @@ -649,10 +649,10 @@ mod test { let index_2_6 = hs.insert(&element_2_6, 1).unwrap(); let index_2_8 = hs.insert(&element_2_8, 1).unwrap(); let index_2_9 = hs.insert(&element_2_9, 1).unwrap(); - assert_eq!(hs.contains(&element_2_3, Some(2)).unwrap(), true); - assert_eq!(hs.contains(&element_2_6, Some(2)).unwrap(), true); - assert_eq!(hs.contains(&element_2_8, Some(2)).unwrap(), true); - assert_eq!(hs.contains(&element_2_9, Some(2)).unwrap(), true); + assert!(hs.contains(&element_2_3, Some(2)).unwrap()); + assert!(hs.contains(&element_2_6, Some(2)).unwrap()); + assert!(hs.contains(&element_2_8, Some(2)).unwrap()); + assert!(hs.contains(&element_2_9, Some(2)).unwrap()); hs.mark_with_sequence_number(index_2_3, 2).unwrap(); hs.mark_with_sequence_number(index_2_6, 2).unwrap(); hs.mark_with_sequence_number(index_2_8, 2).unwrap(); @@ -682,10 +682,10 @@ mod test { let index_3_13 = hs.insert(&element_3_13, 2).unwrap(); let index_3_21 = hs.insert(&element_3_21, 2).unwrap(); let index_3_29 = hs.insert(&element_3_29, 2).unwrap(); - assert_eq!(hs.contains(&element_3_11, Some(3)).unwrap(), true); - assert_eq!(hs.contains(&element_3_13, Some(3)).unwrap(), true); - assert_eq!(hs.contains(&element_3_21, Some(3)).unwrap(), true); - assert_eq!(hs.contains(&element_3_29, Some(3)).unwrap(), true); + assert!(hs.contains(&element_3_11, Some(3)).unwrap()); + assert!(hs.contains(&element_3_13, Some(3)).unwrap()); + assert!(hs.contains(&element_3_21, Some(3)).unwrap()); + assert!(hs.contains(&element_3_29, Some(3)).unwrap()); hs.mark_with_sequence_number(index_3_11, 3).unwrap(); hs.mark_with_sequence_number(index_3_13, 3).unwrap(); hs.mark_with_sequence_number(index_3_21, 3).unwrap(); @@ -715,10 +715,10 @@ mod test { let index_4_65 = hs.insert(&element_4_65, 3).unwrap(); let index_4_72 = hs.insert(&element_4_72, 3).unwrap(); let index_4_15 = hs.insert(&element_4_15, 3).unwrap(); - assert_eq!(hs.contains(&element_4_93, Some(4)).unwrap(), true); - assert_eq!(hs.contains(&element_4_65, Some(4)).unwrap(), true); - assert_eq!(hs.contains(&element_4_72, Some(4)).unwrap(), true); - assert_eq!(hs.contains(&element_4_15, Some(4)).unwrap(), true); + assert!(hs.contains(&element_4_93, Some(4)).unwrap()); + assert!(hs.contains(&element_4_65, Some(4)).unwrap()); + assert!(hs.contains(&element_4_72, Some(4)).unwrap()); + assert!(hs.contains(&element_4_15, Some(4)).unwrap()); hs.mark_with_sequence_number(index_4_93, 4).unwrap(); hs.mark_with_sequence_number(index_4_65, 4).unwrap(); hs.mark_with_sequence_number(index_4_72, 4).unwrap(); @@ -758,6 +758,7 @@ mod test { } /// Test cases with random prime field elements. + #[ignore = "overflows the stack"] #[test] fn test_hash_set_random() { let mut hs = HashSet::new(6857, 2400).unwrap(); @@ -770,22 +771,17 @@ mod test { std::array::from_fn(|_| BigUint::from(Fr::rand(&mut rng))); for nf_chunk in nullifiers.chunks(2400) { for nullifier in nf_chunk.iter() { - assert_eq!(hs.contains(&nullifier, Some(seq)).unwrap(), false); - let index = hs.insert(&nullifier, seq as usize).unwrap(); - assert_eq!(hs.contains(&nullifier, Some(seq)).unwrap(), true); + assert!(hs.contains(nullifier, Some(seq)).unwrap()); + let index = hs.insert(nullifier, seq).unwrap(); + assert!(hs.contains(nullifier, Some(seq)).unwrap()); - let nullifier_bytes = bigint_to_be_bytes_array(&nullifier).unwrap(); + let nullifier_bytes = bigint_to_be_bytes_array(nullifier).unwrap(); - let element = hs - .find_element(&nullifier, Some(seq)) - .unwrap() - .unwrap() - .0 - .clone(); + let element = hs.find_element(nullifier, Some(seq)).unwrap().unwrap().0; assert_eq!( - element, + *element, HashSetCell { - value: bigint_to_be_bytes_array(&nullifier).unwrap(), + value: bigint_to_be_bytes_array(nullifier).unwrap(), sequence_number: None, } ); @@ -796,15 +792,10 @@ mod test { assert!(element.is_valid(seq)); hs.mark_with_sequence_number(index, seq).unwrap(); - let element = hs - .find_element(&nullifier, Some(seq)) - .unwrap() - .unwrap() - .0 - .clone(); + let element = hs.find_element(nullifier, Some(seq)).unwrap().unwrap().0; assert_eq!( - element, + *element, HashSetCell { value: nullifier_bytes, sequence_number: Some(2400 + seq) @@ -819,7 +810,7 @@ mod test { // Trying to insert the same nullifier, before reaching the // sequence threshold, should fail. assert!(matches!( - hs.insert(&nullifier, seq as usize + 2399), + hs.insert(nullifier, seq + 2399), Err(HashSetError::ElementAlreadyExists), )); seq += 1; @@ -1023,7 +1014,7 @@ mod test { std::array::from_fn(|_| BigUint::from(Fr::rand(&mut rng))); for nullifier in nullifiers.iter() { - hs.insert(&nullifier, 0).unwrap(); + hs.insert(nullifier, 0).unwrap(); } let mut sorted_nullifiers = nullifiers.iter().collect::>(); diff --git a/hash-set/src/zero_copy.rs b/hash-set/src/zero_copy.rs index 91e4590..e4e35d6 100644 --- a/hash-set/src/zero_copy.rs +++ b/hash-set/src/zero_copy.rs @@ -119,7 +119,7 @@ impl<'a> HashSetZeroCopy<'a> { } } -impl<'a> Drop for HashSetZeroCopy<'a> { +impl Drop for HashSetZeroCopy<'_> { fn drop(&mut self) { // SAFETY: Don't do anything here! Why? // @@ -134,7 +134,7 @@ impl<'a> Drop for HashSetZeroCopy<'a> { } } -impl<'a> Deref for HashSetZeroCopy<'a> { +impl Deref for HashSetZeroCopy<'_> { type Target = HashSet; fn deref(&self) -> &Self::Target { @@ -142,7 +142,7 @@ impl<'a> Deref for HashSetZeroCopy<'a> { } } -impl<'a> DerefMut for HashSetZeroCopy<'a> { +impl DerefMut for HashSetZeroCopy<'_> { fn deref_mut(&mut self) -> &mut Self::Target { &mut self.hash_set } @@ -190,7 +190,7 @@ mod test { } for (seq, nullifier) in nullifiers.iter().enumerate() { - let index = hs.insert(&nullifier, seq).unwrap(); + let index = hs.insert(nullifier, seq).unwrap(); hs.mark_with_sequence_number(index, seq).unwrap(); } } @@ -201,11 +201,11 @@ mod test { unsafe { HashSetZeroCopy::from_bytes_zero_copy_mut(bytes.as_mut_slice()).unwrap() }; for (seq, nullifier) in nullifiers.iter().enumerate() { - assert_eq!(hs.contains(nullifier, Some(seq)).unwrap(), true); + assert!(hs.contains(nullifier, Some(seq)).unwrap()); } for (seq, nullifier) in nullifiers.iter().enumerate() { - hs.insert(&nullifier, 2400 + seq as usize).unwrap(); + hs.insert(nullifier, 2400 + seq).unwrap(); } drop(hs); } @@ -215,10 +215,7 @@ mod test { let hs = unsafe { HashSet::from_bytes_copy(bytes.as_mut_slice()).unwrap() }; for (seq, nullifier) in nullifiers.iter().enumerate() { - assert_eq!( - hs.contains(nullifier, Some(2400 + seq as usize)).unwrap(), - true - ); + assert!(hs.contains(nullifier, Some(2400 + seq)).unwrap()); } } } diff --git a/indexed/src/array.rs b/indexed/src/array.rs index 6641061..3ef4c57 100644 --- a/indexed/src/array.rs +++ b/indexed/src/array.rs @@ -444,7 +444,7 @@ where } } -impl<'a, H, I> DoubleEndedIterator for IndexingArrayIter<'a, H, I> +impl DoubleEndedIterator for IndexingArrayIter<'_, H, I> where H: Hasher, I: CheckedAdd + CheckedSub + Copy + Clone + PartialOrd + ToBytes + TryFrom + Unsigned, diff --git a/indexed/src/zero_copy.rs b/indexed/src/zero_copy.rs index d0a4fdd..1bb8b37 100644 --- a/indexed/src/zero_copy.rs +++ b/indexed/src/zero_copy.rs @@ -97,8 +97,8 @@ where } } -impl<'a, H, I, const HEIGHT: usize, const NET_HEIGHT: usize> Deref - for IndexedMerkleTreeZeroCopy<'a, H, I, HEIGHT, NET_HEIGHT> +impl Deref + for IndexedMerkleTreeZeroCopy<'_, H, I, HEIGHT, NET_HEIGHT> where H: Hasher, I: CheckedAdd @@ -204,8 +204,8 @@ where } } -impl<'a, H, I, const HEIGHT: usize, const NET_HEIGHT: usize> Deref - for IndexedMerkleTreeZeroCopyMut<'a, H, I, HEIGHT, NET_HEIGHT> +impl Deref + for IndexedMerkleTreeZeroCopyMut<'_, H, I, HEIGHT, NET_HEIGHT> where H: Hasher, I: CheckedAdd @@ -226,8 +226,8 @@ where } } -impl<'a, H, I, const HEIGHT: usize, const NET_HEIGHT: usize> DerefMut - for IndexedMerkleTreeZeroCopyMut<'a, H, I, HEIGHT, NET_HEIGHT> +impl DerefMut + for IndexedMerkleTreeZeroCopyMut<'_, H, I, HEIGHT, NET_HEIGHT> where H: Hasher, I: CheckedAdd diff --git a/indexed/tests/tests.rs b/indexed/tests/tests.rs index c4b93c4..e0f919e 100644 --- a/indexed/tests/tests.rs +++ b/indexed/tests/tests.rs @@ -42,9 +42,9 @@ fn program_insert( where H: Hasher, { - for i in 0..NR_NULLIFIERS { - let nullifier = BigUint::from_be_bytes(nullifiers[i].as_slice()); - queue.insert(&nullifier, merkle_tree.sequence_number())?; + for nullifier in nullifiers.iter() { + let nf = BigUint::from_be_bytes(nullifier.as_slice()); + queue.insert(&nf, merkle_tree.sequence_number())?; } Ok(()) } @@ -57,6 +57,7 @@ enum RelayerUpdateError { /// A mock function which imitates a Merkle tree program instruction for /// inserting nullifiers from the queue to the tree. +#[allow(clippy::too_many_arguments)] fn program_update( // PDAs queue: &mut RefMut<'_, HashSet>, @@ -132,7 +133,7 @@ where ) .unwrap(); let mut low_nullifier_proof = relayer_merkle_tree - .get_proof_of_leaf(usize::from(old_low_nullifier.index), false) + .get_proof_of_leaf(old_low_nullifier.index, false) .unwrap(); // Update on-chain tree. @@ -487,7 +488,7 @@ where // (Invalid) low nullifier. let low_nullifier = local_indexed_array.get(1).cloned().unwrap(); let low_nullifier_next_value = local_indexed_array - .get(usize::from(low_nullifier.next_index)) + .get(low_nullifier.next_index) .cloned() .unwrap() .value; @@ -524,7 +525,7 @@ where // (Invalid) low nullifier. let low_nullifier = local_indexed_array.get(0).cloned().unwrap(); let low_nullifier_next_value = local_indexed_array - .get(usize::from(low_nullifier.next_index)) + .get(low_nullifier.next_index) .cloned() .unwrap() .value; @@ -887,7 +888,7 @@ fn perform_change_log_test< for (i, (address, indexed_array)) in addresses.iter().zip(indexed_arrays.iter_mut()).enumerate() { let (old_low_address, old_low_address_next_value) = indexed_array - .find_low_element_for_nonexistent(&address) + .find_low_element_for_nonexistent(address) .unwrap(); let address_bundle = indexed_array .new_element_with_low_element_index(old_low_address.index, address)