Module grin_core::core::pmmr

source ·
Expand description

Persistent and prunable Merkle Mountain Range implementation. For a high level description of MMRs, see:

https://github. com/opentimestamps/opentimestamps-server/blob/master/doc/merkle-mountain-range. md

This implementation is built in two major parts:

  1. A set of low-level functions that allow navigation within an arbitrary sized binary tree traversed in postorder. To realize why this us useful, we start with the standard height sequence in a MMR: 0010012001… This is in fact identical to the postorder traversal (left-right-top) of a binary tree. In addition postorder traversal is independent of the height of the tree. This allows us, with a few primitive, to get the height of any node in the MMR from its position in the sequence, as well as calculate the position of siblings, parents, etc. As all those functions only rely on binary operations, they’re extremely fast.
  2. The implementation of a prunable MMR tree using the above. Each leaf is required to be Writeable (which implements Hashed). Tree roots can be trivially and efficiently calculated without materializing the full tree. The underlying Hashes are stored in a Backend implementation that can either be a simple Vec or a database.

Modules§

Structs§

  • Prunable Merkle Mountain Range implementation. All positions within the tree start at 0 just like array indices.
  • Readonly view of a PMMR.
  • Rewindable (but still readonly) view of a PMMR.
  • Simple/minimal/naive MMR backend implementation backed by Vec and Vec. Removed pos are maintained in a HashSet.

Traits§

  • Storage backend for the MMR, just needs to be indexed by order of insertion. The PMMR itself does not need the Backend to be accurate on the existence of an element (i.e. remove could be a no-op) but layers above can depend on an accurate Backend to check existence.
  • Trait with common methods for reading from a PMMR

Functions§

  • Iterator over all leaf pos beneath the provided subtree root (including the root itself).
  • Gets the position of the leftmost node (i.e. leaf) beneath the provided subtree root.
  • Iterator over all pos beneath the provided subtree root (including the root itself).
  • The height of a node in a full binary tree from its postorder traversal index.
  • All pos in the subtree beneath the provided root, including root itself.
  • Gets the position of the rightmost node (i.e. leaf) beneath the provided subtree root.
  • Calculates the positions of the parent and sibling of the node at the provided position.
  • For a given starting position calculate the parent and sibling positions for the branch/path from that position to the peak of the tree. We will use the sibling positions to generate the “path” of a Merkle proof.
  • Returns the 0-based pmmr index of 0-based leaf index n
  • Is this position a leaf in the MMR? We know the positions of all leaves based on the postorder height of an MMR of any size (somewhat unintuitively but this is how the PMMR is “append only”).
  • Is the node at this pos the “left” sibling of its parent?
  • The number of leaves in a MMR of the provided size.
  • peak bitmap and height of next node in mmr of given size Example: on size 4 returns (0b11, 0) as mmr tree of size 4 is 2 /
    0 1 3 with 0b11 indicating the presence of peaks of height 0 and 1, and 0 the height of the next node 4, which is a leaf NOTE: the peak map also encodes the path taken from the root to the added node since the path turns left (resp. right) if-and-only-if a peak at that height is absent (resp. present)
  • sizes of peaks and height of next node in mmr of given size similar to peak_map_height but replacing bitmap by vector of sizes Example: on input 5 returns ([3,1], 1) as mmr state before adding 5 was 2 /
    0 1 3 4
  • Gets the postorder traversal 0-based index of all peaks in a MMR given its size. Starts with the top peak, which is always on the left side of the range, and navigates toward lower siblings toward the right of the range. For some odd reason, return empty when next node is not a leaf
  • Returns the insertion index of the given leaf index
  • returns least position >= pos0 with height 0