From 6be89bf94f7f944f8b2a78e953379cc17f61716d Mon Sep 17 00:00:00 2001 From: Martin Habovstiak Date: Sat, 11 Feb 2023 16:52:36 +0100 Subject: [PATCH] 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 --- bitcoin/src/blockdata/transaction.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/bitcoin/src/blockdata/transaction.rs b/bitcoin/src/blockdata/transaction.rs index cc385066..2ebe8f27 100644 --- a/bitcoin/src/blockdata/transaction.rs +++ b/bitcoin/src/blockdata/transaction.rs @@ -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.