pub struct MemoryStorage {
pub root: RwLock<Word>,
pub leaves: RwLock<BTreeMap<u64, SmtLeaf>>,
pub subtrees: RwLock<BTreeMap<NodeIndex, Subtree>>,
}Expand description
In-memory storage for a Sparse Merkle Tree (SMT), implementing the SmtStorage trait.
This structure stores the SMT’s root hash, leaf nodes, and subtrees directly in memory.
Access to these components is synchronized using std::sync::RwLock for thread safety.
It is primarily intended for scenarios where data persistence to disk is not a primary concern. Common use cases include:
- Testing environments.
- Managing SMT instances with a limited operational lifecycle.
- Situations where a higher-level application architecture handles its own data persistence strategy.
Fields§
§root: RwLock<Word>§leaves: RwLock<BTreeMap<u64, SmtLeaf>>§subtrees: RwLock<BTreeMap<NodeIndex, Subtree>>Implementations§
Source§impl MemoryStorage
impl MemoryStorage
Sourcepub fn new() -> MemoryStorage
pub fn new() -> MemoryStorage
Creates a new, empty in-memory storage for a Sparse Merkle Tree.
Initializes the root to the empty SMT root for the defined SMT_DEPTH, and empty maps for leaves and subtrees.
Trait Implementations§
Source§impl Clone for MemoryStorage
impl Clone for MemoryStorage
Source§fn clone(&self) -> MemoryStorage
fn clone(&self) -> MemoryStorage
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MemoryStorage
impl Debug for MemoryStorage
Source§impl Default for MemoryStorage
impl Default for MemoryStorage
Source§fn default() -> MemoryStorage
fn default() -> MemoryStorage
Source§impl SmtStorage for MemoryStorage
impl SmtStorage for MemoryStorage
Source§fn get_root(&self) -> Result<Option<Word>, StorageError>
fn get_root(&self) -> Result<Option<Word>, StorageError>
Retrieves the current root hash of the Sparse Merkle Tree.
Source§fn set_root(&self, root: Word) -> Result<(), StorageError>
fn set_root(&self, root: Word) -> Result<(), StorageError>
Sets the root hash of the Sparse Merkle Tree.
Source§fn leaf_count(&self) -> Result<usize, StorageError>
fn leaf_count(&self) -> Result<usize, StorageError>
Gets the total number of non-empty leaves currently stored.
Source§fn entry_count(&self) -> Result<usize, StorageError>
fn entry_count(&self) -> Result<usize, StorageError>
Gets the total number of key-value entries currently stored.
Source§fn insert_value(
&self,
index: u64,
key: Word,
value: Word,
) -> Result<Option<Word>, StorageError>
fn insert_value( &self, index: u64, key: Word, value: Word, ) -> Result<Option<Word>, StorageError>
Inserts a key-value pair into the leaf at the given index.
- If the leaf at
indexdoes not exist, a newSmtLeaf::Singleis created. - If the leaf exists, the key-value pair is inserted into it.
- Returns the previous value associated with the key, if any.
§Errors
Returns StorageError::Backend if the write lock cannot be acquired.
§Panics
Panics in debug builds if value is EMPTY_WORD.
Source§fn remove_value(
&self,
index: u64,
key: Word,
) -> Result<Option<Word>, StorageError>
fn remove_value( &self, index: u64, key: Word, ) -> Result<Option<Word>, StorageError>
Removes a key-value pair from the leaf at the given index.
- If the leaf at
indexexists and thekeyis found within that leaf, the key-value pair is removed, and the oldWordvalue is returned inOk(Some(Word)). - If the leaf at
indexexists but thekeyis not found within that leaf,Ok(None)is returned (asleaf.get_value(&key)would beNone). - If the leaf at
indexdoes not exist,Ok(None)is returned, as no value could be removed.
§Errors
Returns StorageError::Backend if the write lock for leaves cannot be acquired.
Source§fn get_leaf(&self, index: u64) -> Result<Option<SmtLeaf>, StorageError>
fn get_leaf(&self, index: u64) -> Result<Option<SmtLeaf>, StorageError>
Retrieves a single leaf node.
Source§fn set_leaves(
&self,
leaves_map: BTreeMap<u64, SmtLeaf>,
) -> Result<(), StorageError>
fn set_leaves( &self, leaves_map: BTreeMap<u64, SmtLeaf>, ) -> Result<(), StorageError>
Sets multiple leaf nodes in storage.
If a leaf at a given index already exists, it is overwritten.
§Errors
Returns StorageError::Backend if the write lock for leaves cannot be acquired.
Source§fn remove_leaf(&self, index: u64) -> Result<Option<SmtLeaf>, StorageError>
fn remove_leaf(&self, index: u64) -> Result<Option<SmtLeaf>, StorageError>
Removes a single leaf node.
Source§fn get_leaves(
&self,
indices: &[u64],
) -> Result<Vec<Option<SmtLeaf>>, StorageError>
fn get_leaves( &self, indices: &[u64], ) -> Result<Vec<Option<SmtLeaf>>, StorageError>
Retrieves multiple leaf nodes. Returns Ok(None) for indices not found.
Source§fn has_leaves(&self) -> Result<bool, StorageError>
fn has_leaves(&self) -> Result<bool, StorageError>
Returns true if the storage has any leaves.
Source§fn get_subtree(&self, index: NodeIndex) -> Result<Option<Subtree>, StorageError>
fn get_subtree(&self, index: NodeIndex) -> Result<Option<Subtree>, StorageError>
Retrieves a single Subtree (representing deep nodes) by its root NodeIndex. Assumes index.depth() >= IN_MEMORY_DEPTH. Returns Ok(None) if not found.
Source§fn get_subtrees(
&self,
indices: &[NodeIndex],
) -> Result<Vec<Option<Subtree>>, StorageError>
fn get_subtrees( &self, indices: &[NodeIndex], ) -> Result<Vec<Option<Subtree>>, StorageError>
Retrieves multiple Subtrees. Assumes index.depth() >= IN_MEMORY_DEPTH for all indices. Returns Ok(None) for indices not found.
Source§fn set_subtree(&self, subtree: &Subtree) -> Result<(), StorageError>
fn set_subtree(&self, subtree: &Subtree) -> Result<(), StorageError>
Sets a single Subtree (representing deep nodes) by its root NodeIndex.
If a subtree with the same root NodeIndex already exists, it is overwritten.
Assumes subtree.root_index().depth() >= IN_MEMORY_DEPTH.
§Errors
Returns StorageError::Backend if the write lock for subtrees cannot be acquired.
Source§fn set_subtrees(&self, subtrees_vec: Vec<Subtree>) -> Result<(), StorageError>
fn set_subtrees(&self, subtrees_vec: Vec<Subtree>) -> Result<(), StorageError>
Sets multiple Subtrees (representing deep nodes) by their root NodeIndex.
If a subtree with a given root NodeIndex already exists, it is overwritten.
Assumes subtree.root_index().depth() >= IN_MEMORY_DEPTH for all subtrees in the vector.
§Errors
Returns StorageError::Backend if the write lock for subtrees cannot be acquired.
Source§fn remove_subtree(&self, index: NodeIndex) -> Result<(), StorageError>
fn remove_subtree(&self, index: NodeIndex) -> Result<(), StorageError>
Removes a single Subtree (representing deep nodes) by its root NodeIndex.
Source§fn get_inner_node(
&self,
index: NodeIndex,
) -> Result<Option<InnerNode>, StorageError>
fn get_inner_node( &self, index: NodeIndex, ) -> Result<Option<InnerNode>, StorageError>
Retrieves a single inner node from a Subtree.
This function is intended for accessing nodes within a Subtree, meaning
index.depth() must be greater than or equal to IN_MEMORY_DEPTH.
§Errors
StorageError::Backend: Ifindex.depth() < IN_MEMORY_DEPTH.StorageError::Backend: If the read lock for subtrees cannot be acquired.
Returns Ok(None) if the subtree or the specific inner node within it is not found.
Source§fn set_inner_node(
&self,
index: NodeIndex,
node: InnerNode,
) -> Result<Option<InnerNode>, StorageError>
fn set_inner_node( &self, index: NodeIndex, node: InnerNode, ) -> Result<Option<InnerNode>, StorageError>
Sets a single inner node within a Subtree.
index.depth()must be greater than or equal toIN_MEMORY_DEPTH.- If the target Subtree does not exist, it is created.
- The
nodeis then inserted into the Subtree.
Returns the InnerNode that was previously at this index, if any.
§Errors
StorageError::Backend: Ifindex.depth() < IN_MEMORY_DEPTH.StorageError::Backend: If the write lock for subtrees cannot be acquired.
Source§fn remove_inner_node(
&self,
index: NodeIndex,
) -> Result<Option<InnerNode>, StorageError>
fn remove_inner_node( &self, index: NodeIndex, ) -> Result<Option<InnerNode>, StorageError>
Removes a single inner node from a Subtree.
index.depth()must be greater than or equal toIN_MEMORY_DEPTH.- If the Subtree becomes empty after removing the node, the Subtree itself is removed from storage.
Returns the InnerNode that was removed, if any.
§Errors
StorageError::Backend: Ifindex.depth() < IN_MEMORY_DEPTH.StorageError::Backend: If the write lock for subtrees cannot be acquired.
Source§fn apply(&self, updates: StorageUpdates) -> Result<(), StorageError>
fn apply(&self, updates: StorageUpdates) -> Result<(), StorageError>
Applies a set of updates atomically to the storage.
This method handles updates to:
- Leaves: Inserts new or updated leaves, removes specified leaves.
- Subtrees: Inserts new or updated subtrees, removes specified subtrees.
- Root hash: Sets the new root hash of the SMT.
All operations are performed after acquiring write locks on the root, leaves, and subtrees collections, ensuring atomicity of the batch update.
§Errors
Returns StorageError::Backend if any of the necessary write locks
(for root, leaves, or subtrees) cannot be acquired.
Source§fn iter_leaves(
&self,
) -> Result<Box<dyn Iterator<Item = (u64, SmtLeaf)> + '_>, StorageError>
fn iter_leaves( &self, ) -> Result<Box<dyn Iterator<Item = (u64, SmtLeaf)> + '_>, StorageError>
Returns an iterator over all (index, SmtLeaf) pairs in the storage.
The iterator provides access to the current state of the leaves.
§Errors
Returns StorageError::Backend if the read lock for leaves cannot be acquired.
Source§fn iter_subtrees(
&self,
) -> Result<Box<dyn Iterator<Item = Subtree> + '_>, StorageError>
fn iter_subtrees( &self, ) -> Result<Box<dyn Iterator<Item = Subtree> + '_>, StorageError>
Returns an iterator over all Subtrees in the storage.
The iterator provides access to the current subtrees from storage.
§Errors
Returns StorageError::Backend if the read lock for subtrees cannot be acquired.
Auto Trait Implementations§
impl !Freeze for MemoryStorage
impl RefUnwindSafe for MemoryStorage
impl Send for MemoryStorage
impl Sync for MemoryStorage
impl Unpin for MemoryStorage
impl UnwindSafe for MemoryStorage
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,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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 moreSource§impl<D> OwoColorize for D
impl<D> OwoColorize for D
Source§fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
fn fg<C>(&self) -> FgColorDisplay<'_, C, Self>where
C: Color,
Source§fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
fn bg<C>(&self) -> BgColorDisplay<'_, C, Self>where
C: Color,
Source§fn black(&self) -> FgColorDisplay<'_, Black, Self>
fn black(&self) -> FgColorDisplay<'_, Black, Self>
Source§fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
fn on_black(&self) -> BgColorDisplay<'_, Black, Self>
Source§fn red(&self) -> FgColorDisplay<'_, Red, Self>
fn red(&self) -> FgColorDisplay<'_, Red, Self>
Source§fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
fn on_red(&self) -> BgColorDisplay<'_, Red, Self>
Source§fn green(&self) -> FgColorDisplay<'_, Green, Self>
fn green(&self) -> FgColorDisplay<'_, Green, Self>
Source§fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
fn on_green(&self) -> BgColorDisplay<'_, Green, Self>
Source§fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
fn yellow(&self) -> FgColorDisplay<'_, Yellow, Self>
Source§fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
fn on_yellow(&self) -> BgColorDisplay<'_, Yellow, Self>
Source§fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
fn blue(&self) -> FgColorDisplay<'_, Blue, Self>
Source§fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
fn on_blue(&self) -> BgColorDisplay<'_, Blue, Self>
Source§fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
fn magenta(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_magenta(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
fn purple(&self) -> FgColorDisplay<'_, Magenta, Self>
Source§fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
fn on_purple(&self) -> BgColorDisplay<'_, Magenta, Self>
Source§fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
fn cyan(&self) -> FgColorDisplay<'_, Cyan, Self>
Source§fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
fn on_cyan(&self) -> BgColorDisplay<'_, Cyan, Self>
Source§fn white(&self) -> FgColorDisplay<'_, White, Self>
fn white(&self) -> FgColorDisplay<'_, White, Self>
Source§fn on_white(&self) -> BgColorDisplay<'_, White, Self>
fn on_white(&self) -> BgColorDisplay<'_, White, Self>
Source§fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
fn default_color(&self) -> FgColorDisplay<'_, Default, Self>
Source§fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
fn on_default_color(&self) -> BgColorDisplay<'_, Default, Self>
Source§fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
fn bright_black(&self) -> FgColorDisplay<'_, BrightBlack, Self>
Source§fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
fn on_bright_black(&self) -> BgColorDisplay<'_, BrightBlack, Self>
Source§fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
fn bright_red(&self) -> FgColorDisplay<'_, BrightRed, Self>
Source§fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
fn on_bright_red(&self) -> BgColorDisplay<'_, BrightRed, Self>
Source§fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
fn bright_green(&self) -> FgColorDisplay<'_, BrightGreen, Self>
Source§fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
fn on_bright_green(&self) -> BgColorDisplay<'_, BrightGreen, Self>
Source§fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
fn bright_yellow(&self) -> FgColorDisplay<'_, BrightYellow, Self>
Source§fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
fn on_bright_yellow(&self) -> BgColorDisplay<'_, BrightYellow, Self>
Source§fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
fn bright_blue(&self) -> FgColorDisplay<'_, BrightBlue, Self>
Source§fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
fn on_bright_blue(&self) -> BgColorDisplay<'_, BrightBlue, Self>
Source§fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_magenta(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_magenta(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
fn bright_purple(&self) -> FgColorDisplay<'_, BrightMagenta, Self>
Source§fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
fn on_bright_purple(&self) -> BgColorDisplay<'_, BrightMagenta, Self>
Source§fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
fn bright_cyan(&self) -> FgColorDisplay<'_, BrightCyan, Self>
Source§fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
fn on_bright_cyan(&self) -> BgColorDisplay<'_, BrightCyan, Self>
Source§fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
fn bright_white(&self) -> FgColorDisplay<'_, BrightWhite, Self>
Source§fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
fn on_bright_white(&self) -> BgColorDisplay<'_, BrightWhite, Self>
Source§fn bold(&self) -> BoldDisplay<'_, Self>
fn bold(&self) -> BoldDisplay<'_, Self>
Source§fn dimmed(&self) -> DimDisplay<'_, Self>
fn dimmed(&self) -> DimDisplay<'_, Self>
Source§fn italic(&self) -> ItalicDisplay<'_, Self>
fn italic(&self) -> ItalicDisplay<'_, Self>
Source§fn underline(&self) -> UnderlineDisplay<'_, Self>
fn underline(&self) -> UnderlineDisplay<'_, Self>
Source§fn blink(&self) -> BlinkDisplay<'_, Self>
fn blink(&self) -> BlinkDisplay<'_, Self>
Source§fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
fn blink_fast(&self) -> BlinkFastDisplay<'_, Self>
Source§fn reversed(&self) -> ReversedDisplay<'_, Self>
fn reversed(&self) -> ReversedDisplay<'_, Self>
Source§fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
fn strikethrough(&self) -> StrikeThroughDisplay<'_, Self>
Source§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 moreSource§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