Skip to content

Commit 38c5592

Browse files
committed
Remove node_id from PathBuildingHop
hop node id can be consumed from `CandidateRouteHop::target`
1 parent 45d35f8 commit 38c5592

File tree

1 file changed

+12
-16
lines changed

1 file changed

+12
-16
lines changed

lightning/src/routing/router.rs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,9 +1242,6 @@ fn iter_equal<I1: Iterator, I2: Iterator>(mut iter_a: I1, mut iter_b: I2)
12421242
/// These fee values are useful to choose hops as we traverse the graph "payee-to-payer".
12431243
#[derive(Clone)]
12441244
struct PathBuildingHop<'a> {
1245-
// Note that this should be dropped in favor of loading it from CandidateRouteHop, but doing so
1246-
// is a larger refactor and will require careful performance analysis.
1247-
node_id: NodeId,
12481245
candidate: CandidateRouteHop<'a>,
12491246
fee_msat: u64,
12501247

@@ -1282,7 +1279,7 @@ impl<'a> core::fmt::Debug for PathBuildingHop<'a> {
12821279
fn fmt(&self, f: &mut core::fmt::Formatter) -> Result<(), core::fmt::Error> {
12831280
let mut debug_struct = f.debug_struct("PathBuildingHop");
12841281
debug_struct
1285-
.field("node_id", &self.node_id)
1282+
.field("node_id", &self.candidate.target())
12861283
.field("short_channel_id", &self.candidate.short_channel_id())
12871284
.field("total_fee_msat", &self.total_fee_msat)
12881285
.field("next_hops_fee_msat", &self.next_hops_fee_msat)
@@ -1819,7 +1816,6 @@ where L::Target: Logger {
18191816
// - for regular channels at channel announcement (TODO)
18201817
// - for first and last hops early in get_route
18211818
let src_node_id = $candidate.source();
1822-
let dest_node_id = $candidate.target().unwrap_or(maybe_dummy_payee_node_id);
18231819
if Some($candidate.source()) != $candidate.target() {
18241820
let scid_opt = $candidate.short_channel_id();
18251821
let effective_capacity = $candidate.effective_capacity();
@@ -1935,9 +1931,8 @@ where L::Target: Logger {
19351931
// (recall it goes payee-to-payer) of short_channel_id, first add a
19361932
// semi-dummy record just to compute the fees to reach the source node.
19371933
// This will affect our decision on selecting short_channel_id
1938-
// as a way to reach the $dest_node_id.
1934+
// as a way to reach the target node id.
19391935
PathBuildingHop {
1940-
node_id: dest_node_id.clone(),
19411936
candidate: $candidate.clone(),
19421937
fee_msat: 0,
19431938
next_hops_fee_msat: u64::max_value(),
@@ -2027,7 +2022,6 @@ where L::Target: Logger {
20272022
old_entry.next_hops_fee_msat = $next_hops_fee_msat;
20282023
old_entry.hop_use_fee_msat = hop_use_fee_msat;
20292024
old_entry.total_fee_msat = total_fee_msat;
2030-
old_entry.node_id = dest_node_id.clone();
20312025
old_entry.candidate = $candidate.clone();
20322026
old_entry.fee_msat = 0; // This value will be later filled with hop_use_fee_msat of the following channel
20332027
old_entry.path_htlc_minimum_msat = path_htlc_minimum_msat;
@@ -2396,7 +2390,8 @@ where L::Target: Logger {
23962390

23972391
'path_walk: loop {
23982392
let mut features_set = false;
2399-
if let Some(first_channels) = first_hop_targets.get(&ordered_hops.last().unwrap().0.node_id) {
2393+
let target = ordered_hops.last().unwrap().0.candidate.target().unwrap_or(maybe_dummy_payee_node_id);
2394+
if let Some(first_channels) = first_hop_targets.get(&target) {
24002395
for details in first_channels {
24012396
if let Some(scid) = ordered_hops.last().unwrap().0.candidate.short_channel_id() {
24022397
if details.get_outbound_payment_scid().unwrap() == scid {
@@ -2408,7 +2403,7 @@ where L::Target: Logger {
24082403
}
24092404
}
24102405
if !features_set {
2411-
if let Some(node) = network_nodes.get(&ordered_hops.last().unwrap().0.node_id) {
2406+
if let Some(node) = network_nodes.get(&target) {
24122407
if let Some(node_info) = node.announcement_info.as_ref() {
24132408
ordered_hops.last_mut().unwrap().1 = node_info.features.clone();
24142409
} else {
@@ -2425,11 +2420,11 @@ where L::Target: Logger {
24252420
// save this path for the payment route. Also, update the liquidity
24262421
// remaining on the used hops, so that we take them into account
24272422
// while looking for more paths.
2428-
if ordered_hops.last().unwrap().0.node_id == maybe_dummy_payee_node_id {
2423+
if target == maybe_dummy_payee_node_id {
24292424
break 'path_walk;
24302425
}
24312426

2432-
new_entry = match dist.remove(&ordered_hops.last().unwrap().0.node_id) {
2427+
new_entry = match dist.remove(&target) {
24332428
Some(payment_hop) => payment_hop,
24342429
// We can't arrive at None because, if we ever add an entry to targets,
24352430
// we also fill in the entry in dist (see add_entry!).
@@ -2648,8 +2643,8 @@ where L::Target: Logger {
26482643
});
26492644
for idx in 0..(selected_route.len() - 1) {
26502645
if idx + 1 >= selected_route.len() { break; }
2651-
if iter_equal(selected_route[idx].hops.iter().map(|h| (h.0.candidate.id(), h.0.node_id)),
2652-
selected_route[idx + 1].hops.iter().map(|h| (h.0.candidate.id(), h.0.node_id))) {
2646+
if iter_equal(selected_route[idx].hops.iter().map(|h| (h.0.candidate.id(), h.0.candidate.target())),
2647+
selected_route[idx + 1].hops.iter().map(|h| (h.0.candidate.id(), h.0.candidate.target()))) {
26532648
let new_value = selected_route[idx].get_value_msat() + selected_route[idx + 1].get_value_msat();
26542649
selected_route[idx].update_value_and_recompute_fees(new_value);
26552650
selected_route.remove(idx + 1);
@@ -2662,6 +2657,7 @@ where L::Target: Logger {
26622657
for (hop, node_features) in payment_path.hops.iter()
26632658
.filter(|(h, _)| h.candidate.short_channel_id().is_some())
26642659
{
2660+
let target = hop.candidate.target().expect("target is defined when short_channel_id is defined");
26652661
let maybe_announced_channel = if let CandidateRouteHop::PublicHop { .. } = hop.candidate {
26662662
// If we sourced the hop from the graph we're sure the target node is announced.
26672663
true
@@ -2673,14 +2669,14 @@ where L::Target: Logger {
26732669
// there are announced channels between the endpoints. If so, the hop might be
26742670
// referring to any of the announced channels, as its `short_channel_id` might be
26752671
// an alias, in which case we don't take any chances here.
2676-
network_graph.node(&hop.node_id).map_or(false, |hop_node|
2672+
network_graph.node(&target).map_or(false, |hop_node|
26772673
hop_node.channels.iter().any(|scid| network_graph.channel(*scid)
26782674
.map_or(false, |c| c.as_directed_from(&hop.candidate.source()).is_some()))
26792675
)
26802676
};
26812677

26822678
hops.push(RouteHop {
2683-
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)})?,
2679+
pubkey: PublicKey::from_slice(target.as_slice()).map_err(|_| LightningError{err: format!("Public key {:?} is invalid", &target), action: ErrorAction::IgnoreAndLog(Level::Trace)})?,
26842680
node_features: node_features.clone(),
26852681
short_channel_id: hop.candidate.short_channel_id().unwrap(),
26862682
channel_features: hop.candidate.features(),

0 commit comments

Comments
 (0)