From 64244361c96650cab20240d5daef9945fb964095 Mon Sep 17 00:00:00 2001 From: Mirebella <166803555+Mirebella@users.noreply.github.com> Date: Sun, 11 Aug 2024 17:50:45 +0200 Subject: [PATCH] Add local balance field --- lightning/src/events/mod.rs | 10 ++++++++-- lightning/src/ln/channel.rs | 6 ++++++ lightning/src/ln/channelmanager.rs | 3 +++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lightning/src/events/mod.rs b/lightning/src/events/mod.rs index 3972ea470da..36c1c06374f 100644 --- a/lightning/src/events/mod.rs +++ b/lightning/src/events/mod.rs @@ -1237,6 +1237,8 @@ pub enum Event { /// status of the closing tx. /// Note that for instances serialized in v0.0.119 or prior this will be missing (None). channel_funding_txo: Option, + /// Local balance in msats when a channel was force-closed. + last_local_balance_msats: Option, }, /// Used to indicate to the user that they can abandon the funding transaction and recycle the /// inputs for another purpose. @@ -1516,7 +1518,8 @@ impl Writeable for Event { }); }, &Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason, - ref counterparty_node_id, ref channel_capacity_sats, ref channel_funding_txo + ref counterparty_node_id, ref channel_capacity_sats, ref channel_funding_txo, + ref last_local_balance_msats } => { 9u8.write(writer)?; // `user_channel_id` used to be a single u64 value. In order to remain backwards @@ -1532,6 +1535,7 @@ impl Writeable for Event { (5, counterparty_node_id, option), (7, channel_capacity_sats, option), (9, channel_funding_txo, option), + (11, last_local_balance_msats, option) }); }, &Event::DiscardFunding { ref channel_id, ref transaction } => { @@ -1875,6 +1879,7 @@ impl MaybeReadable for Event { let mut counterparty_node_id = None; let mut channel_capacity_sats = None; let mut channel_funding_txo = None; + let mut last_local_balance_msats = None; read_tlv_fields!(reader, { (0, channel_id, required), (1, user_channel_id_low_opt, option), @@ -1883,6 +1888,7 @@ impl MaybeReadable for Event { (5, counterparty_node_id, option), (7, channel_capacity_sats, option), (9, channel_funding_txo, option), + (11, last_local_balance_msats, option) }); // `user_channel_id` used to be a single u64 value. In order to remain @@ -1892,7 +1898,7 @@ impl MaybeReadable for Event { ((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), - counterparty_node_id, channel_capacity_sats, channel_funding_txo })) + counterparty_node_id, channel_capacity_sats, channel_funding_txo, last_local_balance_msats})) }; f() }, diff --git a/lightning/src/ln/channel.rs b/lightning/src/ln/channel.rs index 9abe997890f..05a9449d5e3 100644 --- a/lightning/src/ln/channel.rs +++ b/lightning/src/ln/channel.rs @@ -939,6 +939,7 @@ pub(crate) struct ShutdownResult { pub(crate) counterparty_node_id: PublicKey, pub(crate) unbroadcasted_funding_tx: Option, pub(crate) channel_funding_txo: Option, + pub(crate) last_local_balance_msats: u64, } /// Tracks the transaction number, along with current and next commitment points. @@ -2058,6 +2059,8 @@ impl ChannelContext where SP::Target: SignerProvider { }) } + pub(crate) fn get_value_to_self_msat(&self) -> u64 {self.value_to_self_msat} + /// Allowed in any state (including after shutdown) pub fn get_update_time_counter(&self) -> u32 { self.update_time_counter @@ -3528,6 +3531,7 @@ impl ChannelContext where SP::Target: SignerProvider { counterparty_node_id: self.counterparty_node_id, unbroadcasted_funding_tx, channel_funding_txo: self.get_funding_txo(), + last_local_balance_msats: self.value_to_self_msat, } } @@ -6218,6 +6222,7 @@ impl Channel where counterparty_node_id: self.context.counterparty_node_id, unbroadcasted_funding_tx: self.context.unbroadcasted_funding(), channel_funding_txo: self.context.get_funding_txo(), + last_local_balance_msats: self.context.value_to_self_msat, }; let tx = self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig); self.context.channel_state = ChannelState::ShutdownComplete; @@ -6253,6 +6258,7 @@ impl Channel where counterparty_node_id: self.context.counterparty_node_id, unbroadcasted_funding_tx: self.context.unbroadcasted_funding(), channel_funding_txo: self.context.get_funding_txo(), + last_local_balance_msats: self.context.value_to_self_msat, }; self.context.channel_state = ChannelState::ShutdownComplete; self.context.update_time_counter += 1; diff --git a/lightning/src/ln/channelmanager.rs b/lightning/src/ln/channelmanager.rs index 103c69f0b6c..f31666716b8 100644 --- a/lightning/src/ln/channelmanager.rs +++ b/lightning/src/ln/channelmanager.rs @@ -3518,6 +3518,7 @@ where counterparty_node_id: Some(shutdown_res.counterparty_node_id), channel_capacity_sats: Some(shutdown_res.channel_capacity_satoshis), channel_funding_txo: shutdown_res.channel_funding_txo, + last_local_balance_msats: Some(shutdown_res.last_local_balance_msats), }, None)); if let Some(transaction) = shutdown_res.unbroadcasted_funding_tx { @@ -11879,6 +11880,7 @@ where counterparty_node_id: Some(channel.context.get_counterparty_node_id()), channel_capacity_sats: Some(channel.context.get_value_satoshis()), channel_funding_txo: channel.context.get_funding_txo(), + last_local_balance_msats: Some(channel.context.get_value_to_self_msat()), }, None)); for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() { let mut found_htlc = false; @@ -11935,6 +11937,7 @@ where counterparty_node_id: Some(channel.context.get_counterparty_node_id()), channel_capacity_sats: Some(channel.context.get_value_satoshis()), channel_funding_txo: channel.context.get_funding_txo(), + last_local_balance_msats: Some(channel.context.get_value_to_self_msat()), }, None)); } else { log_error!(logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", &channel.context.channel_id());