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

test: add an unit test to cover duplicate proof item case #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
24 changes: 22 additions & 2 deletions src/tests/test_mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
helper::pos_height_in_tree,
leaf_index_to_mmr_size,
util::{MemMMR, MemStore},
Error,
Error, Merge, MerkleProof,
};
use faster_hex::hex_string;
use proptest::prelude::*;
Expand Down Expand Up @@ -150,6 +150,27 @@ fn test_gen_proof_with_duplicate_leaves() {
test_mmr(10, vec![5, 5]);
}

#[test]
fn test_duplicate_proof_items() {
let store = MemStore::default();
let mut mmr = MemMMR::<_, MergeNumberHash>::new(0, &store);
let positions: Vec<u64> = (0u32..3)
.map(|i| mmr.push(NumberHash::from(i)).unwrap())
.collect();
mmr.commit().expect("commit changes");
let root = mmr.get_root().expect("get root");
let proof = mmr.gen_proof(vec![positions[2]]).expect("gen proof");
let mut proof_items = proof.proof_items().iter().cloned().collect::<Vec<_>>();
proof_items.push(proof_items.last().unwrap().clone());
let duplicate_proof =
MerkleProof::<NumberHash, MergeNumberHash>::new(proof.mmr_size(), proof_items);

let result = duplicate_proof
.verify(root, vec![(positions[2], NumberHash::from(2))])
.unwrap();
assert!(!result);
}

fn test_invalid_proof_verification(
leaf_count: u32,
positions_to_verify: Vec<u64>,
Expand All @@ -158,7 +179,6 @@ fn test_invalid_proof_verification(
// optionally handroll proof from these positions
handrolled_proof_positions: Option<Vec<u64>>,
) {
use crate::{Merge, MerkleProof};
use std::fmt::{Debug, Formatter};

// Simple item struct to allow debugging the contents of MMR nodes/peaks
Expand Down