Skip to content

Commit c5f233b

Browse files
committed
Introduce the impl functions for UserParameters & InvoiceParameters
1 parent ba91666 commit c5f233b

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
@@ -997,6 +997,12 @@ impl Readable for RouteParametersV2 {
997997
}
998998
}
999999

1000+
impl RouteParametersV2 {
1001+
// TODO: Introduce a function parallel to fn from_payment_params_and_value.
1002+
1003+
// TODO: Introduce fn set_max_path_length(). First create onion_utils::set_max_path_length_v2
1004+
}
1005+
10001006
#[derive(Clone, Copy)]
10011007
pub struct UserParameters {
10021008
/// The maximum total fees, in millisatoshi, that may accrue during route finding.
@@ -1043,6 +1049,48 @@ impl_writeable_tlv_based!(UserParameters, {
10431049
(9, max_channel_saturation_power_of_half, (default_value, DEFAULT_MAX_CHANNEL_SATURATION_POW_HALF))
10441050
});
10451051

1052+
impl UserParameters {
1053+
/// Creates a new set of Parameters with default values.
1054+
pub fn new() -> Self {
1055+
Self {
1056+
max_total_routing_fee_msat: None,
1057+
max_total_cltv_expiry_delta: DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA,
1058+
max_path_count: DEFAULT_MAX_PATH_COUNT,
1059+
max_path_length: MAX_PATH_LENGTH_ESTIMATE,
1060+
max_channel_saturation_power_of_half: DEFAULT_MAX_CHANNEL_SATURATION_POW_HALF,
1061+
}
1062+
}
1063+
1064+
/// Inroduce a limit for the maximum total fees, in millisatoshi, that may accrue during route finding.
1065+
///
1066+
/// This is not exported to bindings users since bindings don't support move semantics
1067+
pub fn with_max_total_routing_fee_msat(self, max_total_routing_fee_msat: u64) -> Self {
1068+
Self { max_total_routing_fee_msat: Some(max_total_routing_fee_msat), ..self }
1069+
}
1070+
1071+
/// Includes a limit for the total CLTV expiry delta which is considered during routing
1072+
///
1073+
/// This is not exported to bindings users since bindings don't support move semantics
1074+
pub fn with_max_total_cltv_expiry_delta(self, max_total_cltv_expiry_delta: u32) -> Self {
1075+
Self { max_total_cltv_expiry_delta, ..self }
1076+
}
1077+
1078+
/// Includes a limit for the maximum number of payment paths that may be used.
1079+
///
1080+
/// This is not exported to bindings users since bindings don't support move semantics
1081+
pub fn with_max_path_count(self, max_path_count: u8) -> Self {
1082+
Self { max_path_count, ..self }
1083+
}
1084+
1085+
/// Includes a limit for the maximum share of a channel's total capacity that can be sent over, as
1086+
/// a power of 1/2. See [`PaymentParameters::max_channel_saturation_power_of_half`].
1087+
///
1088+
/// This is not exported to bindings users since bindings don't support move semantics
1089+
pub fn with_max_channel_saturation_power_of_half(self, max_channel_saturation_power_of_half: u8) -> Self {
1090+
Self { max_channel_saturation_power_of_half, ..self }
1091+
}
1092+
}
1093+
10461094
pub struct InvoiceParameters {
10471095
/// Information about the payee, such as their features and route hints for their channels.
10481096
pub payee: Payee,
@@ -1126,6 +1174,131 @@ impl ReadableArgs<u32> for InvoiceParameters {
11261174
}
11271175
}
11281176

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

0 commit comments

Comments
 (0)