From 7bfed68b82af93331da465be59fe68bb920b69dc Mon Sep 17 00:00:00 2001 From: Ian Slane Date: Mon, 12 Aug 2024 13:46:37 -0600 Subject: [PATCH] Refactor bolt11 `send` methods to use default sending params Refactored the send methods in Bolt11Payment to combine user-set SendingParameters with default values from Config. If the user sets any parameter to None, the related default value is used. --- src/payment/bolt11.rs | 78 +++++++++++++++++++++++++++------ tests/integration_tests_rust.rs | 2 + 2 files changed, 66 insertions(+), 14 deletions(-) diff --git a/src/payment/bolt11.rs b/src/payment/bolt11.rs index 33b240d27..64c66cdd4 100644 --- a/src/payment/bolt11.rs +++ b/src/payment/bolt11.rs @@ -93,13 +93,38 @@ impl Bolt11Payment { } } - if let Some(payment_params) = sending_parameters { - payment_params - .max_total_routing_fee_msat - .map(|fee| route_params.max_total_routing_fee_msat = Some(fee)); - payment_params - .max_total_cltv_expiry_delta - .map(|delta| route_params.payment_params.max_total_cltv_expiry_delta = delta); + if let Some(user_set_params) = sending_parameters { + if let Some(mut default_params) = + self.config.sending_parameters_config.as_ref().cloned() + { + default_params.max_total_routing_fee_msat = user_set_params + .max_total_routing_fee_msat + .or(default_params.max_total_routing_fee_msat); + default_params.max_total_cltv_expiry_delta = user_set_params + .max_total_cltv_expiry_delta + .or(default_params.max_total_cltv_expiry_delta); + default_params.max_path_count = + user_set_params.max_path_count.or(default_params.max_path_count); + default_params.max_channel_saturation_power_of_half = user_set_params + .max_channel_saturation_power_of_half + .or(default_params.max_channel_saturation_power_of_half); + + route_params.max_total_routing_fee_msat = default_params.max_total_routing_fee_msat; + route_params.payment_params.max_total_cltv_expiry_delta = + default_params.max_total_cltv_expiry_delta.unwrap_or_default(); + route_params.payment_params.max_path_count = + default_params.max_path_count.unwrap_or_default(); + route_params.payment_params.max_channel_saturation_power_of_half = + default_params.max_channel_saturation_power_of_half.unwrap_or_default(); + } + } else if let Some(default_params) = &self.config.sending_parameters_config { + route_params.max_total_routing_fee_msat = default_params.max_total_routing_fee_msat; + route_params.payment_params.max_total_cltv_expiry_delta = + default_params.max_total_cltv_expiry_delta.unwrap_or_default(); + route_params.payment_params.max_path_count = + default_params.max_path_count.unwrap_or_default(); + route_params.payment_params.max_channel_saturation_power_of_half = + default_params.max_channel_saturation_power_of_half.unwrap_or_default(); } let payment_secret = Some(*invoice.payment_secret()); @@ -212,13 +237,38 @@ impl Bolt11Payment { let mut route_params = RouteParameters::from_payment_params_and_value(payment_params, amount_msat); - if let Some(payment_params) = sending_parameters { - payment_params - .max_total_routing_fee_msat - .map(|fee| route_params.max_total_routing_fee_msat = Some(fee)); - payment_params - .max_total_cltv_expiry_delta - .map(|delta| route_params.payment_params.max_total_cltv_expiry_delta = delta); + if let Some(user_set_params) = sending_parameters { + if let Some(mut default_params) = + self.config.sending_parameters_config.as_ref().cloned() + { + default_params.max_total_routing_fee_msat = user_set_params + .max_total_routing_fee_msat + .or(default_params.max_total_routing_fee_msat); + default_params.max_total_cltv_expiry_delta = user_set_params + .max_total_cltv_expiry_delta + .or(default_params.max_total_cltv_expiry_delta); + default_params.max_path_count = + user_set_params.max_path_count.or(default_params.max_path_count); + default_params.max_channel_saturation_power_of_half = user_set_params + .max_channel_saturation_power_of_half + .or(default_params.max_channel_saturation_power_of_half); + + route_params.max_total_routing_fee_msat = default_params.max_total_routing_fee_msat; + route_params.payment_params.max_total_cltv_expiry_delta = + default_params.max_total_cltv_expiry_delta.unwrap_or_default(); + route_params.payment_params.max_path_count = + default_params.max_path_count.unwrap_or_default(); + route_params.payment_params.max_channel_saturation_power_of_half = + default_params.max_channel_saturation_power_of_half.unwrap_or_default(); + } + } else if let Some(default_params) = &self.config.sending_parameters_config { + route_params.max_total_routing_fee_msat = default_params.max_total_routing_fee_msat; + route_params.payment_params.max_total_cltv_expiry_delta = + default_params.max_total_cltv_expiry_delta.unwrap_or_default(); + route_params.payment_params.max_path_count = + default_params.max_path_count.unwrap_or_default(); + route_params.payment_params.max_channel_saturation_power_of_half = + default_params.max_channel_saturation_power_of_half.unwrap_or_default(); } let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT); diff --git a/tests/integration_tests_rust.rs b/tests/integration_tests_rust.rs index 055714ce1..3141d8dfc 100644 --- a/tests/integration_tests_rust.rs +++ b/tests/integration_tests_rust.rs @@ -159,6 +159,8 @@ fn multi_hop_sending() { let payment_params = SendingParameters { max_total_routing_fee_msat: Some(75_000), max_total_cltv_expiry_delta: Some(1000), + max_path_count: Some(10), + max_channel_saturation_power_of_half: Some(2), }; let invoice = nodes[4].bolt11_payment().receive(2_500_000, &"asdf", 9217).unwrap();