Browse Source

feat: add PartialMmr::from_parts() constructor

km/mkdocs-impl
Bobbin Threadbare 1 year ago
committed by Bobbin Threadbare
parent
commit
5fcf98669d
2 changed files with 26 additions and 4 deletions
  1. +20
    -4
      src/merkle/mmr/partial.rs
  2. +6
    -0
      src/merkle/mmr/peaks.rs

+ 20
- 4
src/merkle/mmr/partial.rs

@ -10,6 +10,11 @@ use crate::{
}, },
}; };
// TYPE ALIASES
// ================================================================================================
type NodeMap = BTreeMap<InOrderIndex, RpoDigest>;
// PARTIAL MERKLE MOUNTAIN RANGE // PARTIAL MERKLE MOUNTAIN RANGE
// ================================================================================================ // ================================================================================================
/// Partially materialized Merkle Mountain Range (MMR), used to efficiently store and update the /// Partially materialized Merkle Mountain Range (MMR), used to efficiently store and update the
@ -55,7 +60,7 @@ pub struct PartialMmr {
/// permits for easy computation of the relative nodes (left/right children, sibling, parent), /// permits for easy computation of the relative nodes (left/right children, sibling, parent),
/// which is useful for traversal. The indexing is also stable, meaning that merges to the /// which is useful for traversal. The indexing is also stable, meaning that merges to the
/// trees in the MMR can be represented without rewrites of the indexes. /// trees in the MMR can be represented without rewrites of the indexes.
pub(crate) nodes: BTreeMap<InOrderIndex, RpoDigest>,
pub(crate) nodes: NodeMap,
/// Flag indicating if the odd element should be tracked. /// Flag indicating if the odd element should be tracked.
/// ///
@ -68,16 +73,27 @@ impl PartialMmr {
// CONSTRUCTORS // CONSTRUCTORS
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
/// Constructs a [PartialMmr] from the given [MmrPeaks].
/// Returns a new [PartialMmr] instantiated from the specified peaks.
pub fn from_peaks(peaks: MmrPeaks) -> Self { pub fn from_peaks(peaks: MmrPeaks) -> Self {
let forest = peaks.num_leaves(); let forest = peaks.num_leaves();
let peaks = peaks.peaks().to_vec();
let peaks = peaks.into();
let nodes = BTreeMap::new(); let nodes = BTreeMap::new();
let track_latest = false; let track_latest = false;
Self { forest, peaks, nodes, track_latest } Self { forest, peaks, nodes, track_latest }
} }
/// Returns a new [PartialMmr] instantiated from the specified components.
///
/// This constructor does not check the consistency between peaks and nodes. If the specified
/// peaks are nodes are inconsistent, the returned partial MMR may exhibit undefined behavior.
pub fn from_parts(peaks: MmrPeaks, nodes: NodeMap, track_latest: bool) -> Self {
let forest = peaks.num_leaves();
let peaks = peaks.into();
Self { forest, peaks, nodes, track_latest }
}
// PUBLIC ACCESSORS // PUBLIC ACCESSORS
// -------------------------------------------------------------------------------------------- // --------------------------------------------------------------------------------------------
@ -516,7 +532,7 @@ impl From<&PartialMmr> for MmrPeaks {
/// An iterator over every inner node of the [PartialMmr]. /// An iterator over every inner node of the [PartialMmr].
pub struct InnerNodeIterator<'a, I: Iterator<Item = (usize, RpoDigest)>> { pub struct InnerNodeIterator<'a, I: Iterator<Item = (usize, RpoDigest)>> {
nodes: &'a BTreeMap<InOrderIndex, RpoDigest>,
nodes: &'a NodeMap,
leaves: I, leaves: I,
stack: Vec<(InOrderIndex, RpoDigest)>, stack: Vec<(InOrderIndex, RpoDigest)>,
seen_nodes: BTreeSet<InOrderIndex>, seen_nodes: BTreeSet<InOrderIndex>,

+ 6
- 0
src/merkle/mmr/peaks.rs

@ -132,3 +132,9 @@ impl MmrPeaks {
elements elements
} }
} }
impl From<MmrPeaks> for Vec<RpoDigest> {
fn from(peaks: MmrPeaks) -> Self {
peaks.peaks
}
}

Loading…
Cancel
Save