Skip to content

Commit

Permalink
Entry operations test logic but not prop yet
Browse files Browse the repository at this point in the history
  • Loading branch information
Avi-D-coder committed Mar 21, 2024
1 parent 66e60a5 commit d8b4510
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 12 deletions.
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
34 changes: 34 additions & 0 deletions tests/build_store_entry_ops.rs
Original file line number Diff line number Diff line change
@@ -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);
}
}
8 changes: 1 addition & 7 deletions tests/build_store_modify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()(
Expand Down
9 changes: 9 additions & 0 deletions tests/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
4 changes: 2 additions & 2 deletions tests/utils/operations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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<NodeHash>,
db: &MemoryDb<Value>,
Expand Down

0 comments on commit d8b4510

Please sign in to comment.