Remove Option return from `minimal_non_dust`

we replace Option<Amount> return type with Amount in minimal_non_dust

- Use `.expect("dust_relay_fee or script length should not be absurdly large")` to handle overflow from .checked_mul()

`.expect()` is only triggered if the value calculateed overflows u64
such an overflow would require a script size exceeding ~6.15 petabytes
This commit is contained in:
jrakibi 2025-04-14 20:07:41 +08:00
parent 9312ff25dd
commit 0498f7b7b7
3 changed files with 7 additions and 6 deletions

View File

@ -261,7 +261,7 @@ crate::internal_macros::define_extension_trait! {
/// Returns the minimum value an output with this script should have in order to be
/// broadcastable on todays Bitcoin network.
#[deprecated(since = "0.32.0", note = "use `minimal_non_dust` etc. instead")]
fn dust_value(&self) -> Option<Amount> { self.minimal_non_dust() }
fn dust_value(&self) -> Amount { self.minimal_non_dust() }
/// Returns the minimum value an output with this script should have in order to be
/// broadcastable on today's Bitcoin network.
@ -272,8 +272,9 @@ crate::internal_macros::define_extension_trait! {
/// To use a custom value, use [`minimal_non_dust_custom`].
///
/// [`minimal_non_dust_custom`]: Script::minimal_non_dust_custom
fn minimal_non_dust(&self) -> Option<Amount> {
fn minimal_non_dust(&self) -> Amount {
self.minimal_non_dust_internal(DUST_RELAY_TX_FEE.into())
.expect("dust_relay_fee or script length should not be absurdly large")
}
/// Returns the minimum value an output with this script should have in order to be

View File

@ -684,7 +684,7 @@ fn default_dust_value() {
// well-known scriptPubKey types.
let script_p2wpkh = Builder::new().push_int_unchecked(0).push_slice([42; 20]).into_script();
assert!(script_p2wpkh.is_p2wpkh());
assert_eq!(script_p2wpkh.minimal_non_dust(), Some(Amount::from_sat_u32(294)));
assert_eq!(script_p2wpkh.minimal_non_dust(), Amount::from_sat_u32(294));
assert_eq!(
script_p2wpkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)),
Some(Amount::from_sat_u32(588))
@ -698,7 +698,7 @@ fn default_dust_value() {
.push_opcode(OP_CHECKSIG)
.into_script();
assert!(script_p2pkh.is_p2pkh());
assert_eq!(script_p2pkh.minimal_non_dust(), Some(Amount::from_sat_u32(546)));
assert_eq!(script_p2pkh.minimal_non_dust(), Amount::from_sat_u32(546));
assert_eq!(
script_p2pkh.minimal_non_dust_custom(FeeRate::from_sat_per_vb_unchecked(6)),
Some(Amount::from_sat_u32(1092))

View File

@ -183,8 +183,8 @@ crate::internal_macros::define_extension_trait! {
/// To use a custom value, use [`minimal_non_dust_custom`].
///
/// [`minimal_non_dust_custom`]: TxOut::minimal_non_dust_custom
fn minimal_non_dust(script_pubkey: ScriptBuf) -> Option<TxOut> {
Some(TxOut { value: script_pubkey.minimal_non_dust()?, script_pubkey })
fn minimal_non_dust(script_pubkey: ScriptBuf) -> TxOut {
TxOut { value: script_pubkey.minimal_non_dust(), script_pubkey }
}
/// Constructs a new `TxOut` with given script and the smallest possible `value` that is **not** dust