Skip to content

Commit

Permalink
Add last_local_balance_msats field
Browse files Browse the repository at this point in the history
Introduced to show what your local balance is, when channel is closed, that way you can see how many sats you lost to rounding.
The balance may be increased by outbound pending HTLCs and transaction fees. Check [ChainMonitor::get_claimable_balances] to get more accurate balance which contains fee information.
  • Loading branch information
Mirebella committed Sep 14, 2024
1 parent f7cc40e commit 2557e6b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
13 changes: 11 additions & 2 deletions lightning/src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1276,6 +1276,11 @@ 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<transaction::OutPoint>,
/// Local balance in msats when a channel is closed.
///
/// May overstate balance by pending outbound HTLCs and transaction fees.
/// For more accurate balances including fee information see [ChainMonitor::get_claimable_balances]
last_local_balance_msats: Option<u64>,
},
/// Used to indicate to the user that they can abandon the funding transaction and recycle the
/// inputs for another purpose.
Expand Down Expand Up @@ -1555,7 +1560,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
Expand All @@ -1571,6 +1577,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 funding_info } => {
Expand Down Expand Up @@ -1939,6 +1946,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),
Expand All @@ -1947,6 +1955,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
Expand All @@ -1956,7 +1965,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()
},
Expand Down
5 changes: 5 additions & 0 deletions lightning/src/ln/channel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,6 +937,7 @@ pub(crate) struct ShutdownResult {
pub(crate) is_manual_broadcast: bool,
pub(crate) unbroadcasted_funding_tx: Option<Transaction>,
pub(crate) channel_funding_txo: Option<OutPoint>,
pub(crate) last_local_balance_msats: u64,
}

/// Tracks the transaction number, along with current and next commitment points.
Expand Down Expand Up @@ -2066,6 +2067,8 @@ impl<SP: Deref> ChannelContext<SP> 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
Expand Down Expand Up @@ -3532,6 +3535,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
unbroadcasted_funding_tx,
is_manual_broadcast: self.is_manual_broadcast,
channel_funding_txo: self.get_funding_txo(),
last_local_balance_msats: self.value_to_self_msat,
}
}

Expand Down Expand Up @@ -6180,6 +6184,7 @@ impl<SP: Deref> Channel<SP> where
unbroadcasted_funding_tx: self.context.unbroadcasted_funding(),
is_manual_broadcast: self.context.is_manual_broadcast,
channel_funding_txo: self.context.get_funding_txo(),
last_local_balance_msats: self.context.value_to_self_msat,
}
}

Expand Down
3 changes: 3 additions & 0 deletions lightning/src/ln/channelmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3574,6 +3574,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 {
Expand Down Expand Up @@ -12039,6 +12040,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;
Expand Down Expand Up @@ -12095,6 +12097,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());
Expand Down

0 comments on commit 2557e6b

Please sign in to comment.