Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SendingParameters struct for customizable payments #336

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion bindings/ldk_node.udl
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, could we either split this in one commit per field, or add all fields at once? In any case this split of "initial" and "new" fields doesn't make too much sense, given that they are not new. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that during the commit history cleanup yesterday, I accidentally included some code in the wrong commits. I’m still working on my skills with interactive rebasing 😅. But, I’ve restructured the commits so that they should make more sense now. I squashed all the fields into one commit and then implemented SendingParameters in each method in the following commits.

Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ interface Bolt12Payment {

interface SpontaneousPayment {
[Throws=NodeError]
PaymentId send(u64 amount_msat, PublicKey node_id);
PaymentId send(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters);
[Throws=NodeError]
void send_probes(u64 amount_msat, PublicKey node_id);
};
Expand Down
49 changes: 46 additions & 3 deletions src/payment/spontaneous.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::logger::{log_error, log_info, FilesystemLogger, Logger};
use crate::payment::store::{
PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus, PaymentStore,
};
use crate::payment::SendingParameters;
use crate::types::{ChannelManager, KeysManager};

use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry, RetryableSendFailure};
Expand Down Expand Up @@ -41,8 +42,15 @@ impl SpontaneousPayment {
Self { runtime, channel_manager, keys_manager, payment_store, config, logger }
}

/// Send a spontaneous, aka. "keysend", payment
pub fn send(&self, amount_msat: u64, node_id: PublicKey) -> Result<PaymentId, Error> {
/// Send a spontaneous aka. "keysend", payment.
///
/// If [`SendingParameters`] are provided they will override the node's default routing parameters
/// on a per-field basis. Each field in `SendingParameters` that is set replaces the corresponding
/// default value. Fields that are not set fall back to the node's configured defaults. If no
/// `SendingParameters` are provided, the method fully relies on these defaults.
pub fn send(
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
) -> Result<PaymentId, Error> {
let rt_lock = self.runtime.read().unwrap();
if rt_lock.is_none() {
return Err(Error::NotRunning);
Expand All @@ -61,10 +69,45 @@ impl SpontaneousPayment {
}
}

let route_params = RouteParameters::from_payment_params_and_value(
let mut route_params = RouteParameters::from_payment_params_and_value(
PaymentParameters::from_node_id(node_id, self.config.default_cltv_expiry_delta),
amount_msat,
);

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();
slanesuke marked this conversation as resolved.
Show resolved Hide resolved
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 recipient_fields = RecipientOnionFields::spontaneous_empty();

match self.channel_manager.send_spontaneous_payment_with_retry(
Expand Down
2 changes: 1 addition & 1 deletion tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
println!("\nA send_spontaneous_payment");
let keysend_amount_msat = 2500_000;
let keysend_payment_id =
node_a.spontaneous_payment().send(keysend_amount_msat, node_b.node_id()).unwrap();
node_a.spontaneous_payment().send(keysend_amount_msat, node_b.node_id(), None).unwrap();
expect_event!(node_a, PaymentSuccessful);
let received_keysend_amount = match node_b.wait_next_event() {
ref e @ Event::PaymentReceived { amount_msat, .. } => {
Expand Down
Loading