Skip to content

Commit 3b4c1a3

Browse files
committed
Util-func channel removal (fixing a bug in HTLC failure updates)
1 parent 91b23a0 commit 3b4c1a3

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

src/ln/router.rs

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -306,12 +306,7 @@ impl RoutingMessageHandler for Router {
306306
&msgs::HTLCFailChannelUpdate::ChannelClosed { ref short_channel_id } => {
307307
let mut network = self.network_map.write().unwrap();
308308
if let Some(chan) = network.channels.remove(short_channel_id) {
309-
network.nodes.get_mut(&chan.one_to_two.src_node_id).unwrap().channels.retain(|chan_id| {
310-
chan_id != NetworkMap::get_short_id(chan_id)
311-
});
312-
network.nodes.get_mut(&chan.two_to_one.src_node_id).unwrap().channels.retain(|chan_id| {
313-
chan_id != NetworkMap::get_short_id(chan_id)
314-
});
309+
Self::remove_channel_in_nodes(&mut network.nodes, &chan, *short_channel_id);
315310
}
316311
},
317312
}
@@ -462,6 +457,25 @@ impl Router {
462457
unimplemented!();
463458
}
464459

460+
fn remove_channel_in_nodes(nodes: &mut HashMap<PublicKey, NodeInfo>, chan: &ChannelInfo, short_channel_id: u64) {
461+
macro_rules! remove_from_node {
462+
($node_id: expr) => {
463+
if let Entry::Occupied(mut entry) = nodes.entry($node_id) {
464+
entry.get_mut().channels.retain(|chan_id| {
465+
short_channel_id != *NetworkMap::get_short_id(chan_id)
466+
});
467+
if entry.get().channels.is_empty() {
468+
entry.remove_entry();
469+
}
470+
} else {
471+
panic!("Had channel that pointed to unknown node (ie inconsistent network map)!");
472+
}
473+
}
474+
}
475+
remove_from_node!(chan.one_to_two.src_node_id);
476+
remove_from_node!(chan.two_to_one.src_node_id);
477+
}
478+
465479
/// Gets a route from us to the given target node.
466480
/// Extra routing hops between known nodes and the target will be used if they are included in
467481
/// last_hops.

0 commit comments

Comments
 (0)