@@ -899,6 +899,73 @@ struct ClaimablePayments {
899
899
pending_claiming_payments: HashMap<PaymentHash, ClaimingPayment>,
900
900
}
901
901
902
+ impl ClaimablePayments {
903
+ /// Moves a payment from [`Self::claimable_payments`] to [`Self::pending_claiming_payments`].
904
+ ///
905
+ /// If `custom_tlvs_known` is false and custom even TLVs are set by the sender, the set of
906
+ /// pending HTLCs will be returned in the `Err` variant of this method. They MUST then be
907
+ /// failed by the caller as they will not be in either [`Self::claimable_payments`] or
908
+ /// [`Self::pending_claiming_payments`].
909
+ ///
910
+ /// If `custom_tlvs_known` is true, and a matching payment is found, it will always be moved.
911
+ ///
912
+ /// If no payment is found, `Err(Vec::new())` is returned.
913
+ fn begin_claiming_payment<L: Deref, S: Deref>(
914
+ &mut self, payment_hash: PaymentHash, node_signer: &S, logger: &L,
915
+ inbound_payment_id_secret: &[u8; 32], custom_tlvs_known: bool,
916
+ ) -> Result<(Vec<ClaimableHTLC>, ClaimingPayment), Vec<ClaimableHTLC>>
917
+ where L::Target: Logger, S::Target: NodeSigner,
918
+ {
919
+ match self.claimable_payments.remove(&payment_hash) {
920
+ Some(payment) => {
921
+ let mut receiver_node_id = node_signer.get_node_id(Recipient::Node)
922
+ .expect("Failed to get node_id for node recipient");
923
+ for htlc in payment.htlcs.iter() {
924
+ if htlc.prev_hop.phantom_shared_secret.is_some() {
925
+ let phantom_pubkey = node_signer.get_node_id(Recipient::PhantomNode)
926
+ .expect("Failed to get node_id for phantom node recipient");
927
+ receiver_node_id = phantom_pubkey;
928
+ break;
929
+ }
930
+ }
931
+
932
+ if let Some(RecipientOnionFields { custom_tlvs, .. }) = &payment.onion_fields {
933
+ if !custom_tlvs_known && custom_tlvs.iter().any(|(typ, _)| typ % 2 == 0) {
934
+ log_info!(logger, "Rejecting payment with payment hash {} as we cannot accept payment with unknown even TLVs: {}",
935
+ &payment_hash, log_iter!(custom_tlvs.iter().map(|(typ, _)| typ).filter(|typ| *typ % 2 == 0)));
936
+ return Err(payment.htlcs);
937
+ }
938
+ }
939
+
940
+ let payment_id = payment.inbound_payment_id(inbound_payment_id_secret);
941
+ let claiming_payment = self.pending_claiming_payments
942
+ .entry(payment_hash)
943
+ .and_modify(|_| {
944
+ debug_assert!(false, "Shouldn't get a duplicate pending claim event ever");
945
+ log_error!(logger, "Got a duplicate pending claimable event on payment hash {}! Please report this bug",
946
+ &payment_hash);
947
+ })
948
+ .or_insert_with(|| {
949
+ let htlcs = payment.htlcs.iter().map(events::ClaimedHTLC::from).collect();
950
+ let sender_intended_value = payment.htlcs.first().map(|htlc| htlc.total_msat);
951
+ ClaimingPayment {
952
+ amount_msat: payment.htlcs.iter().map(|source| source.value).sum(),
953
+ payment_purpose: payment.purpose,
954
+ receiver_node_id,
955
+ htlcs,
956
+ sender_intended_value,
957
+ onion_fields: payment.onion_fields,
958
+ payment_id: Some(payment_id),
959
+ }
960
+ }).clone();
961
+
962
+ Ok((payment.htlcs, claiming_payment))
963
+ },
964
+ None => Err(Vec::new())
965
+ }
966
+ }
967
+ }
968
+
902
969
/// Events which we process internally but cannot be processed immediately at the generation site
903
970
/// usually because we're running pre-full-init. They are handled immediately once we detect we are
904
971
/// running normally, and specifically must be processed before any other non-background
@@ -6700,60 +6767,24 @@ where
6700
6767
6701
6768
let _persistence_guard = PersistenceNotifierGuard::notify_on_drop(self);
6702
6769
6703
- let claiming_payment;
6704
- let sources = {
6705
- let mut claimable_payments = self.claimable_payments.lock().unwrap();
6706
- if let Some(payment) = claimable_payments.claimable_payments.remove(&payment_hash) {
6707
- let mut receiver_node_id = self.our_network_pubkey;
6708
- for htlc in payment.htlcs.iter() {
6709
- if htlc.prev_hop.phantom_shared_secret.is_some() {
6710
- let phantom_pubkey = self.node_signer.get_node_id(Recipient::PhantomNode)
6711
- .expect("Failed to get node_id for phantom node recipient");
6712
- receiver_node_id = phantom_pubkey;
6713
- break;
6714
- }
6715
- }
6716
-
6717
- let payment_id = payment.inbound_payment_id(&self.inbound_payment_id_secret);
6718
- claiming_payment = claimable_payments.pending_claiming_payments
6719
- .entry(payment_hash)
6720
- .and_modify(|_| {
6721
- debug_assert!(false, "Shouldn't get a duplicate pending claim event ever");
6722
- log_error!(self.logger, "Got a duplicate pending claimable event on payment hash {}! Please report this bug",
6723
- &payment_hash);
6724
- })
6725
- .or_insert_with(|| {
6726
- let htlcs = payment.htlcs.iter().map(events::ClaimedHTLC::from).collect();
6727
- let sender_intended_value = payment.htlcs.first().map(|htlc| htlc.total_msat);
6728
- ClaimingPayment {
6729
- amount_msat: payment.htlcs.iter().map(|source| source.value).sum(),
6730
- payment_purpose: payment.purpose,
6731
- receiver_node_id,
6732
- htlcs,
6733
- sender_intended_value,
6734
- onion_fields: payment.onion_fields,
6735
- payment_id: Some(payment_id),
6736
- }
6737
- }).clone();
6770
+ let (sources, claiming_payment) = {
6771
+ let res = self.claimable_payments.lock().unwrap().begin_claiming_payment(
6772
+ payment_hash, &self.node_signer, &self.logger, &self.inbound_payment_id_secret,
6773
+ custom_tlvs_known,
6774
+ );
6738
6775
6739
- if let Some(RecipientOnionFields { ref custom_tlvs, .. }) = claiming_payment.onion_fields {
6740
- if !custom_tlvs_known && custom_tlvs.iter().any(|(typ, _)| typ % 2 == 0) {
6741
- log_info!(self.logger, "Rejecting payment with payment hash {} as we cannot accept payment with unknown even TLVs: {}",
6742
- &payment_hash, log_iter!(custom_tlvs.iter().map(|(typ, _)| typ).filter(|typ| *typ % 2 == 0)));
6743
- claimable_payments.pending_claiming_payments.remove(&payment_hash);
6744
- mem::drop(claimable_payments);
6745
- for htlc in payment.htlcs {
6746
- let reason = self.get_htlc_fail_reason_from_failure_code(FailureCode::InvalidOnionPayload(None), &htlc);
6747
- let source = HTLCSource::PreviousHopData(htlc.prev_hop);
6748
- let receiver = HTLCDestination::FailedPayment { payment_hash };
6749
- self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
6750
- }
6751
- return;
6776
+ match res {
6777
+ Ok((htlcs, payment_info)) => (htlcs, payment_info),
6778
+ Err(htlcs) => {
6779
+ for htlc in htlcs {
6780
+ let reason = self.get_htlc_fail_reason_from_failure_code(FailureCode::InvalidOnionPayload(None), &htlc);
6781
+ let source = HTLCSource::PreviousHopData(htlc.prev_hop);
6782
+ let receiver = HTLCDestination::FailedPayment { payment_hash };
6783
+ self.fail_htlc_backwards_internal(&source, &payment_hash, &reason, receiver);
6752
6784
}
6785
+ return;
6753
6786
}
6754
-
6755
- payment.htlcs
6756
- } else { return; }
6787
+ }
6757
6788
};
6758
6789
debug_assert!(!sources.is_empty());
6759
6790
0 commit comments