Updated `ClosureReason::HolderForceClosed` with broadcasted txn.
[rust-lightning] / lightning / src / events / mod.rs
index a4bf1501b194376ac8ae2d1894084337d5be2fa0..dfe60a6e45f0678915f3f62698e69c9c0e0a615a 100644 (file)
@@ -38,6 +38,7 @@ use bitcoin::blockdata::script::ScriptBuf;
 use bitcoin::hashes::Hash;
 use bitcoin::hashes::sha256::Hash as Sha256;
 use bitcoin::secp256k1::PublicKey;
+use bitcoin::transaction::Version;
 use crate::io;
 use core::time::Duration;
 use core::ops::Deref;
@@ -277,7 +278,21 @@ pub enum ClosureReason {
        /// Closure generated from [`ChannelManager::force_close_channel`], called by the user.
        ///
        /// [`ChannelManager::force_close_channel`]: crate::ln::channelmanager::ChannelManager::force_close_channel.
-       HolderForceClosed,
+       HolderForceClosed {
+               /// Whether or not the latest transaction was broadcasted when the channel was force
+               /// closed.
+               ///
+               /// Channels closed using [`ChannelManager::force_close_broadcasting_latest_txn`] will have
+               /// this field set to true, whereas channels closed using [`ChannelManager::force_close_without_broadcasting_txn`]
+               /// or force-closed prior to being funded will have this field set to false.
+               ///
+               /// This will be `None` for objects generated or written by LDK 0.0.123 and
+               /// earlier.
+               ///
+               /// [`ChannelManager::force_close_broadcasting_latest_txn`]: crate::ln::channelmanager::ChannelManager::force_close_broadcasting_latest_txn.
+               /// [`ChannelManager::force_close_without_broadcasting_txn`]: crate::ln::channelmanager::ChannelManager::force_close_without_broadcasting_txn.
+               broadcasted_latest_txn: Option<bool>
+       },
        /// The channel was closed after negotiating a cooperative close and we've now broadcasted
        /// the cooperative close transaction. Note the shutdown may have been initiated by us.
        ///
@@ -339,7 +354,14 @@ impl core::fmt::Display for ClosureReason {
                        ClosureReason::CounterpartyForceClosed { peer_msg } => {
                                f.write_fmt(format_args!("counterparty force-closed with message: {}", peer_msg))
                        },
-                       ClosureReason::HolderForceClosed => f.write_str("user force-closed the channel"),
+                       ClosureReason::HolderForceClosed { broadcasted_latest_txn } => {
+                               f.write_str("user force-closed the channel")?;
+                               if let Some(brodcasted) = broadcasted_latest_txn {
+                                       write!(f, " and {} the latest transaction", if *brodcasted { "broadcasted" } else { "did not broadcast" })
+                               } else {
+                                       Ok(())
+                               }
+                       },
                        ClosureReason::LegacyCooperativeClosure => f.write_str("the channel was cooperatively closed"),
                        ClosureReason::CounterpartyInitiatedCooperativeClosure => f.write_str("the channel was cooperatively closed by our peer"),
                        ClosureReason::LocallyInitiatedCooperativeClosure => f.write_str("the channel was cooperatively closed by us"),
@@ -361,7 +383,7 @@ impl core::fmt::Display for ClosureReason {
 impl_writeable_tlv_based_enum_upgradable!(ClosureReason,
        (0, CounterpartyForceClosed) => { (1, peer_msg, required) },
        (1, FundingTimedOut) => {},
-       (2, HolderForceClosed) => {},
+       (2, HolderForceClosed) => { (1, broadcasted_latest_txn, option) },
        (6, CommitmentTxConfirmed) => {},
        (4, LegacyCooperativeClosure) => {},
        (8, ProcessingError) => { (1, err, required) },
@@ -653,6 +675,11 @@ pub enum Event {
                /// The sender-intended sum total of all the MPP parts. This will be `None` for events
                /// serialized prior to LDK version 0.0.117.
                sender_intended_total_msat: Option<u64>,
+               /// The fields in the onion which were received with each HTLC. Only fields which were
+               /// identical in each HTLC involved in the payment will be included here.
+               ///
+               /// Payments received on LDK versions prior to 0.0.124 will have this field unset.
+               onion_fields: Option<RecipientOnionFields>,
        },
        /// Indicates that a peer connection with a node is needed in order to send an [`OnionMessage`].
        ///
@@ -1348,7 +1375,7 @@ impl Writeable for Event {
                                // We never write the OpenChannelRequest events as, upon disconnection, peers
                                // drop any channels which have not yet exchanged funding_signed.
                        },
-                       &Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id, ref htlcs, ref sender_intended_total_msat } => {
+                       &Event::PaymentClaimed { ref payment_hash, ref amount_msat, ref purpose, ref receiver_node_id, ref htlcs, ref sender_intended_total_msat, ref onion_fields } => {
                                19u8.write(writer)?;
                                write_tlv_fields!(writer, {
                                        (0, payment_hash, required),
@@ -1357,6 +1384,7 @@ impl Writeable for Event {
                                        (4, amount_msat, required),
                                        (5, *htlcs, optional_vec),
                                        (7, sender_intended_total_msat, option),
+                                       (9, onion_fields, option),
                                });
                        },
                        &Event::ProbeSuccessful { ref payment_id, ref payment_hash, ref path } => {
@@ -1664,7 +1692,7 @@ impl MaybeReadable for Event {
                        11u8 => {
                                let mut f = || {
                                        let mut channel_id = ChannelId::new_zero();
-                                       let mut transaction = Transaction{ version: 2, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() };
+                                       let mut transaction = Transaction{ version: Version::TWO, lock_time: LockTime::ZERO, input: Vec::new(), output: Vec::new() };
                                        read_tlv_fields!(reader, {
                                                (0, channel_id, required),
                                                (2, transaction, required),
@@ -1719,6 +1747,7 @@ impl MaybeReadable for Event {
                                        let mut receiver_node_id = None;
                                        let mut htlcs: Option<Vec<ClaimedHTLC>> = Some(vec![]);
                                        let mut sender_intended_total_msat: Option<u64> = None;
+                                       let mut onion_fields = None;
                                        read_tlv_fields!(reader, {
                                                (0, payment_hash, required),
                                                (1, receiver_node_id, option),
@@ -1726,6 +1755,7 @@ impl MaybeReadable for Event {
                                                (4, amount_msat, required),
                                                (5, htlcs, optional_vec),
                                                (7, sender_intended_total_msat, option),
+                                               (9, onion_fields, option),
                                        });
                                        Ok(Some(Event::PaymentClaimed {
                                                receiver_node_id,
@@ -1734,6 +1764,7 @@ impl MaybeReadable for Event {
                                                amount_msat,
                                                htlcs: htlcs.unwrap_or(vec![]),
                                                sender_intended_total_msat,
+                                               onion_fields,
                                        }))
                                };
                                f()