Implement Arbitrary for relative::LockTime
What changed, and why it matters
This commit adds a missing test helper that lets a library generate random relative locktime values for fuzz testing. It is not a security fix and does not change any runtime behavior of the Bitcoin library.
No security action required. Treat as a normal feature/test-helper addition.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch implements the Arbitrary trait from the arbitrary crate for relative::LockTime. This is only compiled when the arbitrary feature is enabled and is used by fuzzing/property-based tests. The implementation randomly chooses between the two inner variants (Blocks and Time) and delegates to the existing Arbitrary impls for NumberOfBlocks and NumberOf512Seconds. No consensus, validation, or serialization logic is modified.
Changed components
units/src/locktime/relative/mod.rsInspect captured patch +12 / −0
diff --git a/units/src/locktime/relative/mod.rs b/units/src/locktime/relative/mod.rs
index c5d68d4b..26a02d84 100644
--- a/units/src/locktime/relative/mod.rs
+++ b/units/src/locktime/relative/mod.rs
@@ -579,6 +579,18 @@ impl fmt::Display for NumberOf512Seconds {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fmt::Display::fmt(&self.0, f) }
}
+#[cfg(feature = "arbitrary")]
+impl<'a> Arbitrary<'a> for LockTime {
+ fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
+ let choice = u.int_in_range(0..=1)?;
+
+ match choice {
+ 0 => Ok(Self::Blocks(NumberOfBlocks::arbitrary(u)?)),
+ _ => Ok(Self::Time(NumberOf512Seconds::arbitrary(u)?)),
+ }
+ }
+}
+
#[cfg(feature = "arbitrary")]
impl<'a> Arbitrary<'a> for NumberOfBlocks {
fn arbitrary(u: &mut Unstructured<'a>) -> arbitrary::Result<Self> {
Why this scored 16/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.