restricted_sparse_merkle_tree/
default_store.rs1use crate::{
2 collections,
3 error::Error,
4 traits::Store,
5 tree::{BranchNode, LeafNode},
6 H256,
7};
8
9#[derive(Debug, Clone, Default)]
10pub struct DefaultStore<V> {
11 branches_map: Map<H256, BranchNode>,
12 leaves_map: Map<H256, LeafNode<V>>,
13}
14
15impl<V> DefaultStore<V> {
16 pub fn branches_map(&self) -> &Map<H256, BranchNode> {
17 &self.branches_map
18 }
19 pub fn leaves_map(&self) -> &Map<H256, LeafNode<V>> {
20 &self.leaves_map
21 }
22 pub fn clear(&mut self) {
23 self.branches_map.clear();
24 self.leaves_map.clear();
25 }
26}
27
28impl<V: Clone> Store<V> for DefaultStore<V> {
29 fn get_branch(&self, node: &H256) -> Result<Option<BranchNode>, Error> {
30 Ok(self.branches_map.get(node).map(Clone::clone))
31 }
32 fn get_leaf(&self, leaf_hash: &H256) -> Result<Option<LeafNode<V>>, Error> {
33 Ok(self.leaves_map.get(leaf_hash).map(Clone::clone))
34 }
35 fn insert_branch(&mut self, node: H256, branch: BranchNode) -> Result<(), Error> {
36 self.branches_map.insert(node, branch);
37 Ok(())
38 }
39 fn insert_leaf(&mut self, leaf_hash: H256, leaf: LeafNode<V>) -> Result<(), Error> {
40 self.leaves_map.insert(leaf_hash, leaf);
41 Ok(())
42 }
43 fn remove_branch(&mut self, node: &H256) -> Result<(), Error> {
44 self.branches_map.remove(node);
45 Ok(())
46 }
47 fn remove_leaf(&mut self, leaf_hash: &H256) -> Result<(), Error> {
48 self.leaves_map.remove(leaf_hash);
49 Ok(())
50 }
51}
52
53cfg_if::cfg_if! {
54 if #[cfg(feature = "std")] {
55 pub type Map<K, V> = collections::HashMap<K, V>;
56 pub type Entry<'a, K, V> = collections::hash_map::Entry<'a, K, V>;
57 } else {
58 pub type Map<K, V> = collections::BTreeMap<K, V>;
59 pub type Entry<'a, K, V> = collections::btree_map::Entry<'a, K, V>;
60 }
61}