Merge pull request #2610 from wpaulino/missing-htlc-claim-balance
authorMatt Corallo <649246+TheBlueMatt@users.noreply.github.com>
Fri, 29 Sep 2023 23:55:04 +0000 (23:55 +0000)
committerGitHub <noreply@github.com>
Fri, 29 Sep 2023 23:55:04 +0000 (23:55 +0000)
Fix matching of second-stage HTLC claim in get_htlc_balance

lightning/src/chain/chainmonitor.rs
lightning/src/events/mod.rs
lightning/src/ln/channelmanager.rs
lightning/src/ln/outbound_payment.rs
lightning/src/ln/reload_tests.rs
lightning/src/sign/type_resolver.rs
lightning/src/util/test_utils.rs

index cf51dbab72019d9eb87748e0817171c1c0abfb35..261c0471ca4089dfaa1d21ca0e8c8f810f90c7a5 100644 (file)
@@ -47,23 +47,35 @@ use core::ops::Deref;
 use core::sync::atomic::{AtomicUsize, Ordering};
 use bitcoin::secp256k1::PublicKey;
 
-#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
-/// A specific update's ID stored in a `MonitorUpdateId`, separated out to make the contents
-/// entirely opaque.
-enum UpdateOrigin {
-       /// An update that was generated by the `ChannelManager` (via our `chain::Watch`
-       /// implementation). This corresponds to an actual [`ChannelMonitorUpdate::update_id`] field
-       /// and [`ChannelMonitor::get_latest_update_id`].
-       OffChain(u64),
-       /// An update that was generated during blockchain processing. The ID here is specific to the
-       /// generating [`ChainMonitor`] and does *not* correspond to any on-disk IDs.
-       ChainSync(u64),
+mod update_origin {
+       #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
+       /// A specific update's ID stored in a `MonitorUpdateId`, separated out to make the contents
+       /// entirely opaque.
+       pub(crate) enum UpdateOrigin {
+               /// An update that was generated by the `ChannelManager` (via our [`crate::chain::Watch`]
+               /// implementation). This corresponds to an actual [ChannelMonitorUpdate::update_id] field
+               /// and [ChannelMonitor::get_latest_update_id].
+               ///
+               /// [ChannelMonitor::get_latest_update_id]: crate::chain::channelmonitor::ChannelMonitor::get_latest_update_id
+               /// [ChannelMonitorUpdate::update_id]: crate::chain::channelmonitor::ChannelMonitorUpdate::update_id
+               OffChain(u64),
+               /// An update that was generated during blockchain processing. The ID here is specific to the
+               /// generating [ChannelMonitor] and does *not* correspond to any on-disk IDs.
+               ///
+               /// [ChannelMonitor]: crate::chain::channelmonitor::ChannelMonitor
+               ChainSync(u64),
+       }
 }
 
+#[cfg(any(feature = "_test_utils", test))]
+pub(crate) use update_origin::UpdateOrigin;
+#[cfg(not(any(feature = "_test_utils", test)))]
+use update_origin::UpdateOrigin;
+
 /// An opaque identifier describing a specific [`Persist`] method call.
 #[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
 pub struct MonitorUpdateId {
-       contents: UpdateOrigin,
+       pub(crate) contents: UpdateOrigin,
 }
 
 impl MonitorUpdateId {
@@ -155,8 +167,8 @@ pub trait Persist<ChannelSigner: WriteableEcdsaChannelSigner> {
        /// updated monitor itself to disk/backups. See the [`Persist`] trait documentation for more
        /// details.
        ///
-       /// During blockchain synchronization operations, this may be called with no
-       /// [`ChannelMonitorUpdate`], in which case the full [`ChannelMonitor`] needs to be persisted.
+       /// During blockchain synchronization operations, and in some rare cases, this may be called with
+       /// no [`ChannelMonitorUpdate`], in which case the full [`ChannelMonitor`] needs to be persisted.
        /// Note that after the full [`ChannelMonitor`] is persisted any previous
        /// [`ChannelMonitorUpdate`]s which were persisted should be discarded - they can no longer be
        /// applied to the persisted [`ChannelMonitor`] as they were already applied.
@@ -756,14 +768,20 @@ where C::Target: chain::Filter,
                                let monitor = &monitor_state.monitor;
                                log_trace!(self.logger, "Updating ChannelMonitor for channel {}", log_funding_info!(monitor));
                                let update_res = monitor.update_monitor(update, &self.broadcaster, &*self.fee_estimator, &self.logger);
-                               if update_res.is_err() {
-                                       log_error!(self.logger, "Failed to update ChannelMonitor for channel {}.", log_funding_info!(monitor));
-                               }
-                               // Even if updating the monitor returns an error, the monitor's state will
-                               // still be changed. So, persist the updated monitor despite the error.
+
                                let update_id = MonitorUpdateId::from_monitor_update(update);
                                let mut pending_monitor_updates = monitor_state.pending_monitor_updates.lock().unwrap();
-                               let persist_res = self.persister.update_persisted_channel(funding_txo, Some(update), monitor, update_id);
+                               let persist_res = if update_res.is_err() {
+                                       // Even if updating the monitor returns an error, the monitor's state will
+                                       // still be changed. Therefore, we should persist the updated monitor despite the error.
+                                       // We don't want to persist a `monitor_update` which results in a failure to apply later
+                                       // while reading `channel_monitor` with updates from storage. Instead, we should persist
+                                       // the entire `channel_monitor` here.
+                                       log_warn!(self.logger, "Failed to update ChannelMonitor for channel {}. Going ahead and persisting the entire ChannelMonitor", log_funding_info!(monitor));
+                                       self.persister.update_persisted_channel(funding_txo, None, monitor, update_id)
+                               } else {
+                                       self.persister.update_persisted_channel(funding_txo, Some(update), monitor, update_id)
+                               };
                                match persist_res {
                                        ChannelMonitorUpdateStatus::InProgress => {
                                                pending_monitor_updates.push(update_id);
index 269887a3dbac06fc1ba24dcda084f5c2c2115de2..c8c736c1f713db79a72336fbb44b2ba0ae76abea 100644 (file)
@@ -517,6 +517,7 @@ pub enum Event {
        /// or was explicitly abandoned by [`ChannelManager::abandon_payment`].
        ///
        /// [`ChannelManager::abandon_payment`]: crate::ln::channelmanager::ChannelManager::abandon_payment
+       #[cfg(invreqfailed)]
        InvoiceRequestFailed {
                /// The `payment_id` to have been associated with payment for the requested invoice.
                payment_id: PaymentId,
@@ -1163,6 +1164,7 @@ impl Writeable for Event {
                                        (8, funding_txo, required),
                                });
                        },
+                       #[cfg(invreqfailed)]
                        &Event::InvoiceRequestFailed { ref payment_id } => {
                                33u8.write(writer)?;
                                write_tlv_fields!(writer, {
@@ -1556,6 +1558,7 @@ impl MaybeReadable for Event {
                                };
                                f()
                        },
+                       #[cfg(invreqfailed)]
                        33u8 => {
                                let f = || {
                                        _init_and_read_len_prefixed_tlv_fields!(reader, {
index d27f1be08cddcb75f3e3ca561f3dac86dccdab2a..3f6a2a6b20dfcc692df81fad93ac93c4d37a068e 100644 (file)
@@ -3549,19 +3549,10 @@ where
        /// wait until you receive either a [`Event::PaymentFailed`] or [`Event::PaymentSent`] event to
        /// determine the ultimate status of a payment.
        ///
-       /// # Requested Invoices
-       ///
-       /// In the case of paying a [`Bolt12Invoice`], abandoning the payment prior to receiving the
-       /// invoice will result in an [`Event::InvoiceRequestFailed`] and prevent any attempts at paying
-       /// it once received. The other events may only be generated once the invoice has been received.
-       ///
        /// # Restart Behavior
        ///
        /// If an [`Event::PaymentFailed`] is generated and we restart without first persisting the
-       /// [`ChannelManager`], another [`Event::PaymentFailed`] may be generated; likewise for
-       /// [`Event::InvoiceRequestFailed`].
-       ///
-       /// [`Bolt12Invoice`]: crate::offers::invoice::Bolt12Invoice
+       /// [`ChannelManager`], another [`Event::PaymentFailed`] may be generated.
        pub fn abandon_payment(&self, payment_id: PaymentId) {
                let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
                self.pending_outbound_payments.abandon_payment(payment_id, PaymentFailureReason::UserAbandoned, &self.pending_events);
@@ -3902,7 +3893,7 @@ where
                                btree_map::Entry::Vacant(vacant) => Some(vacant.insert(Vec::new())),
                        }
                });
-               for (channel_idx, &(temporary_channel_id, counterparty_node_id)) in temporary_channels.iter().enumerate() {
+               for &(temporary_channel_id, counterparty_node_id) in temporary_channels.iter() {
                        result = result.and_then(|_| self.funding_transaction_generated_intern(
                                temporary_channel_id,
                                counterparty_node_id,
index 2522f99fbe85dbecd751034215207cbda23ad5ab..1642f28efc7b36dbfa7739bf886ebb58b9eb7c70 100644 (file)
@@ -1502,6 +1502,9 @@ impl OutboundPayments {
                &self, pending_events: &Mutex<VecDeque<(events::Event, Option<EventCompletionAction>)>>)
        {
                let mut pending_outbound_payments = self.pending_outbound_payments.lock().unwrap();
+               #[cfg(not(invreqfailed))]
+               let pending_events = pending_events.lock().unwrap();
+               #[cfg(invreqfailed)]
                let mut pending_events = pending_events.lock().unwrap();
                pending_outbound_payments.retain(|payment_id, payment| {
                        // If an outbound payment was completed, and no pending HTLCs remain, we should remove it
@@ -1540,6 +1543,7 @@ impl OutboundPayments {
                                if *timer_ticks_without_response <= INVOICE_REQUEST_TIMEOUT_TICKS {
                                        true
                                } else {
+                                       #[cfg(invreqfailed)]
                                        pending_events.push_back(
                                                (events::Event::InvoiceRequestFailed { payment_id: *payment_id }, None)
                                        );
@@ -1692,6 +1696,7 @@ impl OutboundPayments {
                                        payment.remove();
                                }
                        } else if let PendingOutboundPayment::AwaitingInvoice { .. } = payment.get() {
+                               #[cfg(invreqfailed)]
                                pending_events.lock().unwrap().push_back((events::Event::InvoiceRequestFailed {
                                        payment_id,
                                }, None));
@@ -1782,7 +1787,9 @@ mod tests {
        use crate::ln::channelmanager::{PaymentId, RecipientOnionFields};
        use crate::ln::features::{ChannelFeatures, NodeFeatures};
        use crate::ln::msgs::{ErrorAction, LightningError};
-       use crate::ln::outbound_payment::{Bolt12PaymentError, INVOICE_REQUEST_TIMEOUT_TICKS, OutboundPayments, Retry, RetryableSendFailure};
+       use crate::ln::outbound_payment::{Bolt12PaymentError, OutboundPayments, Retry, RetryableSendFailure};
+       #[cfg(invreqfailed)]
+       use crate::ln::outbound_payment::INVOICE_REQUEST_TIMEOUT_TICKS;
        use crate::offers::invoice::DEFAULT_RELATIVE_EXPIRY;
        use crate::offers::offer::OfferBuilder;
        use crate::offers::test_utils::*;
@@ -1985,6 +1992,7 @@ mod tests {
        }
 
        #[test]
+       #[cfg(invreqfailed)]
        fn removes_stale_awaiting_invoice() {
                let pending_events = Mutex::new(VecDeque::new());
                let outbound_payments = OutboundPayments::new();
@@ -2023,6 +2031,7 @@ mod tests {
        }
 
        #[test]
+       #[cfg(invreqfailed)]
        fn removes_abandoned_awaiting_invoice() {
                let pending_events = Mutex::new(VecDeque::new());
                let outbound_payments = OutboundPayments::new();
index 151861411b997002de4792d5c031ed8079faee2e..5f4009d642949ca42d0b1bbe73401b9bb657b233 100644 (file)
@@ -25,7 +25,6 @@ use crate::util::ser::{Writeable, ReadableArgs};
 use crate::util::config::UserConfig;
 use crate::util::string::UntrustedString;
 
-use bitcoin::{PackedLockTime, Transaction, TxOut};
 use bitcoin::hash_types::BlockHash;
 
 use crate::prelude::*;
index 8b6e8091785663df18c5ac197eed3c679b2a15d5..73d2cceb3e85f9d84bf2404d7d9cbbfc204598d0 100644 (file)
@@ -24,6 +24,7 @@ impl<ECS: EcdsaChannelSigner> ChannelSignerType<ECS>{
                }
        }
 
+       #[allow(unused)]
        pub(crate) fn as_mut_ecdsa(&mut self) -> Option<&mut ECS> {
                match self {
                        ChannelSignerType::Ecdsa(ecs) => Some(ecs)
index b912221fac372da5247879ff62fd7d506d655a6b..4bcaca57f81ca61cd2af4d499d6ffbfa4f1b51e0 100644 (file)
@@ -13,7 +13,7 @@ use crate::chain::chaininterface;
 use crate::chain::chaininterface::ConfirmationTarget;
 use crate::chain::chaininterface::FEERATE_FLOOR_SATS_PER_KW;
 use crate::chain::chainmonitor;
-use crate::chain::chainmonitor::MonitorUpdateId;
+use crate::chain::chainmonitor::{MonitorUpdateId, UpdateOrigin};
 use crate::chain::channelmonitor;
 use crate::chain::channelmonitor::MonitorEvent;
 use crate::chain::transaction::OutPoint;
@@ -428,7 +428,8 @@ impl<Signer: sign::WriteableEcdsaChannelSigner> chainmonitor::Persist<Signer> fo
                if let Some(update_ret) = self.update_rets.lock().unwrap().pop_front() {
                        ret = update_ret;
                }
-               if update.is_none() {
+               let is_chain_sync = if let UpdateOrigin::ChainSync(_) = update_id.contents { true } else { false };
+               if is_chain_sync {
                        self.chain_sync_monitor_persistences.lock().unwrap().entry(funding_txo).or_insert(HashSet::new()).insert(update_id);
                } else {
                        self.offchain_monitor_updates.lock().unwrap().entry(funding_txo).or_insert(HashSet::new()).insert(update_id);