Skip to content

Commit 6a4a66e

Browse files
committed
Introduce RouteParametersV2 implementations
Also introduces a new set_max_path_length_v2 function that is thei same as the original function but is for RouteParametersV2
1 parent c5f233b commit 6a4a66e

File tree

2 files changed

+87
-3
lines changed

2 files changed

+87
-3
lines changed

lightning/src/ln/onion_utils.rs

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::ln::features::{ChannelFeatures, NodeFeatures};
1616
use crate::ln::msgs;
1717
use crate::ln::types::{PaymentHash, PaymentPreimage};
1818
use crate::routing::gossip::NetworkUpdate;
19-
use crate::routing::router::{Path, RouteHop, RouteParameters};
19+
use crate::routing::router::{Path, RouteHop, RouteParameters, RouteParametersV2};
2020
use crate::sign::NodeSigner;
2121
use crate::util::errors::{self, APIError};
2222
use crate::util::logger::Logger;
@@ -381,6 +381,74 @@ pub(crate) fn set_max_path_length(
381381
Ok(())
382382
}
383383

384+
pub(crate) fn set_max_path_length_v2(
385+
route_params: &mut RouteParametersV2, recipient_onion: &RecipientOnionFields,
386+
keysend_preimage: Option<PaymentPreimage>, best_block_height: u32,
387+
) -> Result<(), ()> {
388+
const PAYLOAD_HMAC_LEN: usize = 32;
389+
let unblinded_intermed_payload_len = msgs::OutboundOnionPayload::Forward {
390+
short_channel_id: 42,
391+
amt_to_forward: TOTAL_BITCOIN_SUPPLY_SATOSHIS,
392+
outgoing_cltv_value: route_params.user_params.max_total_cltv_expiry_delta,
393+
}
394+
.serialized_length()
395+
.saturating_add(PAYLOAD_HMAC_LEN);
396+
397+
const OVERPAY_ESTIMATE_MULTIPLER: u64 = 3;
398+
let final_value_msat_with_overpay_buffer = core::cmp::max(
399+
route_params.final_value_msat.saturating_mul(OVERPAY_ESTIMATE_MULTIPLER),
400+
MIN_FINAL_VALUE_ESTIMATE_WITH_OVERPAY,
401+
);
402+
403+
let blinded_tail_opt = route_params
404+
.invoice_params
405+
.payee
406+
.blinded_route_hints()
407+
.iter()
408+
.max_by_key(|path| path.inner_blinded_path().serialized_length())
409+
.map(|largest_path| BlindedTailHopIter {
410+
hops: largest_path.blinded_hops().iter(),
411+
blinding_point: largest_path.blinding_point(),
412+
final_value_msat: final_value_msat_with_overpay_buffer,
413+
excess_final_cltv_expiry_delta: 0,
414+
});
415+
416+
let unblinded_route_hop = RouteHop {
417+
pubkey: PublicKey::from_slice(&[2; 33]).unwrap(),
418+
node_features: NodeFeatures::empty(),
419+
short_channel_id: 42,
420+
channel_features: ChannelFeatures::empty(),
421+
fee_msat: final_value_msat_with_overpay_buffer,
422+
cltv_expiry_delta: route_params.user_params.max_total_cltv_expiry_delta,
423+
maybe_announced_channel: false,
424+
};
425+
let mut num_reserved_bytes: usize = 0;
426+
let build_payloads_res = build_onion_payloads_callback(
427+
core::iter::once(&unblinded_route_hop),
428+
blinded_tail_opt,
429+
final_value_msat_with_overpay_buffer,
430+
&recipient_onion,
431+
best_block_height,
432+
&keysend_preimage,
433+
|_, payload| {
434+
num_reserved_bytes = num_reserved_bytes
435+
.saturating_add(payload.serialized_length())
436+
.saturating_add(PAYLOAD_HMAC_LEN);
437+
},
438+
);
439+
debug_assert!(build_payloads_res.is_ok());
440+
441+
let max_path_length = 1300usize
442+
.checked_sub(num_reserved_bytes)
443+
.map(|p| p / unblinded_intermed_payload_len)
444+
.and_then(|l| u8::try_from(l.saturating_add(1)).ok())
445+
.ok_or(())?;
446+
447+
route_params.user_params.max_path_length =
448+
core::cmp::min(max_path_length, route_params.user_params.max_path_length);
449+
Ok(())
450+
}
451+
384452
/// Length of the onion data packet. Before TLV-based onions this was 20 65-byte hops, though now
385453
/// the hops can be of variable length.
386454
pub(crate) const ONION_DATA_LEN: usize = 20 * 65;

lightning/src/routing/router.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -998,9 +998,25 @@ impl Readable for RouteParametersV2 {
998998
}
999999

10001000
impl RouteParametersV2 {
1001-
// TODO: Introduce a function parallel to fn from_payment_params_and_value.
1001+
/// Constructs [`RouteParameters`] from the given [`PaymentParameters`] and a payment amount.
1002+
///
1003+
/// [`Self::max_total_routing_fee_msat`] defaults to 1% of the payment amount + 50 sats
1004+
pub fn from_params_and_value(mut user_params: UserParameters, invoice_params: InvoiceParameters, final_value_msat: u64) -> Self {
1005+
user_params.max_total_routing_fee_msat.get_or_insert_with(|| final_value_msat / 100 + 50_000);
10021006

1003-
// TODO: Introduce fn set_max_path_length(). First create onion_utils::set_max_path_length_v2
1007+
Self { user_params, invoice_params, final_value_msat }
1008+
}
1009+
1010+
/// Sets the maximum number of hops that can be included in a payment path, based on the provided
1011+
/// [`RecipientOnionFields`] and blinded paths.
1012+
pub fn set_max_path_length(
1013+
&mut self, recipient_onion: &RecipientOnionFields, is_keysend: bool, best_block_height: u32
1014+
) -> Result<(), ()> {
1015+
let keysend_preimage_opt = is_keysend.then(|| PaymentPreimage([42; 32]));
1016+
onion_utils::set_max_path_length_v2(
1017+
self, recipient_onion, keysend_preimage_opt, best_block_height
1018+
)
1019+
}
10041020
}
10051021

10061022
#[derive(Clone, Copy)]

0 commit comments

Comments
 (0)