Skip to content

Commit

Permalink
fix: Merk#has_node responds with true on empty tree
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Jul 6, 2022
1 parent dbbb807 commit a9a0831
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions merk/src/merk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ where
fn has_node(&self, key: &[u8]) -> CostContext<Result<bool>> {
self.use_tree(move |maybe_tree| {
let mut cursor = match maybe_tree {
None => return Ok(true).wrap_with_cost(Default::default()), // empty tree
None => return Ok(false).wrap_with_cost(Default::default()), // empty tree
Some(tree) => tree,
};

Expand All @@ -240,7 +240,7 @@ where

let left = key < cursor.key();
let link = match cursor.link(left) {
None => return Ok(true).wrap_with_cost(Default::default()), // not found
None => return Ok(false).wrap_with_cost(Default::default()), // not found
Some(link) => link,
};

Expand Down Expand Up @@ -809,6 +809,29 @@ mod test {
assert_invariants(&merk);
}

#[test]
fn test_has_node_with_empty_tree() {
let mut merk = TempMerk::new();

let key = b"something";

let result = merk.has_node(key).unwrap().unwrap();

assert!(!result);

let batch_entry = (key, Op::Put(vec![123; 60]));

let batch = vec![batch_entry];

merk.apply::<_, Vec<_>>(&batch, &[])
.unwrap()
.expect("should ...");

let result = merk.has_node(key).unwrap().unwrap();

assert!(result);
}

#[test]
fn insert_rand() {
let tree_size = 40;
Expand Down

0 comments on commit a9a0831

Please sign in to comment.