Skip to content

Commit

Permalink
feat(rln-v1): return empty metadata if it doesnt exist (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
rymnc authored Mar 6, 2024
1 parent b0be329 commit 78bec51
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
3 changes: 2 additions & 1 deletion rln/src/pm_tree_adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ impl ZerokitMerkleTree for PmTree {
let data = self.tree.db.get(METADATA_KEY)?;

if data.is_none() {
return Err(Report::msg("metadata does not exist"));
// send empty Metadata
return Ok(Vec::new());
}
Ok(data.unwrap())
}
Expand Down
15 changes: 15 additions & 0 deletions rln/src/public.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2108,4 +2108,19 @@ mod test {

assert_eq!(arbitrary_metadata, received_metadata);
}

#[test]
fn test_empty_metadata() {
let tree_height = TEST_TREE_HEIGHT;

let input_buffer =
Cursor::new(json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string());
let rln = RLN::new(tree_height, input_buffer).unwrap();

let mut buffer = Cursor::new(Vec::<u8>::new());
rln.get_metadata(&mut buffer).unwrap();
let received_metadata = buffer.into_inner();

assert_eq!(received_metadata.len(), 0);
}
}
23 changes: 22 additions & 1 deletion rln/tests/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1209,7 +1209,7 @@ mod test {
}

#[test]
fn test_metadata() {
fn test_valid_metadata() {
// We create a RLN instance
let tree_height = TEST_TREE_HEIGHT;

Expand All @@ -1235,4 +1235,25 @@ mod test {

assert_eq!(result_data, seed_bytes.to_vec());
}

#[test]
fn test_empty_metadata() {
// We create a RLN instance
let tree_height = TEST_TREE_HEIGHT;

let mut rln_pointer = MaybeUninit::<*mut RLN>::uninit();
let input_config = json!({ "resources_folder": TEST_RESOURCES_FOLDER }).to_string();
let input_buffer = &Buffer::from(input_config.as_bytes());
let success = new(tree_height, input_buffer, rln_pointer.as_mut_ptr());
assert!(success, "RLN object creation failed");
let rln_pointer = unsafe { &mut *rln_pointer.assume_init() };

let mut output_buffer = MaybeUninit::<Buffer>::uninit();
let success = get_metadata(rln_pointer, output_buffer.as_mut_ptr());
assert!(success, "get_metadata call failed");

let output_buffer = unsafe { output_buffer.assume_init() };

assert_eq!(output_buffer.len, 0);
}
}

0 comments on commit 78bec51

Please sign in to comment.