Skip to content

Commit ad7ff0b

Browse files
Merge pull request #1815 from TheBlueMatt/2022-10-dead-gossip-code
Require directional updates for a DirectionalChannelInfo
2 parents 0f8c8d6 + 4df0ff7 commit ad7ff0b

File tree

3 files changed

+66
-112
lines changed

3 files changed

+66
-112
lines changed

lightning/src/routing/gossip.rs

+16-53
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ impl ChannelInfo {
750750
return None;
751751
}
752752
};
753-
Some((DirectedChannelInfo::new(self, direction), source))
753+
direction.map(|dir| (DirectedChannelInfo::new(self, dir), source))
754754
}
755755

756756
/// Returns a [`DirectedChannelInfo`] for the channel directed from the given `source` to a
@@ -765,7 +765,7 @@ impl ChannelInfo {
765765
return None;
766766
}
767767
};
768-
Some((DirectedChannelInfo::new(self, direction), target))
768+
direction.map(|dir| (DirectedChannelInfo::new(self, dir), target))
769769
}
770770

771771
/// Returns a [`ChannelUpdateInfo`] based on the direction implied by the channel_flag.
@@ -860,29 +860,23 @@ impl Readable for ChannelInfo {
860860
#[derive(Clone)]
861861
pub struct DirectedChannelInfo<'a> {
862862
channel: &'a ChannelInfo,
863-
direction: Option<&'a ChannelUpdateInfo>,
863+
direction: &'a ChannelUpdateInfo,
864864
htlc_maximum_msat: u64,
865865
effective_capacity: EffectiveCapacity,
866866
}
867867

868868
impl<'a> DirectedChannelInfo<'a> {
869869
#[inline]
870-
fn new(channel: &'a ChannelInfo, direction: Option<&'a ChannelUpdateInfo>) -> Self {
871-
let htlc_maximum_msat = direction.map(|direction| direction.htlc_maximum_msat);
870+
fn new(channel: &'a ChannelInfo, direction: &'a ChannelUpdateInfo) -> Self {
871+
let mut htlc_maximum_msat = direction.htlc_maximum_msat;
872872
let capacity_msat = channel.capacity_sats.map(|capacity_sats| capacity_sats * 1000);
873873

874-
let (htlc_maximum_msat, effective_capacity) = match (htlc_maximum_msat, capacity_msat) {
875-
(Some(amount_msat), Some(capacity_msat)) => {
876-
let htlc_maximum_msat = cmp::min(amount_msat, capacity_msat);
877-
(htlc_maximum_msat, EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat: Some(htlc_maximum_msat) })
874+
let effective_capacity = match capacity_msat {
875+
Some(capacity_msat) => {
876+
htlc_maximum_msat = cmp::min(htlc_maximum_msat, capacity_msat);
877+
EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat: htlc_maximum_msat }
878878
},
879-
(Some(amount_msat), None) => {
880-
(amount_msat, EffectiveCapacity::MaximumHTLC { amount_msat })
881-
},
882-
(None, Some(capacity_msat)) => {
883-
(capacity_msat, EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat: None })
884-
},
885-
(None, None) => (EffectiveCapacity::Unknown.as_msat(), EffectiveCapacity::Unknown),
879+
None => EffectiveCapacity::MaximumHTLC { amount_msat: htlc_maximum_msat },
886880
};
887881

888882
Self {
@@ -891,12 +885,11 @@ impl<'a> DirectedChannelInfo<'a> {
891885
}
892886

893887
/// Returns information for the channel.
888+
#[inline]
894889
pub fn channel(&self) -> &'a ChannelInfo { self.channel }
895890

896-
/// Returns information for the direction.
897-
pub fn direction(&self) -> Option<&'a ChannelUpdateInfo> { self.direction }
898-
899891
/// Returns the maximum HTLC amount allowed over the channel in the direction.
892+
#[inline]
900893
pub fn htlc_maximum_msat(&self) -> u64 {
901894
self.htlc_maximum_msat
902895
}
@@ -910,13 +903,9 @@ impl<'a> DirectedChannelInfo<'a> {
910903
self.effective_capacity
911904
}
912905

913-
/// Returns `Some` if [`ChannelUpdateInfo`] is available in the direction.
914-
pub(super) fn with_update(self) -> Option<DirectedChannelInfoWithUpdate<'a>> {
915-
match self.direction {
916-
Some(_) => Some(DirectedChannelInfoWithUpdate { inner: self }),
917-
None => None,
918-
}
919-
}
906+
/// Returns information for the direction.
907+
#[inline]
908+
pub(super) fn direction(&self) -> &'a ChannelUpdateInfo { self.direction }
920909
}
921910

922911
impl<'a> fmt::Debug for DirectedChannelInfo<'a> {
@@ -927,32 +916,6 @@ impl<'a> fmt::Debug for DirectedChannelInfo<'a> {
927916
}
928917
}
929918

930-
/// A [`DirectedChannelInfo`] with [`ChannelUpdateInfo`] available in its direction.
931-
#[derive(Clone)]
932-
pub(super) struct DirectedChannelInfoWithUpdate<'a> {
933-
inner: DirectedChannelInfo<'a>,
934-
}
935-
936-
impl<'a> DirectedChannelInfoWithUpdate<'a> {
937-
/// Returns information for the channel.
938-
#[inline]
939-
pub(super) fn channel(&self) -> &'a ChannelInfo { &self.inner.channel }
940-
941-
/// Returns information for the direction.
942-
#[inline]
943-
pub(super) fn direction(&self) -> &'a ChannelUpdateInfo { self.inner.direction.unwrap() }
944-
945-
/// Returns the [`EffectiveCapacity`] of the channel in the direction.
946-
#[inline]
947-
pub(super) fn effective_capacity(&self) -> EffectiveCapacity { self.inner.effective_capacity() }
948-
}
949-
950-
impl<'a> fmt::Debug for DirectedChannelInfoWithUpdate<'a> {
951-
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
952-
self.inner.fmt(f)
953-
}
954-
}
955-
956919
/// The effective capacity of a channel for routing purposes.
957920
///
958921
/// While this may be smaller than the actual channel capacity, amounts greater than
@@ -976,7 +939,7 @@ pub enum EffectiveCapacity {
976939
/// The funding amount denominated in millisatoshi.
977940
capacity_msat: u64,
978941
/// The maximum HTLC amount denominated in millisatoshi.
979-
htlc_maximum_msat: Option<u64>
942+
htlc_maximum_msat: u64
980943
},
981944
/// A capacity sufficient to route any payment, typically used for private channels provided by
982945
/// an invoice.

lightning/src/routing/router.rs

+18-27
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use bitcoin::secp256k1::PublicKey;
1717
use crate::ln::channelmanager::ChannelDetails;
1818
use crate::ln::features::{ChannelFeatures, InvoiceFeatures, NodeFeatures};
1919
use crate::ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT};
20-
use crate::routing::gossip::{DirectedChannelInfoWithUpdate, EffectiveCapacity, ReadOnlyNetworkGraph, NetworkGraph, NodeId, RoutingFees};
20+
use crate::routing::gossip::{DirectedChannelInfo, EffectiveCapacity, ReadOnlyNetworkGraph, NetworkGraph, NodeId, RoutingFees};
2121
use crate::routing::scoring::{ChannelUsage, Score};
2222
use crate::util::ser::{Writeable, Readable, Writer};
2323
use crate::util::logger::{Level, Logger};
@@ -421,7 +421,7 @@ enum CandidateRouteHop<'a> {
421421
},
422422
/// A hop found in the [`ReadOnlyNetworkGraph`], where the channel capacity may be unknown.
423423
PublicHop {
424-
info: DirectedChannelInfoWithUpdate<'a>,
424+
info: DirectedChannelInfo<'a>,
425425
short_channel_id: u64,
426426
},
427427
/// A hop to the payee found in the payment invoice, though not necessarily a direct channel.
@@ -494,10 +494,8 @@ fn max_htlc_from_capacity(capacity: EffectiveCapacity, max_channel_saturation_po
494494
EffectiveCapacity::Unknown => EffectiveCapacity::Unknown.as_msat(),
495495
EffectiveCapacity::MaximumHTLC { amount_msat } =>
496496
amount_msat.checked_shr(saturation_shift).unwrap_or(0),
497-
EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat: None } =>
498-
capacity_msat.checked_shr(saturation_shift).unwrap_or(0),
499-
EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat: Some(htlc_max) } =>
500-
cmp::min(capacity_msat.checked_shr(saturation_shift).unwrap_or(0), htlc_max),
497+
EffectiveCapacity::Total { capacity_msat, htlc_maximum_msat } =>
498+
cmp::min(capacity_msat.checked_shr(saturation_shift).unwrap_or(0), htlc_maximum_msat),
501499
}
502500
}
503501

@@ -1276,13 +1274,11 @@ where L::Target: Logger {
12761274
for chan_id in $node.channels.iter() {
12771275
let chan = network_channels.get(chan_id).unwrap();
12781276
if !chan.features.requires_unknown_bits() {
1279-
let (directed_channel, source) =
1280-
chan.as_directed_to(&$node_id).expect("inconsistent NetworkGraph");
1281-
if first_hops.is_none() || *source != our_node_id {
1282-
if let Some(direction) = directed_channel.direction() {
1283-
if direction.enabled {
1277+
if let Some((directed_channel, source)) = chan.as_directed_to(&$node_id) {
1278+
if first_hops.is_none() || *source != our_node_id {
1279+
if directed_channel.direction().enabled {
12841280
let candidate = CandidateRouteHop::PublicHop {
1285-
info: directed_channel.with_update().unwrap(),
1281+
info: directed_channel,
12861282
short_channel_id: *chan_id,
12871283
};
12881284
add_entry!(candidate, *source, $node_id,
@@ -1367,8 +1363,7 @@ where L::Target: Logger {
13671363
let candidate = network_channels
13681364
.get(&hop.short_channel_id)
13691365
.and_then(|channel| channel.as_directed_to(&target))
1370-
.and_then(|(channel, _)| channel.with_update())
1371-
.map(|info| CandidateRouteHop::PublicHop {
1366+
.map(|(info, _)| CandidateRouteHop::PublicHop {
13721367
info,
13731368
short_channel_id: hop.short_channel_id,
13741369
})
@@ -1816,10 +1811,8 @@ fn add_random_cltv_offset(route: &mut Route, payment_params: &PaymentParameters,
18161811
random_channel.as_directed_from(&cur_node_id).map(|(dir_info, next_id)| {
18171812
if !nodes_to_avoid.iter().any(|x| x == next_id) {
18181813
nodes_to_avoid[random_hop] = *next_id;
1819-
dir_info.direction().map(|channel_update_info| {
1820-
random_hop_offset = channel_update_info.cltv_expiry_delta.into();
1821-
cur_hop = Some(*next_id);
1822-
});
1814+
random_hop_offset = dir_info.direction().cltv_expiry_delta.into();
1815+
cur_hop = Some(*next_id);
18231816
}
18241817
});
18251818
}
@@ -5214,14 +5207,12 @@ mod tests {
52145207
for channel_id in &cur_node.channels {
52155208
if let Some(channel_info) = network_channels.get(&channel_id) {
52165209
if let Some((dir_info, next_id)) = channel_info.as_directed_from(&cur_node_id) {
5217-
if let Some(channel_update_info) = dir_info.direction() {
5218-
let next_cltv_expiry_delta = channel_update_info.cltv_expiry_delta as u32;
5219-
if cur_path_cltv_deltas.iter().sum::<u32>()
5220-
.saturating_add(next_cltv_expiry_delta) <= observed_cltv_expiry_delta {
5221-
let mut new_path_cltv_deltas = cur_path_cltv_deltas.clone();
5222-
new_path_cltv_deltas.push(next_cltv_expiry_delta);
5223-
candidates.push_back((*next_id, new_path_cltv_deltas));
5224-
}
5210+
let next_cltv_expiry_delta = dir_info.direction().cltv_expiry_delta as u32;
5211+
if cur_path_cltv_deltas.iter().sum::<u32>()
5212+
.saturating_add(next_cltv_expiry_delta) <= observed_cltv_expiry_delta {
5213+
let mut new_path_cltv_deltas = cur_path_cltv_deltas.clone();
5214+
new_path_cltv_deltas.push(next_cltv_expiry_delta);
5215+
candidates.push_back((*next_id, new_path_cltv_deltas));
52255216
}
52265217
}
52275218
}
@@ -5398,7 +5389,7 @@ mod tests {
53985389
let usage = ChannelUsage {
53995390
amount_msat: 0,
54005391
inflight_htlc_msat: 0,
5401-
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024_000, htlc_maximum_msat: Some(1_000) },
5392+
effective_capacity: EffectiveCapacity::Total { capacity_msat: 1_024_000, htlc_maximum_msat: 1_000 },
54025393
};
54035394
scorer.set_manual_penalty(&NodeId::from_pubkey(&nodes[3]), 123);
54045395
scorer.set_manual_penalty(&NodeId::from_pubkey(&nodes[4]), 456);

0 commit comments

Comments
 (0)