Skip to content

Commit 0069271

Browse files
committed
WIP switch to PaymentId fully
.. will require Payment metadata store
1 parent c529c40 commit 0069271

5 files changed

Lines changed: 318 additions & 264 deletions

File tree

bindings/ldk_node.udl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ interface Bolt11Payment {
214214
[Throws=NodeError]
215215
void send_probes_using_amount([ByRef]Bolt11Invoice invoice, u64 amount_msat, RouteParametersConfig? route_parameters);
216216
[Throws=NodeError]
217-
void claim_for_hash(PaymentHash payment_hash, u64 claimable_amount_msat, PaymentPreimage preimage);
217+
void claim_for_id(PaymentId payment_id, u64 claimable_amount_msat, PaymentPreimage preimage);
218218
[Throws=NodeError]
219-
void fail_for_hash(PaymentHash payment_hash);
219+
void fail_for_id(PaymentId payment_id);
220220
[Throws=NodeError]
221221
Bolt11Invoice receive(u64 amount_msat, [ByRef]Bolt11InvoiceDescription description, u32 expiry_secs);
222222
[Throws=NodeError]

src/event.rs

Lines changed: 78 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -635,9 +635,10 @@ where
635635
claim_deadline,
636636
onion_fields,
637637
counterparty_skimmed_fee_msat,
638+
payment_id,
638639
..
639640
} => {
640-
let payment_id = PaymentId(payment_hash.0);
641+
let payment_id = payment_id.unwrap_or(PaymentId(payment_hash.0));
641642
if let Some(info) = self.payment_store.get(&payment_id) {
642643
if info.direction == PaymentDirection::Outbound {
643644
log_info!(
@@ -744,46 +745,6 @@ where
744745
_ => debug_assert!(false, "We only expect the counterparty to get away with withholding fees for JIT payments."),
745746
}
746747
}
747-
748-
// If this is known by the store but ChannelManager doesn't know the preimage,
749-
// the payment has been registered via `_for_hash` variants and needs to be manually claimed via
750-
// user interaction.
751-
match info.kind {
752-
PaymentKind::Bolt11 { preimage, .. }
753-
| PaymentKind::Bolt11Jit { preimage, .. } => {
754-
if purpose.preimage().is_none() {
755-
debug_assert!(
756-
preimage.is_none(),
757-
"We would have registered the preimage if we knew"
758-
);
759-
760-
let custom_records = onion_fields
761-
.map(|cf| {
762-
cf.custom_tlvs().into_iter().map(|tlv| tlv.into()).collect()
763-
})
764-
.unwrap_or_default();
765-
let event = Event::PaymentClaimable {
766-
payment_id,
767-
payment_hash,
768-
claimable_amount_msat: amount_msat,
769-
claim_deadline,
770-
custom_records,
771-
};
772-
match self.event_queue.add_event(event).await {
773-
Ok(_) => return Ok(()),
774-
Err(e) => {
775-
log_error!(
776-
self.logger,
777-
"Failed to push to event queue: {}",
778-
e
779-
);
780-
return Err(ReplayEvent());
781-
},
782-
};
783-
}
784-
},
785-
_ => {},
786-
}
787748
}
788749

789750
log_info!(
@@ -793,7 +754,46 @@ where
793754
amount_msat,
794755
);
795756
let payment_preimage = match purpose {
796-
PaymentPurpose::Bolt11InvoicePayment { payment_preimage, .. } => {
757+
PaymentPurpose::Bolt11InvoicePayment {
758+
payment_preimage,
759+
payment_secret,
760+
..
761+
} => {
762+
let kind = PaymentKind::Bolt11 {
763+
hash: payment_hash,
764+
preimage: payment_preimage,
765+
secret: Some(payment_secret),
766+
};
767+
768+
let payment = PaymentDetails::new(
769+
payment_id,
770+
kind,
771+
Some(amount_msat),
772+
None,
773+
PaymentDirection::Inbound,
774+
PaymentStatus::Pending,
775+
);
776+
777+
match self.payment_store.insert(payment) {
778+
Ok(false) => (),
779+
Ok(true) => {
780+
log_error!(
781+
self.logger,
782+
"Bolt11InvoicePayment with ID {} was previously known",
783+
payment_id,
784+
);
785+
debug_assert!(false);
786+
},
787+
Err(e) => {
788+
log_error!(
789+
self.logger,
790+
"Failed to insert payment with ID {}: {}",
791+
payment_id,
792+
e
793+
);
794+
debug_assert!(false);
795+
},
796+
}
797797
payment_preimage
798798
},
799799
PaymentPurpose::Bolt12OfferPayment {
@@ -892,25 +892,47 @@ where
892892
if let Some(preimage) = payment_preimage {
893893
self.channel_manager.claim_funds(preimage);
894894
} else {
895-
log_error!(
896-
self.logger,
897-
"Failed to claim payment with ID {}: preimage unknown.",
895+
// If ChannelManager doesn't know the preimage the payment needs to be
896+
// manually claimed via user interaction.
897+
//if purpose.preimage().is_none() {
898+
let custom_records = onion_fields
899+
.map(|cf| cf.custom_tlvs().into_iter().map(|tlv| tlv.into()).collect())
900+
.unwrap_or_default();
901+
let event = Event::PaymentClaimable {
898902
payment_id,
899-
);
900-
self.channel_manager.fail_htlc_backwards(&payment_hash);
901-
902-
let update = PaymentDetailsUpdate {
903-
hash: Some(Some(payment_hash)),
904-
status: Some(PaymentStatus::Failed),
905-
..PaymentDetailsUpdate::new(payment_id)
903+
payment_hash,
904+
claimable_amount_msat: amount_msat,
905+
claim_deadline,
906+
custom_records,
906907
};
907-
match self.payment_store.update(&update) {
908+
match self.event_queue.add_event(event).await {
908909
Ok(_) => return Ok(()),
909910
Err(e) => {
910-
log_error!(self.logger, "Failed to access payment store: {}", e);
911+
log_error!(self.logger, "Failed to push to event queue: {}", e);
911912
return Err(ReplayEvent());
912913
},
913914
};
915+
//}
916+
917+
//log_error!(
918+
// self.logger,
919+
// "Failed to claim payment with ID {}: preimage unknown.",
920+
// payment_id,
921+
//);
922+
//self.channel_manager.fail_htlc_backwards(&payment_hash);
923+
924+
//let update = PaymentDetailsUpdate {
925+
// hash: Some(Some(payment_hash)),
926+
// status: Some(PaymentStatus::Failed),
927+
// ..PaymentDetailsUpdate::new(payment_id)
928+
//};
929+
//match self.payment_store.update(&update) {
930+
// Ok(_) => return Ok(()),
931+
// Err(e) => {
932+
// log_error!(self.logger, "Failed to access payment store: {}", e);
933+
// return Err(ReplayEvent());
934+
// },
935+
//};
914936
}
915937
},
916938
LdkEvent::PaymentClaimed {
@@ -921,9 +943,9 @@ where
921943
htlcs: _,
922944
sender_intended_total_msat: _,
923945
onion_fields,
924-
payment_id: _,
946+
payment_id,
925947
} => {
926-
let payment_id = PaymentId(payment_hash.0);
948+
let payment_id = payment_id.unwrap_or(PaymentId(payment_hash.0));
927949
log_info!(
928950
self.logger,
929951
"Claimed payment with ID {} from payment hash {} of {}msat.",

0 commit comments

Comments
 (0)