-
Notifications
You must be signed in to change notification settings - Fork 25
feat: abstract Merk storage #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,5 +2,6 @@ | |
| members = [ | ||
| "grovedb", | ||
| "merk", | ||
| "node-grove" | ||
| "node-grove", | ||
| "storage", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,3 @@ | ||
| #![feature(trivial_bounds)] | ||
| mod subtree; | ||
| #[cfg(test)] | ||
| mod tests; | ||
|
|
@@ -9,9 +8,13 @@ use std::{ | |
| rc::Rc, | ||
| }; | ||
|
|
||
| use merk::{self, rocksdb, Merk}; | ||
| use merk::{self, Merk}; | ||
| use rs_merkle::{algorithms::Sha256, MerkleTree}; | ||
| pub use subtree::Element; | ||
| use storage::{ | ||
| rocksdb_storage::{PrefixedRocksDbStorage, PrefixedRocksDbStorageError}, | ||
| Storage, | ||
| }; | ||
|
|
||
| /// Limit of possible indirections | ||
| const MAX_REFERENCE_HOPS: usize = 10; | ||
|
|
@@ -24,7 +27,7 @@ const ROOT_LEAFS_SERIALIZED_KEY: &[u8] = b"rootLeafsSerialized"; | |
| #[derive(Debug, thiserror::Error)] | ||
| pub enum Error { | ||
| #[error("rocksdb error")] | ||
| RocksDBError(#[from] merk::rocksdb::Error), | ||
| RocksDBError(#[from] PrefixedRocksDbStorageError), | ||
| #[error("unable to open Merk db")] | ||
| MerkError(merk::Error), | ||
| #[error("invalid path: {0}")] | ||
|
|
@@ -46,57 +49,65 @@ impl From<merk::Error> for Error { | |
| pub struct GroveDb { | ||
| root_tree: MerkleTree<Sha256>, | ||
| root_leaf_keys: HashMap<Vec<u8>, usize>, | ||
| subtrees: HashMap<Vec<u8>, Merk>, | ||
| db: Rc<rocksdb::DB>, | ||
| subtrees: HashMap<Vec<u8>, Merk<PrefixedRocksDbStorage>>, | ||
| meta_storage: PrefixedRocksDbStorage, | ||
| db: Rc<storage::rocksdb_storage::DB>, | ||
| } | ||
|
|
||
| impl GroveDb { | ||
| pub fn open<P: AsRef<Path>>(path: P) -> Result<Self, Error> { | ||
| let db = Rc::new(rocksdb::DB::open_cf_descriptors( | ||
| &Merk::default_db_opts(), | ||
| path, | ||
| merk::column_families(), | ||
| )?); | ||
| let db = Rc::new( | ||
| storage::rocksdb_storage::DB::open_cf_descriptors( | ||
| &storage::rocksdb_storage::default_db_opts(), | ||
| path, | ||
| storage::rocksdb_storage::column_families(), | ||
| ) | ||
| .map_err(Into::<PrefixedRocksDbStorageError>::into)?, | ||
| ); | ||
| let meta_storage = PrefixedRocksDbStorage::new(db.clone(), Vec::new())?; | ||
|
|
||
| let mut subtrees = HashMap::new(); | ||
| // TODO: owned `get` is not required for deserialization | ||
| if let Some(prefixes_serialized) = db.get(SUBTRESS_SERIALIZED_KEY)? { | ||
| if let Some(prefixes_serialized) = meta_storage.get_meta(SUBTRESS_SERIALIZED_KEY)? { | ||
| let subtrees_prefixes: Vec<Vec<u8>> = bincode::deserialize(&prefixes_serialized)?; | ||
| for prefix in subtrees_prefixes { | ||
| let subtree_merk = Merk::open(db.clone(), prefix.to_vec())?; | ||
| let subtree_merk = | ||
| Merk::open(PrefixedRocksDbStorage::new(db.clone(), prefix.to_vec())?)?; | ||
| subtrees.insert(prefix.to_vec(), subtree_merk); | ||
| } | ||
| } | ||
|
|
||
| // TODO: owned `get` is not required for deserialization | ||
| let root_leaf_keys: HashMap<Vec<u8>, usize> = | ||
| if let Some(root_leaf_keys_serialized) = db.get(ROOT_LEAFS_SERIALIZED_KEY)? { | ||
| bincode::deserialize(&root_leaf_keys_serialized)? | ||
| } else { | ||
| HashMap::new() | ||
| }; | ||
| let root_leaf_keys: HashMap<Vec<u8>, usize> = if let Some(root_leaf_keys_serialized) = | ||
| meta_storage.get_meta(ROOT_LEAFS_SERIALIZED_KEY)? | ||
| { | ||
| bincode::deserialize(&root_leaf_keys_serialized)? | ||
| } else { | ||
| HashMap::new() | ||
| }; | ||
|
|
||
| Ok(GroveDb { | ||
| root_tree: Self::build_root_tree(&subtrees, &root_leaf_keys), | ||
| db: db.clone(), | ||
| db, | ||
| subtrees, | ||
| root_leaf_keys, | ||
| meta_storage, | ||
| }) | ||
| } | ||
|
|
||
| fn store_subtrees_keys_data(&self) -> Result<(), Error> { | ||
| let prefixes: Vec<Vec<u8>> = self.subtrees.keys().map(|x| x.clone()).collect(); | ||
| self.db | ||
| .put(SUBTRESS_SERIALIZED_KEY, bincode::serialize(&prefixes)?)?; | ||
| self.db.put( | ||
| self.meta_storage | ||
| .put_meta(SUBTRESS_SERIALIZED_KEY, &bincode::serialize(&prefixes)?)?; | ||
| self.meta_storage.put_meta( | ||
| ROOT_LEAFS_SERIALIZED_KEY, | ||
| bincode::serialize(&self.root_leaf_keys)?, | ||
| &bincode::serialize(&self.root_leaf_keys)?, | ||
| )?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn build_root_tree( | ||
| subtrees: &HashMap<Vec<u8>, Merk>, | ||
| subtrees: &HashMap<Vec<u8>, Merk<PrefixedRocksDbStorage>>, | ||
| root_leaf_keys: &HashMap<Vec<u8>, usize>, | ||
| ) -> MerkleTree<Sha256> { | ||
| let mut leaf_hashes: Vec<[u8; 32]> = vec![[0; 32]; root_leaf_keys.len()]; | ||
|
|
@@ -121,13 +132,17 @@ impl GroveDb { | |
| match &mut element { | ||
| Element::Tree(subtree_root_hash) => { | ||
| // Helper closure to create a new subtree under path + key | ||
| let create_subtree_merk = || -> Result<(Vec<u8>, Merk), Error> { | ||
| let compressed_path_subtree = Self::compress_path(path, Some(&key)); | ||
| Ok(( | ||
| compressed_path_subtree.clone(), | ||
| Merk::open(self.db.clone(), compressed_path_subtree)?, | ||
| )) | ||
| }; | ||
| let create_subtree_merk = | ||
| || -> Result<(Vec<u8>, Merk<PrefixedRocksDbStorage>), Error> { | ||
| let compressed_path_subtree = Self::compress_path(path, Some(&key)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. compress path needs to be make sure that ab, a != a, ba |
||
| Ok(( | ||
| compressed_path_subtree.clone(), | ||
| Merk::open(PrefixedRocksDbStorage::new( | ||
| self.db.clone(), | ||
| compressed_path_subtree, | ||
| )?)?, | ||
| )) | ||
| }; | ||
| if path.is_empty() { | ||
| // Add subtree to the root tree | ||
|
|
||
|
|
@@ -200,7 +215,7 @@ impl GroveDb { | |
| Element::get(&merk, key) | ||
| } | ||
|
|
||
| fn follow_reference<'a>(&self, mut path: Vec<Vec<u8>>) -> Result<subtree::Element, Error> { | ||
| fn follow_reference(&self, mut path: Vec<Vec<u8>>) -> Result<subtree::Element, Error> { | ||
| let mut hops_left = MAX_REFERENCE_HOPS; | ||
| let mut current_element; | ||
| let mut visited = HashSet::new(); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't want to keep all merk subtrees in memory.