Skip to content

Commit 349a90b

Browse files
committed
f WIP Account for changes in BOLT11 interface
1 parent f6cc855 commit 349a90b

5 files changed

Lines changed: 42 additions & 134 deletions

File tree

bindings/ldk_node.udl

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dictionary Config {
1212
sequence<PublicKey> trusted_peers_0conf;
1313
u64 probing_liquidity_limit_multiplier;
1414
AnchorChannelsConfig? anchor_channels_config;
15-
SendingParameters? sending_parameters;
15+
RouteParametersConfig? route_parameters;
1616
};
1717

1818
dictionary AnchorChannelsConfig {
@@ -166,9 +166,9 @@ interface Bolt11InvoiceDescription {
166166

167167
interface Bolt11Payment {
168168
[Throws=NodeError]
169-
PaymentId send([ByRef]Bolt11Invoice invoice, SendingParameters? sending_parameters);
169+
PaymentId send([ByRef]Bolt11Invoice invoice, RouteParametersConfig? route_parameters);
170170
[Throws=NodeError]
171-
PaymentId send_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat, SendingParameters? sending_parameters);
171+
PaymentId send_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat, RouteParametersConfig? route_parameters);
172172
[Throws=NodeError]
173173
void send_probes([ByRef]Bolt11Invoice invoice);
174174
[Throws=NodeError]
@@ -208,9 +208,9 @@ interface Bolt12Payment {
208208

209209
interface SpontaneousPayment {
210210
[Throws=NodeError]
211-
PaymentId send(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters);
211+
PaymentId send(u64 amount_msat, PublicKey node_id, RouteParametersConfig? route_parameters);
212212
[Throws=NodeError]
213-
PaymentId send_with_custom_tlvs(u64 amount_msat, PublicKey node_id, SendingParameters? sending_parameters, sequence<CustomTlvRecord> custom_tlvs);
213+
PaymentId send_with_custom_tlvs(u64 amount_msat, PublicKey node_id, RouteParametersConfig? route_parameters, sequence<CustomTlvRecord> custom_tlvs);
214214
[Throws=NodeError]
215215
void send_probes(u64 amount_msat, PublicKey node_id);
216216
};
@@ -446,8 +446,8 @@ dictionary PaymentDetails {
446446
u64 latest_update_timestamp;
447447
};
448448

449-
dictionary SendingParameters {
450-
MaxTotalRoutingFeeLimit? max_total_routing_fee_msat;
449+
dictionary RouteParametersConfig {
450+
u64? max_total_routing_fee_msat;
451451
u32? max_total_cltv_expiry_delta;
452452
u8? max_path_count;
453453
u8? max_channel_saturation_power_of_half;
@@ -511,12 +511,6 @@ enum LSPS1PaymentState {
511511
"Refunded",
512512
};
513513

514-
[Enum]
515-
interface MaxTotalRoutingFeeLimit {
516-
None ();
517-
Some ( u64 amount_msat );
518-
};
519-
520514
[NonExhaustive]
521515
enum Network {
522516
"Bitcoin",

src/config.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use crate::payment::SendingParameters;
1212

1313
use lightning::ln::msgs::SocketAddress;
1414
use lightning::routing::gossip::NodeAlias;
15+
use lightning::routing::router::RouteParametersConfig;
1516
use lightning::util::config::ChannelConfig as LdkChannelConfig;
1617
use lightning::util::config::MaxDustHTLCExposure as LdkMaxDustHTLCExposure;
1718
use lightning::util::config::UserConfig;
@@ -102,9 +103,9 @@ pub const WALLET_KEYS_SEED_LEN: usize = 64;
102103
/// | `probing_liquidity_limit_multiplier` | 3 |
103104
/// | `log_level` | Debug |
104105
/// | `anchor_channels_config` | Some(..) |
105-
/// | `sending_parameters` | None |
106+
/// | `route_parameters` | None |
106107
///
107-
/// See [`AnchorChannelsConfig`] and [`SendingParameters`] for more information regarding their
108+
/// See [`AnchorChannelsConfig`] and [`RouteParametersConfig`] for more information regarding their
108109
/// respective default values.
109110
///
110111
/// [`Node`]: crate::Node
@@ -161,12 +162,12 @@ pub struct Config {
161162
pub anchor_channels_config: Option<AnchorChannelsConfig>,
162163
/// Configuration options for payment routing and pathfinding.
163164
///
164-
/// Setting the `SendingParameters` provides flexibility to customize how payments are routed,
165+
/// Setting the [`RouteParametersConfig`] provides flexibility to customize how payments are routed,
165166
/// including setting limits on routing fees, CLTV expiry, and channel utilization.
166167
///
167168
/// **Note:** If unset, default parameters will be used, and you will be able to override the
168169
/// parameters on a per-payment basis in the corresponding method calls.
169-
pub sending_parameters: Option<SendingParameters>,
170+
pub route_parameters: Option<RouteParametersConfig>,
170171
}
171172

172173
impl Default for Config {
@@ -179,7 +180,7 @@ impl Default for Config {
179180
trusted_peers_0conf: Vec::new(),
180181
probing_liquidity_limit_multiplier: DEFAULT_PROBING_LIQUIDITY_LIMIT_MULTIPLIER,
181182
anchor_channels_config: Some(AnchorChannelsConfig::default()),
182-
sending_parameters: None,
183+
route_parameters: None,
183184
node_alias: None,
184185
}
185186
}

src/payment/bolt11.rs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ use crate::payment::SendingParameters;
2222
use crate::peer_store::{PeerInfo, PeerStore};
2323
use crate::types::ChannelManager;
2424

25-
use lightning::ln::bolt11_payment;
2625
use lightning::ln::channelmanager::{
27-
Bolt11InvoiceParameters, PaymentId, RecipientOnionFields, Retry, RetryableSendFailure,
26+
Bolt11InvoiceParameters, Bolt11PaymentError, PaymentId, RecipientOnionFields, Retry,
27+
RetryableSendFailure,
2828
};
29-
use lightning::routing::router::{PaymentParameters, RouteParameters};
29+
use lightning::routing::router::{PaymentParameters, RouteParameters, RouteParametersConfig};
3030

3131
use lightning_types::payment::{PaymentHash, PaymentPreimage};
3232

@@ -119,22 +119,17 @@ impl Bolt11Payment {
119119

120120
/// Send a payment given an invoice.
121121
///
122-
/// If `sending_parameters` are provided they will override the default as well as the
123-
/// node-wide parameters configured via [`Config::sending_parameters`] on a per-field basis.
122+
/// If `route_parameters` are provided they will override the default as well as the
123+
/// node-wide parameters configured via [`Config::route_parameters`] on a per-field basis.
124124
pub fn send(
125-
&self, invoice: &Bolt11Invoice, sending_parameters: Option<SendingParameters>,
125+
&self, invoice: &Bolt11Invoice, route_parameters: Option<RouteParametersConfig>,
126126
) -> Result<PaymentId, Error> {
127127
let invoice = maybe_convert_invoice(invoice);
128128
let rt_lock = self.runtime.read().unwrap();
129129
if rt_lock.is_none() {
130130
return Err(Error::NotRunning);
131131
}
132132

133-
let (payment_hash, recipient_onion, mut route_params) = bolt11_payment::payment_parameters_from_invoice(&invoice).map_err(|_| {
134-
log_error!(self.logger, "Failed to send payment due to the given invoice being \"zero-amount\". Please use send_using_amount instead.");
135-
Error::InvalidInvoice
136-
})?;
137-
138133
let payment_id = PaymentId(invoice.payment_hash().to_byte_array());
139134
if let Some(payment) = self.payment_store.get(&payment_id) {
140135
if payment.status == PaymentStatus::Pending
@@ -145,29 +140,26 @@ impl Bolt11Payment {
145140
}
146141
}
147142

148-
let override_params =
149-
sending_parameters.as_ref().or(self.config.sending_parameters.as_ref());
150-
if let Some(override_params) = override_params {
151-
override_params
152-
.max_total_routing_fee_msat
153-
.map(|f| route_params.max_total_routing_fee_msat = f.into());
154-
override_params
155-
.max_total_cltv_expiry_delta
156-
.map(|d| route_params.payment_params.max_total_cltv_expiry_delta = d);
157-
override_params.max_path_count.map(|p| route_params.payment_params.max_path_count = p);
158-
override_params
159-
.max_channel_saturation_power_of_half
160-
.map(|s| route_params.payment_params.max_channel_saturation_power_of_half = s);
161-
};
143+
let payment_hash = PaymentHash(invoice.payment_hash().to_byte_array());
144+
let payment_id = PaymentId(invoice.payment_hash().to_byte_array());
145+
if let Some(payment) = self.payment_store.get(&payment_id) {
146+
if payment.status == PaymentStatus::Pending
147+
|| payment.status == PaymentStatus::Succeeded
148+
{
149+
log_error!(self.logger, "Payment error: an invoice must not be paid twice.");
150+
return Err(Error::DuplicatePayment);
151+
}
152+
}
162153

163-
let payment_secret = Some(*invoice.payment_secret());
154+
let route_parameters = route_parameters.or(self.config.route_parameters).unwrap_or_default();
164155
let retry_strategy = Retry::Timeout(LDK_PAYMENT_RETRY_TIMEOUT);
156+
let payment_secret = Some(*invoice.payment_secret());
165157

166-
match self.channel_manager.send_payment(
167-
payment_hash,
168-
recipient_onion,
158+
match self.channel_manager.pay_for_bolt11_invoice(
159+
invoice,
169160
payment_id,
170-
route_params,
161+
None,
162+
route_parameters,
171163
retry_strategy,
172164
) {
173165
Ok(()) => {
@@ -193,7 +185,11 @@ impl Bolt11Payment {
193185

194186
Ok(payment_id)
195187
},
196-
Err(e) => {
188+
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.");
190+
return Err(Error::InvalidInvoice);
191+
},
192+
Err(Bolt11PaymentError::SendingFailed(e)) => {
197193
log_error!(self.logger, "Failed to send payment: {:?}", e);
198194
match e {
199195
RetryableSendFailure::DuplicatePayment => Err(Error::DuplicatePayment),

src/payment/mod.rs

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -22,87 +22,3 @@ pub use store::{
2222
ConfirmationStatus, LSPFeeLimits, PaymentDetails, PaymentDirection, PaymentKind, PaymentStatus,
2323
};
2424
pub use unified_qr::{QrPaymentResult, UnifiedQrPayment};
25-
26-
/// Represents information used to send a payment.
27-
#[derive(Clone, Debug, PartialEq)]
28-
pub struct SendingParameters {
29-
/// The maximum total fees, in millisatoshi, that may accrue during route finding.
30-
///
31-
/// This limit also applies to the total fees that may arise while retrying failed payment
32-
/// paths.
33-
///
34-
/// Note that values below a few sats may result in some paths being spuriously ignored.
35-
#[cfg(not(feature = "uniffi"))]
36-
pub max_total_routing_fee_msat: Option<Option<u64>>,
37-
/// The maximum total fees, in millisatoshi, that may accrue during route finding.
38-
///
39-
/// This limit also applies to the total fees that may arise while retrying failed payment
40-
/// paths.
41-
///
42-
/// Note that values below a few sats may result in some paths being spuriously ignored.
43-
#[cfg(feature = "uniffi")]
44-
pub max_total_routing_fee_msat: Option<MaxTotalRoutingFeeLimit>,
45-
/// The maximum total CLTV delta we accept for the route.
46-
///
47-
/// Defaults to [`DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA`].
48-
///
49-
/// [`DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA`]: lightning::routing::router::DEFAULT_MAX_TOTAL_CLTV_EXPIRY_DELTA
50-
pub max_total_cltv_expiry_delta: Option<u32>,
51-
/// The maximum number of paths that may be used by (MPP) payments.
52-
///
53-
/// Defaults to [`DEFAULT_MAX_PATH_COUNT`].
54-
///
55-
/// [`DEFAULT_MAX_PATH_COUNT`]: lightning::routing::router::DEFAULT_MAX_PATH_COUNT
56-
pub max_path_count: Option<u8>,
57-
/// Selects the maximum share of a channel's total capacity which will be sent over a channel,
58-
/// as a power of 1/2.
59-
///
60-
/// A higher value prefers to send the payment using more MPP parts whereas
61-
/// a lower value prefers to send larger MPP parts, potentially saturating channels and
62-
/// increasing failure probability for those paths.
63-
///
64-
/// Note that this restriction will be relaxed during pathfinding after paths which meet this
65-
/// restriction have been found. While paths which meet this criteria will be searched for, it
66-
/// is ultimately up to the scorer to select them over other paths.
67-
///
68-
/// Examples:
69-
///
70-
/// | Value | Max Proportion of Channel Capacity Used |
71-
/// |-------|-----------------------------------------|
72-
/// | 0 | Up to 100% of the channel’s capacity |
73-
/// | 1 | Up to 50% of the channel’s capacity |
74-
/// | 2 | Up to 25% of the channel’s capacity |
75-
/// | 3 | Up to 12.5% of the channel’s capacity |
76-
///
77-
/// Default value: 2
78-
pub max_channel_saturation_power_of_half: Option<u8>,
79-
}
80-
81-
/// Represents the possible states of [`SendingParameters::max_total_routing_fee_msat`].
82-
//
83-
// Required only in bindings as UniFFI can't expose `Option<Option<..>>`.
84-
#[cfg(feature = "uniffi")]
85-
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
86-
pub enum MaxTotalRoutingFeeLimit {
87-
None,
88-
Some { amount_msat: u64 },
89-
}
90-
91-
#[cfg(feature = "uniffi")]
92-
impl From<MaxTotalRoutingFeeLimit> for Option<u64> {
93-
fn from(value: MaxTotalRoutingFeeLimit) -> Self {
94-
match value {
95-
MaxTotalRoutingFeeLimit::Some { amount_msat } => Some(amount_msat),
96-
MaxTotalRoutingFeeLimit::None => None,
97-
}
98-
}
99-
}
100-
101-
#[cfg(feature = "uniffi")]
102-
impl From<Option<u64>> for MaxTotalRoutingFeeLimit {
103-
fn from(value: Option<u64>) -> Self {
104-
value.map_or(MaxTotalRoutingFeeLimit::None, |amount_msat| MaxTotalRoutingFeeLimit::Some {
105-
amount_msat,
106-
})
107-
}
108-
}

src/uniffi_types.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub use crate::logger::{LogLevel, LogRecord, LogWriter};
2020
pub use crate::payment::store::{
2121
ConfirmationStatus, LSPFeeLimits, PaymentDirection, PaymentKind, PaymentStatus,
2222
};
23-
pub use crate::payment::{MaxTotalRoutingFeeLimit, QrPaymentResult, SendingParameters};
23+
pub use crate::payment::{MaxTotalRoutingFeeLimit, QrPaymentResult};
2424

2525
pub use lightning::chain::channelmonitor::BalanceSource;
2626
pub use lightning::events::{ClosureReason, PaymentFailureReason};
@@ -29,6 +29,7 @@ pub use lightning::offers::invoice::Bolt12Invoice;
2929
pub use lightning::offers::offer::{Offer, OfferId};
3030
pub use lightning::offers::refund::Refund;
3131
pub use lightning::routing::gossip::{NodeAlias, NodeId, RoutingFees};
32+
pub use lightning::routing::router::RouteParametersConfig;
3233
pub use lightning::util::string::UntrustedString;
3334

3435
pub use lightning_types::payment::{PaymentHash, PaymentPreimage, PaymentSecret};

0 commit comments

Comments
 (0)