Improve `from_consensus` example

The existing example at first look seemed to contradict the doc above
that the function would not round trip.

Update the example to show the useage for both a time and height based
lock time.

Replace unwrap() with ?
This commit is contained in:
Jamil Lambert, PhD 2025-01-28 15:44:45 +00:00
parent 25a6742573
commit 2d73746ad1
No known key found for this signature in database
GPG Key ID: 54DC29234AB5D2C0
1 changed files with 13 additions and 4 deletions

View File

@ -77,10 +77,19 @@ impl LockTime {
/// ```rust
/// # use bitcoin_primitives::relative::LockTime;
///
/// // `from_consensus` roundtrips with `to_consensus_u32` for small values.
/// let n_lock_time: u32 = 7000;
/// let lock_time = LockTime::from_consensus(n_lock_time).unwrap();
/// assert_eq!(lock_time.to_consensus_u32(), n_lock_time);
/// // Values with bit 22 set to 0 will be interpreted as height-based lock times.
/// let height: u32 = 144; // 144 blocks, approx 24h.
/// let lock_time = LockTime::from_consensus(height)?;
/// assert!(lock_time.is_block_height());
/// assert_eq!(lock_time.to_consensus_u32(), height);
///
/// // Values with bit 22 set to 1 will be interpreted as time-based lock times.
/// let time: u32 = 168 | (1 << 22) ; // Bit 22 is 1 with time approx 24h.
/// let lock_time = LockTime::from_consensus(time)?;
/// assert!(lock_time.is_block_time());
/// assert_eq!(lock_time.to_consensus_u32(), time);
///
/// # Ok::<_, bitcoin_primitives::relative::DisabledLockTimeError>(())
/// ```
pub fn from_consensus(n: u32) -> Result<Self, DisabledLockTimeError> {
let sequence = crate::Sequence::from_consensus(n);