Skip to content

Commit d0084c2

Browse files
committed
Make CandidateRouteHop::PrivateHop::target_node_id a reference
This avoids bloating `CandidateRouteHop` with a full 33-byte node_id (and avoids repeated public key serialization when we do multiple pathfinding passes).
1 parent 2b7d097 commit d0084c2

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

lightning/src/routing/router.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,7 +1039,7 @@ pub enum CandidateRouteHop<'a> {
10391039
/// Information about the private hop communicated via BOLT 11.
10401040
hint: &'a RouteHintHop,
10411041
/// Node id of the next hop in BOLT 11 route hint.
1042-
target_node_id: NodeId
1042+
target_node_id: &'a NodeId
10431043
},
10441044
/// A blinded path which starts with an introduction point and ultimately terminates with the
10451045
/// payee.
@@ -1250,7 +1250,7 @@ impl<'a> CandidateRouteHop<'a> {
12501250
match self {
12511251
CandidateRouteHop::FirstHop { details, .. } => Some(details.counterparty.node_id.into()),
12521252
CandidateRouteHop::PublicHop { info, .. } => Some(*info.target()),
1253-
CandidateRouteHop::PrivateHop { target_node_id, .. } => Some(*target_node_id),
1253+
CandidateRouteHop::PrivateHop { target_node_id, .. } => Some(**target_node_id),
12541254
CandidateRouteHop::Blinded { .. } => None,
12551255
CandidateRouteHop::OneHopBlinded { .. } => None,
12561256
}
@@ -1795,6 +1795,20 @@ where L::Target: Logger {
17951795
}
17961796
}
17971797

1798+
let mut private_hop_key_cache = HashMap::with_capacity(
1799+
payment_params.payee.unblinded_route_hints().iter().map(|path| path.0.len()).sum()
1800+
);
1801+
1802+
// Because we store references to private hop node_ids in `dist`, below, we need them to exist
1803+
// (as `NodeId`, not `PublicKey`) for the lifetime of `dist`. Thus, we calculate all the keys
1804+
// we'll need here and simply fetch them when routing.
1805+
private_hop_key_cache.insert(maybe_dummy_payee_pk, NodeId::from_pubkey(&maybe_dummy_payee_pk));
1806+
for route in payment_params.payee.unblinded_route_hints().iter() {
1807+
for hop in route.0.iter() {
1808+
private_hop_key_cache.insert(hop.src_node_id, NodeId::from_pubkey(&hop.src_node_id));
1809+
}
1810+
}
1811+
17981812
// The main heap containing all candidate next-hops sorted by their score (max(fee,
17991813
// htlc_minimum)). Ideally this would be a heap which allowed cheap score reduction instead of
18001814
// adding duplicate entries when we find a better path to a given node.
@@ -2353,8 +2367,7 @@ where L::Target: Logger {
23532367
let mut aggregate_path_contribution_msat = path_value_msat;
23542368

23552369
for (idx, (hop, prev_hop_id)) in hop_iter.zip(prev_hop_iter).enumerate() {
2356-
let source = NodeId::from_pubkey(&hop.src_node_id);
2357-
let target = NodeId::from_pubkey(&prev_hop_id);
2370+
let target = private_hop_key_cache.get(&prev_hop_id).unwrap();
23582371

23592372
if let Some(first_channels) = first_hop_targets.get(&target) {
23602373
if first_channels.iter().any(|d| d.outbound_scid_alias == Some(hop.short_channel_id)) {

lightning/src/util/test_utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl<'a> Router for TestRouter<'a> {
171171
};
172172
let candidate = CandidateRouteHop::PrivateHop {
173173
hint: &route_hint,
174-
target_node_id: target_node_id,
174+
target_node_id: &target_node_id,
175175
};
176176
scorer.channel_penalty_msat(&candidate, usage, &());
177177
}

0 commit comments

Comments
 (0)