Skip to content

Commit

Permalink
feat(BonsaiStorage): added new methods for retrieving key-value pairs…
Browse files Browse the repository at this point in the history
… and change id for `BonsaiStorage` (#25)

* test(insert): added tests for insertions into `BonsaiStorage` without skiping first 5 bites

* test(keys): updated key retrieval test

* feat(key-value): added function `get_key_value_pairs`

Also updated `get_keys` to filter out non-leaf nodes + remove the length byte at index 0 when returning
keys.

> Please note that this currently poses problems with the implementation of `HashMapDb`, due to it not
differenciating leaf nodes from branch nodes. This will have to be fixed at a later date.

* feat(id): added `get_latest_id`

* fix(clippy): code now uses `filter_map` and fixed clippy errors

---------

Co-authored-by: Timothée Delabrouille <[email protected]>
  • Loading branch information
Trantorian1 and tdelabro authored Apr 12, 2024
1 parent 2af0cb6 commit 6efb672
Show file tree
Hide file tree
Showing 4 changed files with 511 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ pathfinder-storage = { git = "https://github.com/massalabs/pathfinder.git", pack
rand = "0.8.5"
tempfile = "3.8.0"
rstest = "0.18.2"
test-log = "0.2.15"
indexmap = "2.2.6"
criterion = "0.5.1"

[[bench]]
Expand Down
4 changes: 4 additions & 0 deletions src/key_value_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ where
Ok(self.db.get(&key.into())?)
}

pub(crate) fn get_latest_id(&self) -> Option<ID> {
self.changes_store.id_queue.back().cloned()
}

pub(crate) fn contains(
&self,
key: &TrieKey,
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,20 @@ where
self.tries.get_keys(identifier)
}

/// Get all the key-value pairs in a specific trie.
#[allow(clippy::type_complexity)]
pub fn get_key_value_pairs(
&self,
identifier: &[u8],
) -> Result<Vec<(Vec<u8>, Vec<u8>)>, BonsaiStorageError<DB::DatabaseError>> {
self.tries.get_key_value_pairs(identifier)
}

/// Get the id from the latest commit, or `None` if no commit has taken place yet.
pub fn get_latest_id(&self) -> Option<ChangeID> {
self.tries.db_ref().get_latest_id()
}

/// Verifies a merkle-proof for a given `key` and `value`.
pub fn verify_proof(
root: Felt,
Expand Down
Loading

0 comments on commit 6efb672

Please sign in to comment.