Skip to content

Commit f6b21c9

Browse files
committed
Fix off-by-one max witness estimate for P2WPKH StaticPaymentDescriptor
We were not accounting for the extra byte denoting the number of items in the witness stack.
1 parent eab7050 commit f6b21c9

File tree

2 files changed

+10
-12
lines changed

2 files changed

+10
-12
lines changed

lightning/src/events/bump_transaction.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use crate::ln::chan_utils::{
2626
use crate::ln::features::ChannelTypeFeatures;
2727
use crate::ln::PaymentPreimage;
2828
use crate::prelude::*;
29-
use crate::sign::{EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner};
29+
use crate::sign::{EcdsaChannelSigner, SignerProvider, WriteableEcdsaChannelSigner, P2WPKH_WITNESS_WEIGHT};
3030
use crate::sync::Mutex;
3131
use crate::util::logger::Logger;
3232

@@ -384,12 +384,6 @@ pub struct Utxo {
384384
}
385385

386386
impl Utxo {
387-
const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ +
388-
1 /* sig length */ +
389-
73 /* sig including sighash flag */ +
390-
1 /* pubkey length */ +
391-
33 /* pubkey */;
392-
393387
/// Returns a `Utxo` with the `satisfaction_weight` estimate for a legacy P2PKH output.
394388
pub fn new_p2pkh(outpoint: OutPoint, value: u64, pubkey_hash: &PubkeyHash) -> Self {
395389
let script_sig_size = 1 /* script_sig length */ +
@@ -419,7 +413,7 @@ impl Utxo {
419413
value,
420414
script_pubkey: Script::new_p2sh(&Script::new_v0_p2wpkh(pubkey_hash).script_hash()),
421415
},
422-
satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + Self::P2WPKH_WITNESS_WEIGHT,
416+
satisfaction_weight: script_sig_size * WITNESS_SCALE_FACTOR as u64 + P2WPKH_WITNESS_WEIGHT,
423417
}
424418
}
425419

@@ -431,7 +425,7 @@ impl Utxo {
431425
value,
432426
script_pubkey: Script::new_v0_p2wpkh(pubkey_hash),
433427
},
434-
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + Self::P2WPKH_WITNESS_WEIGHT,
428+
satisfaction_weight: EMPTY_SCRIPT_SIG_WEIGHT + P2WPKH_WITNESS_WEIGHT,
435429
}
436430
}
437431
}

lightning/src/sign/mod.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ impl_writeable_tlv_based!(DelayedPaymentOutputDescriptor, {
107107
(12, channel_value_satoshis, required),
108108
});
109109

110+
pub(crate) const P2WPKH_WITNESS_WEIGHT: u64 = 1 /* num stack items */ +
111+
1 /* sig length */ +
112+
73 /* sig including sighash flag */ +
113+
1 /* pubkey length */ +
114+
33 /* pubkey */;
115+
110116
/// Information about a spendable output to our "payment key".
111117
///
112118
/// See [`SpendableOutputDescriptor::StaticPaymentOutput`] for more details on how to spend this.
@@ -140,9 +146,7 @@ impl StaticPaymentOutputDescriptor {
140146
1 /* num witness items */ + 1 /* sig push */ + 73 /* sig including sighash flag */ +
141147
1 /* witness script push */ + witness_script_weight
142148
} else {
143-
// Calculated as 1 byte legnth + 73 byte signature, 1 byte empty vec push, 1 byte length plus
144-
// redeemscript push length.
145-
1 + 73 + 34
149+
P2WPKH_WITNESS_WEIGHT as usize
146150
}
147151
}
148152
}

0 commit comments

Comments
 (0)