Skip to content

Commit

Permalink
test(archival_mmr): Add panicking test for leaf-getter
Browse files Browse the repository at this point in the history
Requested in #318.

Co-authored-by: Alan Szepieniec <[email protected]>
  • Loading branch information
Sword-Smith and aszepieniec committed Jan 13, 2025
1 parent 0477832 commit c3dfc35
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/util_types/mutator_set/archival_mmr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,13 @@ impl<Storage: StorageVec<Digest>> ArchivalMmr<Storage> {

/// Get a leaf from the MMR, will panic if index is out of range.
pub async fn get_leaf_async(&self, leaf_index: u64) -> Digest {
// Use debug-assert here to limit this lookup to *one* db-lookup in
// production. Otherwise, it would be two lookups.
debug_assert!(
leaf_index < self.num_leafs().await,
"Leaf index out-of-bounds. Got leaf index {leaf_index} but num_leafs was {}",
self.num_leafs().await
);
let node_index = shared_advanced::leaf_index_to_node_index(leaf_index);
self.digests.get(node_index).await
}
Expand Down Expand Up @@ -1164,6 +1171,14 @@ pub(crate) mod mmr_test {
}
}

#[tokio::test]
#[should_panic(expected = "Leaf index out-of-bounds. Got leaf index 17 but num_leafs was 17")]
async fn get_panics_when_out_of_bounds() {
let digests = vec![Digest::default(); 17];
let ammr = mock::get_ammr_from_digests(digests.clone()).await;
ammr.get_leaf_async(17).await;
}

#[tokio::test]
async fn leveldb_persist_storage_schema_test() {
let db = NeptuneLevelDb::open_new_test_database(true, None, None, None)
Expand Down

0 comments on commit c3dfc35

Please sign in to comment.