Skip to content

Commit 962476a

Browse files
committed
Update the payment store in event handling
.. depending on the payment purpose, we derive the inbound payment ID and update the status in `PaymentClaimable`/`PaymentClaimed`
1 parent 8a67d7e commit 962476a

File tree

2 files changed

+142
-105
lines changed

2 files changed

+142
-105
lines changed

src/event.rs

Lines changed: 140 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,9 @@ where
435435
} => {
436436
let payment_id = PaymentId(payment_hash.0);
437437
if let Some(info) = self.payment_store.get(&payment_id) {
438-
if info.status == PaymentStatus::Succeeded {
438+
if info.status == PaymentStatus::Succeeded
439+
|| matches!(info.kind, PaymentKind::Spontaneous { .. })
440+
{
439441
log_info!(
440442
self.logger,
441443
"Refused duplicate inbound payment from payment hash {} of {}msat",
@@ -483,6 +485,7 @@ where
483485
self.channel_manager.fail_htlc_backwards(&payment_hash);
484486

485487
let update = PaymentDetailsUpdate {
488+
hash: Some(Some(payment_hash)),
486489
status: Some(PaymentStatus::Failed),
487490
..PaymentDetailsUpdate::new(payment_id)
488491
};
@@ -501,126 +504,56 @@ where
501504
amount_msat,
502505
);
503506
let payment_preimage = match purpose {
504-
PaymentPurpose::Bolt11InvoicePayment { payment_preimage, payment_secret } => {
505-
if payment_preimage.is_some() {
506-
payment_preimage
507-
} else {
508-
self.channel_manager
509-
.get_payment_preimage(payment_hash, payment_secret)
510-
.ok()
511-
}
512-
},
513-
PaymentPurpose::Bolt12OfferPayment { .. } => {
514-
// TODO: support BOLT12.
515-
log_error!(
516-
self.logger,
517-
"Failed to claim unsupported BOLT12 payment with hash: {}",
518-
payment_hash
519-
);
520-
self.channel_manager.fail_htlc_backwards(&payment_hash);
521-
return;
507+
PaymentPurpose::Bolt11InvoicePayment { payment_preimage, .. } => {
508+
payment_preimage
522509
},
523-
PaymentPurpose::Bolt12RefundPayment { .. } => {
524-
// TODO: support BOLT12.
525-
log_error!(
526-
self.logger,
527-
"Failed to claim unsupported BOLT12 payment with hash: {}",
528-
payment_hash
529-
);
530-
self.channel_manager.fail_htlc_backwards(&payment_hash);
531-
return;
532-
},
533-
PaymentPurpose::SpontaneousPayment(preimage) => Some(preimage),
534-
};
535-
536-
if let Some(preimage) = payment_preimage {
537-
self.channel_manager.claim_funds(preimage);
538-
} else {
539-
log_error!(
540-
self.logger,
541-
"Failed to claim payment with hash {}: preimage unknown.",
542-
hex_utils::to_string(&payment_hash.0),
543-
);
544-
self.channel_manager.fail_htlc_backwards(&payment_hash);
545-
546-
let update = PaymentDetailsUpdate {
547-
status: Some(PaymentStatus::Failed),
548-
..PaymentDetailsUpdate::new(payment_id)
549-
};
550-
self.payment_store.update(&update).unwrap_or_else(|e| {
551-
log_error!(self.logger, "Failed to access payment store: {}", e);
552-
panic!("Failed to access payment store");
553-
});
554-
}
555-
},
556-
LdkEvent::PaymentClaimed {
557-
payment_hash,
558-
purpose,
559-
amount_msat,
560-
receiver_node_id: _,
561-
htlcs: _,
562-
sender_intended_total_msat: _,
563-
} => {
564-
log_info!(
565-
self.logger,
566-
"Claimed payment from payment hash {} of {}msat.",
567-
hex_utils::to_string(&payment_hash.0),
568-
amount_msat,
569-
);
570-
let payment_id = PaymentId(payment_hash.0);
571-
match purpose {
572-
PaymentPurpose::Bolt11InvoicePayment {
510+
PaymentPurpose::Bolt12OfferPayment {
573511
payment_preimage,
574512
payment_secret,
513+
payment_context,
575514
..
576515
} => {
577-
let update = PaymentDetailsUpdate {
578-
preimage: Some(payment_preimage),
579-
secret: Some(Some(payment_secret)),
580-
amount_msat: Some(Some(amount_msat)),
581-
status: Some(PaymentStatus::Succeeded),
582-
..PaymentDetailsUpdate::new(payment_id)
516+
let offer_id = payment_context.offer_id;
517+
let payment = PaymentDetails {
518+
id: payment_id,
519+
kind: PaymentKind::Bolt12Offer {
520+
hash: Some(payment_hash),
521+
preimage: payment_preimage,
522+
secret: Some(payment_secret),
523+
offer_id,
524+
},
525+
amount_msat: Some(amount_msat),
526+
direction: PaymentDirection::Inbound,
527+
status: PaymentStatus::Pending,
583528
};
584-
match self.payment_store.update(&update) {
585-
Ok(true) => (),
586-
Ok(false) => {
529+
530+
match self.payment_store.insert(payment) {
531+
Ok(false) => (),
532+
Ok(true) => {
587533
log_error!(
588534
self.logger,
589-
"Payment with hash {} couldn't be found in store",
590-
hex_utils::to_string(&payment_hash.0)
535+
"Bolt12OfferPayment with ID {} was previously known",
536+
payment_id,
591537
);
592538
debug_assert!(false);
593539
},
594540
Err(e) => {
595541
log_error!(
596542
self.logger,
597-
"Failed to update payment with hash {}: {}",
598-
hex_utils::to_string(&payment_hash.0),
543+
"Failed to insert payment with ID {}: {}",
544+
payment_id,
599545
e
600546
);
601547
debug_assert!(false);
602548
},
603549
}
550+
payment_preimage
604551
},
605-
PaymentPurpose::Bolt12OfferPayment { .. } => {
606-
// TODO: support BOLT12.
607-
log_error!(
608-
self.logger,
609-
"Failed to claim unsupported BOLT12 payment with hash: {}",
610-
payment_hash
611-
);
612-
return;
613-
},
614-
PaymentPurpose::Bolt12RefundPayment { .. } => {
615-
// TODO: support BOLT12.
616-
log_error!(
617-
self.logger,
618-
"Failed to claim unsupported BOLT12 payment with hash: {}",
619-
payment_hash
620-
);
621-
return;
552+
PaymentPurpose::Bolt12RefundPayment { payment_preimage, .. } => {
553+
payment_preimage
622554
},
623555
PaymentPurpose::SpontaneousPayment(preimage) => {
556+
// Since it's spontaneous, we insert it now into our store.
624557
let payment = PaymentDetails {
625558
id: payment_id,
626559
kind: PaymentKind::Spontaneous {
@@ -629,32 +562,133 @@ where
629562
},
630563
amount_msat: Some(amount_msat),
631564
direction: PaymentDirection::Inbound,
632-
status: PaymentStatus::Succeeded,
565+
status: PaymentStatus::Pending,
633566
};
634567

635568
match self.payment_store.insert(payment) {
636569
Ok(false) => (),
637570
Ok(true) => {
638571
log_error!(
639572
self.logger,
640-
"Spontaneous payment with hash {} was previously known",
641-
hex_utils::to_string(&payment_hash.0)
573+
"Spontaneous payment with ID {} was previously known",
574+
payment_id,
642575
);
643576
debug_assert!(false);
644577
},
645578
Err(e) => {
646579
log_error!(
647580
self.logger,
648-
"Failed to insert payment with hash {}: {}",
649-
hex_utils::to_string(&payment_hash.0),
581+
"Failed to insert payment with ID {}: {}",
582+
payment_id,
650583
e
651584
);
652585
debug_assert!(false);
653586
},
654587
}
588+
589+
Some(preimage)
655590
},
656591
};
657592

593+
if let Some(preimage) = payment_preimage {
594+
self.channel_manager.claim_funds(preimage);
595+
} else {
596+
log_error!(
597+
self.logger,
598+
"Failed to claim payment with ID {}: preimage unknown.",
599+
payment_id,
600+
);
601+
self.channel_manager.fail_htlc_backwards(&payment_hash);
602+
603+
let update = PaymentDetailsUpdate {
604+
hash: Some(Some(payment_hash)),
605+
status: Some(PaymentStatus::Failed),
606+
..PaymentDetailsUpdate::new(payment_id)
607+
};
608+
self.payment_store.update(&update).unwrap_or_else(|e| {
609+
log_error!(self.logger, "Failed to access payment store: {}", e);
610+
panic!("Failed to access payment store");
611+
});
612+
}
613+
},
614+
LdkEvent::PaymentClaimed {
615+
payment_hash,
616+
purpose,
617+
amount_msat,
618+
receiver_node_id: _,
619+
htlcs: _,
620+
sender_intended_total_msat: _,
621+
} => {
622+
let payment_id = PaymentId(payment_hash.0);
623+
log_info!(
624+
self.logger,
625+
"Claimed payment with ID {} from payment hash {} of {}msat.",
626+
payment_id,
627+
hex_utils::to_string(&payment_hash.0),
628+
amount_msat,
629+
);
630+
631+
let update = match purpose {
632+
PaymentPurpose::Bolt11InvoicePayment {
633+
payment_preimage,
634+
payment_secret,
635+
..
636+
} => PaymentDetailsUpdate {
637+
preimage: Some(payment_preimage),
638+
secret: Some(Some(payment_secret)),
639+
amount_msat: Some(Some(amount_msat)),
640+
status: Some(PaymentStatus::Succeeded),
641+
..PaymentDetailsUpdate::new(payment_id)
642+
},
643+
PaymentPurpose::Bolt12OfferPayment {
644+
payment_preimage, payment_secret, ..
645+
} => PaymentDetailsUpdate {
646+
preimage: Some(payment_preimage),
647+
secret: Some(Some(payment_secret)),
648+
amount_msat: Some(Some(amount_msat)),
649+
status: Some(PaymentStatus::Succeeded),
650+
..PaymentDetailsUpdate::new(payment_id)
651+
},
652+
PaymentPurpose::Bolt12RefundPayment {
653+
payment_preimage,
654+
payment_secret,
655+
..
656+
} => PaymentDetailsUpdate {
657+
preimage: Some(payment_preimage),
658+
secret: Some(Some(payment_secret)),
659+
amount_msat: Some(Some(amount_msat)),
660+
status: Some(PaymentStatus::Succeeded),
661+
..PaymentDetailsUpdate::new(payment_id)
662+
},
663+
PaymentPurpose::SpontaneousPayment(preimage) => PaymentDetailsUpdate {
664+
preimage: Some(Some(preimage)),
665+
amount_msat: Some(Some(amount_msat)),
666+
status: Some(PaymentStatus::Succeeded),
667+
..PaymentDetailsUpdate::new(payment_id)
668+
},
669+
};
670+
671+
match self.payment_store.update(&update) {
672+
Ok(true) => (),
673+
Ok(false) => {
674+
log_error!(
675+
self.logger,
676+
"Payment with ID {} couldn't be found in store",
677+
payment_id,
678+
);
679+
debug_assert!(false);
680+
},
681+
Err(e) => {
682+
log_error!(
683+
self.logger,
684+
"Failed to update payment with ID {}: {}",
685+
payment_id,
686+
e
687+
);
688+
panic!("Failed to access payment store");
689+
},
690+
}
691+
658692
self.event_queue
659693
.add_event(Event::PaymentReceived {
660694
payment_id: Some(payment_id),
@@ -681,6 +715,7 @@ where
681715
};
682716

683717
let update = PaymentDetailsUpdate {
718+
hash: Some(Some(payment_hash)),
684719
preimage: Some(Some(payment_preimage)),
685720
status: Some(PaymentStatus::Succeeded),
686721
..PaymentDetailsUpdate::new(payment_id)
@@ -727,6 +762,7 @@ where
727762
);
728763

729764
let update = PaymentDetailsUpdate {
765+
hash: Some(Some(payment_hash)),
730766
status: Some(PaymentStatus::Failed),
731767
..PaymentDetailsUpdate::new(payment_id)
732768
};

src/payment/store.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,8 @@ where
357357
*hash = hash_opt
358358
},
359359
_ => {
360-
// We can omit updating the hash for BOLT11 payments as the payment has will always be known from the beginning.
360+
// We can omit updating the hash for BOLT11 payments as the payment hash
361+
// will always be known from the beginning.
361362
},
362363
}
363364
}

0 commit comments

Comments
 (0)