Skip to content

Commit e4a7e8c

Browse files
committed
Introduce the impl functions for UserParameters & InvoiceParameters
1 parent 12a78b0 commit e4a7e8c

File tree

1 file changed

+173
-0
lines changed

1 file changed

+173
-0
lines changed

lightning/src/routing/router.rs

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,12 @@ impl_writeable_tlv_based!(RouteParametersV2, {
977977
(4, final_value_msat, required)
978978
});
979979

980+
impl RouteParametersV2 {
981+
// TODO: Introduce a function parallel to fn from_payment_params_and_value.
982+
983+
// TODO: Introduce fn set_max_path_length(). First create onion_utils::set_max_path_length_v2
984+
}
985+
980986
#[derive(Clone, Copy)]
981987
pub struct UserParameters {
982988
/// The maximum total fees, in millisatoshi, that may accrue during route finding.
@@ -1023,6 +1029,48 @@ impl_writeable_tlv_based!(UserParameters, {
10231029
(9, max_channel_saturation_power_of_half, (default_value, DEFAULT_MAX_CHANNEL_SATURATION_POW_HALF))
10241030
});
10251031

1032+
impl UserParameters {
1033+
/// Creates a new set of Parameters with default values.
1034+
pub fn new() -> Self {
1035+
Self {
1036+
max_total_routing_fee_msat: None,
1037+
max_total_cltv_expiry_delta: DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
1038+
max_path_count: DEFAULT_MAX_PATH_COUNT,
1039+
max_path_length: MAX_PATH_LENGTH_ESTIMATE,
1040+
max_channel_saturation_power_of_half: DEFAULT_MAX_CHANNEL_SATURATION_POW_HALF,
1041+
}
1042+
}
1043+
1044+
/// Inroduce a limit for the maximum total fees, in millisatoshi, that may accrue during route finding.
1045+
///
1046+
/// This is not exported to bindings users since bindings don't support move semantics
1047+
pub fn with_max_total_routing_fee_msat(self, max_total_routing_fee_msat: u64) -> Self {
1048+
Self { max_total_routing_fee_msat: Some(max_total_routing_fee_msat), ..self }
1049+
}
1050+
1051+
/// Includes a limit for the total CLTV expiry delta which is considered during routing
1052+
///
1053+
/// This is not exported to bindings users since bindings don't support move semantics
1054+
pub fn with_max_total_cltv_expiry_delta(self, max_total_cltv_expiry_delta: u32) -> Self {
1055+
Self { max_total_cltv_expiry_delta, ..self }
1056+
}
1057+
1058+
/// Includes a limit for the maximum number of payment paths that may be used.
1059+
///
1060+
/// This is not exported to bindings users since bindings don't support move semantics
1061+
pub fn with_max_path_count(self, max_path_count: u8) -> Self {
1062+
Self { max_path_count, ..self }
1063+
}
1064+
1065+
/// Includes a limit for the maximum share of a channel's total capacity that can be sent over, as
1066+
/// a power of 1/2. See [`PaymentParameters::max_channel_saturation_power_of_half`].
1067+
///
1068+
/// This is not exported to bindings users since bindings don't support move semantics
1069+
pub fn with_max_channel_saturation_power_of_half(self, max_channel_saturation_power_of_half: u8) -> Self {
1070+
Self { max_channel_saturation_power_of_half, ..self }
1071+
}
1072+
}
1073+
10261074
pub struct InvoiceParameters {
10271075
/// Information about the payee, such as their features and route hints for their channels.
10281076
pub payee: Payee,
@@ -1106,6 +1154,131 @@ impl ReadableArgs<u32> for InvoiceParameters {
11061154
}
11071155
}
11081156

1157+
impl InvoiceParameters {
1158+
/// Creates a payee with the node id of the given `pubkey`.
1159+
///
1160+
/// The `final_cltv_expiry_delta` should match the expected final CLTV delta the recipient has
1161+
/// provided.
1162+
pub fn from_node_id(payee_pubkey: PublicKey, final_cltv_expiry_delta: u32) -> Self {
1163+
Self {
1164+
payee: Payee::Clear { node_id: payee_pubkey, route_hints: vec![], features: None, final_cltv_expiry_delta },
1165+
expiry_time: None,
1166+
previously_failed_channels: Vec::new(),
1167+
previously_failed_blinded_path_idxs: Vec::new(),
1168+
}
1169+
}
1170+
1171+
/// Creates a payee with the node id of the given `pubkey` to use for keysend payments.
1172+
///
1173+
/// The `final_cltv_expiry_delta` should match the expected final CLTV delta the recipient has
1174+
/// provided.
1175+
///
1176+
/// Note that MPP keysend is not widely supported yet. The `allow_mpp` lets you choose
1177+
/// whether your router will be allowed to find a multi-part route for this payment. If you
1178+
/// set `allow_mpp` to true, you should ensure a payment secret is set on send, likely via
1179+
/// [`RecipientOnionFields::secret_only`].
1180+
///
1181+
/// [`RecipientOnionFields::secret_only`]: crate::ln::channelmanager::RecipientOnionFields::secret_only
1182+
pub fn for_keysend(payee_pubkey: PublicKey, final_cltv_expiry_delta: u32, allow_mpp: bool) -> Self {
1183+
Self::from_node_id(payee_pubkey, final_cltv_expiry_delta)
1184+
.with_bolt11_features(Bolt11InvoiceFeatures::for_keysend(allow_mpp))
1185+
.expect("PaymentParameters::from_node_id should always initialize the payee as unblinded")
1186+
}
1187+
1188+
/// Creates parameters for paying to a blinded payee from the provided invoice. Sets
1189+
/// [`Payee::Blinded::route_hints`], [`Payee::Blinded::features`], and
1190+
/// [`PaymentParameters::expiry_time`].
1191+
pub fn from_bolt12_invoice(invoice: &Bolt12Invoice) -> Self {
1192+
Self::blinded(invoice.payment_paths().to_vec())
1193+
.with_bolt12_features(invoice.invoice_features().clone()).unwrap()
1194+
.with_expiry_time(invoice.created_at().as_secs().saturating_add(invoice.relative_expiry().as_secs()))
1195+
}
1196+
1197+
/// Creates parameters for paying to a blinded payee from the provided invoice. Sets
1198+
/// [`Payee::Blinded::route_hints`], [`Payee::Blinded::features`], and
1199+
/// [`PaymentParameters::expiry_time`].
1200+
#[cfg(async_payments)]
1201+
pub fn from_static_invoice(invoice: &StaticInvoice) -> Self {
1202+
Self::blinded(invoice.payment_paths().to_vec())
1203+
.with_bolt12_features(invoice.invoice_features().clone()).unwrap()
1204+
.with_expiry_time(invoice.created_at().as_secs().saturating_add(invoice.relative_expiry().as_secs()))
1205+
}
1206+
1207+
/// Creates parameters for paying to a blinded payee from the provided blinded route hints.
1208+
pub fn blinded(blinded_route_hints: Vec<BlindedPaymentPath>) -> Self {
1209+
Self {
1210+
payee: Payee::Blinded { route_hints: blinded_route_hints, features: None },
1211+
expiry_time: None,
1212+
previously_failed_channels: Vec::new(),
1213+
previously_failed_blinded_path_idxs: Vec::new(),
1214+
}
1215+
}
1216+
1217+
/// Includes the payee's features. Errors if the parameters were not initialized with
1218+
/// [`PaymentParameters::from_bolt12_invoice`].
1219+
///
1220+
/// This is not exported to bindings users since bindings don't support move semantics
1221+
pub fn with_bolt12_features(self, features: Bolt12InvoiceFeatures) -> Result<Self, ()> {
1222+
match self.payee {
1223+
Payee::Clear { .. } => Err(()),
1224+
Payee::Blinded { route_hints, .. } =>
1225+
Ok(Self { payee: Payee::Blinded { route_hints, features: Some(features) }, ..self })
1226+
}
1227+
}
1228+
1229+
/// Includes the payee's features. Errors if the parameters were initialized with
1230+
/// [`PaymentParameters::from_bolt12_invoice`].
1231+
///
1232+
/// This is not exported to bindings users since bindings don't support move semantics
1233+
pub fn with_bolt11_features(self, features: Bolt11InvoiceFeatures) -> Result<Self, ()> {
1234+
match self.payee {
1235+
Payee::Blinded { .. } => Err(()),
1236+
Payee::Clear { route_hints, node_id, final_cltv_expiry_delta, .. } =>
1237+
Ok(Self {
1238+
payee: Payee::Clear {
1239+
route_hints, node_id, features: Some(features), final_cltv_expiry_delta
1240+
}, ..self
1241+
})
1242+
}
1243+
}
1244+
1245+
/// Includes hints for routing to the payee. Errors if the parameters were initialized with
1246+
/// [`PaymentParameters::from_bolt12_invoice`].
1247+
///
1248+
/// This is not exported to bindings users since bindings don't support move semantics
1249+
pub fn with_route_hints(self, route_hints: Vec<RouteHint>) -> Result<Self, ()> {
1250+
match self.payee {
1251+
Payee::Blinded { .. } => Err(()),
1252+
Payee::Clear { node_id, features, final_cltv_expiry_delta, .. } =>
1253+
Ok(Self {
1254+
payee: Payee::Clear {
1255+
route_hints, node_id, features, final_cltv_expiry_delta,
1256+
}, ..self
1257+
})
1258+
}
1259+
}
1260+
1261+
/// Includes a payment expiration in seconds relative to the UNIX epoch.
1262+
///
1263+
/// This is not exported to bindings users since bindings don't support move semantics
1264+
pub fn with_expiry_time(self, expiry_time: u64) -> Self {
1265+
Self { expiry_time: Some(expiry_time), ..self }
1266+
}
1267+
1268+
pub(crate) fn insert_previously_failed_blinded_path(&mut self, failed_blinded_tail: &BlindedTail) {
1269+
let mut found_blinded_tail = false;
1270+
for (idx, path) in self.payee.blinded_route_hints().iter().enumerate() {
1271+
if &failed_blinded_tail.hops == path.blinded_hops() &&
1272+
failed_blinded_tail.blinding_point == path.blinding_point()
1273+
{
1274+
self.previously_failed_blinded_path_idxs.push(idx as u64);
1275+
found_blinded_tail = true;
1276+
}
1277+
}
1278+
debug_assert!(found_blinded_tail);
1279+
}
1280+
}
1281+
11091282
/// The recipient of a payment, differing based on whether they've hidden their identity with route
11101283
/// blinding.
11111284
#[derive(Clone, Debug, Hash, PartialEq, Eq)]

0 commit comments

Comments
 (0)