Skip to content

Commit 12a78b0

Browse files
committed
Introduce RouteParamsV2 & UserParameters & InvoiceParameters
1. Introduce Readable & Writable for both the struct
1 parent 6004ee5 commit 12a78b0

File tree

1 file changed

+144
-0
lines changed

1 file changed

+144
-0
lines changed

lightning/src/routing/router.rs

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,6 +962,150 @@ impl PaymentParameters {
962962
}
963963
}
964964

965+
pub struct RouteParametersV2 {
966+
/// Parameters specified by user. Defaults to default if not provided.
967+
pub user_params: UserParameters,
968+
// Parameters derived from the Invoice
969+
pub invoice_params: InvoiceParameters,
970+
/// The amount in msats sent on the failed payment path.
971+
pub final_value_msat: u64,
972+
}
973+
974+
impl_writeable_tlv_based!(RouteParametersV2, {
975+
(0, user_params, required),
976+
(2, invoice_params, required),
977+
(4, final_value_msat, required)
978+
});
979+
980+
#[derive(Clone, Copy)]
981+
pub struct UserParameters {
982+
/// The maximum total fees, in millisatoshi, that may accrue during route finding.
983+
///
984+
/// This limit also applies to the total fees that may arise while retrying failed payment
985+
/// paths.
986+
///
987+
/// Note that values below a few sats may result in some paths being spuriously ignored.
988+
pub max_total_routing_fee_msat: Option<u64>,
989+
990+
/// The maximum total CLTV delta we accept for the route.
991+
/// Defaults to [`DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA`].
992+
pub max_total_cltv_expiry_delta: u32,
993+
994+
/// The maximum number of paths that may be used by (MPP) payments.
995+
/// Defaults to [`DEFAULT_MAX_PATH_COUNT`].
996+
pub max_path_count: u8,
997+
998+
/// The maximum number of [`Path::hops`] in any returned path.
999+
/// Defaults to [`MAX_PATH_LENGTH_ESTIMATE`].
1000+
pub max_path_length: u8,
1001+
1002+
/// Selects the maximum share of a channel's total capacity which will be sent over a channel,
1003+
/// as a power of 1/2. A higher value prefers to send the payment using more MPP parts whereas
1004+
/// a lower value prefers to send larger MPP parts, potentially saturating channels and
1005+
/// increasing failure probability for those paths.
1006+
///
1007+
/// Note that this restriction will be relaxed during pathfinding after paths which meet this
1008+
/// restriction have been found. While paths which meet this criteria will be searched for, it
1009+
/// is ultimately up to the scorer to select them over other paths.
1010+
///
1011+
/// A value of 0 will allow payments up to and including a channel's total announced usable
1012+
/// capacity, a value of one will only use up to half its capacity, two 1/4, etc.
1013+
///
1014+
/// Default value: [`DEFAULT_MAX_CHANNEL_SATURATION_POW_HALF`]
1015+
pub max_channel_saturation_power_of_half: u8,
1016+
}
1017+
1018+
impl_writeable_tlv_based!(UserParameters, {
1019+
(1, max_total_routing_fee_msat, option),
1020+
(3, max_total_cltv_expiry_delta, (default_value, DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA)),
1021+
(5, max_path_count, (default_value, DEFAULT_MAX_PATH_COUNT)),
1022+
(7, max_path_length, (default_value, MAX_PATH_LENGTH_ESTIMATE)),
1023+
(9, max_channel_saturation_power_of_half, (default_value, DEFAULT_MAX_CHANNEL_SATURATION_POW_HALF))
1024+
});
1025+
1026+
pub struct InvoiceParameters {
1027+
/// Information about the payee, such as their features and route hints for their channels.
1028+
pub payee: Payee,
1029+
1030+
/// Expiration of a payment to the payee, in seconds relative to the UNIX epoch.
1031+
pub expiry_time: Option<u64>,
1032+
1033+
/// A list of SCIDs which this payment was previously attempted over and which caused the
1034+
/// payment to fail. Future attempts for the same payment shouldn't be relayed through any of
1035+
/// these SCIDs.
1036+
pub previously_failed_channels: Vec<u64>,
1037+
1038+
/// A list of indices corresponding to blinded paths in [`Payee::Blinded::route_hints`] which this
1039+
/// payment was previously attempted over and which caused the payment to fail. Future attempts
1040+
/// for the same payment shouldn't be relayed through any of these blinded paths.
1041+
pub previously_failed_blinded_path_idxs: Vec<u64>,
1042+
}
1043+
1044+
impl Writeable for InvoiceParameters {
1045+
fn write<W: Writer>(&self, writer: &mut W) -> Result<(), io::Error> {
1046+
let mut clear_hints = &vec![];
1047+
let mut blinded_hints = None;
1048+
match &self.payee {
1049+
Payee::Clear { route_hints, .. } => clear_hints = route_hints,
1050+
Payee::Blinded { route_hints, .. } => {
1051+
let hints_iter = route_hints.iter().map(|path| (&path.payinfo, path.inner_blinded_path()));
1052+
blinded_hints = Some(crate::util::ser::IterableOwned(hints_iter));
1053+
}
1054+
}
1055+
write_tlv_fields!(writer, {
1056+
(0, self.payee.node_id(), option),
1057+
(2, self.payee.features(), option),
1058+
(4, *clear_hints, required_vec),
1059+
(6, self.expiry_time, option),
1060+
(7, self.previously_failed_channels, required_vec),
1061+
(8, blinded_hints, option),
1062+
(9, self.payee.final_cltv_expiry_delta(), option),
1063+
(11, self.previously_failed_blinded_path_idxs, required_vec),
1064+
});
1065+
Ok(())
1066+
}
1067+
}
1068+
1069+
impl ReadableArgs<u32> for InvoiceParameters {
1070+
fn read<R: io::Read>(reader: &mut R, default_final_cltv_expiry_delta: u32) -> Result<Self, DecodeError> {
1071+
_init_and_read_len_prefixed_tlv_fields!(reader, {
1072+
(0, payee_pubkey, option),
1073+
(2, features, (option: ReadableArgs, payee_pubkey.is_some())),
1074+
(4, clear_route_hints, required_vec),
1075+
(6, expiry_time, option),
1076+
(7, previously_failed_channels, optional_vec),
1077+
(8, blinded_route_hints, optional_vec),
1078+
(9, final_cltv_expiry_delta, (default_value, default_final_cltv_expiry_delta)),
1079+
(11, previously_failed_blinded_path_idxs, optional_vec),
1080+
});
1081+
let blinded_route_hints = blinded_route_hints.unwrap_or(vec![]);
1082+
let payee = if blinded_route_hints.len() != 0 {
1083+
if clear_route_hints.len() != 0 || payee_pubkey.is_some() { return Err(DecodeError::InvalidValue) }
1084+
Payee::Blinded {
1085+
route_hints: blinded_route_hints
1086+
.into_iter()
1087+
.map(|(payinfo, path)| BlindedPaymentPath::from_parts(path, payinfo))
1088+
.collect(),
1089+
features: features.and_then(|f: Features| f.bolt12()),
1090+
}
1091+
} else {
1092+
Payee::Clear {
1093+
route_hints: clear_route_hints,
1094+
node_id: payee_pubkey.ok_or(DecodeError::InvalidValue)?,
1095+
features: features.and_then(|f| f.bolt11()),
1096+
final_cltv_expiry_delta: final_cltv_expiry_delta.0.unwrap(),
1097+
}
1098+
};
1099+
1100+
Ok(Self {
1101+
payee,
1102+
expiry_time,
1103+
previously_failed_channels: previously_failed_channels.unwrap_or(Vec::new()),
1104+
previously_failed_blinded_path_idxs: previously_failed_blinded_path_idxs.unwrap_or(Vec::new()),
1105+
})
1106+
}
1107+
}
1108+
9651109
/// The recipient of a payment, differing based on whether they've hidden their identity with route
9661110
/// blinding.
9671111
#[derive(Clone, Debug, Hash, PartialEq, Eq)]

0 commit comments

Comments
 (0)