Merge rust-bitcoin/rust-bitcoin#7112: units: remove `Amount::MAX_MONEY`
What changed, and why it matters
This commit is a routine API cleanup: it removes the duplicate constant name `Amount::MAX_MONEY` and keeps `Amount::MAX` as the single name for the maximum Bitcoin amount. The numeric value (21 million BTC) and all safety checks stay exactly the same. It is not a security fix and does not change how the library protects against invalid amounts.
No security action required. Treat as a normal breaking API change for the upcoming 1.0 release; downstream users should migrate from `Amount::MAX_MONEY` to `Amount::MAX`.
Security signals we found
No security signal: pure public API renaming with identical semantics
All boundary checks preserved, only identifier changed
No new logic, no relaxed validation, no cryptographic change
Evidence from the diff
The patch deletes the MAX_MONEY aliases on Amount and SignedAmount, replacing every internal usage with MAX. A #[doc(alias = "MAX_MONEY")] attribute is added so documentation search still finds the old name. The actual constant value 21_000_000 * 100_000_000 satoshis is unchanged, and all range/overflow validations in from_sat, transaction decoding, fee-filter decoding, and tests remain semantically identical.
Changed components
units::amount::Amountunits::amount::SignedAmountp2p::message::FeeFilter decodingprimitives::transaction::TransactionDecoder output-value validationunit tests and doc commentsInspect captured patch +37 / −41
### p2p/src/message.rs
@@ -466,16 +466,16 @@ crate::decoder_newtype! {
let kvb = u64::from_le_bytes(array);
// BIP-0133 specifies feefilter as int64_t (signed), but negative values and values
- // exceeding Amount::MAX_MONEY are invalid for fee rates.
+ // exceeding Amount::MAX are invalid for fee rates.
// https://github.com/bitcoin/bitcoin/blob/8396b7f2a3be4be7bb2ffc152f87b4cab95dd84e/src/net_processing.cpp#L4984
- if kvb > Amount::MAX_MONEY.to_sat() {
+ if kvb > Amount::MAX.to_sat() {
Err(FeeFilterDecoderError::InvalidFeeRate(kvb))
} else {
// We can't directly construct using kvb with any public constructors on FeeRate
- // because the rate can be up to Amount::MAX_MONEY which overflows all of them.
+ // because the rate can be up to Amount::MAX which overflows all of them.
// Instead, we construct a 1 sat/kvb and multiply by our kvb.
let fee_rate = FeeRate::from_sat_per_kvb(kvb)
- .expect("Amount::MAX_MONEY * 1000 < u64::MAX");
+ .expect("Amount::MAX * 1000 < u64::MAX");
Ok(FeeFilter(fee_rate))
}
@@ -2049,12 +2049,12 @@ pub mod error {
pub enum FeeFilterDecoderError {
/// Unexpected end of data.
UnexpectedEof(encoding::UnexpectedEofError),
- /// Fee rate value is out of valid range (must be in the range 0 to [`Amount::MAX_MONEY`]).
+ /// Fee rate value is out of valid range (must be in the range 0 to [`Amount::MAX`]).
///
/// If an error of this variant is thrown while decoding a `V1NetworkMessage`, the message
/// should be ignored.
///
- /// [`Amount::MAX_MONEY`]: units::Amount::MAX_MONEY
+ /// [`Amount::MAX`]: units::Amount::MAX
InvalidFeeRate(u64),
}
@@ -2840,16 +2840,16 @@ mod test {
#[test]
fn fee_filter_decode_edge_cases() {
- let max_money = units::Amount::MAX_MONEY.to_sat();
+ let max_money = units::Amount::MAX.to_sat();
- // 0 to Amount::MAX_MONEY is valid
+ // 0 to Amount::MAX is valid
let bytes = 0u64.to_le_bytes();
assert_eq!(
encoding::decode_from_slice::<FeeFilter>(&bytes).unwrap(),
FeeFilter(FeeRate::ZERO)
);
let bytes = max_money.to_le_bytes();
- let max_feerate = FeeFilter(FeeRate::from_sat_per_kvb(Amount::MAX_MONEY.to_sat()).unwrap());
+ let max_feerate = FeeFilter(FeeRate::from_sat_per_kvb(Amount::MAX.to_sat()).unwrap());
assert_eq!(encoding::decode_from_slice::<FeeFilter>(&bytes).unwrap(), max_feerate);
// Invalid cases
### primitives/src/transaction.rs
@@ -518,7 +518,7 @@ encoding::encoder_newtype_exact! {
/// * Has more than one input and any of them spends the null (coinbase) prevout.
/// * Is a coinbase transaction with a `script_sig` shorter than 2 or longer than 100 bytes.
/// * Spends the same outpoint more than once.
-/// * Has outputs whose total value exceeds [`Amount::MAX_MONEY`].
+/// * Has outputs whose total value exceeds [`Amount::MAX`].
/// * Has inputs and uses the SegWit serialization but all of its witnesses are empty. Zero-input
/// transactions use SegWit serialization to avoid ambiguity (see [`Transaction`]).
#[cfg(feature = "alloc")]
@@ -729,7 +729,7 @@ impl encoding::Decoder for TransactionDecoder {
let mut total_out: u64 = 0;
for output in &tx.outputs {
total_out = total_out.saturating_add(output.amount.to_sat());
- if total_out > Amount::MAX_MONEY.to_sat() {
+ if total_out > Amount::MAX.to_sat() {
return Err(E(Inner::OutputValueSumTooLarge(total_out)));
}
}
@@ -1667,7 +1667,7 @@ impl<'a> Arbitrary<'a> for Transaction {
outputs.push(TxOut::arbitrary(u)?);
}
- let mut remaining = Amount::MAX_MONEY.to_sat();
+ let mut remaining = Amount::MAX.to_sat();
for output in &mut outputs {
let capped = output.amount.to_sat().min(remaining);
output.amount = Amount::from_sat(capped).expect("capped <= MAX_MONEY");
@@ -2333,7 +2333,7 @@ mod tests {
let tx = decoder.end().expect("sum of output values == MAX_MONEY should be accepted");
let total: u64 = tx.outputs.iter().map(|o| o.amount.to_sat()).sum();
- assert_eq!(total, Amount::MAX_MONEY.to_sat());
+ assert_eq!(total, Amount::MAX.to_sat());
}
#[test]
@@ -3333,7 +3333,7 @@ mod tests {
#[test]
#[cfg(feature = "alloc")]
fn transaction_decoder_output_value_sum_too_large_error() {
- let expected = Amount::MAX_MONEY.to_sat() + 1;
+ let expected = Amount::MAX.to_sat() + 1;
let tx = Transaction {
version: Version::ONE,
lock_time: absolute::LockTime::ZERO,
@@ -3344,7 +3344,7 @@ mod tests {
witness: Witness::default(),
}],
outputs: vec![
- TxOut { amount: Amount::MAX_MONEY, script_pubkey: ScriptPubKeyBuf::new() },
+ TxOut { amount: Amount::MAX, script_pubkey: ScriptPubKeyBuf::new() },
TxOut { amount: Amount::ONE_SAT, script_pubkey: ScriptPubKeyBuf::new() },
],
};
### units/src/amount/signed.rs
@@ -52,6 +52,7 @@ mod encapsulate {
impl SignedAmount {
/// The maximum value of an amount.
+ #[doc(alias = "MAX_MONEY")]
pub const MAX: Self = Self(21_000_000 * 100_000_000);
/// The minimum value of an amount.
@@ -72,7 +73,7 @@ mod encapsulate {
///
/// # Errors
///
- /// If `satoshi` is outside of valid range (see [`Self::MAX_MONEY`]).
+ /// If `satoshi` is outside of valid range (see [`Self::MAX`]).
///
/// # Examples
///
@@ -87,7 +88,7 @@ mod encapsulate {
pub const fn from_sat(satoshi: i64) -> Result<Self, OutOfRangeError> {
if satoshi < Self::MIN.to_sat() {
Err(OutOfRangeError { is_signed: true, is_greater_than_max: false })
- } else if satoshi > Self::MAX_MONEY.to_sat() {
+ } else if satoshi > Self::MAX.to_sat() {
Err(OutOfRangeError { is_signed: true, is_greater_than_max: true })
} else {
Ok(Self(satoshi))
@@ -112,9 +113,6 @@ impl SignedAmount {
/// Exactly fifty bitcoin.
pub const FIFTY_BTC: Self = Self::from_btc_i16(50);
- /// The maximum value allowed as an amount. Useful for sanity checking.
- pub const MAX_MONEY: Self = Self::MAX;
-
/// Gets the number of millisatoshis in this [`SignedAmount`].
///
/// # Examples
@@ -149,11 +147,11 @@ impl SignedAmount {
///
/// # Errors
///
- /// Returns an [`OutOfRangeError`] if the satoshi value > [`Self::MAX_MONEY`].
+ /// Returns an [`OutOfRangeError`] if the satoshi value > [`Self::MAX`].
#[inline]
#[allow(clippy::missing_panics_doc)]
fn from_sat_u64(satoshi: u64) -> Result<Self, ParseAmountError> {
- // u64 -> i64 only fails if value is greater than i64::MAX, which is also > Self::MAX_MONEY.
+ // u64 -> i64 only fails if value is greater than i64::MAX, which is also > Self::MAX.
let amount = i64::try_from(satoshi)
.map_err(|_| OutOfRangeError { is_signed: true, is_greater_than_max: true })
.map_err(ParseAmountErrorInner::OutOfRange)
### units/src/amount/tests.rs
@@ -1052,7 +1052,7 @@ fn sum_amounts() {
let sum = amounts.into_iter().map(NumOpResult::from).sum::<NumOpResult<Amount>>().unwrap();
assert_eq!(sum, sat(1400));
- let amounts = [Amount::MAX_MONEY, sat(1337), sat(21)];
+ let amounts = [Amount::MAX, sat(1337), sat(21)];
assert!(amounts.into_iter().map(NumOpResult::from).sum::<NumOpResult<Amount>>().is_error());
let amounts = [SignedAmount::MIN, ssat(-1), ssat(21)];
@@ -1198,7 +1198,7 @@ fn unsigned_addition() {
assert_eq!(sat(0) + sat(307), res(307));
assert_eq!(sat(307) + sat(0), res(307));
assert_eq!(sat(307) + sat(461), res(768));
- assert_eq!(sat(0) + Amount::MAX_MONEY, NumOpResult::from(Amount::MAX_MONEY));
+ assert_eq!(sat(0) + Amount::MAX, NumOpResult::from(Amount::MAX));
}
#[test]
@@ -1207,14 +1207,14 @@ fn signed_addition() {
assert_eq!(ssat(0) + ssat(307), sres(307));
assert_eq!(ssat(307) + ssat(0), sres(307));
assert_eq!(ssat(307) + ssat(461), sres(768));
- assert_eq!(ssat(0) + SignedAmount::MAX_MONEY, NumOpResult::from(SignedAmount::MAX_MONEY));
+ assert_eq!(ssat(0) + SignedAmount::MAX, NumOpResult::from(SignedAmount::MAX));
assert_eq!(ssat(0) + ssat(-307), sres(-307));
assert_eq!(ssat(-307) + ssat(0), sres(-307));
assert_eq!(ssat(-307) + ssat(461), sres(154));
assert_eq!(ssat(307) + ssat(-461), sres(-154));
assert_eq!(ssat(-307) + ssat(-461), sres(-768));
- assert_eq!(SignedAmount::MAX_MONEY + -SignedAmount::MAX_MONEY, sres(0));
+ assert_eq!(SignedAmount::MAX + -SignedAmount::MAX, sres(0));
}
#[test]
@@ -1230,7 +1230,7 @@ fn signed_subtraction() {
assert_eq!(ssat(0) - ssat(307), sres(-307));
assert_eq!(ssat(307) - ssat(0), sres(307));
assert_eq!(ssat(307) - ssat(461), sres(-154));
- assert_eq!(ssat(0) - SignedAmount::MAX_MONEY, NumOpResult::from(-SignedAmount::MAX_MONEY));
+ assert_eq!(ssat(0) - SignedAmount::MAX, NumOpResult::from(-SignedAmount::MAX));
assert_eq!(ssat(0) - ssat(-307), sres(307));
assert_eq!(ssat(-307) - ssat(0), sres(-307));
@@ -1484,7 +1484,7 @@ fn amount_op_result_neg() {
assert_eq!(neg_zero, SignedAmount::ZERO);
// Test negation of MAX_MONEY
- let max = Amount::MAX_MONEY;
+ let max = Amount::MAX;
let neg_max = -max;
assert_eq!(neg_max, SignedAmount::MIN);
}
@@ -1576,16 +1576,16 @@ fn signed_sub() {
// Any two valid amounts will always produce a valid SignedAmount
// This demonstrates the "cannot overflow" property
- let amt1 = Amount::MAX_MONEY;
+ let amt1 = Amount::MAX;
let amt2 = sat(1);
let diff1 = amt1.signed_sub(amt2);
let diff2 = amt2.signed_sub(amt1);
assert!(diff1.is_positive());
assert!(diff2.is_negative());
- assert_eq!(diff1.to_sat(), (Amount::MAX_MONEY.to_sat() - 1) as i64);
- assert_eq!(diff2.to_sat(), -((Amount::MAX_MONEY.to_sat() - 1) as i64));
+ assert_eq!(diff1.to_sat(), (Amount::MAX.to_sat() - 1) as i64);
+ assert_eq!(diff2.to_sat(), -((Amount::MAX.to_sat() - 1) as i64));
}
#[test]
@@ -1614,13 +1614,13 @@ fn display_dynamic() {
assert_eq!(Amount::ZERO.display_dynamic().to_string(), "0 satoshi");
assert_eq!(Amount::ONE_SAT.display_dynamic().to_string(), "1 satoshi");
assert_eq!(Amount::ONE_BTC.display_dynamic().to_string(), "1 BTC");
- assert_eq!(Amount::MAX_MONEY.display_dynamic().to_string(), "21000000 BTC");
+ assert_eq!(Amount::MAX.display_dynamic().to_string(), "21000000 BTC");
assert_eq!(Amount::MAX.display_dynamic().to_string(), "21000000 BTC");
assert_eq!(SignedAmount::ZERO.display_dynamic().to_string(), "0 satoshi");
assert_eq!(SignedAmount::ONE_SAT.display_dynamic().to_string(), "1 satoshi");
assert_eq!(SignedAmount::ONE_BTC.display_dynamic().to_string(), "1 BTC");
- assert_eq!(SignedAmount::MAX_MONEY.display_dynamic().to_string(), "21000000 BTC");
+ assert_eq!(SignedAmount::MAX.display_dynamic().to_string(), "21000000 BTC");
assert_eq!(SignedAmount::MAX.display_dynamic().to_string(), "21000000 BTC");
// dynamic values
### units/src/amount/unsigned.rs
@@ -55,6 +55,7 @@ mod encapsulate {
impl Amount {
/// The maximum value of an amount.
+ #[doc(alias = "MAX_MONEY")]
pub const MAX: Self = Self(21_000_000 * 100_000_000);
/// The minimum value of an amount.
@@ -75,7 +76,7 @@ mod encapsulate {
///
/// # Errors
///
- /// If `satoshi` is outside of valid range (greater than [`Self::MAX_MONEY`]).
+ /// If `satoshi` is outside of valid range (greater than [`Self::MAX`]).
///
/// # Examples
///
@@ -88,7 +89,7 @@ mod encapsulate {
/// ```
#[inline]
pub const fn from_sat(satoshi: u64) -> Result<Self, OutOfRangeError> {
- if satoshi > Self::MAX_MONEY.to_sat() {
+ if satoshi > Self::MAX.to_sat() {
Err(OutOfRangeError { is_signed: false, is_greater_than_max: true })
} else {
Ok(Self(satoshi))
@@ -112,9 +113,6 @@ impl Amount {
/// Exactly fifty bitcoin.
pub const FIFTY_BTC: Self = Self::from_btc_u16(50);
- /// The maximum value allowed as an amount. Useful for sanity checking.
- pub const MAX_MONEY: Self = Self::MAX;
-
/// The number of bytes that an amount contributes to the size of a transaction.
pub const SIZE: usize = 8; // Serialized length of a u64.
### units/tests/encoding.rs
@@ -118,7 +118,7 @@ test_hardcoded_decoding!(
([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], Amount::ZERO),
([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], Amount::ONE_SAT),
([0x00, 0xe1, 0xf5, 0x05, 0x00, 0x00, 0x00, 0x00], Amount::ONE_BTC),
- ([0x00, 0x40, 0x07, 0x5a, 0xf0, 0x75, 0x07, 0x00], Amount::MAX_MONEY),
+ ([0x00, 0x40, 0x07, 0x5a, 0xf0, 0x75, 0x07, 0x00], Amount::MAX),
);
test_decoder_default!(amount_decoder_default, Amount, AmountDecoder, 8);
@@ -130,7 +130,7 @@ test_round_trip!(
Amount::ZERO,
Amount::ONE_SAT,
Amount::ONE_BTC,
- Amount::MAX_MONEY,
+ Amount::MAX,
);
#[rustfmt::skip]
@@ -141,7 +141,7 @@ test_incremental_decoding!(
([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], Amount::ZERO),
([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], Amount::ONE_SAT),
([0x00, 0xe1, 0xf5, 0x05, 0x00, 0x00, 0x00, 0x00], Amount::ONE_BTC),
- ([0x00, 0x40, 0x07, 0x5a, 0xf0, 0x75, 0x07, 0x00], Amount::MAX_MONEY),
+ ([0x00, 0x40, 0x07, 0x5a, 0xf0, 0x75, 0x07, 0x00], Amount::MAX),
);
// BlockHeight encodes as 4-byte little-endian u32.Why this scored 20/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.