Skip to content

Commit 25a7073

Browse files
Router: clean up Path construction
We don't need to collect a vec of Results anymore.
1 parent 8344222 commit 25a7073

File tree

1 file changed

+21
-30
lines changed

1 file changed

+21
-30
lines changed

lightning/src/routing/router.rs

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2333,47 +2333,38 @@ where L::Target: Logger {
23332333
}
23342334
}
23352335

2336-
let mut selected_paths = Vec::<Vec<Result<RouteHop, LightningError>>>::new();
2336+
let mut paths = Vec::new();
23372337
for payment_path in selected_route {
2338-
let mut path = payment_path.hops.iter().filter(|(h, _)| h.candidate.short_channel_id().is_some())
2339-
.map(|(payment_hop, node_features)| {
2340-
Ok(RouteHop {
2341-
pubkey: PublicKey::from_slice(payment_hop.node_id.as_slice()).map_err(|_| LightningError{err: format!("Public key {:?} is invalid", &payment_hop.node_id), action: ErrorAction::IgnoreAndLog(Level::Trace)})?,
2342-
node_features: node_features.clone(),
2343-
short_channel_id: payment_hop.candidate.short_channel_id().unwrap(),
2344-
channel_features: payment_hop.candidate.features(),
2345-
fee_msat: payment_hop.fee_msat,
2346-
cltv_expiry_delta: payment_hop.candidate.cltv_expiry_delta(),
2347-
})
2348-
}).collect::<Vec<_>>();
2338+
let mut hops = Vec::with_capacity(payment_path.hops.len());
2339+
for (hop, node_features) in payment_path.hops.iter()
2340+
.filter(|(h, _)| h.candidate.short_channel_id().is_some())
2341+
{
2342+
hops.push(RouteHop {
2343+
pubkey: PublicKey::from_slice(hop.node_id.as_slice()).map_err(|_| LightningError{err: format!("Public key {:?} is invalid", &hop.node_id), action: ErrorAction::IgnoreAndLog(Level::Trace)})?,
2344+
node_features: node_features.clone(),
2345+
short_channel_id: hop.candidate.short_channel_id().unwrap(),
2346+
channel_features: hop.candidate.features(),
2347+
fee_msat: hop.fee_msat,
2348+
cltv_expiry_delta: hop.candidate.cltv_expiry_delta(),
2349+
});
2350+
}
23492351
// Propagate the cltv_expiry_delta one hop backwards since the delta from the current hop is
23502352
// applicable for the previous hop.
2351-
path.iter_mut().rev().fold(final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
2352-
core::mem::replace(&mut hop.as_mut().unwrap().cltv_expiry_delta, prev_cltv_expiry_delta)
2353+
hops.iter_mut().rev().fold(final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
2354+
core::mem::replace(&mut hop.cltv_expiry_delta, prev_cltv_expiry_delta)
23532355
});
2354-
selected_paths.push(path);
2356+
paths.push(Path { hops, blinded_tail: None });
23552357
}
23562358
// Make sure we would never create a route with more paths than we allow.
2357-
debug_assert!(selected_paths.len() <= payment_params.max_path_count.into());
2359+
debug_assert!(paths.len() <= payment_params.max_path_count.into());
23582360

23592361
if let Some(node_features) = payment_params.payee.node_features() {
2360-
for path in selected_paths.iter_mut() {
2361-
if let Ok(route_hop) = path.last_mut().unwrap() {
2362-
route_hop.node_features = node_features.clone();
2363-
}
2362+
for path in paths.iter_mut() {
2363+
path.hops.last_mut().unwrap().node_features = node_features.clone();
23642364
}
23652365
}
23662366

2367-
let mut paths: Vec<Path> = Vec::new();
2368-
for results_vec in selected_paths {
2369-
let mut hops = Vec::with_capacity(results_vec.len());
2370-
for res in results_vec { hops.push(res?); }
2371-
paths.push(Path { hops, blinded_tail: None });
2372-
}
2373-
let route = Route {
2374-
paths,
2375-
payment_params: Some(payment_params.clone()),
2376-
};
2367+
let route = Route { paths, payment_params: Some(payment_params.clone()) };
23772368
log_info!(logger, "Got route: {}", log_route!(route));
23782369
Ok(route)
23792370
}

0 commit comments

Comments
 (0)