Skip to content

Commit 92be78e

Browse files
committed
f
1 parent 349a90b commit 92be78e

3 files changed

Lines changed: 50 additions & 74 deletions

File tree

src/config.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
//! Objects for configuring the node.
99
1010
use crate::logger::LogLevel;
11-
use crate::payment::SendingParameters;
1211

1312
use lightning::ln::msgs::SocketAddress;
1413
use lightning::routing::gossip::NodeAlias;

src/payment/bolt11.rs

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use crate::payment::store::{
1818
LSPFeeLimits, PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind,
1919
PaymentStatus, PaymentStore, PaymentStoreUpdateResult,
2020
};
21-
use crate::payment::SendingParameters;
2221
use crate::peer_store::{PeerInfo, PeerStore};
2322
use crate::types::ChannelManager;
2423

@@ -151,7 +150,8 @@ impl Bolt11Payment {
151150
}
152151
}
153152

154-
let route_parameters = route_parameters.or(self.config.route_parameters).unwrap_or_default();
153+
let route_parameters =
154+
route_parameters.or(self.config.route_parameters).unwrap_or_default();
155155
let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT);
156156
let payment_secret = Some(*invoice.payment_secret());
157157

@@ -186,7 +186,9 @@ impl Bolt11Payment {
186186
Ok(payment_id)
187187
},
188188
Err(Bolt11PaymentError::InvalidAmount) => {
189-
log_error!(self.logger, "Failed to send payment due to the given invoice being \"zero-amount\". Please use send_using_amount instead.");
189+
log_error!(self.logger,
190+
"Failed to send payment due to the given invoice being \"zero-amount\". Please use send_using_amount instead."
191+
);
190192
return Err(Error::InvalidInvoice);
191193
},
192194
Err(Bolt11PaymentError::SendingFailed(e)) => {
@@ -223,11 +225,11 @@ impl Bolt11Payment {
223225
/// This can be used to pay a so-called "zero-amount" invoice, i.e., an invoice that leaves the
224226
/// amount paid to be determined by the user.
225227
///
226-
/// If `sending_parameters` are provided they will override the default as well as the
227-
/// node-wide parameters configured via [`Config::sending_parameters`] on a per-field basis.
228+
/// If `route_parameters` are provided they will override the default as well as the
229+
/// node-wide parameters configured via [`Config::route_parameters`] on a per-field basis.
228230
pub fn send_using_amount(
229231
&self, invoice: &Bolt11Invoice, amount_msat: u64,
230-
sending_parameters: Option<SendingParameters>,
232+
route_parameters: Option<RouteParametersConfig>,
231233
) -> Result<PaymentId, Error> {
232234
let invoice = maybe_convert_invoice(invoice);
233235
let rt_lock = self.runtime.read().unwrap();
@@ -255,46 +257,16 @@ impl Bolt11Payment {
255257
}
256258
}
257259

258-
let payment_secret = invoice.payment_secret();
259-
let expiry_time = invoice.duration_since_epoch().saturating_add(invoice.expiry_time());
260-
let mut payment_params = PaymentParameters::from_node_id(
261-
invoice.recover_payee_pub_key(),
262-
invoice.min_final_cltv_expiry_delta() as u32,
263-
)
264-
.with_expiry_time(expiry_time.as_secs())
265-
.with_route_hints(invoice.route_hints())
266-
.map_err(|_| Error::InvalidInvoice)?;
267-
if let Some(features) = invoice.features() {
268-
payment_params = payment_params
269-
.with_bolt11_features(features.clone())
270-
.map_err(|_| Error::InvalidInvoice)?;
271-
}
272-
let mut route_params =
273-
RouteParameters::from_payment_params_and_value(payment_params, amount_msat);
274-
275-
let override_params =
276-
sending_parameters.as_ref().or(self.config.sending_parameters.as_ref());
277-
if let Some(override_params) = override_params {
278-
override_params
279-
.max_total_routing_fee_msat
280-
.map(|f| route_params.max_total_routing_fee_msat = f.into());
281-
override_params
282-
.max_total_cltv_expiry_delta
283-
.map(|d| route_params.payment_params.max_total_cltv_expiry_delta = d);
284-
override_params.max_path_count.map(|p| route_params.payment_params.max_path_count = p);
285-
override_params
286-
.max_channel_saturation_power_of_half
287-
.map(|s| route_params.payment_params.max_channel_saturation_power_of_half = s);
288-
};
289-
260+
let route_parameters =
261+
route_parameters.or(self.config.route_parameters).unwrap_or_default();
290262
let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT);
291-
let recipient_fields = RecipientOnionFields::secret_only(*payment_secret);
263+
let payment_secret = Some(*invoice.payment_secret());
292264

293-
match self.channel_manager.send_payment(
294-
payment_hash,
295-
recipient_fields,
265+
match self.channel_manager.pay_for_bolt11_invoice(
266+
invoice,
296267
payment_id,
297-
route_params,
268+
Some(amount_msat),
269+
route_parameters,
298270
retry_strategy,
299271
) {
300272
Ok(()) => {
@@ -309,7 +281,7 @@ impl Bolt11Payment {
309281
let kind = PaymentKind::Bolt11 {
310282
hash: payment_hash,
311283
preimage: None,
312-
secret: Some(*payment_secret),
284+
secret: payment_secret,
313285
};
314286

315287
let payment = PaymentDetails::new(
@@ -324,16 +296,22 @@ impl Bolt11Payment {
324296

325297
Ok(payment_id)
326298
},
327-
Err(e) => {
299+
Err(Bolt11PaymentError::InvalidAmount) => {
300+
log_error!(
301+
self.logger,
302+
"Failed to send payment due to amount given being insufficient."
303+
);
304+
return Err(Error::InvalidInvoice);
305+
},
306+
Err(Bolt11PaymentError::SendingFailed(e)) => {
328307
log_error!(self.logger, "Failed to send payment: {:?}", e);
329-
330308
match e {
331309
RetryableSendFailure::DuplicatePayment => Err(Error::DuplicatePayment),
332310
_ => {
333311
let kind = PaymentKind::Bolt11 {
334312
hash: payment_hash,
335313
preimage: None,
336-
secret: Some(*payment_secret),
314+
secret: payment_secret,
337315
};
338316
let payment = PaymentDetails::new(
339317
payment_id,
@@ -343,8 +321,8 @@ impl Bolt11Payment {
343321
PaymentDirection::Outbound,
344322
PaymentStatus::Failed,
345323
);
346-
self.payment_store.insert(payment)?;
347324

325+
self.payment_store.insert(payment)?;
348326
Err(Error::PaymentSendingFailed)
349327
},
350328
}

src/payment/spontaneous.rs

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ use crate::logger::{log_error, log_info, LdkLogger, Logger};
1313
use crate::payment::store::{
1414
PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus, PaymentStore,
1515
};
16-
use crate::payment::SendingParameters;
1716
use crate::types::{ChannelManager, CustomTlvRecord, KeysManager};
1817

1918
use lightning::ln::channelmanager::{PaymentId, RecipientOnionFields, Retry, RetryableSendFailure};
20-
use lightning::routing::router::{PaymentParameters, RouteParameters};
19+
use lightning::routing::router::{PaymentParameters, RouteParameters, RouteParametersConfig};
2120
use lightning::sign::EntropySource;
2221

2322
use lightning_types::payment::{PaymentHash, PaymentPreimage};
@@ -54,25 +53,26 @@ impl SpontaneousPayment {
5453

5554
/// Send a spontaneous aka. "keysend", payment.
5655
///
57-
/// If `sending_parameters` are provided they will override the default as well as the
58-
/// node-wide parameters configured via [`Config::sending_parameters`] on a per-field basis.
56+
/// If `route_parameters` are provided they will override the default as well as the
57+
/// node-wide parameters configured via [`Config::route_parameters`] on a per-field basis.
5958
pub fn send(
60-
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
59+
&self, amount_msat: u64, node_id: PublicKey,
60+
route_parameters: Option<RouteParametersConfig>,
6161
) -> Result<PaymentId, Error> {
62-
self.send_inner(amount_msat, node_id, sending_parameters, None)
62+
self.send_inner(amount_msat, node_id, route_parameters, None)
6363
}
6464

6565
/// Send a spontaneous payment including a list of custom TLVs.
6666
pub fn send_with_custom_tlvs(
67-
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
68-
custom_tlvs: Vec<CustomTlvRecord>,
67+
&self, amount_msat: u64, node_id: PublicKey,
68+
route_parameters: Option<RouteParametersConfig>, custom_tlvs: Vec<CustomTlvRecord>,
6969
) -> Result<PaymentId, Error> {
70-
self.send_inner(amount_msat, node_id, sending_parameters, Some(custom_tlvs))
70+
self.send_inner(amount_msat, node_id, route_parameters, Some(custom_tlvs))
7171
}
7272

7373
fn send_inner(
74-
&self, amount_msat: u64, node_id: PublicKey, sending_parameters: Option<SendingParameters>,
75-
custom_tlvs: Option<Vec<CustomTlvRecord>>,
74+
&self, amount_msat: u64, node_id: PublicKey,
75+
route_parameters: Option<RouteParametersConfig>, custom_tlvs: Option<Vec<CustomTlvRecord>>,
7676
) -> Result<PaymentId, Error> {
7777
let rt_lock = self.runtime.read().unwrap();
7878
if rt_lock.is_none() {
@@ -97,20 +97,19 @@ impl SpontaneousPayment {
9797
amount_msat,
9898
);
9999

100-
let override_params =
101-
sending_parameters.as_ref().or(self.config.sending_parameters.as_ref());
102-
if let Some(override_params) = override_params {
103-
override_params
104-
.max_total_routing_fee_msat
105-
.map(|f| route_params.max_total_routing_fee_msat = f.into());
106-
override_params
107-
.max_total_cltv_expiry_delta
108-
.map(|d| route_params.payment_params.max_total_cltv_expiry_delta = d);
109-
override_params.max_path_count.map(|p| route_params.payment_params.max_path_count = p);
110-
override_params
111-
.max_channel_saturation_power_of_half
112-
.map(|s| route_params.payment_params.max_channel_saturation_power_of_half = s);
113-
};
100+
if let Some(RouteParametersConfig {
101+
max_total_routing_fee_msat,
102+
max_total_cltv_expiry_delta,
103+
max_path_count,
104+
max_channel_saturation_power_of_half,
105+
}) = route_parameters.as_ref().or(self.config.route_parameters.as_ref())
106+
{
107+
route_params.max_total_routing_fee_msat = *max_total_routing_fee_msat;
108+
route_params.payment_params.max_total_cltv_expiry_delta = *max_total_cltv_expiry_delta;
109+
route_params.payment_params.max_path_count = *max_path_count;
110+
route_params.payment_params.max_channel_saturation_power_of_half =
111+
*max_channel_saturation_power_of_half;
112+
}
114113

115114
let recipient_fields = match custom_tlvs {
116115
Some(tlvs) => RecipientOnionFields::spontaneous_empty()

0 commit comments

Comments
 (0)