Skip to content

Commit a44b282

Browse files
committed
Improve ChannelDetails readability significantly.
After the merge of lightningdevkit#984, Jeff pointed out that `ChannelDetails` has become a bit of a "bag of variables", and that a few of the variable names in lightningdevkit#984 were more confusing than necessary in context. This addresses several issues by: * Splitting counterparty parameters into a separate `ChannelCounterpartyParameters` struct, * using the name `unspendable_punishment_reserve` for both outbound and inbound channel reserves, differentiating them based on their position in the counterparty parameters struct or not, * Using the name `force_close_spend_delay` instead of `spend_csv_on_our_commitment_funds` to better communicate what is occurring.
1 parent 431f807 commit a44b282

File tree

4 files changed

+82
-165
lines changed

4 files changed

+82
-165
lines changed

fuzz/src/router.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use bitcoin::hash_types::BlockHash;
1313

1414
use lightning::chain;
1515
use lightning::chain::transaction::OutPoint;
16-
use lightning::ln::channelmanager::ChannelDetails;
16+
use lightning::ln::channelmanager::{ChannelDetails, ChannelCounterpartyParameters};
1717
use lightning::ln::features::InitFeatures;
1818
use lightning::ln::msgs;
1919
use lightning::routing::router::{get_route, RouteHint, RouteHintHop};
@@ -207,20 +207,22 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
207207
let rnid = node_pks.iter().skip(slice_to_be16(get_slice!(2))as usize % node_pks.len()).next().unwrap();
208208
first_hops_vec.push(ChannelDetails {
209209
channel_id: [0; 32],
210+
counterparty: ChannelCounterpartyParameters {
211+
node_id: *rnid,
212+
features: InitFeatures::known(),
213+
unspendable_punishment_reserve: 0,
214+
forwarding_info: None,
215+
},
210216
funding_txo: Some(OutPoint { txid: bitcoin::Txid::from_slice(&[0; 32]).unwrap(), index: 0 }),
211217
short_channel_id: Some(scid),
212-
remote_network_id: *rnid,
213-
counterparty_features: InitFeatures::known(),
214218
channel_value_satoshis: slice_to_be64(get_slice!(8)),
215219
user_id: 0, inbound_capacity_msat: 0,
216-
to_self_reserve_satoshis: None,
217-
to_remote_reserve_satoshis: 0,
220+
unspendable_punishment_reserve: None,
218221
confirmations_required: None,
219-
spend_csv_on_our_commitment_funds: None,
222+
force_close_spend_delay: None,
220223
is_outbound: true, is_funding_locked: true,
221224
is_usable: true, is_public: true,
222225
outbound_capacity_msat: 0,
223-
counterparty_forwarding_info: None,
224226
});
225227
}
226228
Some(&first_hops_vec[..])

lightning-invoice/src/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ where
3636
Some(id) => id,
3737
None => continue,
3838
};
39-
let forwarding_info = match channel.counterparty_forwarding_info {
39+
let forwarding_info = match channel.counterparty.forwarding_info {
4040
Some(info) => info,
4141
None => continue,
4242
};
4343
route_hints.push(RouteHint(vec![RouteHintHop {
44-
src_node_id: channel.remote_network_id,
44+
src_node_id: channel.counterparty.node_id,
4545
short_channel_id,
4646
fees: RoutingFees {
4747
base_msat: forwarding_info.fee_base_msat,

lightning/src/ln/channelmanager.rs

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -602,6 +602,29 @@ const CHECK_CLTV_EXPIRY_SANITY: u32 = MIN_CLTV_EXPIRY_DELTA as u32 - LATENCY_GRA
602602
#[allow(dead_code)]
603603
const CHECK_CLTV_EXPIRY_SANITY_2: u32 = MIN_CLTV_EXPIRY_DELTA as u32 - LATENCY_GRACE_PERIOD_BLOCKS - 2*CLTV_CLAIM_BUFFER;
604604

605+
/// Channel parameters which apply to our counterparty. These are split out from [`ChannelDetails`]
606+
/// to better separate parameters.
607+
#[derive(Clone, Debug, PartialEq)]
608+
pub struct ChannelCounterpartyParameters {
609+
/// The Features the channel counterparty provided upon last connection.
610+
/// Useful for routing as it is the most up-to-date copy of the counterparty's features and
611+
/// many routing-relevant features are present in the init context.
612+
pub features: InitFeatures,
613+
/// The node_id of our counterparty
614+
pub node_id: PublicKey,
615+
/// The value, in satoshis, that must always be held in the channel for our counterparty. This
616+
/// value ensures that if our counterparty broadcasts a revoked state, we can punish them by
617+
/// claiming at least this value on chain.
618+
///
619+
/// This value is not included in [`inbound_capacity_msat`] as it can never be spent.
620+
///
621+
/// [`inbound_capacity_msat`]: ChannelDetails::inbound_capacity_msat
622+
pub unspendable_punishment_reserve: u64,
623+
/// Information on the fees and requirements that the counterparty requires when forwarding
624+
/// payments to us through this channel.
625+
pub forwarding_info: Option<CounterpartyForwardingInfo>,
626+
}
627+
605628
/// Details of a channel, as returned by ChannelManager::list_channels and ChannelManager::list_usable_channels
606629
#[derive(Clone, Debug, PartialEq)]
607630
pub struct ChannelDetails {
@@ -610,6 +633,8 @@ pub struct ChannelDetails {
610633
/// Note that this means this value is *not* persistent - it can change once during the
611634
/// lifetime of the channel.
612635
pub channel_id: [u8; 32],
636+
/// Parameters which apply to our counterparty. See individual fields for more information.
637+
pub counterparty: ChannelCounterpartyParameters,
613638
/// The Channel's funding transaction output, if we've negotiated the funding transaction with
614639
/// our counterparty already.
615640
///
@@ -619,12 +644,6 @@ pub struct ChannelDetails {
619644
/// The position of the funding transaction in the chain. None if the funding transaction has
620645
/// not yet been confirmed and the channel fully opened.
621646
pub short_channel_id: Option<u64>,
622-
/// The node_id of our counterparty
623-
pub remote_network_id: PublicKey,
624-
/// The Features the channel counterparty provided upon last connection.
625-
/// Useful for routing as it is the most up-to-date copy of the counterparty's features and
626-
/// many routing-relevant features are present in the init context.
627-
pub counterparty_features: InitFeatures,
628647
/// The value, in satoshis, of this channel as appears in the funding output
629648
pub channel_value_satoshis: u64,
630649
/// The value, in satoshis, that must always be held in the channel for us. This value ensures
@@ -636,15 +655,7 @@ pub struct ChannelDetails {
636655
/// This value will be `None` for outbound channels until the counterparty accepts the channel.
637656
///
638657
/// [`outbound_capacity_msat`]: ChannelDetails::outbound_capacity_msat
639-
pub to_self_reserve_satoshis: Option<u64>,
640-
/// The value, in satoshis, that must always be held in the channel for our counterparty. This
641-
/// value ensures that if our counterparty broadcasts a revoked state, we can punish them by
642-
/// claiming at least this value on chain.
643-
///
644-
/// This value is not included in [`inbound_capacity_msat`] as it can never be spent.
645-
///
646-
/// [`inbound_capacity_msat`]: ChannelDetails::inbound_capacity_msat
647-
pub to_remote_reserve_satoshis: u64,
658+
pub unspendable_punishment_reserve: Option<u64>,
648659
/// The user_id passed in to create_channel, or 0 if the channel was inbound.
649660
pub user_id: u64,
650661
/// The available outbound capacity for sending HTLCs to the remote peer. This does not include
@@ -685,7 +696,7 @@ pub struct ChannelDetails {
685696
/// time to claim our non-HTLC-encumbered funds.
686697
///
687698
/// This value will be `None` for outbound channels until the counterparty accepts the channel.
688-
pub spend_csv_on_our_commitment_funds: Option<u16>,
699+
pub force_close_spend_delay: Option<u16>,
689700
/// True if the channel was initiated (and thus funded) by us.
690701
pub is_outbound: bool,
691702
/// True if the channel is confirmed, funding_locked messages have been exchanged, and the
@@ -703,9 +714,6 @@ pub struct ChannelDetails {
703714
pub is_usable: bool,
704715
/// True if this channel is (or will be) publicly-announced.
705716
pub is_public: bool,
706-
/// Information on the fees and requirements that the counterparty requires when forwarding
707-
/// payments to us through this channel.
708-
pub counterparty_forwarding_info: Option<CounterpartyForwardingInfo>,
709717
}
710718

711719
/// If a payment fails to send, it can be in one of several states. This enum is returned as the
@@ -1170,30 +1178,32 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
11701178
channel.get_holder_counterparty_selected_channel_reserve_satoshis();
11711179
res.push(ChannelDetails {
11721180
channel_id: (*channel_id).clone(),
1181+
counterparty: ChannelCounterpartyParameters {
1182+
node_id: channel.get_counterparty_node_id(),
1183+
features: InitFeatures::empty(),
1184+
unspendable_punishment_reserve: to_remote_reserve_satoshis,
1185+
forwarding_info: channel.counterparty_forwarding_info(),
1186+
},
11731187
funding_txo: channel.get_funding_txo(),
11741188
short_channel_id: channel.get_short_channel_id(),
1175-
remote_network_id: channel.get_counterparty_node_id(),
1176-
counterparty_features: InitFeatures::empty(),
11771189
channel_value_satoshis: channel.get_value_satoshis(),
1178-
to_self_reserve_satoshis,
1179-
to_remote_reserve_satoshis,
1190+
unspendable_punishment_reserve: to_self_reserve_satoshis,
11801191
inbound_capacity_msat,
11811192
outbound_capacity_msat,
11821193
user_id: channel.get_user_id(),
11831194
confirmations_required: channel.minimum_depth(),
1184-
spend_csv_on_our_commitment_funds: channel.get_counterparty_selected_contest_delay(),
1195+
force_close_spend_delay: channel.get_counterparty_selected_contest_delay(),
11851196
is_outbound: channel.is_outbound(),
11861197
is_funding_locked: channel.is_usable(),
11871198
is_usable: channel.is_live(),
11881199
is_public: channel.should_announce(),
1189-
counterparty_forwarding_info: channel.counterparty_forwarding_info(),
11901200
});
11911201
}
11921202
}
11931203
let per_peer_state = self.per_peer_state.read().unwrap();
11941204
for chan in res.iter_mut() {
1195-
if let Some(peer_state) = per_peer_state.get(&chan.remote_network_id) {
1196-
chan.counterparty_features = peer_state.lock().unwrap().latest_features.clone();
1205+
if let Some(peer_state) = per_peer_state.get(&chan.counterparty.node_id) {
1206+
chan.counterparty.features = peer_state.lock().unwrap().latest_features.clone();
11971207
}
11981208
}
11991209
res
@@ -4286,7 +4296,7 @@ impl<Signer: Sign, M: Deref , T: Deref , K: Deref , F: Deref , L: Deref >
42864296

42874297
if msg.channel_id == [0; 32] {
42884298
for chan in self.list_channels() {
4289-
if chan.remote_network_id == *counterparty_node_id {
4299+
if chan.counterparty.node_id == *counterparty_node_id {
42904300
// Untrusted messages from peer, we throw away the error if id points to a non-existent channel
42914301
let _ = self.force_close_channel_with_peer(&chan.channel_id, Some(counterparty_node_id));
42924302
}

0 commit comments

Comments
 (0)