Skip to content

Commit dfb8464

Browse files
committed
f - One directed method returning an Option
1 parent 5a3e99e commit dfb8464

File tree

2 files changed

+8
-32
lines changed

2 files changed

+8
-32
lines changed

lightning/src/routing/network_graph.rs

Lines changed: 5 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -650,44 +650,19 @@ pub struct ChannelInfo {
650650
}
651651

652652
impl ChannelInfo {
653-
/// Returns a [`DirectedChannelInfo`] for the channel from `source` to `target`.
654-
///
655-
/// # Panics
656-
///
657-
/// Panics if `source` and `target` are not the channel's counterparties.
658-
pub fn as_directed(&self, source: &NodeId, target: &NodeId) -> DirectedChannelInfo {
659-
let (direction, source, target) = {
660-
if source == &self.node_one && target == &self.node_two {
661-
(self.one_to_two.as_ref(), &self.node_one, &self.node_two)
662-
} else if source == &self.node_two && target == &self.node_one {
663-
(self.two_to_one.as_ref(), &self.node_two, &self.node_one)
664-
} else if source != &self.node_one && source != &self.node_two {
665-
panic!("Unknown source node: {:?}", source)
666-
} else if target != &self.node_one && target != &self.node_two {
667-
panic!("Unknown target node: {:?}", target)
668-
} else {
669-
unreachable!()
670-
}
671-
};
672-
DirectedChannelInfo { channel: self, direction, source, target }
673-
}
674-
675-
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target`.
676-
///
677-
/// # Panics
678-
///
679-
/// Panics if `target` is not one of the channel's counterparties.
680-
pub fn directed_to(&self, target: &NodeId) -> DirectedChannelInfo {
653+
/// Returns a [`DirectedChannelInfo`] for the channel directed to the given `target`, or `None`
654+
/// if `target` is not one of the channel's counterparties.
655+
pub fn as_directed_to(&self, target: &NodeId) -> Option<DirectedChannelInfo> {
681656
let (direction, source, target) = {
682657
if target == &self.node_one {
683658
(self.two_to_one.as_ref(), &self.node_two, &self.node_one)
684659
} else if target == &self.node_two {
685660
(self.one_to_two.as_ref(), &self.node_one, &self.node_two)
686661
} else {
687-
panic!("Unknown target node: {:?}", target)
662+
return None;
688663
}
689664
};
690-
DirectedChannelInfo { channel: self, direction, source, target }
665+
Some(DirectedChannelInfo { channel: self, direction, source, target })
691666
}
692667
}
693668

lightning/src/routing/router.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,7 +1049,8 @@ where L::Target: Logger {
10491049
for chan_id in $node.channels.iter() {
10501050
let chan = network_channels.get(chan_id).unwrap();
10511051
if !chan.features.requires_unknown_bits() {
1052-
let directed_channel = chan.directed_to(&$node_id);
1052+
let directed_channel =
1053+
chan.as_directed_to(&$node_id).expect("inconsistent NetworkGraph");
10531054
let source = directed_channel.source();
10541055
let target = directed_channel.target();
10551056
if first_hops.is_none() || *source != our_node_id {
@@ -1134,7 +1135,7 @@ where L::Target: Logger {
11341135
let target = NodeId::from_pubkey(&prev_hop_id);
11351136
let candidate = network_channels
11361137
.get(&hop.short_channel_id)
1137-
.map(|channel| channel.as_directed(&source, &target).into_parts())
1138+
.and_then(|channel| channel.as_directed_to(&target).map(|d| d.into_parts()))
11381139
.and_then(|(channel, direction)| {
11391140
direction.map(|direction|
11401141
CandidateRouteHop::PublicHop {

0 commit comments

Comments
 (0)