Skip to content

Commit

Permalink
Refactor bolt11 send methods to use default sending params
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
slanesuke committed Aug 12, 2024
1 parent de80ed1 commit 9242342
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
78 changes: 64 additions & 14 deletions src/payment/bolt11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 9242342

Please sign in to comment.