Skip to content

Commit d638308

Browse files
committed
Introduce PaymentKind to payment store, track by PaymentId
1 parent b01fd4b commit d638308

File tree

9 files changed

+389
-194
lines changed

9 files changed

+389
-194
lines changed

bindings/ldk_node.udl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ interface Node {
6868
void update_channel_config([ByRef]UserChannelId user_channel_id, PublicKey counterparty_node_id, ChannelConfig channel_config);
6969
[Throws=NodeError]
7070
void sync_wallets();
71-
PaymentDetails? payment([ByRef]PaymentHash payment_hash);
71+
PaymentDetails? payment([ByRef]PaymentId payment_id);
7272
[Throws=NodeError]
73-
void remove_payment([ByRef]PaymentHash payment_hash);
73+
void remove_payment([ByRef]PaymentId payment_id);
7474
BalanceDetails list_balances();
7575
sequence<PaymentDetails> list_payments();
7676
sequence<PeerDetails> list_peers();
@@ -82,9 +82,9 @@ interface Node {
8282

8383
interface Bolt11Payment {
8484
[Throws=NodeError]
85-
PaymentHash send([ByRef]Bolt11Invoice invoice);
85+
PaymentId send([ByRef]Bolt11Invoice invoice);
8686
[Throws=NodeError]
87-
PaymentHash send_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat);
87+
PaymentId send_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat);
8888
[Throws=NodeError]
8989
void send_probes([ByRef]Bolt11Invoice invoice);
9090
[Throws=NodeError]
@@ -101,7 +101,7 @@ interface Bolt11Payment {
101101

102102
interface SpontaneousPayment {
103103
[Throws=NodeError]
104-
PaymentHash send(u64 amount_msat, PublicKey node_id);
104+
PaymentId send(u64 amount_msat, PublicKey node_id);
105105
[Throws=NodeError]
106106
void send_probes(u64 amount_msat, PublicKey node_id);
107107
};
@@ -139,6 +139,7 @@ enum NodeError {
139139
"InvalidSocketAddress",
140140
"InvalidPublicKey",
141141
"InvalidSecretKey",
142+
"InvalidPaymentId",
142143
"InvalidPaymentHash",
143144
"InvalidPaymentPreimage",
144145
"InvalidPaymentSecret",
@@ -218,6 +219,14 @@ interface ClosureReason {
218219
FundingBatchClosure();
219220
};
220221

222+
[Enum]
223+
interface PaymentKind {
224+
Onchain();
225+
Bolt11(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret);
226+
Bolt11Jit(PaymentHash hash, PaymentPreimage? preimage, PaymentSecret? secret, LSPFeeLimits lsp_fee_limits);
227+
Spontaneous(PaymentHash hash, PaymentPreimage? preimage);
228+
};
229+
221230
enum PaymentDirection {
222231
"Inbound",
223232
"Outbound",
@@ -235,13 +244,11 @@ dictionary LSPFeeLimits {
235244
};
236245

237246
dictionary PaymentDetails {
238-
PaymentHash hash;
239-
PaymentPreimage? preimage;
240-
PaymentSecret? secret;
247+
PaymentId id;
248+
PaymentKind kind;
241249
u64? amount_msat;
242250
PaymentDirection direction;
243251
PaymentStatus status;
244-
LSPFeeLimits? lsp_fee_limits;
245252
};
246253

247254
[NonExhaustive]
@@ -363,6 +370,9 @@ typedef string Address;
363370
[Custom]
364371
typedef string Bolt11Invoice;
365372

373+
[Custom]
374+
typedef string PaymentId;
375+
366376
[Custom]
367377
typedef string PaymentHash;
368378

src/error.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ pub enum Error {
4747
InvalidPublicKey,
4848
/// The given secret key is invalid.
4949
InvalidSecretKey,
50+
/// The given payment id is invalid.
51+
InvalidPaymentId,
5052
/// The given payment hash is invalid.
5153
InvalidPaymentHash,
5254
/// The given payment pre-image is invalid.
@@ -100,6 +102,7 @@ impl fmt::Display for Error {
100102
Self::InvalidSocketAddress => write!(f, "The given network address is invalid."),
101103
Self::InvalidPublicKey => write!(f, "The given public key is invalid."),
102104
Self::InvalidSecretKey => write!(f, "The given secret key is invalid."),
105+
Self::InvalidPaymentId => write!(f, "The given payment id is invalid."),
103106
Self::InvalidPaymentHash => write!(f, "The given payment hash is invalid."),
104107
Self::InvalidPaymentPreimage => write!(f, "The given payment preimage is invalid."),
105108
Self::InvalidPaymentSecret => write!(f, "The given payment secret is invalid."),

src/event.rs

Lines changed: 49 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::{
44
};
55

66
use crate::payment::payment_store::{
7-
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentStatus, PaymentStore,
7+
PaymentDetails, PaymentDetailsUpdate, PaymentDirection, PaymentKind, PaymentStatus,
8+
PaymentStore,
89
};
910

1011
use crate::io::{
@@ -17,6 +18,7 @@ use lightning::chain::chaininterface::ConfirmationTarget;
1718
use lightning::events::{ClosureReason, PaymentPurpose};
1819
use lightning::events::{Event as LdkEvent, PaymentFailureReason};
1920
use lightning::impl_writeable_tlv_based_enum;
21+
use lightning::ln::channelmanager::PaymentId;
2022
use lightning::ln::{ChannelId, PaymentHash};
2123
use lightning::routing::gossip::NodeId;
2224
use lightning::util::errors::APIError;
@@ -410,7 +412,8 @@ where
410412
onion_fields: _,
411413
counterparty_skimmed_fee_msat,
412414
} => {
413-
if let Some(info) = self.payment_store.get(&payment_hash) {
415+
let payment_id = PaymentId(payment_hash.0);
416+
if let Some(info) = self.payment_store.get(&payment_id) {
414417
if info.status == PaymentStatus::Succeeded {
415418
log_info!(
416419
self.logger,
@@ -422,7 +425,7 @@ where
422425

423426
let update = PaymentDetailsUpdate {
424427
status: Some(PaymentStatus::Failed),
425-
..PaymentDetailsUpdate::new(payment_hash)
428+
..PaymentDetailsUpdate::new(payment_id)
426429
};
427430
self.payment_store.update(&update).unwrap_or_else(|e| {
428431
log_error!(self.logger, "Failed to access payment store: {}", e);
@@ -431,17 +434,22 @@ where
431434
return;
432435
}
433436

434-
let max_total_opening_fee_msat = info
435-
.lsp_fee_limits
436-
.and_then(|l| {
437-
l.max_total_opening_fee_msat.or_else(|| {
438-
l.max_proportional_opening_fee_ppm_msat.and_then(|max_prop_fee| {
439-
// If it's a variable amount payment, compute the actual fee.
440-
compute_opening_fee(amount_msat, 0, max_prop_fee)
437+
let max_total_opening_fee_msat = match info.kind {
438+
PaymentKind::Bolt11Jit { lsp_fee_limits, .. } => {
439+
lsp_fee_limits
440+
.max_total_opening_fee_msat
441+
.or_else(|| {
442+
lsp_fee_limits.max_proportional_opening_fee_ppm_msat.and_then(
443+
|max_prop_fee| {
444+
// If it's a variable amount payment, compute the actual fee.
445+
compute_opening_fee(amount_msat, 0, max_prop_fee)
446+
},
447+
)
441448
})
442-
})
443-
})
444-
.unwrap_or(0);
449+
.unwrap_or(0)
450+
},
451+
_ => 0,
452+
};
445453

446454
if counterparty_skimmed_fee_msat > max_total_opening_fee_msat {
447455
log_info!(
@@ -455,7 +463,7 @@ where
455463

456464
let update = PaymentDetailsUpdate {
457465
status: Some(PaymentStatus::Failed),
458-
..PaymentDetailsUpdate::new(payment_hash)
466+
..PaymentDetailsUpdate::new(payment_id)
459467
};
460468
self.payment_store.update(&update).unwrap_or_else(|e| {
461469
log_error!(self.logger, "Failed to access payment store: {}", e);
@@ -496,7 +504,7 @@ where
496504

497505
let update = PaymentDetailsUpdate {
498506
status: Some(PaymentStatus::Failed),
499-
..PaymentDetailsUpdate::new(payment_hash)
507+
..PaymentDetailsUpdate::new(payment_id)
500508
};
501509
self.payment_store.update(&update).unwrap_or_else(|e| {
502510
log_error!(self.logger, "Failed to access payment store: {}", e);
@@ -518,14 +526,15 @@ where
518526
hex_utils::to_string(&payment_hash.0),
519527
amount_msat,
520528
);
529+
let payment_id = PaymentId(payment_hash.0);
521530
match purpose {
522531
PaymentPurpose::InvoicePayment { payment_preimage, payment_secret, .. } => {
523532
let update = PaymentDetailsUpdate {
524533
preimage: Some(payment_preimage),
525534
secret: Some(Some(payment_secret)),
526535
amount_msat: Some(Some(amount_msat)),
527536
status: Some(PaymentStatus::Succeeded),
528-
..PaymentDetailsUpdate::new(payment_hash)
537+
..PaymentDetailsUpdate::new(payment_id)
529538
};
530539
match self.payment_store.update(&update) {
531540
Ok(true) => (),
@@ -549,14 +558,17 @@ where
549558
}
550559
},
551560
PaymentPurpose::SpontaneousPayment(preimage) => {
552-
let payment = PaymentDetails {
553-
preimage: Some(preimage),
561+
let id = PaymentId(payment_hash.0);
562+
let kind = PaymentKind::Spontaneous {
554563
hash: payment_hash,
555-
secret: None,
564+
preimage: Some(preimage),
565+
};
566+
let payment = PaymentDetails {
567+
id,
568+
kind,
556569
amount_msat: Some(amount_msat),
557570
direction: PaymentDirection::Inbound,
558571
status: PaymentStatus::Succeeded,
559-
lsp_fee_limits: None,
560572
};
561573

562574
match self.payment_store.insert(payment) {
@@ -590,13 +602,19 @@ where
590602
});
591603
},
592604
LdkEvent::PaymentSent { payment_preimage, payment_hash, fee_paid_msat, .. } => {
593-
if let Some(mut payment) = self.payment_store.get(&payment_hash) {
594-
payment.preimage = Some(payment_preimage);
595-
payment.status = PaymentStatus::Succeeded;
596-
self.payment_store.insert(payment.clone()).unwrap_or_else(|e| {
597-
log_error!(self.logger, "Failed to access payment store: {}", e);
598-
panic!("Failed to access payment store");
599-
});
605+
let payment_id = PaymentId(payment_hash.0);
606+
let update = PaymentDetailsUpdate {
607+
preimage: Some(Some(payment_preimage)),
608+
status: Some(PaymentStatus::Succeeded),
609+
..PaymentDetailsUpdate::new(payment_id)
610+
};
611+
612+
self.payment_store.update(&update).unwrap_or_else(|e| {
613+
log_error!(self.logger, "Failed to access payment store: {}", e);
614+
panic!("Failed to access payment store");
615+
});
616+
617+
self.payment_store.get(&payment_id).map(|payment| {
600618
log_info!(
601619
self.logger,
602620
"Successfully sent payment of {}msat{} from \
@@ -610,7 +628,8 @@ where
610628
hex_utils::to_string(&payment_hash.0),
611629
hex_utils::to_string(&payment_preimage.0)
612630
);
613-
}
631+
});
632+
614633
self.event_queue
615634
.add_event(Event::PaymentSuccessful { payment_hash, fee_paid_msat })
616635
.unwrap_or_else(|e| {
@@ -626,9 +645,10 @@ where
626645
reason
627646
);
628647

648+
let payment_id = PaymentId(payment_hash.0);
629649
let update = PaymentDetailsUpdate {
630650
status: Some(PaymentStatus::Failed),
631-
..PaymentDetailsUpdate::new(payment_hash)
651+
..PaymentDetailsUpdate::new(payment_id)
632652
};
633653
self.payment_store.update(&update).unwrap_or_else(|e| {
634654
log_error!(self.logger, "Failed to access payment store: {}", e);

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ pub use types::{ChannelDetails, PeerDetails, UserChannelId};
141141
use logger::{log_error, log_info, log_trace, FilesystemLogger, Logger};
142142

143143
use lightning::chain::{BestBlock, Confirm};
144+
use lightning::ln::channelmanager::PaymentId;
144145
use lightning::ln::msgs::SocketAddress;
145-
use lightning::ln::PaymentHash;
146146

147147
use lightning::util::config::{ChannelHandshakeConfig, UserConfig};
148148
pub use lightning::util::logger::Level as LogLevel;
@@ -1143,16 +1143,16 @@ impl Node {
11431143
}
11441144
}
11451145

1146-
/// Retrieve the details of a specific payment with the given hash.
1146+
/// Retrieve the details of a specific payment with the given id.
11471147
///
11481148
/// Returns `Some` if the payment was known and `None` otherwise.
1149-
pub fn payment(&self, payment_hash: &PaymentHash) -> Option<PaymentDetails> {
1150-
self.payment_store.get(payment_hash)
1149+
pub fn payment(&self, payment_id: &PaymentId) -> Option<PaymentDetails> {
1150+
self.payment_store.get(payment_id)
11511151
}
11521152

1153-
/// Remove the payment with the given hash from the store.
1154-
pub fn remove_payment(&self, payment_hash: &PaymentHash) -> Result<(), Error> {
1155-
self.payment_store.remove(&payment_hash)
1153+
/// Remove the payment with the given id from the store.
1154+
pub fn remove_payment(&self, payment_id: &PaymentId) -> Result<(), Error> {
1155+
self.payment_store.remove(&payment_id)
11561156
}
11571157

11581158
/// Retrieves an overview of all known balances.

0 commit comments

Comments
 (0)