refactor: return MmrPeaks from PartialMmr::peaks()
This commit is contained in:
parent
59d93cb8ba
commit
4d0d8d3058
3 changed files with 28 additions and 12 deletions
|
@ -88,9 +88,11 @@ impl PartialMmr {
|
||||||
self.forest
|
self.forest
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns a reference to the current peaks in the [PartialMmr]
|
// Returns a reference to the current peaks in the [PartialMmr].
|
||||||
pub fn peaks(&self) -> &[RpoDigest] {
|
pub fn peaks(&self) -> MmrPeaks {
|
||||||
&self.peaks
|
// expect() is OK here because the constructor ensures that MMR peaks can be constructed
|
||||||
|
// correctly
|
||||||
|
MmrPeaks::new(self.forest, self.peaks.clone()).expect("invalid MMR peaks")
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Given a leaf position, returns the Merkle path to its corresponding peak. If the position
|
/// Given a leaf position, returns the Merkle path to its corresponding peak. If the position
|
||||||
|
|
|
@ -36,6 +36,14 @@ pub struct MmrPeaks {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MmrPeaks {
|
impl MmrPeaks {
|
||||||
|
// CONSTRUCTOR
|
||||||
|
// --------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
/// Returns new [MmrPeaks] instantiated from the provided vector of peaks and the number of
|
||||||
|
/// leaves in the underlying MMR.
|
||||||
|
///
|
||||||
|
/// # Errors
|
||||||
|
/// Returns an error if the number of leaves and the number of peaks are inconsistent.
|
||||||
pub fn new(num_leaves: usize, peaks: Vec<RpoDigest>) -> Result<Self, MmrError> {
|
pub fn new(num_leaves: usize, peaks: Vec<RpoDigest>) -> Result<Self, MmrError> {
|
||||||
if num_leaves.count_ones() as usize != peaks.len() {
|
if num_leaves.count_ones() as usize != peaks.len() {
|
||||||
return Err(MmrError::InvalidPeaks);
|
return Err(MmrError::InvalidPeaks);
|
||||||
|
@ -47,17 +55,23 @@ impl MmrPeaks {
|
||||||
// ACCESSORS
|
// ACCESSORS
|
||||||
// --------------------------------------------------------------------------------------------
|
// --------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
/// Returns a count of the MMR's leaves.
|
/// Returns a count of leaves in the underlying MMR.
|
||||||
pub fn num_leaves(&self) -> usize {
|
pub fn num_leaves(&self) -> usize {
|
||||||
self.num_leaves
|
self.num_leaves
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the current peaks of the MMR.
|
/// Returns the number of peaks of the underlying MMR.
|
||||||
|
pub fn num_peaks(&self) -> usize {
|
||||||
|
self.peaks.len()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns the list of peaks of the underlying MMR.
|
||||||
pub fn peaks(&self) -> &[RpoDigest] {
|
pub fn peaks(&self) -> &[RpoDigest] {
|
||||||
&self.peaks
|
&self.peaks
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the current num_leaves and peaks of the MMR.
|
/// Converts this [MmrPeaks] into its components: number of leaves and a vector of peaks of
|
||||||
|
/// the underlying MMR.
|
||||||
pub fn into_parts(self) -> (usize, Vec<RpoDigest>) {
|
pub fn into_parts(self) -> (usize, Vec<RpoDigest>) {
|
||||||
(self.num_leaves, self.peaks)
|
(self.num_leaves, self.peaks)
|
||||||
}
|
}
|
||||||
|
|
|
@ -755,14 +755,14 @@ fn test_mmr_delta_old_forest() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_partial_mmr_simple() {
|
fn test_partial_mmr_simple() {
|
||||||
let mmr: Mmr = LEAVES.into();
|
let mmr: Mmr = LEAVES.into();
|
||||||
let acc = mmr.peaks(mmr.forest()).unwrap();
|
let peaks = mmr.peaks(mmr.forest()).unwrap();
|
||||||
let mut partial: PartialMmr = acc.clone().into();
|
let mut partial: PartialMmr = peaks.clone().into();
|
||||||
|
|
||||||
// check initial state of the partial mmr
|
// check initial state of the partial mmr
|
||||||
assert_eq!(partial.peaks(), acc.peaks());
|
assert_eq!(partial.peaks(), peaks);
|
||||||
assert_eq!(partial.forest(), acc.num_leaves());
|
assert_eq!(partial.forest(), peaks.num_leaves());
|
||||||
assert_eq!(partial.forest(), LEAVES.len());
|
assert_eq!(partial.forest(), LEAVES.len());
|
||||||
assert_eq!(partial.peaks().len(), 3);
|
assert_eq!(partial.peaks().num_peaks(), 3);
|
||||||
assert_eq!(partial.nodes.len(), 0);
|
assert_eq!(partial.nodes.len(), 0);
|
||||||
|
|
||||||
// check state after adding tracking one element
|
// check state after adding tracking one element
|
||||||
|
@ -808,7 +808,7 @@ fn test_partial_mmr_update_single() {
|
||||||
partial.apply(delta).unwrap();
|
partial.apply(delta).unwrap();
|
||||||
|
|
||||||
assert_eq!(partial.forest(), full.forest());
|
assert_eq!(partial.forest(), full.forest());
|
||||||
assert_eq!(partial.peaks(), full.peaks(full.forest()).unwrap().peaks());
|
assert_eq!(partial.peaks(), full.peaks(full.forest()).unwrap());
|
||||||
|
|
||||||
let proof1 = full.open(i as usize, full.forest()).unwrap();
|
let proof1 = full.open(i as usize, full.forest()).unwrap();
|
||||||
partial.add(proof1.position, node, &proof1.merkle_path).unwrap();
|
partial.add(proof1.position, node, &proof1.merkle_path).unwrap();
|
||||||
|
|
Loading…
Add table
Reference in a new issue