From d8b4510716fe0acda6b4a1189ea76f4d237a856b Mon Sep 17 00:00:00 2001 From: Avi Dessauer Date: Thu, 21 Mar 2024 08:50:58 -0500 Subject: [PATCH] Entry operations test logic but not prop yet --- Cargo.toml | 8 +++++--- tests/build_store_entry_ops.rs | 34 ++++++++++++++++++++++++++++++++++ tests/build_store_modify.rs | 8 +------- tests/utils/mod.rs | 9 +++++++++ tests/utils/operations.rs | 4 ++-- 5 files changed, 51 insertions(+), 12 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 76fa236..3e80dd2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,8 +5,10 @@ edition = "2021" license = "MIT OR Apache-2.0" [features] -default = ["std"] +default = ["std", "proptest"] std = [] +proptest = ["dep:proptest", "dep:proptest-derive"] + [profile.test] opt-level = 3 @@ -17,7 +19,7 @@ overflow-checks = true [dependencies] sha2 = "0.10" bumpalo = "3" +proptest-derive = { version = "0.4", optional = true } +proptest = { version = "1", optional = true } [dev-dependencies] -proptest = "1" -proptest-derive = "0.4" diff --git a/tests/build_store_entry_ops.rs b/tests/build_store_entry_ops.rs index 615b6ac..e36a8b1 100644 --- a/tests/build_store_entry_ops.rs +++ b/tests/build_store_entry_ops.rs @@ -1,2 +1,36 @@ mod utils; +use std::collections::HashMap; + +use kairos_trie::{ + stored::{memory_db::MemoryDb, merkle::SnapshotBuilder}, + Transaction, TrieRoot, +}; use utils::operations::*; + +pub fn end_to_end_entry_ops(batches: &[&[Operation]]) { + let db = &MemoryDb::<[u8; 8]>::empty(); + + let mut prior_root_hash = TrieRoot::default(); + // used as a reference for trie behavior + let mut hash_map = HashMap::new(); + + for batch in batches.iter() { + let (new_root_hash, snapshot) = + run_against_snapshot_builder(batch, prior_root_hash, db, &mut hash_map); + + run_against_snapshot(batch, snapshot, new_root_hash, prior_root_hash); + prior_root_hash = new_root_hash; + } + + // After all batches are applied, the trie and the hashmap should be in sync + let bump = bumpalo::Bump::new(); + let txn = Transaction::from_snapshot_builder( + SnapshotBuilder::<_, [u8; 8]>::empty(db, &bump).with_trie_root_hash(prior_root_hash), + ); + + // Check that the trie and the hashmap are in sync + for (k, v) in hash_map.iter() { + let ret_v = txn.get(k).unwrap().unwrap(); + assert_eq!(v, ret_v); + } +} diff --git a/tests/build_store_modify.rs b/tests/build_store_modify.rs index 5f02edf..b4cd766 100644 --- a/tests/build_store_modify.rs +++ b/tests/build_store_modify.rs @@ -8,13 +8,7 @@ use kairos_trie::{ stored::{memory_db::MemoryDb, merkle::SnapshotBuilder}, KeyHash, Transaction, TrieRoot, }; -use utils::insert_get::*; - -prop_compose! { - fn arb_key_hash()(data in any::<[u8; 32]>()) -> KeyHash { - KeyHash::from(&data) - } -} +use utils::{insert_get::*, *}; prop_compose! { fn arb_hashmap()( diff --git a/tests/utils/mod.rs b/tests/utils/mod.rs index 09f72a1..5e186c8 100644 --- a/tests/utils/mod.rs +++ b/tests/utils/mod.rs @@ -1,2 +1,11 @@ +use kairos_trie::KeyHash; +use proptest::prelude::*; + pub mod insert_get; pub mod operations; + +prop_compose! { + pub fn arb_key_hash()(data in any::<[u8; 32]>()) -> KeyHash { + KeyHash::from(&data) + } +} diff --git a/tests/utils/operations.rs b/tests/utils/operations.rs index 0643168..fe9c110 100644 --- a/tests/utils/operations.rs +++ b/tests/utils/operations.rs @@ -9,7 +9,7 @@ use kairos_trie::{ KeyHash, NodeHash, Transaction, TrieRoot, }; -type Value = [u8; 8]; +pub type Value = [u8; 8]; pub enum Operation { Get(KeyHash), Insert(KeyHash, Value), @@ -20,7 +20,7 @@ pub enum Operation { } // Code like this runs in the server. -fn run_against_snapshot_builder( +pub fn run_against_snapshot_builder( batch: &[Operation], old_root_hash: TrieRoot, db: &MemoryDb,