Skip to content

Commit 0dd598a

Browse files
committed
Split finalize_package into separate methods per malleability
1 parent 838443a commit 0dd598a

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
@@ -656,47 +656,46 @@ impl PackageTemplate {
656656
};
657657
transaction_weight + inputs_weight + witnesses_weight + output_weight
658658
}
659-
pub(crate) fn finalize_package<L: Deref, Signer: Sign>(&self, onchain_handler: &mut OnchainTxHandler<Signer>, value: u64, destination_script: Script, logger: &L) -> Option<Transaction>
660-
where L::Target: Logger,
661-
{
662-
match self.malleability {
663-
PackageMalleability::Malleable => {
664-
let mut bumped_tx = Transaction {
665-
version: 2,
666-
lock_time: PackedLockTime::ZERO,
667-
input: vec![],
668-
output: vec![TxOut {
669-
script_pubkey: destination_script,
670-
value,
671-
}],
672-
};
673-
for (outpoint, _) in self.inputs.iter() {
674-
bumped_tx.input.push(TxIn {
675-
previous_output: *outpoint,
676-
script_sig: Script::new(),
677-
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
678-
witness: Witness::new(),
679-
});
680-
}
681-
for (i, (outpoint, out)) in self.inputs.iter().enumerate() {
682-
log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
683-
if !out.finalize_input(&mut bumped_tx, i, onchain_handler) { return None; }
684-
}
685-
log_debug!(logger, "Finalized transaction {} ready to broadcast", bumped_tx.txid());
686-
return Some(bumped_tx);
687-
},
688-
PackageMalleability::Untractable => {
689-
debug_assert_eq!(value, 0, "value is ignored for non-malleable packages, should be zero to ensure callsites are correct");
690-
if let Some((outpoint, outp)) = self.inputs.first() {
691-
if let Some(final_tx) = outp.get_finalized_tx(outpoint, onchain_handler) {
692-
log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
693-
log_debug!(logger, "Finalized transaction {} ready to broadcast", final_tx.txid());
694-
return Some(final_tx);
695-
}
696-
return None;
697-
} else { panic!("API Error: Package must not be inputs empty"); }
698-
},
659+
pub(crate) fn finalize_malleable_package<L: Deref, Signer: Sign>(
660+
&self, onchain_handler: &mut OnchainTxHandler<Signer>, value: u64, destination_script: Script, logger: &L,
661+
) -> Option<Transaction> where L::Target: Logger {
662+
debug_assert!(self.is_malleable());
663+
let mut bumped_tx = Transaction {
664+
version: 2,
665+
lock_time: PackedLockTime::ZERO,
666+
input: vec![],
667+
output: vec![TxOut {
668+
script_pubkey: destination_script,
669+
value,
670+
}],
671+
};
672+
for (outpoint, _) in self.inputs.iter() {
673+
bumped_tx.input.push(TxIn {
674+
previous_output: *outpoint,
675+
script_sig: Script::new(),
676+
sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
677+
witness: Witness::new(),
678+
});
699679
}
680+
for (i, (outpoint, out)) in self.inputs.iter().enumerate() {
681+
log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
682+
if !out.finalize_input(&mut bumped_tx, i, onchain_handler) { return None; }
683+
}
684+
log_debug!(logger, "Finalized transaction {} ready to broadcast", bumped_tx.txid());
685+
Some(bumped_tx)
686+
}
687+
pub(crate) fn finalize_untractable_package<L: Deref, Signer: Sign>(
688+
&self, onchain_handler: &mut OnchainTxHandler<Signer>, logger: &L,
689+
) -> Option<Transaction> where L::Target: Logger {
690+
debug_assert!(!self.is_malleable());
691+
if let Some((outpoint, outp)) = self.inputs.first() {
692+
if let Some(final_tx) = outp.get_finalized_tx(outpoint, onchain_handler) {
693+
log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
694+
log_debug!(logger, "Finalized transaction {} ready to broadcast", final_tx.txid());
695+
return Some(final_tx);
696+
}
697+
return None;
698+
} else { panic!("API Error: Package must not be inputs empty"); }
700699
}
701700
/// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
702701
/// 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)