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
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class LibraryTest {

val invoice = node2.bolt11Payment().receive(2500000u, "asdf", 9217u)

node1.bolt11Payment().send(invoice)
node1.bolt11Payment().send(invoice, null)

val paymentSuccessfulEvent = node1.waitNextEvent()
println("Got event: $paymentSuccessfulEvent")
Expand Down
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 @@ -94,7 +94,7 @@ interface Node {

interface Bolt11Payment {
[Throws=NodeError]
PaymentId send([ByRef]Bolt11Invoice invoice);
PaymentId send([ByRef]Bolt11Invoice invoice, SendingParameters? sending_parameters);
[Throws=NodeError]
PaymentId send_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat);
[Throws=NodeError]
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
//! node.event_handled();
//!
//! let invoice = Bolt11Invoice::from_str("INVOICE_STR").unwrap();
//! node.bolt11_payment().send(&invoice).unwrap();
//! node.bolt11_payment().send(&invoice, None).unwrap();
//!
//! node.stop().unwrap();
//! }
Expand Down
46 changes: 44 additions & 2 deletions src/payment/bolt11.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::payment::store::{
LSPFeeLimits, PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind,
PaymentStatus, PaymentStore,
};
use crate::payment::SendingParameters;
use crate::peer_store::{PeerInfo, PeerStore};
use crate::types::{ChannelManager, KeysManager};

Expand Down Expand Up @@ -69,13 +70,20 @@ impl Bolt11Payment {
}

/// Send a payment given an invoice.
pub fn send(&self, invoice: &Bolt11Invoice) -> Result<PaymentId, Error> {
///
/// 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, invoice: &Bolt11Invoice, sending_parameters: Option<SendingParameters>,
) -> Result<PaymentId, Error> {
let rt_lock = self.runtime.read().unwrap();
if rt_lock.is_none() {
return Err(Error::NotRunning);
}

let (payment_hash, recipient_onion, route_params) = payment::payment_parameters_from_invoice(&invoice).map_err(|_| {
let (payment_hash, recipient_onion, mut route_params) = payment::payment_parameters_from_invoice(&invoice).map_err(|_| {
log_error!(self.logger, "Failed to send payment due to the given invoice being \"zero-amount\". Please use send_using_amount instead.");
Error::InvalidInvoice
})?;
Expand All @@ -90,6 +98,40 @@ impl Bolt11Payment {
}
}

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());
let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT);

Expand Down
2 changes: 1 addition & 1 deletion src/payment/unified_qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl UnifiedQrPayment {
}

if let Some(invoice) = uri_network_checked.extras.bolt11_invoice {
match self.bolt11_invoice.send(&invoice) {
match self.bolt11_invoice.send(&invoice, None) {
Ok(payment_id) => return Ok(QrPaymentResult::Bolt11 { payment_id }),
Err(e) => log_error!(self.logger, "Failed to send BOLT11 invoice: {:?}. This is part of a unified QR code payment. Falling back to the on-chain transaction.", e),
}
Expand Down
12 changes: 6 additions & 6 deletions tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,8 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
let invoice = node_b.bolt11_payment().receive(invoice_amount_1_msat, &"asdf", 9217).unwrap();

println!("\nA send");
let payment_id = node_a.bolt11_payment().send(&invoice).unwrap();
assert_eq!(node_a.bolt11_payment().send(&invoice), Err(NodeError::DuplicatePayment));
let payment_id = node_a.bolt11_payment().send(&invoice, None).unwrap();
assert_eq!(node_a.bolt11_payment().send(&invoice, None), Err(NodeError::DuplicatePayment));

assert_eq!(node_a.list_payments().first().unwrap().id, payment_id);

Expand Down Expand Up @@ -526,7 +526,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
assert!(matches!(node_b.payment(&payment_id).unwrap().kind, PaymentKind::Bolt11 { .. }));

// Assert we fail duplicate outbound payments and check the status hasn't changed.
assert_eq!(Err(NodeError::DuplicatePayment), node_a.bolt11_payment().send(&invoice));
assert_eq!(Err(NodeError::DuplicatePayment), node_a.bolt11_payment().send(&invoice, None));
assert_eq!(node_a.payment(&payment_id).unwrap().status, PaymentStatus::Succeeded);
assert_eq!(node_a.payment(&payment_id).unwrap().direction, PaymentDirection::Outbound);
assert_eq!(node_a.payment(&payment_id).unwrap().amount_msat, Some(invoice_amount_1_msat));
Expand Down Expand Up @@ -579,7 +579,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
let determined_amount_msat = 2345_678;
assert_eq!(
Err(NodeError::InvalidInvoice),
node_a.bolt11_payment().send(&variable_amount_invoice)
node_a.bolt11_payment().send(&variable_amount_invoice, None)
);
println!("\nA send_using_amount");
let payment_id = node_a
Expand Down Expand Up @@ -616,7 +616,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
.bolt11_payment()
.receive_for_hash(invoice_amount_3_msat, &"asdf", 9217, manual_payment_hash)
.unwrap();
let manual_payment_id = node_a.bolt11_payment().send(&manual_invoice).unwrap();
let manual_payment_id = node_a.bolt11_payment().send(&manual_invoice, None).unwrap();

let claimable_amount_msat = expect_payment_claimable_event!(
node_b,
Expand Down Expand Up @@ -654,7 +654,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
.bolt11_payment()
.receive_for_hash(invoice_amount_3_msat, &"asdf", 9217, manual_fail_payment_hash)
.unwrap();
let manual_fail_payment_id = node_a.bolt11_payment().send(&manual_fail_invoice).unwrap();
let manual_fail_payment_id = node_a.bolt11_payment().send(&manual_fail_invoice, None).unwrap();

expect_payment_claimable_event!(
node_b,
Expand Down
2 changes: 1 addition & 1 deletion tests/integration_tests_cln.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn test_cln() {
cln_client.invoice(Some(10_000_000), &rand_label, &rand_label, None, None, None).unwrap();
let parsed_invoice = Bolt11Invoice::from_str(&cln_invoice.bolt11).unwrap();

node.bolt11_payment().send(&parsed_invoice).unwrap();
node.bolt11_payment().send(&parsed_invoice, None).unwrap();
common::expect_event!(node, PaymentSuccessful);
let cln_listed_invoices =
cln_client.listinvoices(Some(&rand_label), None, None, None).unwrap().invoices;
Expand Down
11 changes: 9 additions & 2 deletions tests/integration_tests_rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use common::{
setup_node, setup_two_nodes, wait_for_tx, TestSyncStore,
};

use ldk_node::payment::{PaymentKind, QrPaymentResult};
use ldk_node::payment::{PaymentKind, QrPaymentResult, SendingParameters};
use ldk_node::{Builder, Event, NodeError};

use lightning::ln::channelmanager::PaymentId;
Expand Down Expand Up @@ -156,8 +156,15 @@ fn multi_hop_sending() {
// Sleep a bit for gossip to propagate.
std::thread::sleep(std::time::Duration::from_secs(1));

let sending_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();
nodes[0].bolt11_payment().send(&invoice).unwrap();
nodes[0].bolt11_payment().send(&invoice, Some(sending_params)).unwrap();

let payment_id = expect_payment_received_event!(&nodes[4], 2_500_000);
let fee_paid_msat = Some(2000);
Expand Down