|
10 | 10 | use crate::blinded_path::BlindedHop;
|
11 | 11 | use crate::crypto::chacha20::ChaCha20;
|
12 | 12 | use crate::crypto::streams::ChaChaReader;
|
| 13 | +use crate::ln::channel::TOTAL_BITCOIN_SUPPLY_SATOSHIS; |
13 | 14 | use crate::ln::channelmanager::{HTLCSource, RecipientOnionFields};
|
| 15 | +use crate::ln::features::{ChannelFeatures, NodeFeatures}; |
14 | 16 | use crate::ln::msgs;
|
15 | 17 | use crate::ln::types::{PaymentHash, PaymentPreimage};
|
16 | 18 | use crate::ln::wire::Encode;
|
17 | 19 | use crate::routing::gossip::NetworkUpdate;
|
18 |
| -use crate::routing::router::{Path, RouteHop}; |
| 20 | +use crate::routing::router::{Path, RouteHop, RouteParameters, MAX_PATH_LENGTH_ESTIMATE}; |
19 | 21 | use crate::sign::NodeSigner;
|
20 | 22 | use crate::util::errors::{self, APIError};
|
21 | 23 | use crate::util::logger::Logger;
|
@@ -310,6 +312,81 @@ where
|
310 | 312 | Ok((cur_value_msat, cur_cltv))
|
311 | 313 | }
|
312 | 314 |
|
| 315 | +pub(crate) const MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY: u64 = 100_000_000; |
| 316 | + |
| 317 | +pub(crate) fn set_max_path_length( |
| 318 | + route_params: &mut RouteParameters, recipient_onion: &RecipientOnionFields, |
| 319 | + keysend_preimage: Option<PaymentPreimage>, best_block_height: u32, |
| 320 | +) -> Result<(), ()> { |
| 321 | + const PAYLOAD_HMAC_LEN: usize = 32; |
| 322 | + let unblinded_intermed_payload_len = msgs::OutboundOnionPayload::Forward { |
| 323 | + short_channel_id: 42, |
| 324 | + amt_to_forward: TOTAL_BITCOIN_SUPPLY_SATOSHIS, |
| 325 | + outgoing_cltv_value: route_params.payment_params.max_total_cltv_expiry_delta, |
| 326 | + } |
| 327 | + .serialized_length() |
| 328 | + .saturating_add(PAYLOAD_HMAC_LEN); |
| 329 | + |
| 330 | + const OVERPAY_ESTIMATE_MULTIPLER: u64 = 3; |
| 331 | + let final_value_msat_with_overpay_buffer = core::cmp::max( |
| 332 | + route_params.final_value_msat.saturating_mul(OVERPAY_ESTIMATE_MULTIPLER), |
| 333 | + MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY, |
| 334 | + ); |
| 335 | + |
| 336 | + let (num_reserved_hops, blinded_tail_opt) = route_params |
| 337 | + .payment_params |
| 338 | + .payee |
| 339 | + .blinded_route_hints() |
| 340 | + .iter() |
| 341 | + .map(|(_, path)| path) |
| 342 | + .max_by_key(|path| path.serialized_length()) |
| 343 | + .map(|largest_path| { |
| 344 | + let blinded_tail = BlindedTailHopIter { |
| 345 | + hops: largest_path.blinded_hops.iter(), |
| 346 | + blinding_point: largest_path.blinding_point, |
| 347 | + final_value_msat: final_value_msat_with_overpay_buffer, |
| 348 | + excess_final_cltv_expiry_delta: 0, |
| 349 | + }; |
| 350 | + (largest_path.blinded_hops.len(), Some(blinded_tail)) |
| 351 | + }) |
| 352 | + .unwrap_or((1, None)); |
| 353 | + |
| 354 | + let unblinded_route_hop = RouteHop { |
| 355 | + pubkey: PublicKey::from_slice(&[2; 33]).unwrap(), |
| 356 | + node_features: NodeFeatures::empty(), |
| 357 | + short_channel_id: 42, |
| 358 | + channel_features: ChannelFeatures::empty(), |
| 359 | + fee_msat: final_value_msat_with_overpay_buffer, |
| 360 | + cltv_expiry_delta: route_params.payment_params.max_total_cltv_expiry_delta, |
| 361 | + maybe_announced_channel: false, |
| 362 | + }; |
| 363 | + let mut num_reserved_bytes: usize = 0; |
| 364 | + let build_payloads_res = build_onion_payloads_callback( |
| 365 | + core::iter::once(&unblinded_route_hop), |
| 366 | + blinded_tail_opt, |
| 367 | + final_value_msat_with_overpay_buffer, |
| 368 | + &recipient_onion, |
| 369 | + best_block_height, |
| 370 | + &keysend_preimage, |
| 371 | + |_, payload| { |
| 372 | + num_reserved_bytes = num_reserved_bytes |
| 373 | + .saturating_add(payload.serialized_length()) |
| 374 | + .saturating_add(PAYLOAD_HMAC_LEN); |
| 375 | + }, |
| 376 | + ); |
| 377 | + debug_assert!(build_payloads_res.is_ok()); |
| 378 | + |
| 379 | + let max_path_length = 1300usize |
| 380 | + .checked_sub(num_reserved_bytes) |
| 381 | + .map(|p| p / unblinded_intermed_payload_len) |
| 382 | + .and_then(|l| u8::try_from(l.saturating_add(num_reserved_hops)).ok()) |
| 383 | + .ok_or(())?; |
| 384 | + |
| 385 | + route_params.payment_params.max_path_length = |
| 386 | + core::cmp::min(max_path_length, MAX_PATH_LENGTH_ESTIMATE); |
| 387 | + Ok(()) |
| 388 | +} |
| 389 | + |
313 | 390 | /// Length of the onion data packet. Before TLV-based onions this was 20 65-byte hops, though now
|
314 | 391 | /// the hops can be of variable length.
|
315 | 392 | pub(crate) const ONION_DATA_LEN: usize = 20 * 65;
|
|
0 commit comments