Skip to content

Commit 5d5d5f5

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 87cda06 commit 5d5d5f5

File tree

1 file changed

+175
-104
lines changed

1 file changed

+175
-104
lines changed

src/event.rs

Lines changed: 175 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,91 @@ 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;
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;
507+
PaymentPurpose::Bolt11InvoicePayment { payment_preimage, .. } => {
508+
payment_preimage
532509
},
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 {
553+
payment_preimage,
554+
payment_secret,
555+
..
556+
} => {
557+
let payment = PaymentDetails {
558+
id: payment_id,
559+
kind: PaymentKind::Bolt12Refund {
560+
hash: Some(payment_hash),
561+
preimage: payment_preimage,
562+
secret: Some(payment_secret),
563+
},
564+
amount_msat: Some(amount_msat),
565+
direction: PaymentDirection::Inbound,
566+
status: PaymentStatus::Pending,
567+
};
568+
match self.payment_store.insert(payment) {
569+
Ok(false) => (),
570+
Ok(true) => {
571+
log_error!(
572+
self.logger,
573+
"Bolt12RefundPayment with ID {} was previously known",
574+
payment_id,
575+
);
576+
debug_assert!(false);
577+
},
578+
Err(e) => {
579+
log_error!(
580+
self.logger,
581+
"Failed to insert payment with ID {}: {}",
582+
payment_id,
583+
e
584+
);
585+
debug_assert!(false);
586+
},
587+
}
588+
payment_preimage
622589
},
623590
PaymentPurpose::SpontaneousPayment(preimage) => {
591+
// Since it's spontaneous, we insert it now into our store.
624592
let payment = PaymentDetails {
625593
id: payment_id,
626594
kind: PaymentKind::Spontaneous {
@@ -629,32 +597,133 @@ where
629597
},
630598
amount_msat: Some(amount_msat),
631599
direction: PaymentDirection::Inbound,
632-
status: PaymentStatus::Succeeded,
600+
status: PaymentStatus::Pending,
633601
};
634602

635603
match self.payment_store.insert(payment) {
636604
Ok(false) => (),
637605
Ok(true) => {
638606
log_error!(
639607
self.logger,
640-
"Spontaneous payment with hash {} was previously known",
641-
hex_utils::to_string(&payment_hash.0)
608+
"Spontaneous payment with ID {} was previously known",
609+
payment_id,
642610
);
643611
debug_assert!(false);
644612
},
645613
Err(e) => {
646614
log_error!(
647615
self.logger,
648-
"Failed to insert payment with hash {}: {}",
649-
hex_utils::to_string(&payment_hash.0),
616+
"Failed to insert payment with ID {}: {}",
617+
payment_id,
650618
e
651619
);
652620
debug_assert!(false);
653621
},
654622
}
623+
624+
Some(preimage)
625+
},
626+
};
627+
628+
if let Some(preimage) = payment_preimage {
629+
self.channel_manager.claim_funds(preimage);
630+
} else {
631+
log_error!(
632+
self.logger,
633+
"Failed to claim payment with ID {}: preimage unknown.",
634+
payment_id,
635+
);
636+
self.channel_manager.fail_htlc_backwards(&payment_hash);
637+
638+
let update = PaymentDetailsUpdate {
639+
hash: Some(Some(payment_hash)),
640+
status: Some(PaymentStatus::Failed),
641+
..PaymentDetailsUpdate::new(payment_id)
642+
};
643+
self.payment_store.update(&update).unwrap_or_else(|e| {
644+
log_error!(self.logger, "Failed to access payment store: {}", e);
645+
panic!("Failed to access payment store");
646+
});
647+
}
648+
},
649+
LdkEvent::PaymentClaimed {
650+
payment_hash,
651+
purpose,
652+
amount_msat,
653+
receiver_node_id: _,
654+
htlcs: _,
655+
sender_intended_total_msat: _,
656+
} => {
657+
let payment_id = PaymentId(payment_hash.0);
658+
log_info!(
659+
self.logger,
660+
"Claimed payment with ID {} from payment hash {} of {}msat.",
661+
payment_id,
662+
hex_utils::to_string(&payment_hash.0),
663+
amount_msat,
664+
);
665+
666+
let update = match purpose {
667+
PaymentPurpose::Bolt11InvoicePayment {
668+
payment_preimage,
669+
payment_secret,
670+
..
671+
} => PaymentDetailsUpdate {
672+
preimage: Some(payment_preimage),
673+
secret: Some(Some(payment_secret)),
674+
amount_msat: Some(Some(amount_msat)),
675+
status: Some(PaymentStatus::Succeeded),
676+
..PaymentDetailsUpdate::new(payment_id)
677+
},
678+
PaymentPurpose::Bolt12OfferPayment {
679+
payment_preimage, payment_secret, ..
680+
} => PaymentDetailsUpdate {
681+
preimage: Some(payment_preimage),
682+
secret: Some(Some(payment_secret)),
683+
amount_msat: Some(Some(amount_msat)),
684+
status: Some(PaymentStatus::Succeeded),
685+
..PaymentDetailsUpdate::new(payment_id)
686+
},
687+
PaymentPurpose::Bolt12RefundPayment {
688+
payment_preimage,
689+
payment_secret,
690+
..
691+
} => PaymentDetailsUpdate {
692+
preimage: Some(payment_preimage),
693+
secret: Some(Some(payment_secret)),
694+
amount_msat: Some(Some(amount_msat)),
695+
status: Some(PaymentStatus::Succeeded),
696+
..PaymentDetailsUpdate::new(payment_id)
697+
},
698+
PaymentPurpose::SpontaneousPayment(preimage) => PaymentDetailsUpdate {
699+
preimage: Some(Some(preimage)),
700+
amount_msat: Some(Some(amount_msat)),
701+
status: Some(PaymentStatus::Succeeded),
702+
..PaymentDetailsUpdate::new(payment_id)
655703
},
656704
};
657705

706+
match self.payment_store.update(&update) {
707+
Ok(true) => (),
708+
Ok(false) => {
709+
log_error!(
710+
self.logger,
711+
"Payment with ID {} couldn't be found in store",
712+
payment_id,
713+
);
714+
debug_assert!(false);
715+
},
716+
Err(e) => {
717+
log_error!(
718+
self.logger,
719+
"Failed to update payment with ID {}: {}",
720+
payment_id,
721+
e
722+
);
723+
panic!("Failed to access payment store");
724+
},
725+
}
726+
658727
self.event_queue
659728
.add_event(Event::PaymentReceived {
660729
payment_id: Some(payment_id),
@@ -681,6 +750,7 @@ where
681750
};
682751

683752
let update = PaymentDetailsUpdate {
753+
hash: Some(Some(payment_hash)),
684754
preimage: Some(Some(payment_preimage)),
685755
status: Some(PaymentStatus::Succeeded),
686756
..PaymentDetailsUpdate::new(payment_id)
@@ -727,6 +797,7 @@ where
727797
);
728798

729799
let update = PaymentDetailsUpdate {
800+
hash: Some(Some(payment_hash)),
730801
status: Some(PaymentStatus::Failed),
731802
..PaymentDetailsUpdate::new(payment_id)
732803
};

0 commit comments

Comments
 (0)