Merge pull request #2203 from wpaulino/retry-untractable-packages
[rust-lightning] / lightning / src / chain / onchaintx.rs
index 2c570f580bd32153bb0488fd454a8345368f1436..cd0cb08eab3522c00a057a7e9043c45c6a2a43a0 100644 (file)
@@ -12,6 +12,8 @@
 //! OnchainTxHandler objects are fully-part of ChannelMonitor and encapsulates all
 //! building, tracking, bumping and notifications functions.
 
+#[cfg(anchors)]
+use bitcoin::PackedLockTime;
 use bitcoin::blockdata::transaction::Transaction;
 use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
 use bitcoin::blockdata::script::Script;
@@ -201,6 +203,7 @@ pub(crate) enum ClaimEvent {
        BumpHTLC {
                target_feerate_sat_per_1000_weight: u32,
                htlcs: Vec<ExternalHTLCClaim>,
+               tx_lock_time: PackedLockTime,
        },
 }
 
@@ -486,7 +489,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
        ///
        /// Panics if there are signing errors, because signing operations in reaction to on-chain
        /// events are not expected to fail, and if they do, we may lose funds.
-       fn generate_claim<F: Deref, L: Deref>(&mut self, cur_height: u32, cached_request: &PackageTemplate, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L) -> Option<(Option<u32>, u64, OnchainClaim)>
+       fn generate_claim<F: Deref, L: Deref>(&mut self, cur_height: u32, cached_request: &PackageTemplate, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L) -> Option<(u32, u64, OnchainClaim)>
                where F::Target: FeeEstimator,
                                        L::Target: Logger,
        {
@@ -530,7 +533,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
 
                // Compute new height timer to decide when we need to regenerate a new bumped version of the claim tx (if we
                // didn't receive confirmation of it before, or not enough reorg-safe depth on top of it).
-               let new_timer = Some(cached_request.get_height_timer(cur_height));
+               let new_timer = cached_request.get_height_timer(cur_height);
                if cached_request.is_malleable() {
                        #[cfg(anchors)]
                        { // Attributes are not allowed on if expressions on our current MSRV of 1.41.
@@ -544,6 +547,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                                                        OnchainClaim::Event(ClaimEvent::BumpHTLC {
                                                                target_feerate_sat_per_1000_weight,
                                                                htlcs,
+                                                               tx_lock_time: PackedLockTime(cached_request.package_locktime(cur_height)),
                                                        }),
                                                ));
                                        } else {
@@ -558,8 +562,10 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                        ) {
                                assert!(new_feerate != 0);
 
-                               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);
+                               let transaction = cached_request.finalize_malleable_package(
+                                       cur_height, self, output_value, self.destination_script.clone(), logger
+                               ).unwrap();
+                               log_trace!(logger, "...with timer {} and feerate {}", new_timer, new_feerate);
                                assert!(predicted_weight >= transaction.weight());
                                return Some((new_timer, new_feerate, OnchainClaim::Tx(transaction)));
                        }
@@ -577,7 +583,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                                None => return None,
                        };
                        if !cached_request.requires_external_funding() {
-                               return Some((None, 0, OnchainClaim::Tx(tx)));
+                               return Some((new_timer, 0, OnchainClaim::Tx(tx)));
                        }
                        #[cfg(anchors)]
                        return inputs.find_map(|input| match input {
@@ -610,7 +616,7 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                                                // attempt to broadcast the transaction with its current fee rate and hope
                                                // it confirms. This is essentially the same behavior as a commitment
                                                // transaction without anchor outputs.
-                                               None => Some((None, 0, OnchainClaim::Tx(tx.clone()))),
+                                               None => Some((new_timer, 0, OnchainClaim::Tx(tx.clone()))),
                                        }
                                },
                                _ => {
@@ -654,16 +660,17 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
                                        .find(|locked_package| locked_package.outpoints() == req.outpoints());
                                if let Some(package) = timelocked_equivalent_package {
                                        log_info!(logger, "Ignoring second claim for outpoint {}:{}, we already have one which we're waiting on a timelock at {} for.",
-                                               req.outpoints()[0].txid, req.outpoints()[0].vout, package.package_timelock());
+                                               req.outpoints()[0].txid, req.outpoints()[0].vout, package.package_locktime(cur_height));
                                        continue;
                                }
 
-                               if req.package_timelock() > cur_height + 1 {
-                                       log_info!(logger, "Delaying claim of package until its timelock at {} (current height {}), the following outpoints are spent:", req.package_timelock(), cur_height);
+                               let package_locktime = req.package_locktime(cur_height);
+                               if package_locktime > cur_height + 1 {
+                                       log_info!(logger, "Delaying claim of package until its timelock at {} (current height {}), the following outpoints are spent:", package_locktime, cur_height);
                                        for outpoint in req.outpoints() {
                                                log_info!(logger, "  Outpoint {}", outpoint);
                                        }
-                                       self.locktimed_packages.entry(req.package_timelock()).or_insert(Vec::new()).push(req);
+                                       self.locktimed_packages.entry(package_locktime).or_insert(Vec::new()).push(req);
                                        continue;
                                }
 
@@ -878,10 +885,8 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
 
                // Check if any pending claim request must be rescheduled
                for (package_id, request) in self.pending_claim_requests.iter() {
-                       if let Some(h) = request.timer() {
-                               if cur_height >= h {
-                                       bump_candidates.insert(*package_id, request.clone());
-                               }
+                       if cur_height >= request.timer() {
+                               bump_candidates.insert(*package_id, request.clone());
                        }
                }