Merge rust-bitcoin/rust-bitcoin#4258: units: Improve code enforcing privacy boundry

ca6c607953 Adhere to sanity rules for amount types (Tobin C. Harding)
6c614d9320 units: Fix panic message (Tobin C. Harding)

Pull request description:

  This is a follow up to #4256 - onwards and upwards!

  - Patch 1: Fix the incorrect BTC value in panic message
  - Patch 2: Strictly adhere to the sanity rules (#4090)

   Close: #4140

ACKs for top commit:
  apoelstra:
    ACK ca6c607953c03aa2dc168f58329681d9e69eee04; successfully ran local tests

Tree-SHA512: 6d7fd60830e1a0f6d6262ab02ec6e297b095d0fe8fb7737563979652e4a3b4a9477a79982201c42b08e2555fd23dc5c430549966b534bdf45f40621ae81da83a
This commit is contained in:
merge-script 2025-04-16 18:33:09 +00:00
commit b473a2090a
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
2 changed files with 26 additions and 18 deletions

View File

@ -57,14 +57,6 @@ mod encapsulate {
/// The minimum value of an amount.
pub const MIN: Self = Self(-21_000_000 * 100_000_000);
/// Constructs a new [`SignedAmount`] with satoshi precision and the given number of satoshis.
///
/// Accepts an `i32` which is guaranteed to be in range for the type, but which can only
/// represent roughly -21.47 to 21.47 BTC.
pub const fn from_sat_i32(satoshi: i32) -> Self {
Self(satoshi as i64) // cannot use i64::from in a constfn
}
/// Gets the number of satoshis in this [`SignedAmount`].
///
/// # Examples
@ -116,6 +108,18 @@ impl SignedAmount {
/// The maximum value allowed as an amount. Useful for sanity checking.
pub const MAX_MONEY: Self = Self::MAX;
/// Constructs a new [`SignedAmount`] with satoshi precision and the given number of satoshis.
///
/// Accepts an `i32` which is guaranteed to be in range for the type, but which can only
/// represent roughly -21.47 to 21.47 BTC.
pub const fn from_sat_i32(satoshi: i32) -> Self {
let sats = satoshi as i64; // cannot use i64::from in a constfn
match Self::from_sat(sats) {
Ok(amount) => amount,
Err(_) => panic!("unreachable - 32,767 BTC is within range"),
}
}
/// Converts from a value expressing a decimal number of bitcoin to a [`SignedAmount`].
///
/// # Errors
@ -152,7 +156,7 @@ impl SignedAmount {
match Self::from_sat(sats) {
Ok(amount) => amount,
Err(_) => panic!("unreachable - 65536 BTC is within range"),
Err(_) => panic!("unreachable - 32,767 BTC is within range"),
}
}

View File

@ -57,14 +57,6 @@ mod encapsulate {
/// The minimum value of an amount.
pub const MIN: Self = Self(0);
/// Constructs a new [`Amount`] with satoshi precision and the given number of satoshis.
///
/// Accepts an `u32` which is guaranteed to be in range for the type, but which can only
/// represent roughly 0 to 42.95 BTC.
pub const fn from_sat_u32(satoshi: u32) -> Self {
Self(satoshi as u64) // cannot use u64::from in a constfn
}
/// Gets the number of satoshis in this [`Amount`].
///
/// # Examples
@ -116,6 +108,18 @@ impl Amount {
/// The number of bytes that an amount contributes to the size of a transaction.
pub const SIZE: usize = 8; // Serialized length of a u64.
/// Constructs a new [`Amount`] with satoshi precision and the given number of satoshis.
///
/// Accepts an `u32` which is guaranteed to be in range for the type, but which can only
/// represent roughly 0 to 42.95 BTC.
pub const fn from_sat_u32(satoshi: u32) -> Self {
let sats = satoshi as u64; // cannot use i64::from in a constfn
match Self::from_sat(sats) {
Ok(amount) => amount,
Err(_) => panic!("unreachable - 65,536 BTC is within range"),
}
}
/// Converts from a value expressing a decimal number of bitcoin to an [`Amount`].
///
/// # Errors
@ -152,7 +156,7 @@ impl Amount {
match Self::from_sat(sats) {
Ok(amount) => amount,
Err(_) => panic!("unreachable - 65536 BTC is within range"),
Err(_) => panic!("unreachable - 65,535 BTC is within range"),
}
}