Skip to content

Commit 3cd6ada

Browse files
MirebellaTheBlueMatt
authored andcommitted
Add Event::ChannelClosed::last_local_balance_msats
Users commonly want to know what their balance was when a channel was closed, which this provides in a somewhat simplified manner. It does not consider pending HTLCs and will always overstate our balance by transaction fees.
1 parent f7cc40e commit 3cd6ada

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

lightning/src/events/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1276,6 +1276,15 @@ pub enum Event {
12761276
/// status of the closing tx.
12771277
/// Note that for instances serialized in v0.0.119 or prior this will be missing (None).
12781278
channel_funding_txo: Option<transaction::OutPoint>,
1279+
/// An upper bound on the our last local balance in msats before the channel was closed.
1280+
///
1281+
/// Will overstate our balance as it ignores pending outbound HTLCs and transaction fees.
1282+
///
1283+
/// For more accurate balances including fee information see
1284+
/// [`ChainMonitor::get_claimable_balances`].
1285+
///
1286+
/// This field will be `None` only for objects serialized prior to LDK 0.1.
1287+
last_local_balance_msats: Option<u64>,
12791288
},
12801289
/// Used to indicate to the user that they can abandon the funding transaction and recycle the
12811290
/// inputs for another purpose.
@@ -1555,7 +1564,8 @@ impl Writeable for Event {
15551564
});
15561565
},
15571566
&Event::ChannelClosed { ref channel_id, ref user_channel_id, ref reason,
1558-
ref counterparty_node_id, ref channel_capacity_sats, ref channel_funding_txo
1567+
ref counterparty_node_id, ref channel_capacity_sats, ref channel_funding_txo,
1568+
ref last_local_balance_msats,
15591569
} => {
15601570
9u8.write(writer)?;
15611571
// `user_channel_id` used to be a single u64 value. In order to remain backwards
@@ -1571,6 +1581,7 @@ impl Writeable for Event {
15711581
(5, counterparty_node_id, option),
15721582
(7, channel_capacity_sats, option),
15731583
(9, channel_funding_txo, option),
1584+
(11, last_local_balance_msats, option)
15741585
});
15751586
},
15761587
&Event::DiscardFunding { ref channel_id, ref funding_info } => {
@@ -1939,6 +1950,7 @@ impl MaybeReadable for Event {
19391950
let mut counterparty_node_id = None;
19401951
let mut channel_capacity_sats = None;
19411952
let mut channel_funding_txo = None;
1953+
let mut last_local_balance_msats = None;
19421954
read_tlv_fields!(reader, {
19431955
(0, channel_id, required),
19441956
(1, user_channel_id_low_opt, option),
@@ -1947,6 +1959,7 @@ impl MaybeReadable for Event {
19471959
(5, counterparty_node_id, option),
19481960
(7, channel_capacity_sats, option),
19491961
(9, channel_funding_txo, option),
1962+
(11, last_local_balance_msats, option)
19501963
});
19511964

19521965
// `user_channel_id` used to be a single u64 value. In order to remain
@@ -1956,7 +1969,7 @@ impl MaybeReadable for Event {
19561969
((user_channel_id_high_opt.unwrap_or(0) as u128) << 64);
19571970

19581971
Ok(Some(Event::ChannelClosed { channel_id, user_channel_id, reason: _init_tlv_based_struct_field!(reason, upgradable_required),
1959-
counterparty_node_id, channel_capacity_sats, channel_funding_txo }))
1972+
counterparty_node_id, channel_capacity_sats, channel_funding_txo, last_local_balance_msats}))
19601973
};
19611974
f()
19621975
},

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_msats: 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_msats: 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_msats: self.context.value_to_self_msat,
61836188
}
61846189
}
61856190

lightning/src/ln/channelmanager.rs

+3
Original file line numberDiff line numberDiff line change
@@ -3574,6 +3574,7 @@ where
35743574
counterparty_node_id: Some(shutdown_res.counterparty_node_id),
35753575
channel_capacity_sats: Some(shutdown_res.channel_capacity_satoshis),
35763576
channel_funding_txo: shutdown_res.channel_funding_txo,
3577+
last_local_balance_msats: Some(shutdown_res.last_local_balance_msats),
35773578
}, None));
35783579

35793580
if let Some(transaction) = shutdown_res.unbroadcasted_funding_tx {
@@ -12039,6 +12040,7 @@ where
1203912040
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
1204012041
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
1204112042
channel_funding_txo: channel.context.get_funding_txo(),
12043+
last_local_balance_msats: Some(channel.context.get_value_to_self_msat()),
1204212044
}, None));
1204312045
for (channel_htlc_source, payment_hash) in channel.inflight_htlc_sources() {
1204412046
let mut found_htlc = false;
@@ -12095,6 +12097,7 @@ where
1209512097
counterparty_node_id: Some(channel.context.get_counterparty_node_id()),
1209612098
channel_capacity_sats: Some(channel.context.get_value_satoshis()),
1209712099
channel_funding_txo: channel.context.get_funding_txo(),
12100+
last_local_balance_msats: Some(channel.context.get_value_to_self_msat()),
1209812101
}, None));
1209912102
} else {
1210012103
log_error!(logger, "Missing ChannelMonitor for channel {} needed by ChannelManager.", &channel.context.channel_id());

0 commit comments

Comments
 (0)