Add `minimal_non_dust` to `TxOut`

In some scenarios it's useful to create outputs with minimal relayable
value. E.g. outputs designated for fee bumping using CPFP. A method for
this is useful.

This implements a constructor of `TxOut` that computes the minimal
non-dust value from the passed script.

Closes #1459
This commit is contained in:
Martin Habovstiak 2023-02-11 16:52:36 +01:00
parent 9615dd12b8
commit 6be89bf94f
1 changed files with 19 additions and 0 deletions

View File

@ -535,6 +535,25 @@ impl TxOut {
// Then we multiply by 4 to convert to WU
(8 + VarInt(script_len as u64).len() + script_len) * 4
}
/// Creates a `TxOut` with given script and the smallest possible `value` that is **not** dust
/// per current Core policy.
///
/// The current dust fee rate is 3 sat/vB.
pub fn minimal_non_dust(script_pubkey: ScriptBuf) -> Self {
let len = script_pubkey.len() + VarInt(script_pubkey.len() as u64).len() + 8;
let len = len + if script_pubkey.is_witness_program() {
32 + 4 + 1 + (107 / 4) + 4
} else {
32 + 4 + 1 + 107 + 4
};
let dust_amount = (len as u64) * 3;
TxOut {
value: dust_amount + 1, // minimal non-dust amount is one higher than dust amount
script_pubkey,
}
}
}
// This is used as a "null txout" in consensus signing code.