Skip to content

Commit

Permalink
fix(mmr): support zero sized balanced merkle proof (#5474)
Browse files Browse the repository at this point in the history
Description
---
Remove incorrect merkle proof semantics check

Motivation and Context
---
A Merkle proof with empty paths and node indices == [0] is valid (merkle
root == leaf[0]).

How Has This Been Tested?
---
Added new test for empty proof validity

What process can a PR reviewer use to test or verify this change?
---
Run tests in mmr crate
<!-- Checklist -->
<!-- 1. Is the title of your PR in the form that would make nice release
notes? The title, excluding the conventional commit
tag, will be included exactly as is in the CHANGELOG, so please think
about it carefully. -->


Breaking Changes
---

- [x] None
- [ ] Requires data directory on base node to be deleted
- [ ] Requires hard fork
- [ ] Other - Please specify

<!-- Does this include a breaking change? If so, include this line as a
footer -->
<!-- BREAKING CHANGE: Description what the user should do, e.g. delete a
database, resync the chain -->
  • Loading branch information
sdbondi authored Jun 20, 2023
1 parent 8106f92 commit ef98482
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions base_layer/mmr/src/balanced_binary_merkle_proof.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,6 @@ where D: Digest + DomainDigest
.max()
.ok_or(BalancedBinaryMerkleProofError::CantMergeZeroProofs)?;

if max_height == 0 {
return Err(BalancedBinaryMerkleProofError::BadProofSemantics);
}

let mut indices = proofs.iter().map(|proof| proof.node_index).collect::<Vec<_>>();
let mut paths = vec![Vec::new(); proofs.len()];
let mut join_indices = vec![false; proofs.len()];
Expand Down Expand Up @@ -444,4 +440,21 @@ mod test {
let merged = MergedBalancedBinaryMerkleProof::create_from_proofs(&proofs).unwrap();
assert!(merged.verify_consume(&root, leaf_hashes).unwrap());
}

#[test]
fn test_single_node_proof() {
let leaves = vec![vec![1 as u8; 32]];
let bmt = BalancedBinaryMerkleTree::<TestHasher>::create(leaves.clone());

assert_eq!(bmt.num_nodes(), 1);
assert_eq!(bmt.num_leaf_nodes(), 1);
let root = bmt.get_merkle_root();
assert_eq!(root, leaves[0]);
let proof = BalancedBinaryMerkleProof::generate_proof(&bmt, 0).unwrap();
assert!(proof.verify(&root, leaves[0].clone()));
assert!(proof.path.is_empty());

let merged = MergedBalancedBinaryMerkleProof::create_from_proofs(&[proof]).unwrap();
assert!(merged.verify_consume(&root, vec![leaves[0].clone()]).unwrap());
}
}

0 comments on commit ef98482

Please sign in to comment.