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

fix: incorrect proof hash count causing panic #28

Merged
merged 2 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions src/merkle_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,11 @@ impl<T: Hasher> MerkleProof<T> {
// The next lines copy hashes from proof hashes and group them by layer index
let mut proof_layers: Vec<Vec<(usize, T::Hash)>> = Vec::with_capacity(tree_depth + 1);
let mut proof_copy = self.proof_hashes.clone();

for proof_indices in proof_indices_by_layers {
if proof_copy.len() < proof_indices.len() {
return Err(Error::not_enough_hashes_to_calculate_root());
}
let proof_hashes = proof_copy.splice(0..proof_indices.len(), []);
proof_layers.push(proof_indices.iter().cloned().zip(proof_hashes).collect());
}
Expand Down
27 changes: 27 additions & 0 deletions tests/merkle_proof_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,33 @@ pub mod root {

Ok(())
}

#[test]
pub fn should_return_error_not_enough_hashes_to_calculate_root() {
let test_data = common::setup();
let leaf_hashes = &test_data.leaf_hashes;
let expected_root = test_data.expected_root_hex.clone();
let indices_to_prove = vec![3, 4];

let leaves_to_prove: Vec<[u8; 32]> = indices_to_prove
.iter()
.map(|i| leaf_hashes.get(*i).unwrap().clone())
.collect();

let merkle_tree = MerkleTree::<Sha256>::from_leaves(&test_data.leaf_hashes);
let proof = merkle_tree.proof(&indices_to_prove);
let extracted_root = proof.root(
&indices_to_prove,
&leaves_to_prove,
test_data.leaf_values.len() + 1,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@witter-deland, could you pls look into this test again.
the +1 shouldn't be here iiuc. When I remove it, this test fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 means incorrect proof hash count, why did you remove it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i see, sry i was confused for a second. I hope you don't mind if a change the test to actually remove the hash #31

);

assert_eq!(
extracted_root.err().unwrap().to_string(),
Error::not_enough_hashes_to_calculate_root().to_string(),
"Should return error not_enough_hashes_to_calculate_root"
);
}
}

pub mod to_bytes {
Expand Down