Skip to content

Commit

Permalink
Add example usage
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelienFT committed Nov 21, 2023
1 parent 12882c7 commit 16b550e
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
69 changes: 64 additions & 5 deletions documentation/specification.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,6 @@ pub enum AccumulatorError {
impl Accumulator<ID>
where:
ID: Id {
// Increase the point in time to start a new batch of changes and clear the previous one
fn new_block(&mut self, block_number: u64);
// Ask for the read of a key (should return also with modifications that are not applied to the database yet)
fn get(&self, key: &[u8]) -> Result<TrieValue, AccumulatorError>;
Expand All @@ -349,7 +346,7 @@ where:
fn goto(&mut self, id: ID) -> Result<(), AccumulatorError>;
// Apply all the changes to the database and create a trielog (and a snapshot if we are at X point in time) with all the changes and clear them.
fn commit(&mut self);
fn commit(&mut self, id: ID);
}
----

Expand Down Expand Up @@ -430,7 +427,69 @@ This interface should be the only interface used by the caller to have a compreh

==== Definition

For now, the interface is the same as the one of the accumulator.
[source,rust]
----
pub trait BonsaiStorage<'a, ID>
where:
ID: Id {
// Create a new bonsai storage instance
fn new(db: &'a mut KeyValueDB<DB, ID>, root: &'a mut BonsaiTrieHash) -> Self;
// Insert a new key/value in the trie
fn insert(&mut self, key: &[u8], value: &[u8]);
// Remove a key/value in the trie
fn remove(&mut self, key: &[u8]);
// Commit all the changes to the trie
fn commit(&mut self, id: ID);
// Get a value in the trie
fn get(&self, key: &[u8]);
// Go to a specific point in time, if possible, using trie logs and snapshots.
fn go_to(&mut self, requested_id: ID);
// Get the root hash of the trie
fn root(&self) -> Hash;
}
----

==== Usage example

For example you will be able to use the interface like this:

[source,rust]
----
fn main() {
let mut db = KeyValueDB::<RocksDB, BasicId>::new(RocksDB::new("./rocksdb")); // add empty node
let mut root = BonsaiTrieHash::default();
let mut bonsai_storage = BonsaiStorage::new(&mut db, &mut root);
let mut id_builder = BasicIdBuilder::new();
bonsai_storage.insert(&[1, 2, 3, 4, 5, 6], &[4, 5, 6]);
bonsai_storage.insert(&[1, 2, 3, 4, 5, 7], &[4, 5, 8]);
bonsai_storage.commit(id_builder.new_id());
bonsai_storage.insert(&[1, 2, 2], &[7, 5, 6]);
let go_to_id = id_builder.new_id();
bonsai_storage.commit(go_to_id);
bonsai_storage.remove(&[1, 2, 2]);
bonsai_storage.commit(id_builder.new_id());
println!("root: {:#?}", bonsai_storage.root());
println!(
"value: {:#?}",
bonsai_storage.get(&[1, 2, 3, 4, 5, 6]).unwrap()
);
bonsai_storage.go_to(go_to_id).unwrap();
println!("root: {:#?}", bonsai_storage.root());
println!("value: {:#?}", bonsai_storage.get(&[1, 2, 2]).unwrap());
}
----

==== Disclaimer

All the interfaces above are subjects to little adaptations changes while making the implementation.

Every methods are shown in a trait here to illustrate them in this document but if genericity is not needed, we will use a struct with methods directly.

=== E2E & Benchmarks

Expand Down
Binary file modified documentation/specification.pdf
Binary file not shown.

0 comments on commit 16b550e

Please sign in to comment.