Skip to content

Commit 097fd01

Browse files
Router: clean up Path construction
We don't need to collect a vec of Results anymore.
1 parent 0eb744a commit 097fd01

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
@@ -2327,47 +2327,38 @@ where L::Target: Logger {
23272327
}
23282328
}
23292329

2330-
let mut selected_paths = Vec::<Vec<Result<RouteHop, LightningError>>>::new();
2330+
let mut paths = Vec::new();
23312331
for payment_path in selected_route {
2332-
let mut path = payment_path.hops.iter().filter(|(h, _)| h.candidate.short_channel_id().is_some())
2333-
.map(|(payment_hop, node_features)| {
2334-
Ok(RouteHop {
2335-
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)})?,
2336-
node_features: node_features.clone(),
2337-
short_channel_id: payment_hop.candidate.short_channel_id().unwrap(),
2338-
channel_features: payment_hop.candidate.features(),
2339-
fee_msat: payment_hop.fee_msat,
2340-
cltv_expiry_delta: payment_hop.candidate.cltv_expiry_delta(),
2341-
})
2342-
}).collect::<Vec<_>>();
2332+
let mut hops = Vec::with_capacity(payment_path.hops.len());
2333+
for (hop, node_features) in payment_path.hops.iter()
2334+
.filter(|(h, _)| h.candidate.short_channel_id().is_some())
2335+
{
2336+
hops.push(RouteHop {
2337+
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)})?,
2338+
node_features: node_features.clone(),
2339+
short_channel_id: hop.candidate.short_channel_id().unwrap(),
2340+
channel_features: hop.candidate.features(),
2341+
fee_msat: hop.fee_msat,
2342+
cltv_expiry_delta: hop.candidate.cltv_expiry_delta(),
2343+
});
2344+
}
23432345
// Propagate the cltv_expiry_delta one hop backwards since the delta from the current hop is
23442346
// applicable for the previous hop.
2345-
path.iter_mut().rev().fold(final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
2346-
core::mem::replace(&mut hop.as_mut().unwrap().cltv_expiry_delta, prev_cltv_expiry_delta)
2347+
hops.iter_mut().rev().fold(final_cltv_expiry_delta, |prev_cltv_expiry_delta, hop| {
2348+
core::mem::replace(&mut hop.cltv_expiry_delta, prev_cltv_expiry_delta)
23472349
});
2348-
selected_paths.push(path);
2350+
paths.push(Path { hops, blinded_tail: None });
23492351
}
23502352
// Make sure we would never create a route with more paths than we allow.
2351-
debug_assert!(selected_paths.len() <= payment_params.max_path_count.into());
2353+
debug_assert!(paths.len() <= payment_params.max_path_count.into());
23522354

23532355
if let Some(node_features) = payment_params.payee.node_features() {
2354-
for path in selected_paths.iter_mut() {
2355-
if let Ok(route_hop) = path.last_mut().unwrap() {
2356-
route_hop.node_features = node_features.clone();
2357-
}
2356+
for path in paths.iter_mut() {
2357+
path.hops.last_mut().unwrap().node_features = node_features.clone();
23582358
}
23592359
}
23602360

2361-
let mut paths: Vec<Path> = Vec::new();
2362-
for results_vec in selected_paths {
2363-
let mut hops = Vec::with_capacity(results_vec.len());
2364-
for res in results_vec { hops.push(res?); }
2365-
paths.push(Path { hops, blinded_tail: None });
2366-
}
2367-
let route = Route {
2368-
paths,
2369-
payment_params: Some(payment_params.clone()),
2370-
};
2361+
let route = Route { paths, payment_params: Some(payment_params.clone()) };
23712362
log_info!(logger, "Got route: {}", log_route!(route));
23722363
Ok(route)
23732364
}

0 commit comments

Comments
 (0)