From ff4168d747c23776f8407688e1ee5be40ed3b20f Mon Sep 17 00:00:00 2001 From: Jeffrey Czyz Date: Fri, 28 Jan 2022 07:50:00 -0600 Subject: [PATCH] f - Enforce direction is Some --- lightning/src/routing/network_graph.rs | 23 ++++++++++++++++++++++- lightning/src/routing/router.rs | 19 +++++++++---------- 2 files changed, 31 insertions(+), 11 deletions(-) diff --git a/lightning/src/routing/network_graph.rs b/lightning/src/routing/network_graph.rs index e3ce80138..ee0ca48a1 100644 --- a/lightning/src/routing/network_graph.rs +++ b/lightning/src/routing/network_graph.rs @@ -718,7 +718,7 @@ impl<'a: 'b, 'b> DirectedChannelInfo<'a, 'b> { /// Returns the node id for the target. pub fn target(&self) -> &'b NodeId { self.target } - /// Returns the [`EffectiveCapacity`] of the channel in a specific direction. + /// Returns the [`EffectiveCapacity`] of the channel in the direction. /// /// This is either the total capacity from the funding transaction, if known, or the /// `htlc_maximum_msat` for the direction as advertised by the gossip network, if known, @@ -739,6 +739,27 @@ impl<'a: 'b, 'b> DirectedChannelInfo<'a, 'b> { EffectiveCapacity::Total { capacity_msat })) .unwrap_or(EffectiveCapacity::Unknown) } + + /// Returns `Some` if [`ChannelUpdateInfo`] is available in the direction. + pub(super) fn with_update(self) -> Option> { + match self.direction { + Some(_) => Some(DirectedChannelInfoWithUpdate { inner: self }), + None => None, + } + } +} + +/// A [`DirectedChannelInfo`] with [`ChannelUpdateInfo`] available in its the direction. +#[derive(Clone, Debug)] +pub(super) struct DirectedChannelInfoWithUpdate<'a: 'b, 'b> { + inner: DirectedChannelInfo<'a, 'b>, +} + +impl<'a: 'b, 'b> DirectedChannelInfoWithUpdate<'a, 'b> { + /// Returns the underlying [`DirectedChannelInfo`]. + pub(super) fn inner(&self) -> &DirectedChannelInfo<'a, 'b> { + &self.inner + } } /// The effective capacity of a channel for routing purposes. diff --git a/lightning/src/routing/router.rs b/lightning/src/routing/router.rs index 6df36382d..671b4a127 100644 --- a/lightning/src/routing/router.rs +++ b/lightning/src/routing/router.rs @@ -18,7 +18,7 @@ use ln::channelmanager::ChannelDetails; use ln::features::{ChannelFeatures, InvoiceFeatures, NodeFeatures}; use ln::msgs::{DecodeError, ErrorAction, LightningError, MAX_VALUE_MSAT}; use routing::scoring::Score; -use routing::network_graph::{DirectedChannelInfo, EffectiveCapacity, NetworkGraph, NodeId, RoutingFees}; +use routing::network_graph::{DirectedChannelInfoWithUpdate, EffectiveCapacity, NetworkGraph, NodeId, RoutingFees}; use util::ser::{Writeable, Readable}; use util::logger::{Level, Logger}; @@ -355,8 +355,7 @@ enum CandidateRouteHop<'a> { }, /// A hop found in the [`NetworkGraph`], where the channel capacity may or may not be known. PublicHop { - // `DirectedChannelInfo::direction` MUST NOT be `None`. - info: DirectedChannelInfo<'a, 'a>, + info: DirectedChannelInfoWithUpdate<'a, 'a>, short_channel_id: u64, }, /// A hop to the payee found in the payment invoice, though not necessarily a direct channel. @@ -377,7 +376,7 @@ impl<'a> CandidateRouteHop<'a> { fn features(&self) -> ChannelFeatures { match self { CandidateRouteHop::FirstHop { details } => details.counterparty.features.to_context(), - CandidateRouteHop::PublicHop { info, .. } => info.channel().features.clone(), + CandidateRouteHop::PublicHop { info, .. } => info.inner().channel().features.clone(), CandidateRouteHop::PrivateHop { .. } => ChannelFeatures::empty(), } } @@ -386,7 +385,7 @@ impl<'a> CandidateRouteHop<'a> { match self { CandidateRouteHop::FirstHop { .. } => 0, CandidateRouteHop::PublicHop { info, .. } => { - info.direction().unwrap().cltv_expiry_delta as u32 + info.inner().direction().unwrap().cltv_expiry_delta as u32 }, CandidateRouteHop::PrivateHop { hint } => hint.cltv_expiry_delta as u32, } @@ -396,7 +395,7 @@ impl<'a> CandidateRouteHop<'a> { match self { CandidateRouteHop::FirstHop { .. } => 0, CandidateRouteHop::PublicHop { info, .. } => { - info.direction().unwrap().htlc_minimum_msat + info.inner().direction().unwrap().htlc_minimum_msat }, CandidateRouteHop::PrivateHop { hint } => hint.htlc_minimum_msat.unwrap_or(0), } @@ -407,7 +406,7 @@ impl<'a> CandidateRouteHop<'a> { CandidateRouteHop::FirstHop { .. } => RoutingFees { base_msat: 0, proportional_millionths: 0, }, - CandidateRouteHop::PublicHop { info, .. } => info.direction().unwrap().fees, + CandidateRouteHop::PublicHop { info, .. } => info.inner().direction().unwrap().fees, CandidateRouteHop::PrivateHop { hint } => hint.fees, } } @@ -417,7 +416,7 @@ impl<'a> CandidateRouteHop<'a> { CandidateRouteHop::FirstHop { details } => EffectiveCapacity::ExactLiquidity { liquidity_msat: details.outbound_capacity_msat, }, - CandidateRouteHop::PublicHop { info, .. } => info.effective_capacity(), + CandidateRouteHop::PublicHop { info, .. } => info.inner().effective_capacity(), CandidateRouteHop::PrivateHop { .. } => EffectiveCapacity::Infinite, } } @@ -1084,7 +1083,7 @@ where L::Target: Logger { if let Some(direction) = directed_channel.direction() { if direction.enabled { let candidate = CandidateRouteHop::PublicHop { - info: directed_channel, + info: directed_channel.with_update().unwrap(), short_channel_id: *chan_id, }; add_entry!(candidate, *source, *target, $fee_to_target_msat, $next_hops_value_contribution, $next_hops_path_htlc_minimum_msat, $next_hops_path_penalty_msat, $next_hops_cltv_delta); @@ -1162,7 +1161,7 @@ where L::Target: Logger { let candidate = network_channels .get(&hop.short_channel_id) .and_then(|channel| channel.as_directed_to(&target)) - .and_then(|channel| channel.direction().map(|_| channel)) + .and_then(|channel| channel.with_update()) .map(|info| CandidateRouteHop::PublicHop { info, short_channel_id: hop.short_channel_id, -- 2.39.5