Skip to content

Commit

Permalink
test that subtree read fns work right with excluded start bound
Browse files Browse the repository at this point in the history
  • Loading branch information
arya2 committed Oct 19, 2023
1 parent 4ae71e5 commit 7ec6f21
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion zebra-chain/src/subtree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub const TRACKED_SUBTREE_HEIGHT: u8 = 16;

/// A note commitment subtree index, used to identify a subtree in a shielded pool.
/// Also used to count subtrees.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize)]
#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Serialize, Deserialize, Default)]
#[cfg_attr(any(test, feature = "proptest-impl"), derive(Arbitrary))]
#[serde(transparent)]
pub struct NoteCommitmentSubtreeIndex(pub u16);
Expand Down
27 changes: 27 additions & 0 deletions zebra-state/src/service/read/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ async fn populated_read_state_responds_correctly() -> Result<()> {
/// non-finalized states correctly.
#[tokio::test]
async fn test_read_subtrees() -> Result<()> {
use std::ops::Bound::{self, *};

let dummy_subtree = |(index, height)| {
NoteCommitmentSubtree::new(
u16::try_from(index).expect("should fit in u16"),
Expand Down Expand Up @@ -179,6 +181,31 @@ async fn test_read_subtrees() -> Result<()> {
assert_eq!(first_chain_index, index, "subtree indexes should match");
assert_eq!(end_height, subtree.end, "subtree end heights should match");

// Check that Zebra retrieves subtrees correctly when using a range with an Excluded start bound

#[derive(Clone, Default, PartialEq, Eq, Hash)]
struct RangeExclusiveStart<Idx> {
pub start: Idx,
pub end: Option<Idx>,
}

impl<T> std::ops::RangeBounds<T> for RangeExclusiveStart<T> {
fn start_bound(&self) -> Bound<&T> {
Excluded(&self.start)
}
fn end_bound(&self) -> Bound<&T> {
self.end.as_ref().map(Excluded).unwrap_or(Unbounded)
}
}

let range = RangeExclusiveStart::default();
let subtrees = sapling_subtrees(Some(chain), &db, range.clone());
assert_eq!(subtrees.len(), 11);
assert!(
!subtrees.contains_key(&range.start),
"should not contain excluded start bound"
);

Ok(())
}

Expand Down

0 comments on commit 7ec6f21

Please sign in to comment.