Skip to content

Commit

Permalink
Remove useless prints, add more comments and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
AurelienFT committed Jan 26, 2024
1 parent cdc7dff commit fe355fe
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ pathfinder-merkle-tree = { git = "https://github.com/massalabs/pathfinder.git",
pathfinder-storage = { git = "https://github.com/massalabs/pathfinder.git", package = "pathfinder-storage", rev = "b7b6d76a76ab0e10f92e5f84ce099b5f727cb4db" }
rand = "0.8.5"
tempfile = "3.8.0"
rstest = "0.18.2"
1 change: 0 additions & 1 deletion src/changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ impl ChangeBatch {
}
}

//TODO: Use serde
pub fn serialize<ID: Id>(&self, id: &ID) -> Vec<(Vec<u8>, &[u8])> {
let id = id.to_bytes();
self.0
Expand Down
3 changes: 1 addition & 2 deletions src/tests/madara_comparison.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ fn trie_height_251() {
let mut id_builder = BasicIdBuilder::new();
let id = id_builder.new_id();
bonsai_storage.commit(id).unwrap();
let root_hash = bonsai_storage.root_hash().unwrap();
println!("root_hash: {:?}", root_hash);
bonsai_storage.root_hash().unwrap();
}
// Test to add on Madara side to check with a tree of height 251 and see that we have same hash
// #[test]// fn test_height_251() {
Expand Down
12 changes: 0 additions & 12 deletions src/tests/simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,7 @@ fn basics() {
);
let bitvec = BitVec::from_vec(pair3.0.clone());
bonsai_storage.insert(&bitvec, &pair3.1).unwrap();
println!(
"get: {:?}",
bonsai_storage.get(&BitVec::from_vec(vec![1, 2, 1]))
);
bonsai_storage.commit(id_builder.new_id()).unwrap();
println!(
"get: {:?}",
bonsai_storage.get(&BitVec::from_vec(vec![1, 2, 2]))
);
println!(
"get: {:?}",
bonsai_storage.get(&BitVec::from_vec(vec![1, 2, 3]))
);
let bitvec = BitVec::from_vec(vec![1, 2, 1]);
bonsai_storage.remove(&bitvec).unwrap();
assert_eq!(
Expand Down
3 changes: 0 additions & 3 deletions src/tests/trie_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,7 @@ fn remove_and_reinsert() {
bonsai_storage.remove(&bitvec).unwrap();
bonsai_storage.commit(id1).unwrap();
let root_hash1 = bonsai_storage.root_hash().unwrap();
println!("root_hash1: {:?}", root_hash1);

let id2 = id_builder.new_id();
println!("before second insert");
bonsai_storage.insert(&bitvec, &pair1.1).unwrap();
bonsai_storage.commit(id2).unwrap();

Expand Down
23 changes: 19 additions & 4 deletions src/trie/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ use super::{merkle_node::Direction, TrieKey};
#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

#[cfg(all(feature = "std", test))]
use rstest::rstest;

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Path(pub BitVec<u8, Msb0>);

impl Encode for Path {
fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
// Inspired from scale_bits crate but don't use it to avoid copy and u32 length encoding
// Copied from scale_bits crate (https://github.com/paritytech/scale-bits/blob/820a3e8e0c9db18ef6acfa2a9a19f738400b0637/src/scale/encode_iter.rs#L28)
// but don't use it directly to avoid copy and u32 length encoding
// How it works ?
// 1. We encode the number of bits in the bitvec as a u8
// 2. We build elements of a size of u8 using bit shifting
// 3. A last element, not full, is created if there is a remainder of bits
let iter = self.0.iter();
let len = iter.len();
// SAFETY: len is <= 251
Expand Down Expand Up @@ -118,9 +126,16 @@ impl From<&Path> for TrieKey {
}
}

#[test]
fn test_shared_path_encode_decode() {
let path = Path(BitVec::<u8, Msb0>::from_slice(&[0b10101010, 0b10101010]));
#[cfg(all(feature = "std", test))]
#[rstest]
#[case(&[0b10101010, 0b10101010])]
#[case(&[])]
#[case(&[0b10101010])]
#[case(&[0b00000000])]
#[case(&[0b11111111])]
#[case(&[0b11111111, 0b00000000, 0b10101010, 0b10101010, 0b11111111, 0b00000000, 0b10101010, 0b10101010, 0b11111111, 0b00000000, 0b10101010, 0b10101010])]
fn test_shared_path_encode_decode(#[case] input: &[u8]) {
let path = Path(BitVec::<u8, Msb0>::from_slice(input));
let mut encoded = Vec::new();
path.encode_to(&mut encoded);

Expand Down

0 comments on commit fe355fe

Please sign in to comment.