From fe355fe8c33cb64859f786dae87420e38cae18ae Mon Sep 17 00:00:00 2001 From: AurelienFT Date: Fri, 26 Jan 2024 16:29:31 +0100 Subject: [PATCH] Remove useless prints, add more comments and tests --- Cargo.toml | 1 + src/changes.rs | 1 - src/tests/madara_comparison.rs | 3 +-- src/tests/simple.rs | 12 ------------ src/tests/trie_log.rs | 3 --- src/trie/path.rs | 23 +++++++++++++++++++---- 6 files changed, 21 insertions(+), 22 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index dd4f93a..85e7393 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/changes.rs b/src/changes.rs index 3af5c3e..3d9a19c 100644 --- a/src/changes.rs +++ b/src/changes.rs @@ -37,7 +37,6 @@ impl ChangeBatch { } } - //TODO: Use serde pub fn serialize(&self, id: &ID) -> Vec<(Vec, &[u8])> { let id = id.to_bytes(); self.0 diff --git a/src/tests/madara_comparison.rs b/src/tests/madara_comparison.rs index 6a525f5..48b1a82 100644 --- a/src/tests/madara_comparison.rs +++ b/src/tests/madara_comparison.rs @@ -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() { diff --git a/src/tests/simple.rs b/src/tests/simple.rs index a67902f..9ae3eb6 100644 --- a/src/tests/simple.rs +++ b/src/tests/simple.rs @@ -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!( diff --git a/src/tests/trie_log.rs b/src/tests/trie_log.rs index 06477ee..56c6ecc 100644 --- a/src/tests/trie_log.rs +++ b/src/tests/trie_log.rs @@ -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(); diff --git a/src/trie/path.rs b/src/trie/path.rs index 2604244..78906e4 100644 --- a/src/trie/path.rs +++ b/src/trie/path.rs @@ -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); impl Encode for Path { fn encode_to(&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 @@ -118,9 +126,16 @@ impl From<&Path> for TrieKey { } } -#[test] -fn test_shared_path_encode_decode() { - let path = Path(BitVec::::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::::from_slice(input)); let mut encoded = Vec::new(); path.encode_to(&mut encoded);