+ let private_hop_targets_node_counter_offset = network_graph.max_node_counter() + 2;
+ let mut private_node_id_to_node_counter = HashMap::new();
+
+ let mut private_hop_key_cache = HashMap::with_capacity(
+ payment_params.payee.unblinded_route_hints().iter().map(|path| path.0.len()).sum()
+ );
+
+ // Because we store references to private hop node_ids in `dist`, below, we need them to exist
+ // (as `NodeId`, not `PublicKey`) for the lifetime of `dist`. Thus, we calculate all the keys
+ // we'll need here and simply fetch them when routing.
+ let payee_node_counter = payee_node_id_opt
+ .and_then(|payee| network_nodes.get(&payee))
+ .map(|node| node.node_counter)
+ .unwrap_or(private_hop_targets_node_counter_offset);
+ private_node_id_to_node_counter.insert(maybe_dummy_payee_node_id, payee_node_counter);
+ private_hop_key_cache.insert(maybe_dummy_payee_pk, (NodeId::from_pubkey(&maybe_dummy_payee_pk), payee_node_counter));
+
+ for route in payment_params.payee.unblinded_route_hints().iter() {
+ for hop in route.0.iter() {
+ let hop_node_id = NodeId::from_pubkey(&hop.src_node_id);
+ let node_counter = if let Some(node) = network_nodes.get(&hop_node_id) {
+ node.node_counter
+ } else {
+ let next_node_counter = private_hop_targets_node_counter_offset + private_node_id_to_node_counter.len() as u32;
+ *private_node_id_to_node_counter.entry(hop_node_id)
+ .or_insert(next_node_counter)
+ };
+ private_hop_key_cache.insert(hop.src_node_id, (hop_node_id, node_counter));
+ }
+ }
+