Merge rust-bitcoin/rust-bitcoin#1642: Add `minimal_non_dust` to `TxOut`

6be89bf94f Add `minimal_non_dust` to `TxOut` (Martin Habovstiak)

Pull request description:

  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 one is quite easy, so if we could get it in 0.30, that'd be great.

ACKs for top commit:
  apoelstra:
    ACK 6be89bf94f
  tcharding:
    ACK 6be89bf94f

Tree-SHA512: f31ae5f649fbba95ccaabf465cb814df193e7ef89c6e0de7b316a2a484e172beada0da8851da96b195a69a4da1b0991741d4c119f9b0c94fff34150e4f033bd5
This commit is contained in:
Andrew Poelstra 2023-02-13 20:36:17 +00:00
commit 1451370660
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
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.