-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: verify a old root and all incremental items after it
- Loading branch information
1 parent
8206b58
commit e92a3da
Showing
3 changed files
with
251 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod test_accumulate_headers; | ||
mod test_helper; | ||
mod test_incremental; | ||
mod test_mmr; | ||
mod test_sequence; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use proptest::proptest; | ||
|
||
use super::{MergeNumberHash, NumberHash}; | ||
use crate::util::{MemMMR, MemStore}; | ||
|
||
proptest! { | ||
#[test] | ||
fn test_incremental(start in 1u32..500, steps in 1usize..50, turns in 10usize..20) { | ||
test_incremental_with_params(start, steps, turns); | ||
} | ||
} | ||
|
||
fn test_incremental_with_params(start: u32, steps: usize, turns: usize) { | ||
let store = MemStore::default(); | ||
let mut mmr = MemMMR::<_, MergeNumberHash>::new(0, &store); | ||
|
||
let mut curr = 0; | ||
|
||
let _positions: Vec<u64> = (0u32..start) | ||
.map(|_| { | ||
let pos = mmr.push(NumberHash::from(curr)).unwrap(); | ||
curr += 1; | ||
pos | ||
}) | ||
.collect(); | ||
mmr.commit().expect("commit changes"); | ||
|
||
for turn in 0..turns { | ||
let prev_root = mmr.get_root().expect("get root"); | ||
let prev_leaves_count = u64::from(curr); | ||
let (positions, leaves) = (0..steps).fold( | ||
(Vec::new(), Vec::new()), | ||
|(mut positions, mut leaves), _| { | ||
let leaf = NumberHash::from(curr); | ||
let pos = mmr.push(leaf.clone()).unwrap(); | ||
curr += 1; | ||
positions.push(pos); | ||
leaves.push(leaf); | ||
(positions, leaves) | ||
}, | ||
); | ||
mmr.commit().expect("commit changes"); | ||
let proof = mmr.gen_proof(positions).expect("gen proof"); | ||
let root = mmr.get_root().expect("get root"); | ||
let result = proof | ||
.verify_incremental(root, prev_leaves_count, prev_root, leaves) | ||
.unwrap(); | ||
assert!( | ||
result, | ||
"start: {}, steps: {}, turn: {}, curr: {}", | ||
start, steps, turn, curr | ||
); | ||
} | ||
} |