Struct tari_mmr::MutableMmr [−][src]
Expand description
Unlike a pure MMR, which is append-only, in MutableMmr
, leaf nodes can be marked as deleted.
In MutableMmr
a roaring bitmap tracks which data have been marked as deleted, and the merklish root is modified
to include the hash of the roaring bitmap.
The MutableMmr
API maps nearly 1:1 to that of MerkleMountainRange so that you should be able to use it as a
drop-in replacement for the latter in most cases.
Implementations
pub fn new(
mmr_backend: B,
deleted: Bitmap
) -> Result<MutableMmr<D, B>, MerkleMountainRangeError>
pub fn new(
mmr_backend: B,
deleted: Bitmap
) -> Result<MutableMmr<D, B>, MerkleMountainRangeError>
Create a new mutable MMR using the backend provided
Clear the MutableMmr and assign the MMR state from the set of leaf_hashes and deleted nodes given in state
.
Return the number of leaf nodes in the MutableMmr
that have not been marked as deleted.
NB: This is semantically different to MerkleMountainRange::len()
. The latter returns the total number of
nodes in the MMR, while this function returns the number of leaf nodes minus the number of nodes marked for
deletion.
Returns true if the the MMR contains no nodes, OR all nodes have been marked for deletion
pub fn get_leaf_hash(
&self,
leaf_index: u32
) -> Result<Option<Hash>, MerkleMountainRangeError>
pub fn get_leaf_hash(
&self,
leaf_index: u32
) -> Result<Option<Hash>, MerkleMountainRangeError>
This function returns the hash of the leaf index provided, indexed from 0. If the hash does not exist, or if it
has been marked for deletion, None
is returned.
pub fn get_leaf_status(
&self,
leaf_index: u32
) -> Result<(Option<Hash>, bool), MerkleMountainRangeError>
pub fn get_leaf_status(
&self,
leaf_index: u32
) -> Result<(Option<Hash>, bool), MerkleMountainRangeError>
Returns the hash of the leaf index provided, as well as its deletion status. The node has been marked for deletion if the boolean value is true.
Returns the number of leaf nodes in the MMR.
Returns a merkle(ish) root for this merkle set.
The root is calculated by concatenating the MMR merkle root with the compressed serialisation of the bitmap and then hashing the result.
Returns only the MMR merkle root without the compressed serialisation of the bitmap
Push a new element into the MMR. Computes new related peaks at the same time if applicable. Returns the new number of leaf nodes (regardless of deleted state) in the mutable MMR
Mark a node for deletion and optionally compress the deletion bitmap. Don’t call this function unless you’re in a tight loop and want to eke out some extra performance by delaying the bitmap compression until after the batch deletion.
Note that this function doesn’t actually delete anything (the underlying MMR structure is immutable), but marks the leaf node as deleted. Once a leaf node has been marked for deletion:
get_leaf_hash(n)
will return None,len()
will not count this node anymore
NB: You should call compress before calling get_merkle_root()
. If you don’t, the merkle root will be
incorrect.
Parameters
leaf_node_index
: The index of the leaf node to mark for deletion, zero-based.
Return
The function returns true if a node was actually marked for deletion. If the index is out of bounds, or was already deleted, the function returns false.
Compress the roaring bitmap mapping deleted nodes. You never have to call this method unless you have been
calling [delete_and_compress] with compress
set to false
ahead of a call to [get_merkle_root].
Walks the nodes in the MMR and validates all parent hashes
This just calls through to the underlying MMR’s validate method. There’s nothing we can do to check whether
the roaring bitmap represents all the leaf nodes that we want to delete. Note: A struct that uses
MutableMmr
and links it to actual data should be able to do this though.
pub fn to_leaf_nodes(
&self,
leaf_index: usize,
count: usize
) -> Result<MutableMmrLeafNodes, MerkleMountainRangeError>
pub fn to_leaf_nodes(
&self,
leaf_index: usize,
count: usize
) -> Result<MutableMmrLeafNodes, MerkleMountainRangeError>
Returns the state of the MMR that consists of the leaf hashes and the deleted nodes.
Expose the MerkleMountainRange for verifying proofs