Skip to content

Commit 602a306

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 d147cbb commit 602a306

File tree

1 file changed

+135
-106
lines changed

1 file changed

+135
-106
lines changed

src/event.rs

Lines changed: 135 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -433,9 +433,19 @@ where
433433
onion_fields: _,
434434
counterparty_skimmed_fee_msat,
435435
} => {
436-
let payment_id = PaymentId(payment_hash.0);
436+
let payment_id = match purpose {
437+
PaymentPurpose::Bolt11InvoicePayment { .. } => PaymentId(payment_hash.0),
438+
PaymentPurpose::Bolt12OfferPayment { ref payment_context, .. } => {
439+
PaymentId(payment_context.offer_id.0)
440+
},
441+
PaymentPurpose::Bolt12RefundPayment { .. } => PaymentId(payment_hash.0),
442+
PaymentPurpose::SpontaneousPayment(..) => PaymentId(payment_hash.0),
443+
};
444+
437445
if let Some(info) = self.payment_store.get(&payment_id) {
438-
if info.status == PaymentStatus::Succeeded {
446+
if info.status == PaymentStatus::Succeeded
447+
|| matches!(info.kind, PaymentKind::Spontaneous { .. })
448+
{
439449
log_info!(
440450
self.logger,
441451
"Refused duplicate inbound payment from payment hash {} of {}msat",
@@ -445,6 +455,7 @@ where
445455
self.channel_manager.fail_htlc_backwards(&payment_hash);
446456

447457
let update = PaymentDetailsUpdate {
458+
hash: Some(Some(payment_hash)),
448459
status: Some(PaymentStatus::Failed),
449460
..PaymentDetailsUpdate::new(payment_id)
450461
};
@@ -483,6 +494,7 @@ where
483494
self.channel_manager.fail_htlc_backwards(&payment_hash);
484495

485496
let update = PaymentDetailsUpdate {
497+
hash: Some(Some(payment_hash)),
486498
status: Some(PaymentStatus::Failed),
487499
..PaymentDetailsUpdate::new(payment_id)
488500
};
@@ -502,48 +514,75 @@ where
502514
);
503515
let payment_preimage = match purpose {
504516
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-
}
517+
payment_preimage.or(self
518+
.channel_manager
519+
.get_payment_preimage(payment_hash, payment_secret)
520+
.ok())
512521
},
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;
522-
},
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;
522+
PaymentPurpose::Bolt12OfferPayment {
523+
payment_preimage, payment_secret, ..
524+
} => payment_preimage.or(self
525+
.channel_manager
526+
.get_payment_preimage(payment_hash, payment_secret)
527+
.ok()),
528+
PaymentPurpose::Bolt12RefundPayment {
529+
payment_preimage,
530+
payment_secret,
531+
..
532+
} => payment_preimage.or(self
533+
.channel_manager
534+
.get_payment_preimage(payment_hash, payment_secret)
535+
.ok()),
536+
PaymentPurpose::SpontaneousPayment(preimage) => {
537+
// Since it's spontaneous, we insert it now into our store.
538+
let payment = PaymentDetails {
539+
id: payment_id,
540+
kind: PaymentKind::Spontaneous {
541+
hash: payment_hash,
542+
preimage: Some(preimage),
543+
},
544+
amount_msat: Some(amount_msat),
545+
direction: PaymentDirection::Inbound,
546+
status: PaymentStatus::Pending,
547+
};
548+
549+
match self.payment_store.insert(payment) {
550+
Ok(false) => (),
551+
Ok(true) => {
552+
log_error!(
553+
self.logger,
554+
"Spontaneous payment with ID {} was previously known",
555+
payment_id,
556+
);
557+
debug_assert!(false);
558+
},
559+
Err(e) => {
560+
log_error!(
561+
self.logger,
562+
"Failed to insert payment with ID {}: {}",
563+
payment_id,
564+
e
565+
);
566+
debug_assert!(false);
567+
},
568+
}
569+
570+
Some(preimage)
532571
},
533-
PaymentPurpose::SpontaneousPayment(preimage) => Some(preimage),
534572
};
535573

536574
if let Some(preimage) = payment_preimage {
537575
self.channel_manager.claim_funds(preimage);
538576
} else {
539577
log_error!(
540578
self.logger,
541-
"Failed to claim payment with hash {}: preimage unknown.",
542-
hex_utils::to_string(&payment_hash.0),
579+
"Failed to claim payment with ID {}: preimage unknown.",
580+
payment_id,
543581
);
544582
self.channel_manager.fail_htlc_backwards(&payment_hash);
545583

546584
let update = PaymentDetailsUpdate {
585+
hash: Some(Some(payment_hash)),
547586
status: Some(PaymentStatus::Failed),
548587
..PaymentDetailsUpdate::new(payment_id)
549588
};
@@ -561,99 +600,87 @@ where
561600
htlcs: _,
562601
sender_intended_total_msat: _,
563602
} => {
603+
let payment_id = match purpose {
604+
PaymentPurpose::Bolt11InvoicePayment { .. } => PaymentId(payment_hash.0),
605+
PaymentPurpose::Bolt12OfferPayment { ref payment_context, .. } => {
606+
PaymentId(payment_context.offer_id.0)
607+
},
608+
PaymentPurpose::Bolt12RefundPayment { .. } => PaymentId(payment_hash.0),
609+
PaymentPurpose::SpontaneousPayment(..) => PaymentId(payment_hash.0),
610+
};
611+
564612
log_info!(
565613
self.logger,
566-
"Claimed payment from payment hash {} of {}msat.",
614+
"Claimed payment with ID {} from payment hash {} of {}msat.",
615+
payment_id,
567616
hex_utils::to_string(&payment_hash.0),
568617
amount_msat,
569618
);
570-
let payment_id = PaymentId(payment_hash.0);
571-
match purpose {
619+
620+
let update = match purpose {
572621
PaymentPurpose::Bolt11InvoicePayment {
573622
payment_preimage,
574623
payment_secret,
575624
..
576-
} => {
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)
583-
};
584-
match self.payment_store.update(&update) {
585-
Ok(true) => (),
586-
Ok(false) => {
587-
log_error!(
588-
self.logger,
589-
"Payment with hash {} couldn't be found in store",
590-
hex_utils::to_string(&payment_hash.0)
591-
);
592-
debug_assert!(false);
593-
},
594-
Err(e) => {
595-
log_error!(
596-
self.logger,
597-
"Failed to update payment with hash {}: {}",
598-
hex_utils::to_string(&payment_hash.0),
599-
e
600-
);
601-
debug_assert!(false);
602-
},
603-
}
625+
} => PaymentDetailsUpdate {
626+
hash: Some(Some(payment_hash)),
627+
preimage: Some(payment_preimage),
628+
secret: Some(Some(payment_secret)),
629+
amount_msat: Some(Some(amount_msat)),
630+
status: Some(PaymentStatus::Succeeded),
631+
..PaymentDetailsUpdate::new(payment_id)
604632
},
605-
PaymentPurpose::Bolt12OfferPayment { .. } => {
606-
// TODO: support BOLT12.
633+
PaymentPurpose::Bolt12OfferPayment {
634+
payment_preimage, payment_secret, ..
635+
} => PaymentDetailsUpdate {
636+
hash: Some(Some(payment_hash)),
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::Bolt12RefundPayment {
644+
payment_preimage,
645+
payment_secret,
646+
..
647+
} => PaymentDetailsUpdate {
648+
hash: Some(Some(payment_hash)),
649+
preimage: Some(payment_preimage),
650+
secret: Some(Some(payment_secret)),
651+
amount_msat: Some(Some(amount_msat)),
652+
status: Some(PaymentStatus::Succeeded),
653+
..PaymentDetailsUpdate::new(payment_id)
654+
},
655+
PaymentPurpose::SpontaneousPayment(preimage) => PaymentDetailsUpdate {
656+
hash: Some(Some(payment_hash)),
657+
preimage: Some(Some(preimage)),
658+
amount_msat: Some(Some(amount_msat)),
659+
status: Some(PaymentStatus::Succeeded),
660+
..PaymentDetailsUpdate::new(payment_id)
661+
},
662+
};
663+
664+
match self.payment_store.update(&update) {
665+
Ok(true) => (),
666+
Ok(false) => {
607667
log_error!(
608668
self.logger,
609-
"Failed to claim unsupported BOLT12 payment with hash: {}",
610-
payment_hash
669+
"Payment with ID {} couldn't be found in store",
670+
payment_id,
611671
);
612-
return;
672+
debug_assert!(false);
613673
},
614-
PaymentPurpose::Bolt12RefundPayment { .. } => {
615-
// TODO: support BOLT12.
674+
Err(e) => {
616675
log_error!(
617676
self.logger,
618-
"Failed to claim unsupported BOLT12 payment with hash: {}",
619-
payment_hash
677+
"Failed to update payment with ID {}: {}",
678+
payment_id,
679+
e
620680
);
621-
return;
622-
},
623-
PaymentPurpose::SpontaneousPayment(preimage) => {
624-
let payment = PaymentDetails {
625-
id: payment_id,
626-
kind: PaymentKind::Spontaneous {
627-
hash: payment_hash,
628-
preimage: Some(preimage),
629-
},
630-
amount_msat: Some(amount_msat),
631-
direction: PaymentDirection::Inbound,
632-
status: PaymentStatus::Succeeded,
633-
};
634-
635-
match self.payment_store.insert(payment) {
636-
Ok(false) => (),
637-
Ok(true) => {
638-
log_error!(
639-
self.logger,
640-
"Spontaneous payment with hash {} was previously known",
641-
hex_utils::to_string(&payment_hash.0)
642-
);
643-
debug_assert!(false);
644-
},
645-
Err(e) => {
646-
log_error!(
647-
self.logger,
648-
"Failed to insert payment with hash {}: {}",
649-
hex_utils::to_string(&payment_hash.0),
650-
e
651-
);
652-
debug_assert!(false);
653-
},
654-
}
681+
panic!("Failed to access payment store");
655682
},
656-
};
683+
}
657684

658685
self.event_queue
659686
.add_event(Event::PaymentReceived {
@@ -681,6 +708,7 @@ where
681708
};
682709

683710
let update = PaymentDetailsUpdate {
711+
hash: Some(Some(payment_hash)),
684712
preimage: Some(Some(payment_preimage)),
685713
status: Some(PaymentStatus::Succeeded),
686714
..PaymentDetailsUpdate::new(payment_id)
@@ -727,6 +755,7 @@ where
727755
);
728756

729757
let update = PaymentDetailsUpdate {
758+
hash: Some(Some(payment_hash)),
730759
status: Some(PaymentStatus::Failed),
731760
..PaymentDetailsUpdate::new(payment_id)
732761
};

0 commit comments

Comments
 (0)