Skip to content

Commit

Permalink
review feedback: remove get_peaks_and_heights_async completely.
Browse files Browse the repository at this point in the history
  • Loading branch information
zkclay authored and Sword-Smith committed Apr 26, 2024
1 parent e1bf9dd commit 9a79732
Showing 1 changed file with 13 additions and 43 deletions.
56 changes: 13 additions & 43 deletions src/util_types/mutator_set/archival_mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ where

/// Return the digests of the peaks of the MMR
pub async fn get_peaks(&self) -> Vec<Digest> {
let peaks_and_heights = self.get_peaks_with_heights_async().await;
peaks_and_heights.into_iter().map(|x| x.0).collect()
let leaf_count = self.count_leaves().await;
let (_, peak_node_indices) = get_peak_heights_and_peak_node_indices(leaf_count);
self.digests.get_many(&peak_node_indices).await
}

/// Whether the MMR is empty. Note that since indexing starts at
Expand Down Expand Up @@ -220,21 +221,6 @@ impl<H: AlgebraicHasher, Storage: StorageVec<Digest>> ArchivalMmr<H, Storage> {
MmrMembershipProof::new(leaf_index, authentication_path)
}

/// Return a list of tuples (peaks, height)
pub async fn get_peaks_with_heights_async(&self) -> Vec<(Digest, u32)> {
let leaf_count = self.count_leaves().await;
let (peak_heights, peak_node_indices) = get_peak_heights_and_peak_node_indices(leaf_count);
let peaks = self.digests.get_many(&peak_node_indices).await;

let peaks_and_heights: Vec<_> = peaks
.iter()
.zip(peak_heights.iter())
.map(|(&x, &y)| (x, y))
.collect();

peaks_and_heights
}

/// Remove the last leaf from the archival MMR
pub async fn remove_last_leaf_async(&mut self) -> Option<Digest> {
if self.is_empty().await {
Expand Down Expand Up @@ -274,7 +260,6 @@ pub(crate) mod mmr_test {
use twenty_first::util_types::merkle_tree_maker::MerkleTreeMaker;
use twenty_first::util_types::mmr::mmr_accumulator::MmrAccumulator;
use twenty_first::util_types::mmr::shared_advanced::get_peak_heights;
use twenty_first::util_types::mmr::shared_advanced::get_peak_heights_and_peak_node_indices;

type Storage = OrdinaryVec<Digest>;

Expand Down Expand Up @@ -768,10 +753,8 @@ pub(crate) mod mmr_test {
assert_eq!(1, mmr.count_leaves().await);
assert_eq!(1, mmr.count_nodes().await);

let original_peaks_and_heights: Vec<(Digest, u32)> =
mmr.get_peaks_with_heights_async().await;
assert_eq!(1, original_peaks_and_heights.len());
assert_eq!(0, original_peaks_and_heights[0].1);
let original_peaks: Vec<Digest> = mmr.get_peaks().await;
assert_eq!(1, original_peaks.len());

{
let leaf_index = 0;
Expand All @@ -785,11 +768,9 @@ pub(crate) mod mmr_test {
assert_eq!(2, mmr.count_leaves().await);
assert_eq!(3, mmr.count_nodes().await);

let new_peaks_and_heights = mmr.get_peaks_with_heights_async().await;
assert_eq!(1, new_peaks_and_heights.len());
assert_eq!(1, new_peaks_and_heights[0].1);
let new_peaks = mmr.get_peaks().await;
assert_eq!(1, new_peaks.len());

let new_peaks: Vec<Digest> = new_peaks_and_heights.iter().map(|x| x.0).collect();
assert!(
original_mmr
.verify_batch_update(&new_peaks, &[new_input_hash], &[])
Expand Down Expand Up @@ -839,10 +820,9 @@ pub(crate) mod mmr_test {
assert_eq!(num_leaves, mmr.count_leaves().await);
assert_eq!(1 + num_leaves, mmr.count_nodes().await);

let original_peaks_and_heights: Vec<(Digest, u32)> =
mmr.get_peaks_with_heights_async().await;
let original_peaks: Vec<Digest> = mmr.get_peaks().await;
let expected_peaks = 2;
assert_eq!(expected_peaks, original_peaks_and_heights.len());
assert_eq!(expected_peaks, original_peaks.len());

{
let leaf_index = 0;
Expand Down Expand Up @@ -898,13 +878,8 @@ pub(crate) mod mmr_test {
assert_eq!(leaf_count, mmr.count_leaves().await);
assert_eq!(node_count, mmr.count_nodes().await);

let original_peaks_and_heights = mmr.get_peaks_with_heights_async().await;
let peak_heights_1: Vec<u32> = original_peaks_and_heights.iter().map(|x| x.1).collect();

let (peak_heights_2, _) = get_peak_heights_and_peak_node_indices(leaf_count);
assert_eq!(peak_heights_1, peak_heights_2);

let actual_peak_count = original_peaks_and_heights.len() as u64;
let original_peaks = mmr.get_peaks().await;
let actual_peak_count = original_peaks.len() as u64;
assert_eq!(peak_count, actual_peak_count);

// Verify that MMR root from odd number of digests and MMR bagged peaks agree
Expand Down Expand Up @@ -1025,12 +1000,8 @@ pub(crate) mod mmr_test {
mock::get_ammr_from_digests::<H>(input_digests.clone()).await;
assert_eq!(size, mmr.count_leaves().await);
assert_eq!(node_count, mmr.count_nodes().await);
let original_peaks_and_heights: Vec<(Digest, u32)> =
mmr.get_peaks_with_heights_async().await;
let peak_heights_1: Vec<u32> = original_peaks_and_heights.iter().map(|x| x.1).collect();
let (peak_heights_2, _) = get_peak_heights_and_peak_node_indices(size);
assert_eq!(peak_heights_1, peak_heights_2);
assert_eq!(peak_count, original_peaks_and_heights.len() as u64);
let original_peaks: Vec<Digest> = mmr.get_peaks().await;
assert_eq!(peak_count, original_peaks.len() as u64);

// Verify that MMR root from odd number of digests and MMR bagged peaks agree
let mmra_root = mmr.bag_peaks().await;
Expand All @@ -1048,7 +1019,6 @@ pub(crate) mod mmr_test {
let valid_res =
membership_proof.verify(&peaks, input_digests[leaf_index as usize], size);
assert!(valid_res);

let new_leaf: Digest = random();

// The below verify_modify tests should only fail if `wrong_leaf_index` is
Expand Down

0 comments on commit 9a79732

Please sign in to comment.