Add counterparty_node_id & channel_capacity to ChannelClosed
authorVladimir Fomene <vladimirfomene@gmail.com>
Fri, 30 Jun 2023 10:03:26 +0000 (13:03 +0300)
committerVladimir Fomene <vladimirfomene@gmail.com>
Tue, 8 Aug 2023 10:58:40 +0000 (13:58 +0300)
The current ChannelClosed event does not let
you know the counterparty during a channel close
event. This change adds the counterparty_node_id
and the channel_capacity to the ChannelClosed event.
This helps users to have more context during a
channel close event. Solves #2343

lightning/src/events/mod.rs
lightning/src/ln/channelmanager.rs

index d08e563cbf6195e4566bae0691b783e947033156..01a3fef6733cfb3a7f26b0f719b5450f9d1ad468 100644 (file)
@@ -755,7 +755,15 @@ pub enum Event {
                /// [`UserConfig::manually_accept_inbound_channels`]: crate::util::config::UserConfig::manually_accept_inbound_channels
                user_channel_id: u128,
                /// The reason the channel was closed.
-               reason: ClosureReason
+               reason: ClosureReason,
+               /// Counterparty in the closed channel. 
+               /// 
+               /// This field will be `None` for objects serialized prior to LDK 0.0.117.
+               counterparty_node_id: Option<PublicKey>,
+               /// Channel capacity of the closing channel (sats). 
+               /// 
+               /// This field will be `None` for objects serialized prior to LDK 0.0.117.
+               channel_capacity_sats: Option<u64>,
        },
        /// Used to indicate to the user that they can abandon the funding transaction and recycle the
        /// inputs for another purpose.
@@ -956,7 +964,9 @@ impl Writeable for Event {
                                        (5, outbound_amount_forwarded_msat, option),
                                });
                        },
-                       &Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason } => {
+                       &Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason, 
+                               ref counterparty_node_id, ref channel_capacity_sats 
+                       } => {
                                9u8.write(writer)?;
                                // `user_channel_id` used to be a single u64 value. In order to remain backwards
                                // compatible with versions prior to 0.0.113, the u128 is serialized as two
@@ -968,6 +978,8 @@ impl Writeable for Event {
                                        (1, user_channel_id_low, required),
                                        (2, reason, required),
                                        (3, user_channel_id_high, required),
+                                       (5, counterparty_node_id, option),
+                                       (7, channel_capacity_sats, option),
                                });
                        },
                        &Event::DiscardFunding { ref channel_id, ref transaction } => {
@@ -1252,11 +1264,15 @@ impl MaybeReadable for Event {
                                        let mut reason = UpgradableRequired(None);
                                        let mut user_channel_id_low_opt: Option<u64> = None;
                                        let mut user_channel_id_high_opt: Option<u64> = None;
+                                       let mut counterparty_node_id = None;
+                                       let mut channel_capacity_sats = None;
                                        read_tlv_fields!(reader, {
                                                (0, channel_id, required),
                                                (1, user_channel_id_low_opt, option),
                                                (2, reason, upgradable_required),
                                                (3, user_channel_id_high_opt, option),
+                                               (5, counterparty_node_id, option),
+                                               (7, channel_capacity_sats, option),
                                        });
 
                                        // `user_channel_id` used to be a single u64 value. In order to remain
@@ -1265,7 +1281,8 @@ impl MaybeReadable for Event {
                                        let user_channel_id = (user_channel_id_low_opt.unwrap_or(0) as u128) +
                                                ((user_channel_id_high_opt.unwrap_or(0) as u128) << 64);
 
-                                       Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required) }))
+                                       Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required),
+                                               counterparty_node_id, channel_capacity_sats }))
                                };
                                f()
                        },
index 6cd4799ff3cf77063e5b313e8a6d6a70631373a7..000a70594b3ad40e4ff01da91da81c394ff82695 100644 (file)
@@ -376,6 +376,7 @@ struct MsgHandleErrInternal {
        err: msgs::LightningError,
        chan_id: Option<([u8; 32], u128)>, // If Some a channel of ours has been closed
        shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
+       channel_capacity: Option<u64>,
 }
 impl MsgHandleErrInternal {
        #[inline]
@@ -392,14 +393,15 @@ impl MsgHandleErrInternal {
                        },
                        chan_id: None,
                        shutdown_finish: None,
+                       channel_capacity: None,
                }
        }
        #[inline]
        fn from_no_close(err: msgs::LightningError) -> Self {
-               Self { err, chan_id: None, shutdown_finish: None }
+               Self { err, chan_id: None, shutdown_finish: None, channel_capacity: None }
        }
        #[inline]
-       fn from_finish_shutdown(err: String, channel_id: [u8; 32], user_channel_id: u128, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
+       fn from_finish_shutdown(err: String, channel_id: [u8; 32], user_channel_id: u128, shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>, channel_capacity: u64) -> Self {
                Self {
                        err: LightningError {
                                err: err.clone(),
@@ -412,6 +414,7 @@ impl MsgHandleErrInternal {
                        },
                        chan_id: Some((channel_id, user_channel_id)),
                        shutdown_finish: Some((shutdown_res, channel_update)),
+                       channel_capacity: Some(channel_capacity)
                }
        }
        #[inline]
@@ -444,6 +447,7 @@ impl MsgHandleErrInternal {
                        },
                        chan_id: None,
                        shutdown_finish: None,
+                       channel_capacity: None,
                }
        }
 }
@@ -1659,7 +1663,7 @@ macro_rules! handle_error {
 
                match $internal {
                        Ok(msg) => Ok(msg),
-                       Err(MsgHandleErrInternal { err, chan_id, shutdown_finish }) => {
+                       Err(MsgHandleErrInternal { err, chan_id, shutdown_finish, channel_capacity }) => {
                                let mut msg_events = Vec::with_capacity(2);
 
                                if let Some((shutdown_res, update_option)) = shutdown_finish {
@@ -1672,7 +1676,9 @@ macro_rules! handle_error {
                                        if let Some((channel_id, user_channel_id)) = chan_id {
                                                $self.pending_events.lock().unwrap().push_back((events::Event::ChannelClosed {
                                                        channel_id, user_channel_id,
-                                                       reason: ClosureReason::ProcessingError { err: err.err.clone() }
+                                                       reason: ClosureReason::ProcessingError { err: err.err.clone() },
+                                                       counterparty_node_id: Some($counterparty_node_id),
+                                                       channel_capacity_sats: channel_capacity,
                                                }, None));
                                        }
                                }
@@ -1745,7 +1751,7 @@ macro_rules! convert_chan_err {
                                update_maps_on_chan_removal!($self, &$channel.context);
                                let shutdown_res = $channel.context.force_shutdown(true);
                                (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel.context.get_user_id(),
-                                       shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok()))
+                                       shutdown_res, $self.get_channel_update_for_broadcast(&$channel).ok(), $channel.context.get_value_satoshis()))
                        },
                }
        };
@@ -1758,7 +1764,7 @@ macro_rules! convert_chan_err {
                                update_maps_on_chan_removal!($self, &$channel_context);
                                let shutdown_res = $channel_context.force_shutdown(false);
                                (true, MsgHandleErrInternal::from_finish_shutdown(msg, *$channel_id, $channel_context.get_user_id(),
-                                       shutdown_res, None))
+                                       shutdown_res, None, $channel_context.get_value_satoshis()))
                        },
                }
        }
@@ -1937,7 +1943,7 @@ macro_rules! handle_new_monitor_update {
                                let res = Err(MsgHandleErrInternal::from_finish_shutdown(
                                        "ChannelMonitor storage failure".to_owned(), $chan.context.channel_id(),
                                        $chan.context.get_user_id(), $chan.context.force_shutdown(false),
-                                       $self.get_channel_update_for_broadcast(&$chan).ok()));
+                                       $self.get_channel_update_for_broadcast(&$chan).ok(), $chan.context.get_value_satoshis()));
                                $remove;
                                res
                        },
@@ -2371,7 +2377,9 @@ where
                pending_events_lock.push_back((events::Event::ChannelClosed {
                        channel_id: context.channel_id(),
                        user_channel_id: context.get_user_id(),
-                       reason: closure_reason
+                       reason: closure_reason,
+                       counterparty_node_id: Some(context.get_counterparty_node_id()),
+                       channel_capacity_sats: Some(context.get_value_satoshis()),
                }, None));
        }
 
@@ -3371,7 +3379,8 @@ where
                                                let channel_id = chan.context.channel_id();
                                                let user_id = chan.context.get_user_id();
                                                let shutdown_res = chan.context.force_shutdown(false);
-                                               (chan, MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, user_id, shutdown_res, None))
+                                               let channel_capacity = chan.context.get_value_satoshis();
+                                               (chan, MsgHandleErrInternal::from_finish_shutdown(msg, channel_id, user_id, shutdown_res, None, channel_capacity))
                                        } else { unreachable!(); });
                                match funding_res {
                                        Ok((chan, funding_msg)) => (chan, funding_msg),
@@ -5408,7 +5417,7 @@ where
                                                        let user_id = inbound_chan.context.get_user_id();
                                                        let shutdown_res = inbound_chan.context.force_shutdown(false);
                                                        return Err(MsgHandleErrInternal::from_finish_shutdown(format!("{}", err),
-                                                               msg.temporary_channel_id, user_id, shutdown_res, None));
+                                                               msg.temporary_channel_id, user_id, shutdown_res, None, inbound_chan.context.get_value_satoshis()));
                                                },
                                        }
                                },
@@ -8356,7 +8365,9 @@ where
                                        channel_closures.push_back((events::Event::ChannelClosed {
                                                channel_id: channel.context.channel_id(),
                                                user_channel_id: channel.context.get_user_id(),
-                                               reason: ClosureReason::OutdatedChannelManager
+                                               reason: ClosureReason::OutdatedChannelManager,
+                                               counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
+                                               channel_capacity_sats: Some(channel.context.get_value_satoshis()),
                                        }, None));
                                        for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
                                                let mut found_htlc = false;
@@ -8408,6 +8419,8 @@ where
                                        channel_id: channel.context.channel_id(),
                                        user_channel_id: channel.context.get_user_id(),
                                        reason: ClosureReason::DisconnectedPeer,
+                                       counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
+                                       channel_capacity_sats: Some(channel.context.get_value_satoshis()),
                                }, None));
                        } else {
                                log_error!(args.logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", log_bytes!(channel.context.channel_id()));