Skip to content

Commit 96c5941

Browse files
committed
f - DRY up process_pending_htlc_forwards spontaneous receive
1 parent 9624d9b commit 96c5941

File tree

1 file changed

+14
-45
lines changed

1 file changed

+14
-45
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 14 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -3493,7 +3493,7 @@ where
34933493
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
34943494
}
34953495
};
3496-
let mut claimable_htlc = ClaimableHTLC {
3496+
let claimable_htlc = ClaimableHTLC {
34973497
prev_hop: HTLCPreviousHopData {
34983498
short_channel_id: prev_short_channel_id,
34993499
outpoint: prev_funding_outpoint,
@@ -3543,7 +3543,7 @@ where
35433543
}
35443544

35453545
macro_rules! check_total_value {
3546-
($payment_data: expr, $payment_preimage: expr, $is_keysend: expr) => {{
3546+
($payment_secret: expr, $payment_preimage: expr, $total_msat: expr, $is_keysend: expr) => {{
35473547
let mut payment_claimable_generated = false;
35483548
let purpose = if $is_keysend {
35493549
events::PaymentPurpose::SpontaneousPayment(
@@ -3552,13 +3552,17 @@ where
35523552
} else {
35533553
events::PaymentPurpose::InvoicePayment {
35543554
payment_preimage: $payment_preimage,
3555-
payment_secret: $payment_data.payment_secret,
3555+
payment_secret: $payment_secret.unwrap(),
35563556
}
35573557
};
35583558
let mut claimable_payments = self.claimable_payments.lock().unwrap();
35593559
if claimable_payments.pending_claiming_payments.contains_key(&payment_hash) {
35603560
fail_htlc!(claimable_htlc, payment_hash);
35613561
}
3562+
if $is_keysend && $payment_secret.is_none() && claimable_payments.claimable_payments.get(&payment_hash).is_some() {
3563+
log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} for duplicative payment hash", log_bytes!(payment_hash.0));
3564+
fail_htlc!(claimable_htlc, payment_hash);
3565+
}
35623566
let ref mut claimable_payment = claimable_payments.claimable_payments
35633567
.entry(payment_hash)
35643568
// Note that if we insert here we MUST NOT fail_htlc!()
@@ -3587,9 +3591,9 @@ where
35873591
for htlc in htlcs.iter() {
35883592
total_value += htlc.sender_intended_value;
35893593
earliest_expiry = cmp::min(earliest_expiry, htlc.cltv_expiry);
3590-
if htlc.total_msat != $payment_data.total_msat {
3594+
if htlc.total_msat != $total_msat {
35913595
log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the HTLCs had inconsistent total values (eg {} and {})",
3592-
log_bytes!(payment_hash.0), $payment_data.total_msat, htlc.total_msat);
3596+
log_bytes!(payment_hash.0), $total_msat, htlc.total_msat);
35933597
total_value = msgs::MAX_VALUE_MSAT;
35943598
}
35953599
if total_value >= msgs::MAX_VALUE_MSAT { break; }
@@ -3598,11 +3602,11 @@ where
35983602
// match exactly the condition used in `timer_tick_occurred`
35993603
if total_value >= msgs::MAX_VALUE_MSAT {
36003604
fail_htlc!(claimable_htlc, payment_hash);
3601-
} else if total_value - claimable_htlc.sender_intended_value >= $payment_data.total_msat {
3605+
} else if total_value - claimable_htlc.sender_intended_value >= $total_msat {
36023606
log_trace!(self.logger, "Failing HTLC with payment_hash {} as payment is already claimable",
36033607
log_bytes!(payment_hash.0));
36043608
fail_htlc!(claimable_htlc, payment_hash);
3605-
} else if total_value >= $payment_data.total_msat {
3609+
} else if total_value >= $total_msat {
36063610
#[allow(unused_assignments)] {
36073611
committed_to_claimable = true;
36083612
}
@@ -3661,45 +3665,10 @@ where
36613665
fail_htlc!(claimable_htlc, payment_hash);
36623666
}
36633667
}
3664-
check_total_value!(payment_data, payment_preimage, false);
3668+
check_total_value!(Some(payment_data.payment_secret), payment_preimage, payment_data.total_msat, false);
36653669
},
36663670
OnionPayload::Spontaneous(preimage) => {
3667-
if let Some(payment_data) = payment_data {
3668-
check_total_value!(payment_data, Some(preimage), true);
3669-
} else {
3670-
let mut claimable_payments = self.claimable_payments.lock().unwrap();
3671-
if claimable_payments.pending_claiming_payments.contains_key(&payment_hash) {
3672-
fail_htlc!(claimable_htlc, payment_hash);
3673-
}
3674-
match claimable_payments.claimable_payments.entry(payment_hash) {
3675-
hash_map::Entry::Vacant(e) => {
3676-
let amount_msat = claimable_htlc.value;
3677-
claimable_htlc.total_value_received = Some(amount_msat);
3678-
let claim_deadline = Some(claimable_htlc.cltv_expiry - HTLC_FAIL_BACK_BUFFER);
3679-
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
3680-
e.insert(ClaimablePayment {
3681-
purpose: purpose.clone(),
3682-
onion_fields: Some(onion_fields.clone()),
3683-
htlcs: vec![claimable_htlc],
3684-
});
3685-
let prev_channel_id = prev_funding_outpoint.to_channel_id();
3686-
new_events.push_back((events::Event::PaymentClaimable {
3687-
receiver_node_id: Some(receiver_node_id),
3688-
payment_hash,
3689-
amount_msat,
3690-
purpose,
3691-
via_channel_id: Some(prev_channel_id),
3692-
via_user_channel_id: Some(prev_user_channel_id),
3693-
claim_deadline,
3694-
onion_fields: Some(onion_fields),
3695-
}, None));
3696-
},
3697-
hash_map::Entry::Occupied(_) => {
3698-
log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} for a duplicative payment hash", log_bytes!(payment_hash.0));
3699-
fail_htlc!(claimable_htlc, payment_hash);
3700-
}
3701-
}
3702-
}
3671+
check_total_value!(payment_data.as_ref().map(|d| d.payment_secret), Some(preimage), payment_data.as_ref().map_or(claimable_htlc.value, |d| d.total_msat), true);
37033672
}
37043673
}
37053674
},
@@ -3717,7 +3686,7 @@ where
37173686
log_bytes!(payment_hash.0), payment_data.total_msat, inbound_payment.get().min_value_msat.unwrap());
37183687
fail_htlc!(claimable_htlc, payment_hash);
37193688
} else {
3720-
let payment_claimable_generated = check_total_value!(payment_data, inbound_payment.get().payment_preimage, false);
3689+
let payment_claimable_generated = check_total_value!(Some(payment_data.payment_secret), inbound_payment.get().payment_preimage, payment_data.total_msat, false);
37213690
if payment_claimable_generated {
37223691
inbound_payment.remove_entry();
37233692
}

0 commit comments

Comments
 (0)