Skip to content

Commit f94bf98

Browse files
authored
Merge pull request #3235 from Mirebella/add-local-balance-msats
Add `last_local_balance_msats` field
2 parents e4ec917 + 5d48d58 commit f94bf98

File tree

3 files changed

+28
-3
lines changed

3 files changed

+28
-3
lines changed

lightning/src/events/mod.rs

+20-3
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,17 @@ pub enum Event {
12931293
/// status of the closing tx.
12941294
/// Note that for instances serialized in v0.0.119 or prior this will be missing (None).
12951295
channel_funding_txo: Option<transaction::OutPoint>,
1296+
/// An upper bound on the our last local balance in msats before the channel was closed.
1297+
///
1298+
/// Will overstate our balance as it ignores pending outbound HTLCs and transaction fees.
1299+
///
1300+
/// For more accurate balances including fee information see
1301+
/// [`ChainMonitor::get_claimable_balances`].
1302+
///
1303+
/// This field will be `None` only for objects serialized prior to LDK 0.1.
1304+
///
1305+
/// [`ChainMonitor::get_claimable_balances`]: crate::chain::chainmonitor::ChainMonitor::get_claimable_balances
1306+
last_local_balance_msat: Option<u64>,
12961307
},
12971308
/// Used to indicate to the user that they can abandon the funding transaction and recycle the
12981309
/// inputs for another purpose.
@@ -1573,7 +1584,8 @@ impl Writeable for Event {
15731584
});
15741585
},
15751586
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason,
1576-
ref counterparty_node_id, ref channel_capacity_sats, ref channel_funding_txo
1587+
ref counterparty_node_id, ref channel_capacity_sats, ref channel_funding_txo,
1588+
ref last_local_balance_msat,
15771589
} => {
15781590
9u8.write(writer)?;
15791591
// `user_channel_id` used to be a single u64 value. In order to remain backwards
@@ -1589,6 +1601,7 @@ impl Writeable for Event {
15891601
(5, counterparty_node_id, option),
15901602
(7, channel_capacity_sats, option),
15911603
(9, channel_funding_txo, option),
1604+
(11, last_local_balance_msat, option)
15921605
});
15931606
},
15941607
&Event::DiscardFunding { ref channel_id, ref funding_info } => {
@@ -1964,6 +1977,7 @@ impl MaybeReadable for Event {
19641977
let mut counterparty_node_id = None;
19651978
let mut channel_capacity_sats = None;
19661979
let mut channel_funding_txo = None;
1980+
let mut last_local_balance_msat = None;
19671981
read_tlv_fields!(reader, {
19681982
(0, channel_id, required),
19691983
(1, user_channel_id_low_opt, option),
@@ -1972,6 +1986,7 @@ impl MaybeReadable for Event {
19721986
(5, counterparty_node_id, option),
19731987
(7, channel_capacity_sats, option),
19741988
(9, channel_funding_txo, option),
1989+
(11, last_local_balance_msat, option)
19751990
});
19761991

19771992
// `user_channel_id` used to be a single u64 value. In order to remain
@@ -1980,8 +1995,10 @@ impl MaybeReadable for Event {
19801995
let user_channel_id = (user_channel_id_low_opt.unwrap_or(0) as u128) +
19811996
((user_channel_id_high_opt.unwrap_or(0) as u128) << 64);
19821997

1983-
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required),
1984-
counterparty_node_id, channel_capacity_sats, channel_funding_txo }))
1998+
Ok(Some(Event::ChannelClosed {
1999+
channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required),
2000+
counterparty_node_id, channel_capacity_sats, channel_funding_txo, last_local_balance_msat,
2001+
}))
19852002
};
19862003
f()
19872004
},

lightning/src/ln/channel.rs

+5
Original file line numberDiff line numberDiff line change
@@ -937,6 +937,7 @@ pub(crate) struct ShutdownResult {
937937
pub(crate) is_manual_broadcast: bool,
938938
pub(crate) unbroadcasted_funding_tx: Option<Transaction>,
939939
pub(crate) channel_funding_txo: Option<OutPoint>,
940+
pub(crate) last_local_balance_msat: u64,
940941
}
941942

942943
/// Tracks the transaction number, along with current and next commitment points.
@@ -2066,6 +2067,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
20662067
})
20672068
}
20682069

2070+
pub(crate) fn get_value_to_self_msat(&self) -> u64 {self.value_to_self_msat}
2071+
20692072
/// Allowed in any state (including after shutdown)
20702073
pub fn get_update_time_counter(&self) -> u32 {
20712074
self.update_time_counter
@@ -3532,6 +3535,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
35323535
unbroadcasted_funding_tx,
35333536
is_manual_broadcast: self.is_manual_broadcast,
35343537
channel_funding_txo: self.get_funding_txo(),
3538+
last_local_balance_msat: self.value_to_self_msat,
35353539
}
35363540
}
35373541

@@ -6180,6 +6184,7 @@ impl<SP: Deref> Channel<SP> where
61806184
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
61816185
is_manual_broadcast: self.context.is_manual_broadcast,
61826186
channel_funding_txo: self.context.get_funding_txo(),
6187+
last_local_balance_msat: self.context.value_to_self_msat,
61836188
}
61846189
}
61856190

lightning/src/ln/channelmanager.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3684,6 +3684,7 @@ where
36843684
counterparty_node_id: Some(shutdown_res.counterparty_node_id),
36853685
channel_capacity_sats: Some(shutdown_res.channel_capacity_satoshis),
36863686
channel_funding_txo: shutdown_res.channel_funding_txo,
3687+
last_local_balance_msat: Some(shutdown_res.last_local_balance_msat),
36873688
}, None));
36883689

36893690
if let Some(transaction) = shutdown_res.unbroadcasted_funding_tx {
@@ -12294,6 +12295,7 @@ where
1229412295
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
1229512296
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
1229612297
channel_funding_txo: channel.context.get_funding_txo(),
12298+
last_local_balance_msat: Some(channel.context.get_value_to_self_msat()),
1229712299
}, None));
1229812300
for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
1229912301
let mut found_htlc = false;
@@ -12350,6 +12352,7 @@ where
1235012352
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
1235112353
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
1235212354
channel_funding_txo: channel.context.get_funding_txo(),
12355+
last_local_balance_msat: Some(channel.context.get_value_to_self_msat()),
1235312356
}, None));
1235412357
} else {
1235512358
log_error!(logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", &channel.context.channel_id());

0 commit comments

Comments
 (0)