Skip to content

Commit 37949ce

Browse files
committed
Split finalize_package into separate methods per malleability
1 parent a941f67 commit 37949ce

File tree

2 files changed

+41
-42
lines changed

2 files changed

+41
-42
lines changed

lightning/src/chain/onchaintx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
393393
cached_request.compute_package_output(predicted_weight, self.destination_script.dust_value().to_sat(), fee_estimator, logger) {
394394
assert!(new_feerate != 0);
395395

396-
let transaction = cached_request.finalize_package(self, output_value, self.destination_script.clone(), logger).unwrap();
396+
let transaction = cached_request.finalize_malleable_package(self, output_value, self.destination_script.clone(), logger).unwrap();
397397
log_trace!(logger, "...with timer {} and feerate {}", new_timer.unwrap(), new_feerate);
398398
assert!(predicted_weight >= transaction.weight());
399399
return Some((new_timer, new_feerate, transaction))
@@ -402,7 +402,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
402402
// Note: Currently, amounts of holder outputs spending witnesses aren't used
403403
// as we can't malleate spending package to increase their feerate. This
404404
// should change with the remaining anchor output patchset.
405-
if let Some(transaction) = cached_request.finalize_package(self, 0, self.destination_script.clone(), logger) {
405+
if let Some(transaction) = cached_request.finalize_untractable_package(self, logger) {
406406
return Some((None, 0, transaction));
407407
}
408408
}

lightning/src/chain/package.rs

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -636,47 +636,46 @@ impl PackageTemplate {
636636
let output_weight = (8 + 1 + destination_script.len()) * WITNESS_SCALE_FACTOR;
637637
inputs_weight + witnesses_weight + transaction_weight + output_weight
638638
}
639-
pub(crate) fn finalize_package<L: Deref, Signer: Sign>(&self, onchain_handler: &mut OnchainTxHandler<Signer>, value: u64, destination_script: Script, logger: &L) -> Option<Transaction>
640-
where L::Target: Logger,
641-
{
642-
match self.malleability {
643-
PackageMalleability::Malleable => {
644-
let mut bumped_tx = Transaction {
645-
version: 2,
646-
lock_time: PackedLockTime::ZERO,
647-
input: vec![],
648-
output: vec![TxOut {
649-
script_pubkey: destination_script,
650-
value,
651-
}],
652-
};
653-
for (outpoint, _) in self.inputs.iter() {
654-
bumped_tx.input.push(TxIn {
655-
previous_output: *outpoint,
656-
script_sig: Script::new(),
657-
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
658-
witness: Witness::new(),
659-
});
660-
}
661-
for (i, (outpoint, out)) in self.inputs.iter().enumerate() {
662-
log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
663-
if !out.finalize_input(&mut bumped_tx, i, onchain_handler) { return None; }
664-
}
665-
log_debug!(logger, "Finalized transaction {} ready to broadcast", bumped_tx.txid());
666-
return Some(bumped_tx);
667-
},
668-
PackageMalleability::Untractable => {
669-
debug_assert_eq!(value, 0, "value is ignored for non-malleable packages, should be zero to ensure callsites are correct");
670-
if let Some((outpoint, outp)) = self.inputs.first() {
671-
if let Some(final_tx) = outp.get_finalized_tx(outpoint, onchain_handler) {
672-
log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
673-
log_debug!(logger, "Finalized transaction {} ready to broadcast", final_tx.txid());
674-
return Some(final_tx);
675-
}
676-
return None;
677-
} else { panic!("API Error: Package must not be inputs empty"); }
678-
},
639+
pub(crate) fn finalize_malleable_package<L: Deref, Signer: Sign>(
640+
&self, onchain_handler: &mut OnchainTxHandler<Signer>, value: u64, destination_script: Script, logger: &L,
641+
) -> Option<Transaction> where L::Target: Logger {
642+
debug_assert!(self.is_malleable());
643+
let mut bumped_tx = Transaction {
644+
version: 2,
645+
lock_time: PackedLockTime::ZERO,
646+
input: vec![],
647+
output: vec![TxOut {
648+
script_pubkey: destination_script,
649+
value,
650+
}],
651+
};
652+
for (outpoint, _) in self.inputs.iter() {
653+
bumped_tx.input.push(TxIn {
654+
previous_output: *outpoint,
655+
script_sig: Script::new(),
656+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
657+
witness: Witness::new(),
658+
});
679659
}
660+
for (i, (outpoint, out)) in self.inputs.iter().enumerate() {
661+
log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
662+
if !out.finalize_input(&mut bumped_tx, i, onchain_handler) { return None; }
663+
}
664+
log_debug!(logger, "Finalized transaction {} ready to broadcast", bumped_tx.txid());
665+
Some(bumped_tx)
666+
}
667+
pub(crate) fn finalize_untractable_package<L: Deref, Signer: Sign>(
668+
&self, onchain_handler: &mut OnchainTxHandler<Signer>, logger: &L,
669+
) -> Option<Transaction> where L::Target: Logger {
670+
debug_assert!(!self.is_malleable());
671+
if let Some((outpoint, outp)) = self.inputs.first() {
672+
if let Some(final_tx) = outp.get_finalized_tx(outpoint, onchain_handler) {
673+
log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
674+
log_debug!(logger, "Finalized transaction {} ready to broadcast", final_tx.txid());
675+
return Some(final_tx);
676+
}
677+
return None;
678+
} else { panic!("API Error: Package must not be inputs empty"); }
680679
}
681680
/// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
682681
/// output detection, we generate a first version of a claim tx and associate to it a height timer. A height timer is an absolute block

0 commit comments

Comments
 (0)