Skip to content

Commit ed17921

Browse files
author
Antoine Riard
committed
Modify HoldersCommitment's package_weight/amount to account for CPFP
A HolderCommitmentTx package may be attached a CPFP if its feerate needs a bumping and thus an accurate operation need to scope both parent and children weight. Also disable feerate computation for pre-committed feerate package as their package amounts available for feerate-reducing will be 0.
1 parent 92adfdf commit ed17921

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

lightning/src/ln/onchain_utils.rs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use bitcoin::hash_types::Txid;
88

99
use bitcoin::secp256k1::key::{SecretKey,PublicKey};
1010

11+
use ln::channel::ANCHOR_OUTPUT_VALUE;
1112
use ln::channelmanager::PaymentPreimage;
1213
use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment};
1314
use ln::chan_utils;
@@ -409,11 +410,18 @@ impl PackageTemplate {
409410
}
410411
amounts
411412
},
412-
_ => 0,
413+
PackageTemplate::HolderCommitmentTx { ref utxo_input, .. } => {
414+
if let Some(utxo_input) = utxo_input {
415+
return utxo_input.1.amount + ANCHOR_OUTPUT_VALUE;
416+
} else { return 0 }
417+
},
418+
PackageTemplate::HolderHTLCTx { ref input } => {
419+
input.1.amount
420+
},
413421
};
414422
amounts
415423
}
416-
pub(crate) fn package_weight(&self, destination_script: &Script) -> usize {
424+
pub(crate) fn package_weight(&self, destination_script: &Script, local_commitment: &Transaction) -> usize {
417425
let mut input = Vec::new();
418426
let witnesses_weight = match self {
419427
PackageTemplate::MalleableJusticeTx { ref inputs } => {
@@ -443,7 +451,33 @@ impl PackageTemplate {
443451
}
444452
weight
445453
},
446-
_ => { return 0 }
454+
PackageTemplate::HolderCommitmentTx { ref utxo_input, .. } => {
455+
// Post-Anchor Commitment Package weight accoutning:
456+
let commitment_weight =
457+
900 // base commitment tx (900 WU)
458+
+ local_commitment.output.len() * 172 // num-htlc-outputs * htlc-output (172 WU)
459+
+ 224; // funding spending witness (224 WU)
460+
// If a feerate-bump is required:
461+
let cpfp_weight: usize = if let Some(utxo_input) = utxo_input {
462+
40 // CPFP transaction basic fields (40 WU)
463+
+ 2 // witness marker (2 WU)
464+
+ 164 // anchor input (164 WU)
465+
+ 115 // anchor witness (115 WU)
466+
+ 164 // bumping input (164 WU)
467+
+ utxo_input.1.witness_weight as usize // bumping witness (`utxo_input.1.witness_weight`)
468+
+ 32 // output amount (32 WU)
469+
+ 4 // output scriptpubkey-length (4 WU)
470+
+ destination_script.len() * 4 // output scriptpubkey (`destination_script.len() * 4`)
471+
} else { 0 };
472+
return commitment_weight + cpfp_weight;
473+
},
474+
PackageTemplate::HolderHTLCTx { ref input } => {
475+
if input.1.preimage.is_some() {
476+
return 706; // HTLC-Success with option_anchor_outputs
477+
} else {
478+
return 666; // HTLC-Timeout with option_anchor_outputs
479+
}
480+
},
447481
};
448482
let bumped_tx = Transaction {
449483
version: 2,
@@ -927,6 +961,12 @@ pub(crate) fn compute_output_value<F: Deref, L: Deref>(predicted_weight: usize,
927961
where F::Target: FeeEstimator,
928962
L::Target: Logger,
929963
{
964+
// If transaction is still relying ont its pre-committed feerate to get confirmed return
965+
// a 0-value output-value as it won't be consumed further
966+
if input_amounts == 0 {
967+
return Some((0, previous_feerate));
968+
}
969+
930970
// If old feerate is 0, first iteration of this claim, use normal fee calculation
931971
if previous_feerate != 0 {
932972
if let Some((new_fee, feerate)) = feerate_bump(predicted_weight, input_amounts, previous_feerate, fee_estimator, logger) {

0 commit comments

Comments
 (0)