Skip to content

Commit

Permalink
Merge rust-bitcoin#3420: Refactor Arbitrary for Sequence
Browse files Browse the repository at this point in the history
d39334e Refactor Arbitrary for Sequence (yancy)

Pull request description:

  Personally, I think this is a bit nicer and easier to reason about.

ACKs for top commit:
  tcharding:
    ACK d39334e
  apoelstra:
    ACK d39334e successfully ran local tests

Tree-SHA512: 9816e81873c7c052e7204877f3cfc605f20feb399575af4c4527899bbdfe20031dd06d681539fe21226b4926891d1a69c5c3299976e553c7c75878e07f5ed068
  • Loading branch information
apoelstra committed Sep 28, 2024
2 parents 02a4538 + d39334e commit 8740f0e
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions primitives/src/sequence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,29 +242,37 @@ impl fmt::Debug for Sequence {
units::impl_parse_str_from_int_infallible!(Sequence, u32, from_consensus);

#[cfg(feature = "arbitrary")]
#[cfg(feature = "alloc")]
impl<'a> Arbitrary<'a> for Sequence {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
let mut choice_range = 4;
if cfg!(feature = "alloc") {
choice_range = 8;
}

// Equally weight the cases of meaningful sequence numbers
let choice = u.int_in_range(0..=choice_range)?;
let choice = u.int_in_range(0..=8)?;
match choice {
0 => Ok(Sequence::MAX),
1 => Ok(Sequence::ZERO),
2 => Ok(Sequence::MIN_NO_RBF),
3 => Ok(Sequence::ENABLE_RBF_NO_LOCKTIME),
#[cfg(feature = "alloc")]
4 => Ok(Sequence::from_consensus(relative::Height::MIN.to_consensus_u32())),
#[cfg(feature = "alloc")]
5 => Ok(Sequence::from_consensus(relative::Height::MAX.to_consensus_u32())),
#[cfg(feature = "alloc")]
6 => Ok(Sequence::from_consensus(relative::Time::MIN.to_consensus_u32())),
#[cfg(feature = "alloc")]
7 => Ok(Sequence::from_consensus(relative::Time::MAX.to_consensus_u32())),
_ => Ok(Sequence(u.arbitrary()?))
}
}
}

#[cfg(feature = "arbitrary")]
#[cfg(not(feature = "alloc"))]
impl<'a> Arbitrary<'a> for Sequence {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
// Equally weight the cases of meaningful sequence numbers
let choice = u.int_in_range(0..=4)?;
match choice {
0 => Ok(Sequence::MAX),
1 => Ok(Sequence::ZERO),
2 => Ok(Sequence::MIN_NO_RBF),
3 => Ok(Sequence::ENABLE_RBF_NO_LOCKTIME),
_ => Ok(Sequence(u.arbitrary()?))
}
}
}

0 comments on commit 8740f0e

Please sign in to comment.