Skip to content

Commit bbfa15e

Browse files
authored
Merge pull request #3247 from dunxen/2024-08-deprecate-balancemsat
Deprecate AvailableBalances::balance_msat
2 parents 3605777 + 0a1adeb commit bbfa15e

File tree

5 files changed

+140
-34
lines changed

5 files changed

+140
-34
lines changed

lightning/src/chain/chainmonitor.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,8 +394,7 @@ where C::Target: chain::Filter,
394394
/// claims which are awaiting confirmation.
395395
///
396396
/// Includes the balances from each [`ChannelMonitor`] *except* those included in
397-
/// `ignored_channels`, allowing you to filter out balances from channels which are still open
398-
/// (and whose balance should likely be pulled from the [`ChannelDetails`]).
397+
/// `ignored_channels`.
399398
///
400399
/// See [`ChannelMonitor::get_claimable_balances`] for more details on the exact criteria for
401400
/// inclusion in the return value.

lightning/src/ln/channel.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ pub struct ChannelValueStat {
8080

8181
pub struct AvailableBalances {
8282
/// The amount that would go to us if we close the channel, ignoring any on-chain fees.
83+
#[deprecated(since = "0.0.124", note = "use [`ChainMonitor::get_claimable_balances`] instead")]
8384
pub balance_msat: u64,
8485
/// Total amount available for our counterparty to send to us.
8586
pub inbound_capacity_msat: u64,
@@ -3215,6 +3216,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
32153216
available_capacity_msat = 0;
32163217
}
32173218

3219+
#[allow(deprecated)] // TODO: Remove once balance_msat is removed.
32183220
AvailableBalances {
32193221
inbound_capacity_msat: cmp::max(context.channel_value_satoshis as i64 * 1000
32203222
- context.value_to_self_msat as i64

lightning/src/ln/channel_state.rs

Lines changed: 134 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,14 @@ impl_writeable_tlv_based!(ChannelCounterparty, {
273273

274274
/// Details of a channel, as returned by [`ChannelManager::list_channels`] and [`ChannelManager::list_usable_channels`]
275275
///
276+
/// Balances of a channel are available through [`ChainMonitor::get_claimable_balances`] and
277+
/// [`ChannelMonitor::get_claimable_balances`], calculated with respect to the corresponding on-chain
278+
/// transactions.
279+
///
276280
/// [`ChannelManager::list_channels`]: crate::ln::channelmanager::ChannelManager::list_channels
277281
/// [`ChannelManager::list_usable_channels`]: crate::ln::channelmanager::ChannelManager::list_usable_channels
282+
/// [`ChainMonitor::get_claimable_balances`]: crate::chain::chainmonitor::ChainMonitor::get_claimable_balances
283+
/// [`ChannelMonitor::get_claimable_balances`]: crate::chain::channelmonitor::ChannelMonitor::get_claimable_balances
278284
#[derive(Clone, Debug, PartialEq)]
279285
pub struct ChannelDetails {
280286
/// The channel's ID (prior to funding transaction generation, this is a random 32 bytes,
@@ -363,6 +369,7 @@ pub struct ChannelDetails {
363369
/// This does not consider any on-chain fees.
364370
///
365371
/// See also [`ChannelDetails::outbound_capacity_msat`]
372+
#[deprecated(since = "0.0.124", note = "use [`ChainMonitor::get_claimable_balances`] instead")]
366373
pub balance_msat: u64,
367374
/// The available outbound capacity for sending HTLCs to the remote peer. This does not include
368375
/// any pending HTLCs which are not yet fully resolved (and, thus, whose balance is not
@@ -495,6 +502,7 @@ impl ChannelDetails {
495502
let balance = context.get_available_balances(fee_estimator);
496503
let (to_remote_reserve_satoshis, to_self_reserve_satoshis) =
497504
context.get_holder_counterparty_selected_channel_reserve_satoshis();
505+
#[allow(deprecated)] // TODO: Remove once balance_msat is removed.
498506
ChannelDetails {
499507
channel_id: context.channel_id(),
500508
counterparty: ChannelCounterparty {
@@ -561,38 +569,41 @@ impl Writeable for ChannelDetails {
561569
// versions prior to 0.0.113, the u128 is serialized as two separate u64 values.
562570
let user_channel_id_low = self.user_channel_id as u64;
563571
let user_channel_id_high_opt = Some((self.user_channel_id >> 64) as u64);
564-
write_tlv_fields!(writer, {
565-
(1, self.inbound_scid_alias, option),
566-
(2, self.channel_id, required),
567-
(3, self.channel_type, option),
568-
(4, self.counterparty, required),
569-
(5, self.outbound_scid_alias, option),
570-
(6, self.funding_txo, option),
571-
(7, self.config, option),
572-
(8, self.short_channel_id, option),
573-
(9, self.confirmations, option),
574-
(10, self.channel_value_satoshis, required),
575-
(12, self.unspendable_punishment_reserve, option),
576-
(14, user_channel_id_low, required),
577-
(16, self.balance_msat, required),
578-
(18, self.outbound_capacity_msat, required),
579-
(19, self.next_outbound_htlc_limit_msat, required),
580-
(20, self.inbound_capacity_msat, required),
581-
(21, self.next_outbound_htlc_minimum_msat, required),
582-
(22, self.confirmations_required, option),
583-
(24, self.force_close_spend_delay, option),
584-
(26, self.is_outbound, required),
585-
(28, self.is_channel_ready, required),
586-
(30, self.is_usable, required),
587-
(32, self.is_public, required),
588-
(33, self.inbound_htlc_minimum_msat, option),
589-
(35, self.inbound_htlc_maximum_msat, option),
590-
(37, user_channel_id_high_opt, option),
591-
(39, self.feerate_sat_per_1000_weight, option),
592-
(41, self.channel_shutdown_state, option),
593-
(43, self.pending_inbound_htlcs, optional_vec),
594-
(45, self.pending_outbound_htlcs, optional_vec),
595-
});
572+
#[allow(deprecated)] // TODO: Remove once balance_msat is removed.
573+
{
574+
write_tlv_fields!(writer, {
575+
(1, self.inbound_scid_alias, option),
576+
(2, self.channel_id, required),
577+
(3, self.channel_type, option),
578+
(4, self.counterparty, required),
579+
(5, self.outbound_scid_alias, option),
580+
(6, self.funding_txo, option),
581+
(7, self.config, option),
582+
(8, self.short_channel_id, option),
583+
(9, self.confirmations, option),
584+
(10, self.channel_value_satoshis, required),
585+
(12, self.unspendable_punishment_reserve, option),
586+
(14, user_channel_id_low, required),
587+
(16, self.balance_msat, required),
588+
(18, self.outbound_capacity_msat, required),
589+
(19, self.next_outbound_htlc_limit_msat, required),
590+
(20, self.inbound_capacity_msat, required),
591+
(21, self.next_outbound_htlc_minimum_msat, required),
592+
(22, self.confirmations_required, option),
593+
(24, self.force_close_spend_delay, option),
594+
(26, self.is_outbound, required),
595+
(28, self.is_channel_ready, required),
596+
(30, self.is_usable, required),
597+
(32, self.is_public, required),
598+
(33, self.inbound_htlc_minimum_msat, option),
599+
(35, self.inbound_htlc_maximum_msat, option),
600+
(37, user_channel_id_high_opt, option),
601+
(39, self.feerate_sat_per_1000_weight, option),
602+
(41, self.channel_shutdown_state, option),
603+
(43, self.pending_inbound_htlcs, optional_vec),
604+
(45, self.pending_outbound_htlcs, optional_vec),
605+
});
606+
}
596607
Ok(())
597608
}
598609
}
@@ -640,6 +651,7 @@ impl Readable for ChannelDetails {
640651
let user_channel_id = user_channel_id_low as u128
641652
+ ((user_channel_id_high_opt.unwrap_or(0 as u64) as u128) << 64);
642653

654+
#[allow(deprecated)] // TODO: Remove once balance_msat is removed.
643655
Ok(Self {
644656
inbound_scid_alias,
645657
channel_id: channel_id.0.unwrap(),
@@ -703,3 +715,93 @@ impl_writeable_tlv_based_enum!(ChannelShutdownState,
703715
(6, NegotiatingClosingFee) => {},
704716
(8, ShutdownComplete) => {},
705717
);
718+
719+
#[cfg(test)]
720+
mod tests {
721+
use bitcoin::{hashes::Hash as _, secp256k1::PublicKey};
722+
use lightning_types::features::Features;
723+
use types::payment::PaymentHash;
724+
725+
use crate::{
726+
chain::transaction::OutPoint,
727+
ln::{
728+
channel_state::{
729+
InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails,
730+
OutboundHTLCStateDetails,
731+
},
732+
types::ChannelId,
733+
},
734+
util::{
735+
config::ChannelConfig,
736+
ser::{Readable, Writeable},
737+
},
738+
};
739+
740+
use super::{ChannelCounterparty, ChannelDetails, ChannelShutdownState};
741+
742+
#[test]
743+
fn test_channel_details_serialization() {
744+
#[allow(deprecated)]
745+
let channel_details = ChannelDetails {
746+
channel_id: ChannelId::new_zero(),
747+
counterparty: ChannelCounterparty {
748+
features: Features::empty(),
749+
node_id: PublicKey::from_slice(&[2; 33]).unwrap(),
750+
unspendable_punishment_reserve: 1983,
751+
forwarding_info: None,
752+
outbound_htlc_minimum_msat: None,
753+
outbound_htlc_maximum_msat: None,
754+
},
755+
funding_txo: Some(OutPoint {
756+
txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(),
757+
index: 1,
758+
}),
759+
channel_type: None,
760+
short_channel_id: None,
761+
outbound_scid_alias: None,
762+
inbound_scid_alias: None,
763+
channel_value_satoshis: 50_100,
764+
user_channel_id: (u64::MAX as u128) + 1, // Gets us into the high bytes
765+
balance_msat: 23_100,
766+
outbound_capacity_msat: 24_300,
767+
next_outbound_htlc_limit_msat: 20_000,
768+
next_outbound_htlc_minimum_msat: 132,
769+
inbound_capacity_msat: 42,
770+
unspendable_punishment_reserve: Some(8273),
771+
confirmations_required: Some(5),
772+
confirmations: Some(73),
773+
force_close_spend_delay: Some(10),
774+
is_outbound: true,
775+
is_channel_ready: false,
776+
is_usable: true,
777+
is_public: false,
778+
inbound_htlc_minimum_msat: Some(98),
779+
inbound_htlc_maximum_msat: Some(983274),
780+
config: Some(ChannelConfig::default()),
781+
feerate_sat_per_1000_weight: Some(212),
782+
channel_shutdown_state: Some(ChannelShutdownState::NotShuttingDown),
783+
pending_inbound_htlcs: vec![InboundHTLCDetails {
784+
htlc_id: 12,
785+
amount_msat: 333,
786+
cltv_expiry: 127,
787+
payment_hash: PaymentHash([3; 32]),
788+
state: Some(InboundHTLCStateDetails::AwaitingRemoteRevokeToAdd),
789+
is_dust: true,
790+
}],
791+
pending_outbound_htlcs: vec![OutboundHTLCDetails {
792+
htlc_id: Some(81),
793+
amount_msat: 5000,
794+
cltv_expiry: 129,
795+
payment_hash: PaymentHash([4; 32]),
796+
state: Some(OutboundHTLCStateDetails::AwaitingRemoteRevokeToAdd),
797+
skimmed_fee_msat: Some(42),
798+
is_dust: false,
799+
}],
800+
};
801+
let mut buffer = Vec::new();
802+
channel_details.write(&mut buffer).unwrap();
803+
let deser_channel_details = ChannelDetails::read(&mut buffer.as_slice()).unwrap();
804+
805+
assert_eq!(deser_channel_details, channel_details);
806+
}
807+
}

lightning/src/routing/router.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3591,6 +3591,7 @@ mod tests {
35913591

35923592
fn get_channel_details(short_channel_id: Option<u64>, node_id: PublicKey,
35933593
features: InitFeatures, outbound_capacity_msat: u64) -> ChannelDetails {
3594+
#[allow(deprecated)] // TODO: Remove once balance_msat is removed.
35943595
ChannelDetails {
35953596
channel_id: ChannelId::new_zero(),
35963597
counterparty: ChannelCounterparty {
@@ -8776,6 +8777,7 @@ pub(crate) mod bench_utils {
87768777

87778778
#[inline]
87788779
pub(crate) fn first_hop(node_id: PublicKey) -> ChannelDetails {
8780+
#[allow(deprecated)] // TODO: Remove once balance_msat is removed.
87798781
ChannelDetails {
87808782
channel_id: ChannelId::new_zero(),
87818783
counterparty: ChannelCounterparty {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* The `AvailableBalances::balance_msat` field has been deprecated in favor of `ChainMonitor::get_claimable_balances`. `AvailableBalances::balance_msat` will be removed in an upcoming release (#3247).

0 commit comments

Comments
 (0)