+pub(crate) const MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY: u64 = 100_000_000;
+
+pub(crate) fn set_max_path_length(
+ route_params: &mut RouteParameters, recipient_onion: &RecipientOnionFields,
+ keysend_preimage: Option<PaymentPreimage>, best_block_height: u32,
+) -> Result<(), ()> {
+ const PAYLOAD_HMAC_LEN: usize = 32;
+ let unblinded_intermed_payload_len = msgs::OutboundOnionPayload::Forward {
+ short_channel_id: 42,
+ amt_to_forward: TOTAL_BITCOIN_SUPPLY_SATOSHIS,
+ outgoing_cltv_value: route_params.payment_params.max_total_cltv_expiry_delta,
+ }
+ .serialized_length()
+ .saturating_add(PAYLOAD_HMAC_LEN);
+
+ const OVERPAY_ESTIMATE_MULTIPLER: u64 = 3;
+ let final_value_msat_with_overpay_buffer = core::cmp::max(
+ route_params.final_value_msat.saturating_mul(OVERPAY_ESTIMATE_MULTIPLER),
+ MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY,
+ );
+
+ let blinded_tail_opt = route_params
+ .payment_params
+ .payee
+ .blinded_route_hints()
+ .iter()
+ .map(|(_, path)| path)
+ .max_by_key(|path| path.serialized_length())
+ .map(|largest_path| BlindedTailHopIter {
+ hops: largest_path.blinded_hops.iter(),
+ blinding_point: largest_path.blinding_point,
+ final_value_msat: final_value_msat_with_overpay_buffer,
+ excess_final_cltv_expiry_delta: 0,
+ });
+
+ let unblinded_route_hop = RouteHop {
+ pubkey: PublicKey::from_slice(&[2; 33]).unwrap(),
+ node_features: NodeFeatures::empty(),
+ short_channel_id: 42,
+ channel_features: ChannelFeatures::empty(),
+ fee_msat: final_value_msat_with_overpay_buffer,
+ cltv_expiry_delta: route_params.payment_params.max_total_cltv_expiry_delta,
+ maybe_announced_channel: false,
+ };
+ let mut num_reserved_bytes: usize = 0;
+ let build_payloads_res = build_onion_payloads_callback(
+ core::iter::once(&unblinded_route_hop),
+ blinded_tail_opt,
+ final_value_msat_with_overpay_buffer,
+ &recipient_onion,
+ best_block_height,
+ &keysend_preimage,
+ |_, payload| {
+ num_reserved_bytes = num_reserved_bytes
+ .saturating_add(payload.serialized_length())
+ .saturating_add(PAYLOAD_HMAC_LEN);
+ },
+ );
+ debug_assert!(build_payloads_res.is_ok());
+
+ let max_path_length = 1300usize
+ .checked_sub(num_reserved_bytes)
+ .map(|p| p / unblinded_intermed_payload_len)
+ .and_then(|l| u8::try_from(l.saturating_add(1)).ok())
+ .ok_or(())?;
+
+ route_params.payment_params.max_path_length =
+ core::cmp::min(max_path_length, route_params.payment_params.max_path_length);
+ Ok(())
+}
+