Struct MerkleStore
pub struct MerkleStore { /* private fields */ }Expand description
An in-memory data store for Merkelized data.
This is a in memory data store for Merkle trees, this store allows all the nodes of multiple trees to live as long as necessary and without duplication, this allows the implementation of space efficient persistent data structures.
Example usage:
let mut store: MerkleStore = MerkleStore::new();
// the store is initialized with the SMT empty nodes
assert_eq!(store.num_internal_nodes(), 255);
let tree1 = MerkleTree::new(vec![A, B, C, D, E, F, G, H0]).unwrap();
let tree2 = MerkleTree::new(vec![A, B, C, D, E, F, G, H1]).unwrap();
// populates the store with two merkle trees, common nodes are shared
store.extend(tree1.inner_nodes());
store.extend(tree2.inner_nodes());
// every leaf except the last are the same
for i in 0..7 {
let idx0 = NodeIndex::new(3, i).unwrap();
let d0 = store.get_node(ROOT0, idx0).unwrap();
let idx1 = NodeIndex::new(3, i).unwrap();
let d1 = store.get_node(ROOT1, idx1).unwrap();
assert_eq!(d0, d1, "Both trees have the same leaf at pos {i}");
}
// The leaves A-B-C-D are the same for both trees, so are their 2 immediate parents
for i in 0..4 {
let idx0 = NodeIndex::new(3, i).unwrap();
let d0 = store.get_path(ROOT0, idx0).unwrap();
let idx1 = NodeIndex::new(3, i).unwrap();
let d1 = store.get_path(ROOT1, idx1).unwrap();
assert_eq!(d0.path[0..2], d1.path[0..2], "Both sub-trees are equal up to two levels");
}
// Common internal nodes are shared, the two added trees have a total of 30, but the store has
// only 10 new entries, corresponding to the 10 unique internal nodes of these trees.
assert_eq!(store.num_internal_nodes() - 255, 10);Implementations§
§impl MerkleStore
impl MerkleStore
pub fn new() -> MerkleStore
pub fn new() -> MerkleStore
Creates an empty MerkleStore instance.
pub fn num_internal_nodes(&self) -> usize
pub fn num_internal_nodes(&self) -> usize
Return a count of the non-leaf nodes in the store.
pub fn get_node(
&self,
root: Word,
index: NodeIndex,
) -> Result<Word, MerkleError>
pub fn get_node( &self, root: Word, index: NodeIndex, ) -> Result<Word, MerkleError>
Returns the node at index rooted on the tree root.
§Errors
This method can return the following errors:
RootNotInStoreif therootis not present in the store.NodeNotInStoreif a node needed to traverse fromroottoindexis not present in the store.
pub fn get_path(
&self,
root: Word,
index: NodeIndex,
) -> Result<MerkleProof, MerkleError>
pub fn get_path( &self, root: Word, index: NodeIndex, ) -> Result<MerkleProof, MerkleError>
Returns the node at the specified index and its opening to the root.
The path starts at the sibling of the target leaf.
§Errors
This method can return the following errors:
RootNotInStoreif therootis not present in the store.NodeNotInStoreif a node needed to traverse fromroottoindexis not present in the store.
pub fn has_path(&self, root: Word, index: NodeIndex) -> bool
pub fn has_path(&self, root: Word, index: NodeIndex) -> bool
Returns true if a valid path exists from root to the specified index, false
otherwise.
This method checks if all nodes needed to traverse from root to index are present in the
store, without building the actual path. It is more efficient than get_path when only
existence verification is needed.
pub fn get_leaf_depth(
&self,
root: Word,
tree_depth: u8,
index: u64,
) -> Result<u8, MerkleError>
pub fn get_leaf_depth( &self, root: Word, tree_depth: u8, index: u64, ) -> Result<u8, MerkleError>
Returns the depth of the first leaf or an empty node encountered while traversing the tree from the specified root down according to the provided index.
The tree_depth parameter specifies the depth of the tree rooted at root. The
maximum value the argument accepts is u64::BITS.
§Errors
Will return an error if:
- The provided root is not found.
- The provided
tree_depthis greater than 64. - The provided
indexis not valid for a depth equivalent totree_depth. - No leaf or an empty node was found while traversing the tree down to
tree_depth.
pub fn find_lone_leaf(
&self,
root: Word,
root_index: NodeIndex,
tree_depth: u8,
) -> Result<Option<(NodeIndex, Word)>, MerkleError>
pub fn find_lone_leaf( &self, root: Word, root_index: NodeIndex, tree_depth: u8, ) -> Result<Option<(NodeIndex, Word)>, MerkleError>
Returns index and value of a leaf node which is the only leaf node in a subtree defined by the provided root. If the subtree contains zero or more than one leaf nodes None is returned.
The tree_depth parameter specifies the depth of the parent tree such that root is
located in this tree at root_index. The maximum value the argument accepts is
u64::BITS.
§Errors
Will return an error if:
- The provided root is not found.
- The provided
tree_depthis greater than 64. - The provided
root_indexhas depth greater thantree_depth. - A lone node at depth
tree_depthis not a leaf node.
pub fn subset<I, R>(&self, roots: I) -> MerkleStore
pub fn subset<I, R>(&self, roots: I) -> MerkleStore
Returns a subset of this Merkle store such that the returned Merkle store contains all nodes which are descendants of the specified roots.
The roots for which no descendants exist in this Merkle store are ignored.
pub fn inner_nodes(&self) -> impl Iterator<Item = InnerNodeInfo>
pub fn inner_nodes(&self) -> impl Iterator<Item = InnerNodeInfo>
Iterator over the inner nodes of the MerkleStore.
pub fn non_empty_leaves(
&self,
root: Word,
max_depth: u8,
) -> impl Iterator<Item = (NodeIndex, Word)>
pub fn non_empty_leaves( &self, root: Word, max_depth: u8, ) -> impl Iterator<Item = (NodeIndex, Word)>
Iterator over the non-empty leaves of the Merkle tree associated with the specified root
and max_depth.
pub fn add_merkle_path(
&mut self,
index: u64,
node: Word,
path: MerklePath,
) -> Result<Word, MerkleError>
pub fn add_merkle_path( &mut self, index: u64, node: Word, path: MerklePath, ) -> Result<Word, MerkleError>
Adds all the nodes of a Merkle path represented by path, opening to node. Returns the
new root.
This will compute the sibling elements determined by the Merkle path and node, and
include all the nodes into the store.
pub fn add_merkle_paths<I>(&mut self, paths: I) -> Result<(), MerkleError>
pub fn add_merkle_paths<I>(&mut self, paths: I) -> Result<(), MerkleError>
Adds all the nodes of multiple Merkle paths into the store.
This will compute the sibling elements for each Merkle path and include all the nodes
into the store.
For further reference, check MerkleStore::add_merkle_path.
pub fn set_node(
&mut self,
root: Word,
index: NodeIndex,
value: Word,
) -> Result<RootPath, MerkleError>
pub fn set_node( &mut self, root: Word, index: NodeIndex, value: Word, ) -> Result<RootPath, MerkleError>
Sets a node to value.
§Errors
This method can return the following errors:
RootNotInStoreif therootis not present in the store.NodeNotInStoreif a node needed to traverse fromroottoindexis not present in the store.
pub fn merge_roots(
&mut self,
left_root: Word,
right_root: Word,
) -> Result<Word, MerkleError>
pub fn merge_roots( &mut self, left_root: Word, right_root: Word, ) -> Result<Word, MerkleError>
Merges two elements and adds the resulting node into the store.
Merges arbitrary values. They may be leaves, nodes, or a mixture of both.
pub fn into_inner(self) -> HashMap<Word, StoreNode>
pub fn into_inner(self) -> HashMap<Word, StoreNode>
Returns the inner storage of this MerkleStore while consuming self.
Trait Implementations§
§impl Clone for MerkleStore
impl Clone for MerkleStore
§fn clone(&self) -> MerkleStore
fn clone(&self) -> MerkleStore
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more§impl Debug for MerkleStore
impl Debug for MerkleStore
§impl Default for MerkleStore
impl Default for MerkleStore
§fn default() -> MerkleStore
fn default() -> MerkleStore
§impl Deserializable for MerkleStore
impl Deserializable for MerkleStore
§fn min_serialized_size() -> usize
fn min_serialized_size() -> usize
Minimum serialized size: u64 length prefix (0 entries).
§fn read_from<R>(source: &mut R) -> Result<MerkleStore, DeserializationError>where
R: ByteReader,
fn read_from<R>(source: &mut R) -> Result<MerkleStore, DeserializationError>where
R: ByteReader,
source, attempts to deserialize these bytes
into Self, and returns the result. Read more§fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>
fn read_from_bytes(bytes: &[u8]) -> Result<Self, DeserializationError>
§fn read_from_bytes_with_budget(
bytes: &[u8],
budget: usize,
) -> Result<Self, DeserializationError>
fn read_from_bytes_with_budget( bytes: &[u8], budget: usize, ) -> Result<Self, DeserializationError>
Self from bytes with a byte budget limit. Read more§impl<'de> Deserialize<'de> for MerkleStore
impl<'de> Deserialize<'de> for MerkleStore
§fn deserialize<__D>(
__deserializer: __D,
) -> Result<MerkleStore, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<MerkleStore, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
§impl Extend<InnerNodeInfo> for MerkleStore
impl Extend<InnerNodeInfo> for MerkleStore
§fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = InnerNodeInfo>,
fn extend<I>(&mut self, iter: I)where
I: IntoIterator<Item = InnerNodeInfo>,
Source§fn extend_one(&mut self, item: A)
fn extend_one(&mut self, item: A)
extend_one)Source§fn extend_reserve(&mut self, additional: usize)
fn extend_reserve(&mut self, additional: usize)
extend_one)§impl From<&MerkleTree> for MerkleStore
impl From<&MerkleTree> for MerkleStore
§fn from(value: &MerkleTree) -> MerkleStore
fn from(value: &MerkleTree) -> MerkleStore
§impl From<&Mmr> for MerkleStore
impl From<&Mmr> for MerkleStore
§fn from(value: &Mmr) -> MerkleStore
fn from(value: &Mmr) -> MerkleStore
§impl From<&PartialMerkleTree> for MerkleStore
impl From<&PartialMerkleTree> for MerkleStore
§fn from(value: &PartialMerkleTree) -> MerkleStore
fn from(value: &PartialMerkleTree) -> MerkleStore
§impl<const DEPTH: u8> From<&SimpleSmt<DEPTH>> for MerkleStore
impl<const DEPTH: u8> From<&SimpleSmt<DEPTH>> for MerkleStore
§fn from(value: &SimpleSmt<DEPTH>) -> MerkleStore
fn from(value: &SimpleSmt<DEPTH>) -> MerkleStore
§impl From<&Smt> for MerkleStore
impl From<&Smt> for MerkleStore
§fn from(value: &Smt) -> MerkleStore
fn from(value: &Smt) -> MerkleStore
§impl FromIterator<(Word, StoreNode)> for MerkleStore
impl FromIterator<(Word, StoreNode)> for MerkleStore
§fn from_iter<I>(iter: I) -> MerkleStore
fn from_iter<I>(iter: I) -> MerkleStore
§impl FromIterator<InnerNodeInfo> for MerkleStore
impl FromIterator<InnerNodeInfo> for MerkleStore
§fn from_iter<I>(iter: I) -> MerkleStorewhere
I: IntoIterator<Item = InnerNodeInfo>,
fn from_iter<I>(iter: I) -> MerkleStorewhere
I: IntoIterator<Item = InnerNodeInfo>,
§impl PartialEq for MerkleStore
impl PartialEq for MerkleStore
§impl Serializable for MerkleStore
impl Serializable for MerkleStore
§fn write_into<W>(&self, target: &mut W)where
W: ByteWriter,
fn write_into<W>(&self, target: &mut W)where
W: ByteWriter,
self into bytes and writes these bytes into the target.§fn get_size_hint(&self) -> usize
fn get_size_hint(&self) -> usize
§impl Serialize for MerkleStore
impl Serialize for MerkleStore
§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
impl Eq for MerkleStore
impl StructuralPartialEq for MerkleStore
Auto Trait Implementations§
impl Freeze for MerkleStore
impl RefUnwindSafe for MerkleStore
impl Send for MerkleStore
impl Sync for MerkleStore
impl Unpin for MerkleStore
impl UnsafeUnpin for MerkleStore
impl UnwindSafe for MerkleStore
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<D> OwoColorize for D
impl<D> OwoColorize for D
§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
§fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn color<Color>(&self, color: Color) -> FgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::fg] or
a color-specific method, such as [OwoColorize::green], Read more§fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
fn on_color<Color>(&self, color: Color) -> BgDynColorDisplay<'_, Color, Self>where
Color: DynColor,
OwoColorize::bg] or
a color-specific method, such as [OwoColorize::on_yellow], Read more