Merge pull request #1761 from TheBlueMatt/2022-10-user-idempotency-token
[rust-lightning] / lightning / src / chain / onchaintx.rs
index 0d328075c3bcfd5b9f98eb7bef55998f6a13cb2c..9cfa9d288ce3590a585a3e7e708e0bcb54911b30 100644 (file)
@@ -21,23 +21,31 @@ use bitcoin::hash_types::Txid;
 use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature};
 use bitcoin::secp256k1;
 
-use ln::msgs::DecodeError;
-use ln::PaymentPreimage;
-use ln::chan_utils::{ChannelTransactionParameters, HolderCommitmentTransaction};
-use chain::chaininterface::{FeeEstimator, BroadcasterInterface, LowerBoundedFeeEstimator};
-use chain::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER};
-use chain::keysinterface::{Sign, KeysInterface};
-use chain::package::PackageTemplate;
-use util::logger::Logger;
-use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, VecWriter};
-use util::byte_utils;
-
-use io;
-use prelude::*;
+use crate::ln::msgs::DecodeError;
+use crate::ln::PaymentPreimage;
+#[cfg(anchors)]
+use crate::ln::chan_utils;
+use crate::ln::chan_utils::{ChannelTransactionParameters, HolderCommitmentTransaction};
+#[cfg(anchors)]
+use crate::chain::chaininterface::ConfirmationTarget;
+use crate::chain::chaininterface::{FeeEstimator, BroadcasterInterface, LowerBoundedFeeEstimator};
+use crate::chain::channelmonitor::{ANTI_REORG_DELAY, CLTV_SHARED_CLAIM_BUFFER};
+use crate::chain::keysinterface::{Sign, KeysInterface};
+#[cfg(anchors)]
+use crate::chain::package::PackageSolvingData;
+use crate::chain::package::PackageTemplate;
+use crate::util::logger::Logger;
+use crate::util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, VecWriter};
+use crate::util::byte_utils;
+
+use crate::io;
+use crate::prelude::*;
 use alloc::collections::BTreeMap;
 use core::cmp;
 use core::ops::Deref;
 use core::mem::replace;
+#[cfg(anchors)]
+use core::mem::swap;
 use bitcoin::hashes::Hash;
 
 const MAX_ALLOC_SIZE: usize = 64*1024;
@@ -162,6 +170,29 @@ impl Writeable for Option<Vec<Option<(usize, Signature)>>> {
        }
 }
 
+// Represents the different types of claims for which events are yielded externally to satisfy said
+// claims.
+#[cfg(anchors)]
+pub(crate) enum ClaimEvent {
+       /// Event yielded to signal that the commitment transaction fee must be bumped to claim any
+       /// encumbered funds and proceed to HTLC resolution, if any HTLCs exist.
+       BumpCommitment {
+               package_target_feerate_sat_per_1000_weight: u32,
+               commitment_tx: Transaction,
+               anchor_output_idx: u32,
+       },
+}
+
+/// Represents the different ways an output can be claimed (i.e., spent to an address under our
+/// control) onchain.
+pub(crate) enum OnchainClaim {
+       /// A finalized transaction pending confirmation spending the output to claim.
+       Tx(Transaction),
+       #[cfg(anchors)]
+       /// An event yielded externally to signal additional inputs must be added to a transaction
+       /// pending confirmation spending the output to claim.
+       Event(ClaimEvent),
+}
 
 /// OnchainTxHandler receives claiming requests, aggregates them if it's sound, broadcast and
 /// do RBF bumping if possible.
@@ -193,6 +224,8 @@ pub struct OnchainTxHandler<ChannelSigner: Sign> {
        pub(crate) pending_claim_requests: HashMap<Txid, PackageTemplate>,
        #[cfg(not(test))]
        pending_claim_requests: HashMap<Txid, PackageTemplate>,
+       #[cfg(anchors)]
+       pending_claim_events: HashMap<Txid, ClaimEvent>,
 
        // Used to link outpoints claimed in a connected block to a pending claim request.
        // Key is outpoint than monitor parsing has detected we have keys/scripts to claim
@@ -342,6 +375,8 @@ impl<'a, K: KeysInterface> ReadableArgs<&'a K> for OnchainTxHandler<K::Signer> {
                        locktimed_packages,
                        pending_claim_requests,
                        onchain_events_awaiting_threshold_conf,
+                       #[cfg(anchors)]
+                       pending_claim_events: HashMap::new(),
                        secp_ctx,
                })
        }
@@ -361,6 +396,8 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                        claimable_outpoints: HashMap::new(),
                        locktimed_packages: BTreeMap::new(),
                        onchain_events_awaiting_threshold_conf: Vec::new(),
+                       #[cfg(anchors)]
+                       pending_claim_events: HashMap::new(),
 
                        secp_ctx,
                }
@@ -374,15 +411,62 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                self.holder_commitment.to_broadcaster_value_sat()
        }
 
-       /// Lightning security model (i.e being able to redeem/timeout HTLC or penalize coutnerparty onchain) lays on the assumption of claim transactions getting confirmed before timelock expiration
-       /// (CSV or CLTV following cases). In case of high-fee spikes, claim tx may stuck in the mempool, so you need to bump its feerate quickly using Replace-By-Fee or Child-Pay-For-Parent.
-       /// 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_tx<F: Deref, L: Deref>(&mut self, cur_height: u32, cached_request: &PackageTemplate, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L) -> Option<(Option<u32>, u64, Transaction)>
+       #[cfg(anchors)]
+       pub(crate) fn get_and_clear_pending_claim_events(&mut self) -> Vec<ClaimEvent> {
+               let mut ret = HashMap::new();
+               swap(&mut ret, &mut self.pending_claim_events);
+               ret.into_iter().map(|(_, event)| event).collect::<Vec<_>>()
+       }
+
+       /// Lightning security model (i.e being able to redeem/timeout HTLC or penalize counterparty
+       /// onchain) lays on the assumption of claim transactions getting confirmed before timelock
+       /// expiration (CSV or CLTV following cases). In case of high-fee spikes, claim tx may get stuck
+       /// in the mempool, so you need to bump its feerate quickly using Replace-By-Fee or
+       /// Child-Pay-For-Parent.
+       ///
+       /// 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)>
                where F::Target: FeeEstimator,
                                        L::Target: Logger,
        {
-               if cached_request.outpoints().len() == 0 { return None } // But don't prune pending claiming request yet, we may have to resurrect HTLCs
+               let request_outpoints = cached_request.outpoints();
+               if request_outpoints.is_empty() {
+                       // Don't prune pending claiming request yet, we may have to resurrect HTLCs. Untractable
+                       // packages cannot be aggregated and will never be split, so we cannot end up with an
+                       // empty claim.
+                       debug_assert!(cached_request.is_malleable());
+                       return None;
+               }
+               // If we've seen transaction inclusion in the chain for all outpoints in our request, we
+               // don't need to continue generating more claims. We'll keep tracking the request to fully
+               // remove it once it reaches the confirmation threshold, or to generate a new claim if the
+               // transaction is reorged out.
+               let mut all_inputs_have_confirmed_spend = true;
+               for outpoint in &request_outpoints {
+                       if let Some(first_claim_txid_height) = self.claimable_outpoints.get(outpoint) {
+                               // We check for outpoint spends within claims individually rather than as a set
+                               // since requests can have outpoints split off.
+                               if !self.onchain_events_awaiting_threshold_conf.iter()
+                                       .any(|event_entry| if let OnchainEvent::Claim { claim_request } = event_entry.event {
+                                               first_claim_txid_height.0 == claim_request
+                                       } else {
+                                               // The onchain event is not a claim, keep seeking until we find one.
+                                               false
+                                       })
+                               {
+                                       // Either we had no `OnchainEvent::Claim`, or we did but none matched the
+                                       // outpoint's registered spend.
+                                       all_inputs_have_confirmed_spend = false;
+                               }
+                       } else {
+                               // The request's outpoint spend does not exist yet.
+                               all_inputs_have_confirmed_spend = false;
+                       }
+               }
+               if all_inputs_have_confirmed_spend {
+                       return None;
+               }
 
                // 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).
@@ -393,18 +477,66 @@ 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))
+                               return Some((new_timer, new_feerate, OnchainClaim::Tx(transaction)))
                        }
                } else {
-                       // 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) {
-                               return Some((None, 0, transaction));
+                       // Untractable packages cannot have their fees bumped through Replace-By-Fee. Some
+                       // packages may support fee bumping through Child-Pays-For-Parent, indicated by those
+                       // which require external funding.
+                       #[cfg(not(anchors))]
+                       let inputs = cached_request.inputs();
+                       #[cfg(anchors)]
+                       let mut inputs = cached_request.inputs();
+                       debug_assert_eq!(inputs.len(), 1);
+                       let tx = match cached_request.finalize_untractable_package(self, logger) {
+                               Some(tx) => tx,
+                               None => return None,
+                       };
+                       if !cached_request.requires_external_funding() {
+                               return Some((None, 0, OnchainClaim::Tx(tx)));
                        }
+                       #[cfg(anchors)]
+                       return inputs.find_map(|input| match input {
+                               // Commitment inputs with anchors support are the only untractable inputs supported
+                               // thus far that require external funding.
+                               PackageSolvingData::HolderFundingOutput(..) => {
+                                       debug_assert_eq!(tx.txid(), self.holder_commitment.trust().txid(),
+                                               "Holder commitment transaction mismatch");
+                                       // We'll locate an anchor output we can spend within the commitment transaction.
+                                       let funding_pubkey = &self.channel_transaction_parameters.holder_pubkeys.funding_pubkey;
+                                       match chan_utils::get_anchor_output(&tx, funding_pubkey) {
+                                               // An anchor output was found, so we should yield a funding event externally.
+                                               Some((idx, _)) => {
+                                                       // TODO: Use a lower confirmation target when both our and the
+                                                       // counterparty's latest commitment don't have any HTLCs present.
+                                                       let conf_target = ConfirmationTarget::HighPriority;
+                                                       let package_target_feerate_sat_per_1000_weight = cached_request
+                                                               .compute_package_feerate(fee_estimator, conf_target);
+                                                       Some((
+                                                               new_timer,
+                                                               package_target_feerate_sat_per_1000_weight as u64,
+                                                               OnchainClaim::Event(ClaimEvent::BumpCommitment {
+                                                                       package_target_feerate_sat_per_1000_weight,
+                                                                       commitment_tx: tx.clone(),
+                                                                       anchor_output_idx: idx,
+                                                               }),
+                                                       ))
+                                               },
+                                               // An anchor output was not found. There's nothing we can do other than
+                                               // 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()))),
+                                       }
+                               },
+                               _ => {
+                                       debug_assert!(false, "Only HolderFundingOutput inputs should be untractable and require external funding");
+                                       None
+                               },
+                       });
                }
                None
        }
@@ -475,17 +607,30 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                // Generate claim transactions and track them to bump if necessary at
                // height timer expiration (i.e in how many blocks we're going to take action).
                for mut req in preprocessed_requests {
-                       if let Some((new_timer, new_feerate, tx)) = self.generate_claim_tx(cur_height, &req, &*fee_estimator, &*logger) {
+                       if let Some((new_timer, new_feerate, claim)) = self.generate_claim(cur_height, &req, &*fee_estimator, &*logger) {
                                req.set_timer(new_timer);
                                req.set_feerate(new_feerate);
-                               let txid = tx.txid();
+                               let txid = match claim {
+                                       OnchainClaim::Tx(tx) => {
+                                               log_info!(logger, "Broadcasting onchain {}", log_tx!(tx));
+                                               broadcaster.broadcast_transaction(&tx);
+                                               tx.txid()
+                                       },
+                                       #[cfg(anchors)]
+                                       OnchainClaim::Event(claim_event) => {
+                                               log_info!(logger, "Yielding onchain event to spend inputs {:?}", req.outpoints());
+                                               let txid = match claim_event {
+                                                       ClaimEvent::BumpCommitment { ref commitment_tx, .. } => commitment_tx.txid(),
+                                               };
+                                               self.pending_claim_events.insert(txid, claim_event);
+                                               txid
+                                       },
+                               };
                                for k in req.outpoints() {
                                        log_info!(logger, "Registering claiming request for {}:{}", k.txid, k.vout);
                                        self.claimable_outpoints.insert(k.clone(), (txid, conf_height));
                                }
                                self.pending_claim_requests.insert(txid, req);
-                               log_info!(logger, "Broadcasting onchain {}", log_tx!(tx));
-                               broadcaster.broadcast_transaction(&tx);
                        }
                }
 
@@ -577,6 +722,8 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                                                        for outpoint in request.outpoints() {
                                                                log_debug!(logger, "Removing claim tracking for {} due to maturation of claim tx {}.", outpoint, claim_request);
                                                                self.claimable_outpoints.remove(&outpoint);
+                                                               #[cfg(anchors)]
+                                                               self.pending_claim_events.remove(&claim_request);
                                                        }
                                                }
                                        },
@@ -603,9 +750,18 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                // Build, bump and rebroadcast tx accordingly
                log_trace!(logger, "Bumping {} candidates", bump_candidates.len());
                for (first_claim_txid, request) in bump_candidates.iter() {
-                       if let Some((new_timer, new_feerate, bump_tx)) = self.generate_claim_tx(cur_height, &request, &*fee_estimator, &*logger) {
-                               log_info!(logger, "Broadcasting RBF-bumped onchain {}", log_tx!(bump_tx));
-                               broadcaster.broadcast_transaction(&bump_tx);
+                       if let Some((new_timer, new_feerate, bump_claim)) = self.generate_claim(cur_height, &request, &*fee_estimator, &*logger) {
+                               match bump_claim {
+                                       OnchainClaim::Tx(bump_tx) => {
+                                               log_info!(logger, "Broadcasting RBF-bumped onchain {}", log_tx!(bump_tx));
+                                               broadcaster.broadcast_transaction(&bump_tx);
+                                       },
+                                       #[cfg(anchors)]
+                                       OnchainClaim::Event(claim_event) => {
+                                               log_info!(logger, "Yielding RBF-bumped onchain event to spend inputs {:?}", request.outpoints());
+                                               self.pending_claim_events.insert(*first_claim_txid, claim_event);
+                                       },
+                               }
                                if let Some(request) = self.pending_claim_requests.get_mut(first_claim_txid) {
                                        request.set_timer(new_timer);
                                        request.set_feerate(new_feerate);
@@ -667,12 +823,21 @@ impl<ChannelSigner: Sign> OnchainTxHandler<ChannelSigner> {
                                self.onchain_events_awaiting_threshold_conf.push(entry);
                        }
                }
-               for (_, request) in bump_candidates.iter_mut() {
-                       if let Some((new_timer, new_feerate, bump_tx)) = self.generate_claim_tx(height, &request, fee_estimator, &&*logger) {
+               for (_first_claim_txid_height, request) in bump_candidates.iter_mut() {
+                       if let Some((new_timer, new_feerate, bump_claim)) = self.generate_claim(height, &request, fee_estimator, &&*logger) {
                                request.set_timer(new_timer);
                                request.set_feerate(new_feerate);
-                               log_info!(logger, "Broadcasting onchain {}", log_tx!(bump_tx));
-                               broadcaster.broadcast_transaction(&bump_tx);
+                               match bump_claim {
+                                       OnchainClaim::Tx(bump_tx) => {
+                                               log_info!(logger, "Broadcasting onchain {}", log_tx!(bump_tx));
+                                               broadcaster.broadcast_transaction(&bump_tx);
+                                       },
+                                       #[cfg(anchors)]
+                                       OnchainClaim::Event(claim_event) => {
+                                               log_info!(logger, "Yielding onchain event after reorg to spend inputs {:?}", request.outpoints());
+                                               self.pending_claim_events.insert(_first_claim_txid_height.0, claim_event);
+                                       },
+                               }
                        }
                }
                for (ancestor_claim_txid, request) in bump_candidates.drain() {