Skip to content

Commit

Permalink
Add multi trie management (#21)
Browse files Browse the repository at this point in the history
* Add multi trie management and update of the documentation

* Uncomment trie log test

* Fix clippy and batch db writes

* Format

* Re-ignore doc test

* Add function to init tree

* Fix a bug in deletion of keys

* Remove dead code

* Add new way to merge parent branch

* Fix double merge in deletion

* Update build

* Fix bad preload nodes

* Fix last deletion issue

* Fix clippy

* Add more test and fix a special case in deletion

* Fix don't traverse when key doesn't exists

* Fix issue binary as a last node

* Format

* Add rayon support for hash calculation

* Format

* Fix clippy warnings

* Add verification to not load trie for nothing

* Fix no std

* Add get keys and improve iteration on changes

* Remove misleading function and add a constructor for triekey

* Fix no std
  • Loading branch information
AurelienFT authored Apr 1, 2024
1 parent 3146910 commit 8ecf0c6
Show file tree
Hide file tree
Showing 13 changed files with 1,495 additions and 420 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ derive_more = { version = "0.99.17", default-features = false, features = [
hashbrown = "0.14.3"
log = "0.4.20"
smallvec = "1.11.2"
rayon = "1.9.0"

parity-scale-codec = { version = "3.0.0", default-features = false, features = [
"derive",
Expand Down
31 changes: 17 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,19 @@ fn main() {
// Create a simple incremental ID builder for commit IDs.
// This is not necessary, you can use any kind of strictly monotonically increasing value to tag your commits.
let mut id_builder = BasicIdBuilder::new();

// Define an idenfitier for a trie. All insert, get, remove and root hash will use this identifier. Define multiple identifier to use multiple tries that have separate root hash.
let identifier = vec![];

// Insert an item `pair1`.
let pair1 = (vec![1, 2, 1], Felt::from_hex("0x66342762FDD54D033c195fec3ce2568b62052e").unwrap());
let bitvec_1 = BitVec::from_vec(pair1.0.clone());
bonsai_storage.insert(&bitvec_1, &pair1.1).unwrap();
bonsai_storage.insert(&identifier, &bitvec_1, &pair1.1).unwrap();

// Insert a second item `pair2`.
let pair2 = (vec![1, 2, 2], Felt::from_hex("0x66342762FD54D033c195fec3ce2568b62052e").unwrap());
let bitvec = BitVec::from_vec(pair2.0.clone());
bonsai_storage.insert(&bitvec, &pair2.1).unwrap();
bonsai_storage.insert(&identifier, &bitvec, &pair2.1).unwrap();

// Commit the insertion of `pair1` and `pair2`.
let id1 = id_builder.new_id()
Expand All @@ -73,31 +76,31 @@ fn main() {
// Insert a new item `pair3`.
let pair3 = (vec![1, 2, 2], Felt::from_hex("0x664D033c195fec3ce2568b62052e").unwrap());
let bitvec = BitVec::from_vec(pair3.0.clone());
bonsai_storage.insert(&bitvec, &pair3.1).unwrap();
bonsai_storage.insert(&identifier, &bitvec, &pair3.1).unwrap();

// Commit the insertion of `pair3`. Save the commit ID to the `revert_to_id` variable.
let revert_to_id = id_builder.new_id();
bonsai_storage.commit(revert_to_id);

// Remove `pair3`.
bonsai_storage.remove(&bitvec).unwrap();
bonsai_storage.remove(&identifier, &bitvec).unwrap();

// Commit the removal of `pair3`.
bonsai_storage.commit(id_builder.new_id());

// Print the root hash and item `pair1`.
println!("root: {:#?}", bonsai_storage.root_hash());
println!("root: {:#?}", bonsai_storage.root_hash(&identifier));
println!(
"value: {:#?}",
bonsai_storage.get(&bitvec_1).unwrap()
bonsai_storage.get(&identifier, &bitvec_1).unwrap()
);

// Revert the collection state back to the commit tagged by the `revert_to_id` variable.
bonsai_storage.revert_to(revert_to_id).unwrap();

// Print the root hash and item `pair3`.
println!("root: {:#?}", bonsai_storage.root_hash());
println!("value: {:#?}", bonsai_storage.get(&bitvec).unwrap());
println!("root: {:#?}", bonsai_storage.root_hash(&identifier));
println!("value: {:#?}", bonsai_storage.get(&identifier, &bitvec).unwrap());

// Launch two threads that will simultaneously take transactional states to the commit identified by `id1`,
// asserting in both of them that the item `pair1` is present and has the right value.
Expand All @@ -108,7 +111,7 @@ fn main() {
.unwrap()
.unwrap();
let bitvec = BitVec::from_vec(pair1.0.clone());
assert_eq!(bonsai_at_txn.get(&bitvec).unwrap().unwrap(), pair1.1);
assert_eq!(bonsai_at_txn.get(&identifier, &bitvec).unwrap().unwrap(), pair1.1);
});

s.spawn(|| {
Expand All @@ -117,13 +120,13 @@ fn main() {
.unwrap()
.unwrap();
let bitvec = BitVec::from_vec(pair1.0.clone());
assert_eq!(bonsai_at_txn.get(&bitvec).unwrap().unwrap(), pair1.1);
assert_eq!(bonsai_at_txn.get(&identifier, &bitvec).unwrap().unwrap(), pair1.1);
});
});

// Read item `pair1`.
let pair1_val = bonsai_storage
.get(&BitVec::from_vec(vec![1, 2, 2]))
.get(&identifier, &BitVec::from_vec(vec![1, 2, 2]))
.unwrap();

// Insert a new item and commit.
Expand All @@ -132,15 +135,15 @@ fn main() {
Felt::from_hex("0x66342762FDD54D033c195fec3ce2568b62052e").unwrap(),
);
bonsai_storage
.insert(&BitVec::from_vec(pair4.0.clone()), &pair4.1)
.insert(&identifier, &BitVec::from_vec(pair4.0.clone()), &pair4.1)
.unwrap();
bonsai_storage.commit(id_builder.new_id()).unwrap();
let proof = bonsai_storage
.get_proof(&BitVec::from_vec(pair3.0.clone()))
.get_proof(&identifier, &BitVec::from_vec(pair3.0.clone()))
.unwrap();
assert_eq!(
BonsaiStorage::<BasicId, RocksDB<BasicId>>::verify_proof(
bonsai_storage.root_hash().unwrap(),
bonsai_storage.root_hash(&identifier).unwrap(),
&BitVec::from_vec(pair3.0.clone()),
pair3.1,
&proof
Expand Down
52 changes: 52 additions & 0 deletions ensure_no_std/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8ecf0c6

Please sign in to comment.