Split finalize_package into separate methods per malleability
authorWilmer Paulino <wilmer.paulino@gmail.com>
Wed, 31 Aug 2022 18:07:45 +0000 (11:07 -0700)
committerWilmer Paulino <wilmer.paulino@gmail.com>
Tue, 18 Oct 2022 19:22:59 +0000 (12:22 -0700)
lightning/src/chain/onchaintx.rs
lightning/src/chain/package.rs

index 0d328075c3bcfd5b9f98eb7bef55998f6a13cb2c..565d9050170bd99a134870a81e7a4c90ae734e58 100644 (file)
@@ -393,7 +393,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                                        cached_request.compute_package_output(predicted_weight, self.destination_script.dust_value().to_sat(), fee_estimator, logger) {
                                assert!(new_feerate != 0);
 
-                               let transaction = cached_request.finalize_package(self, output_value, self.destination_script.clone(), logger).unwrap();
+                               let transaction = cached_request.finalize_malleable_package(self, output_value, self.destination_script.clone(), logger).unwrap();
                                log_trace!(logger, "...with timer {} and feerate {}", new_timer.unwrap(), new_feerate);
                                assert!(predicted_weight >= transaction.weight());
                                return Some((new_timer, new_feerate, transaction))
@@ -402,7 +402,7 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                        // Note: Currently, amounts of holder outputs spending witnesses aren't used
                        // as we can't malleate spending package to increase their feerate. This
                        // should change with the remaining anchor output patchset.
-                       if let Some(transaction) = cached_request.finalize_package(self, 0, self.destination_script.clone(), logger) {
+                       if let Some(transaction) = cached_request.finalize_untractable_package(self, logger) {
                                return Some((None, 0, transaction));
                        }
                }
index 20507c3f942278974905ab878e3a5f4c4c828f49..866ff66b48fd6d95ddc8f470782984138e2b435e 100644 (file)
@@ -636,47 +636,46 @@ impl PackageTemplate {
                let output_weight = (8 + 1 + destination_script.len()) * WITNESS_SCALE_FACTOR;
                inputs_weight + witnesses_weight + transaction_weight + output_weight
        }
-       pub(crate) fn finalize_package<L: Deref, Signer: Sign>(&self, onchain_handler: &mut OnchainTxHandler<Signer>, value: u64, destination_script: Script, logger: &L) -> Option<Transaction>
-               where L::Target: Logger,
-       {
-               match self.malleability {
-                       PackageMalleability::Malleable => {
-                               let mut bumped_tx = Transaction {
-                                       version: 2,
-                                       lock_time: PackedLockTime::ZERO,
-                                       input: vec![],
-                                       output: vec![TxOut {
-                                               script_pubkey: destination_script,
-                                               value,
-                                       }],
-                               };
-                               for (outpoint, _) in self.inputs.iter() {
-                                       bumped_tx.input.push(TxIn {
-                                               previous_output: *outpoint,
-                                               script_sig: Script::new(),
-                                               sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
-                                               witness: Witness::new(),
-                                       });
-                               }
-                               for (i, (outpoint, out)) in self.inputs.iter().enumerate() {
-                                       log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
-                                       if !out.finalize_input(&mut bumped_tx, i, onchain_handler) { return None; }
-                               }
-                               log_debug!(logger, "Finalized transaction {} ready to broadcast", bumped_tx.txid());
-                               return Some(bumped_tx);
-                       },
-                       PackageMalleability::Untractable => {
-                               debug_assert_eq!(value, 0, "value is ignored for non-malleable packages, should be zero to ensure callsites are correct");
-                               if let Some((outpoint, outp)) = self.inputs.first() {
-                                       if let Some(final_tx) = outp.get_finalized_tx(outpoint, onchain_handler) {
-                                               log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
-                                               log_debug!(logger, "Finalized transaction {} ready to broadcast", final_tx.txid());
-                                               return Some(final_tx);
-                                       }
-                                       return None;
-                               } else { panic!("API Error: Package must not be inputs empty"); }
-                       },
+       pub(crate) fn finalize_malleable_package<L: Deref, Signer: Sign>(
+               &self, onchain_handler: &mut OnchainTxHandler<Signer>, value: u64, destination_script: Script, logger: &L,
+       ) -> Option<Transaction> where L::Target: Logger {
+               debug_assert!(self.is_malleable());
+               let mut bumped_tx = Transaction {
+                       version: 2,
+                       lock_time: PackedLockTime::ZERO,
+                       input: vec![],
+                       output: vec![TxOut {
+                               script_pubkey: destination_script,
+                               value,
+                       }],
+               };
+               for (outpoint, _) in self.inputs.iter() {
+                       bumped_tx.input.push(TxIn {
+                               previous_output: *outpoint,
+                               script_sig: Script::new(),
+                               sequence: Sequence::ENABLE_RBF_NO_LOCKTIME,
+                               witness: Witness::new(),
+                       });
                }
+               for (i, (outpoint, out)) in self.inputs.iter().enumerate() {
+                       log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
+                       if !out.finalize_input(&mut bumped_tx, i, onchain_handler) { return None; }
+               }
+               log_debug!(logger, "Finalized transaction {} ready to broadcast", bumped_tx.txid());
+               Some(bumped_tx)
+       }
+       pub(crate) fn finalize_untractable_package<L: Deref, Signer: Sign>(
+               &self, onchain_handler: &mut OnchainTxHandler<Signer>, logger: &L,
+       ) -> Option<Transaction> where L::Target: Logger {
+               debug_assert!(!self.is_malleable());
+               if let Some((outpoint, outp)) = self.inputs.first() {
+                       if let Some(final_tx) = outp.get_finalized_tx(outpoint, onchain_handler) {
+                               log_debug!(logger, "Adding claiming input for outpoint {}:{}", outpoint.txid, outpoint.vout);
+                               log_debug!(logger, "Finalized transaction {} ready to broadcast", final_tx.txid());
+                               return Some(final_tx);
+                       }
+                       return None;
+               } else { panic!("API Error: Package must not be inputs empty"); }
        }
        /// In LN, output claimed are time-sensitive, which means we have to spend them before reaching some timelock expiration. At in-channel
        /// 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