Skip to content

Commit 0b9f3cd

Browse files
committed
Add SendingParameters to spontaneous send
Added optional SendingParameters to the send method in SpontaneousPayment. If the user provides sending params the values will be overridden otherwise they remain the same.
1 parent 3ccdf1e commit 0b9f3cd

File tree

3 files changed

+48
-5
lines changed

3 files changed

+48
-5
lines changed

bindings/ldk_node.udl

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ interface Bolt12Payment {
136136

137137
interface SpontaneousPayment {
138138
[Throws=NodeError]
139-
PaymentId send(u64 amount_msat, PublicKey node_id);
139+
PaymentId send(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters);
140140
[Throws=NodeError]
141141
void send_probes(u64 amount_msat, PublicKey node_id);
142142
};

src/payment/spontaneous.rs

+46-3
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use crate::logger::{log_error, log_info, FilesystemLogger, Logger};
66
use crate::payment::store::{
77
PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus, PaymentStore,
88
};
9+
use crate::payment::SendingParameters;
910
use crate::types::{ChannelManager, KeysManager};
1011

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

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

64-
let route_params = RouteParameters::from_payment_params_and_value(
72+
let mut route_params = RouteParameters::from_payment_params_and_value(
6573
PaymentParameters::from_node_id(node_id, self.config.default_cltv_expiry_delta),
6674
amount_msat,
6775
);
76+
77+
if let Some(user_set_params) = sending_parameters {
78+
if let Some(mut default_params) =
79+
self.config.sending_parameters_config.as_ref().cloned()
80+
{
81+
default_params.max_total_routing_fee_msat = user_set_params
82+
.max_total_routing_fee_msat
83+
.or(default_params.max_total_routing_fee_msat);
84+
default_params.max_total_cltv_expiry_delta = user_set_params
85+
.max_total_cltv_expiry_delta
86+
.or(default_params.max_total_cltv_expiry_delta);
87+
default_params.max_path_count =
88+
user_set_params.max_path_count.or(default_params.max_path_count);
89+
default_params.max_channel_saturation_power_of_half = user_set_params
90+
.max_channel_saturation_power_of_half
91+
.or(default_params.max_channel_saturation_power_of_half);
92+
93+
route_params.max_total_routing_fee_msat = default_params.max_total_routing_fee_msat;
94+
route_params.payment_params.max_total_cltv_expiry_delta =
95+
default_params.max_total_cltv_expiry_delta.unwrap_or_default();
96+
route_params.payment_params.max_path_count =
97+
default_params.max_path_count.unwrap_or_default();
98+
route_params.payment_params.max_channel_saturation_power_of_half =
99+
default_params.max_channel_saturation_power_of_half.unwrap_or_default();
100+
}
101+
} else if let Some(default_params) = &self.config.sending_parameters_config {
102+
route_params.max_total_routing_fee_msat = default_params.max_total_routing_fee_msat;
103+
route_params.payment_params.max_total_cltv_expiry_delta =
104+
default_params.max_total_cltv_expiry_delta.unwrap_or_default();
105+
route_params.payment_params.max_path_count =
106+
default_params.max_path_count.unwrap_or_default();
107+
route_params.payment_params.max_channel_saturation_power_of_half =
108+
default_params.max_channel_saturation_power_of_half.unwrap_or_default();
109+
}
110+
68111
let recipient_fields = RecipientOnionFields::spontaneous_empty();
69112

70113
match self.channel_manager.send_spontaneous_payment_with_retry(

tests/common/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ pub(crate) fn do_channel_full_cycle<E: ElectrumApi>(
695695
println!("\nA send_spontaneous_payment");
696696
let keysend_amount_msat = 2500_000;
697697
let keysend_payment_id =
698-
node_a.spontaneous_payment().send(keysend_amount_msat, node_b.node_id()).unwrap();
698+
node_a.spontaneous_payment().send(keysend_amount_msat, node_b.node_id(), None).unwrap();
699699
expect_event!(node_a, PaymentSuccessful);
700700
let received_keysend_amount = match node_b.wait_next_event() {
701701
ref e @ Event::PaymentReceived { amount_msat, .. } => {

0 commit comments

Comments
 (0)