Skip to content

Commit 52a3195

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 64e6695 commit 52a3195

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
@@ -1038,7 +1038,7 @@ pub enum CandidateRouteHop<'a> {
10381038
/// Information about the private hop communicated via BOLT 11.
10391039
hint: &'a RouteHintHop,
10401040
/// Node id of the next hop in BOLT 11 route hint.
1041-
target_node_id: NodeId
1041+
target_node_id: &'a NodeId
10421042
},
10431043
/// A blinded path which starts with an introduction point and ultimately terminates with the
10441044
/// payee.
@@ -1249,7 +1249,7 @@ impl<'a> CandidateRouteHop<'a> {
12491249
match self {
12501250
CandidateRouteHop::FirstHop { details, .. } => Some(details.counterparty.node_id.into()),
12511251
CandidateRouteHop::PublicHop { info, .. } => Some(*info.target()),
1252-
CandidateRouteHop::PrivateHop { target_node_id, .. } => Some(*target_node_id),
1252+
CandidateRouteHop::PrivateHop { target_node_id, .. } => Some(**target_node_id),
12531253
CandidateRouteHop::Blinded { .. } => None,
12541254
CandidateRouteHop::OneHopBlinded { .. } => None,
12551255
}
@@ -1794,6 +1794,20 @@ where L::Target: Logger {
17941794
}
17951795
}
17961796

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

23542368
for (idx, (hop, prev_hop_id)) in hop_iter.zip(prev_hop_iter).enumerate() {
2355-
let source = NodeId::from_pubkey(&hop.src_node_id);
2356-
let target = NodeId::from_pubkey(&prev_hop_id);
2369+
let target = private_hop_key_cache.get(&prev_hop_id).unwrap();
23572370

23582371
if let Some(first_channels) = first_hop_targets.get(&target) {
23592372
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)