]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Fix off-by-one max witness estimate for P2WPKH StaticPaymentDescriptor
authorWilmer Paulino <wilmer@wilmerpaulino.com>
Thu, 28 Sep 2023 16:07:10 +0000 (09:07 -0700)
committerWilmer Paulino <wilmer@wilmerpaulino.com>
Fri, 29 Sep 2023 20:46:59 +0000 (13:46 -0700)
We were not accounting for the extra byte denoting the number of items
in the witness stack.

lightning/src/events/bump_transaction.rs
lightning/src/sign/mod.rs

index 35e9da60544ddc680535983767117e9faf732afd..55c12d23e749cf020a7308f998ab686f28ea22d3 100644 (file)
@@ -26,7 +26,7 @@ use crate::ln::chan_utils::{
 use crate::ln::features::ChannelTypeFeatures;
 use crate::ln::PaymentPreimage;
 use crate::prelude::*;
-use crate::sign::{EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner};
+use crate::sign::{EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner, P2WPKH_WITNESS_WEIGHT};
 use crate::sync::Mutex;
 use crate::util::logger::Logger;
 
@@ -384,12 +384,6 @@ pub struct Utxo {
 }
 
 impl Utxo {
-       const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ +
-               1 /* sig length */ +
-               73 /* sig including sighash flag */ +
-               1 /* pubkey length */ +
-               33 /* pubkey */;
-
        /// Returns a `Utxo` with the `satisfaction_weight` estimate for a legacy P2PKH output.
        pub fn new_p2pkh(outpoint: OutPoint, value: u64, pubkey_hash: &PubkeyHash) -> Self {
                let script_sig_size = 1 /* script_sig length */ +
@@ -419,7 +413,7 @@ impl Utxo {
                                value,
                                script_pubkey: Script::new_p2sh(&Script::new_v0_p2wpkh(pubkey_hash).script_hash()),
                        },
-                       satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + Self::P2WPKH_WITNESS_WEIGHT,
+                       satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + P2WPKH_WITNESS_WEIGHT,
                }
        }
 
@@ -431,7 +425,7 @@ impl Utxo {
                                value,
                                script_pubkey: Script::new_v0_p2wpkh(pubkey_hash),
                        },
-                       satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + Self::P2WPKH_WITNESS_WEIGHT,
+                       satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + P2WPKH_WITNESS_WEIGHT,
                }
        }
 }
index 04652166f05978b6dd8bb1ab0a4b4c2ed9c37d5f..9a30253049d1bf0816626377ce0c258e568db7e9 100644 (file)
@@ -107,6 +107,12 @@ impl_writeable_tlv_based!(DelayedPaymentOutputDescriptor, {
        (12, channel_value_satoshis, required),
 });
 
+pub(crate) const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ +
+       1 /* sig length */ +
+       73 /* sig including sighash flag */ +
+       1 /* pubkey length */ +
+       33 /* pubkey */;
+
 /// Information about a spendable output to our "payment key".
 ///
 /// See [`SpendableOutputDescriptor::StaticPaymentOutput`] for more details on how to spend this.
@@ -141,9 +147,7 @@ impl StaticPaymentOutputDescriptor {
                        1 /* num witness items */ + 1 /* sig push */ + 73 /* sig including sighash flag */ +
                                1 /* witness script push */ + witness_script_weight
                } else {
-                       // Calculated as 1 byte legnth + 73 byte signature, 1 byte empty vec push, 1 byte length plus
-                       // redeemscript push length.
-                       1 + 73 + 34
+                       P2WPKH_WITNESS_WEIGHT as usize
                }
        }
 }