Skip to content

Commit a7f5e04

Browse files
committed
f - refactor check_total_value, check purpose matches other htlcs
1 parent 694fe22 commit a7f5e04

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3548,26 +3548,26 @@ where
35483548
}
35493549

35503550
macro_rules! check_total_value {
3551-
($payment_secret: expr, $payment_preimage: expr, $is_keysend: expr) => {{
3551+
($payment_secret: expr, $payment_preimage: expr) => {{
35523552
let mut payment_claimable_generated = false;
3553-
let purpose = if $is_keysend {
3553+
let is_keysend = match claimable_htlc.onion_payload {
3554+
OnionPayload::Spontaneous(_) => true,
3555+
OnionPayload::Invoice { .. } => false,
3556+
};
3557+
let purpose = if is_keysend {
35543558
events::PaymentPurpose::SpontaneousPayment(
3555-
$payment_preimage.expect("Should never call check_total_value with $is_keysend as true but no preimage")
3559+
$payment_preimage.expect("Should never call check_total_value with keysend payment but no preimage")
35563560
)
35573561
} else {
35583562
events::PaymentPurpose::InvoicePayment {
35593563
payment_preimage: $payment_preimage,
3560-
payment_secret: $payment_secret.expect("Should never call check_total_value with $is_keysend as false but no payment secret"),
3564+
payment_secret: $payment_secret.expect("Should never call check_total_value with non-keysend payment but no payment secret"),
35613565
}
35623566
};
35633567
let mut claimable_payments = self.claimable_payments.lock().unwrap();
35643568
if claimable_payments.pending_claiming_payments.contains_key(&payment_hash) {
35653569
fail_htlc!(claimable_htlc, payment_hash);
35663570
}
3567-
if $is_keysend && $payment_secret.is_none() && claimable_payments.claimable_payments.get(&payment_hash).is_some() {
3568-
log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} for duplicative payment hash", log_bytes!(payment_hash.0));
3569-
fail_htlc!(claimable_htlc, payment_hash);
3570-
}
35713571
let ref mut claimable_payment = claimable_payments.claimable_payments
35723572
.entry(payment_hash)
35733573
// Note that if we insert here we MUST NOT fail_htlc!()
@@ -3577,6 +3577,11 @@ where
35773577
purpose: purpose.clone(), htlcs: Vec::new(), onion_fields: None,
35783578
}
35793579
});
3580+
if purpose != claimable_payment.purpose {
3581+
let log_keysend = |keysend| if keysend { "keysend" } else { "non-keysend" };
3582+
log_trace!(self.logger, "Failing new {} HTLC with payment_hash {} as we already had an existing {} HTLC with the same payment hash", log_keysend(is_keysend), log_bytes!(payment_hash.0), log_keysend(!is_keysend));
3583+
fail_htlc!(claimable_htlc, payment_hash);
3584+
}
35803585
if let Some(earlier_fields) = &mut claimable_payment.onion_fields {
35813586
if earlier_fields.check_merge(&mut onion_fields).is_err() {
35823587
fail_htlc!(claimable_htlc, payment_hash);
@@ -3585,12 +3590,6 @@ where
35853590
claimable_payment.onion_fields = Some(onion_fields);
35863591
}
35873592
let ref mut htlcs = &mut claimable_payment.htlcs;
3588-
if !htlcs.is_empty() && !$is_keysend {
3589-
if let OnionPayload::Spontaneous(_) = htlcs[0].onion_payload {
3590-
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as we already had an existing keysend HTLC with the same payment hash", log_bytes!(payment_hash.0));
3591-
fail_htlc!(claimable_htlc, payment_hash);
3592-
}
3593-
}
35943593
let mut total_value = claimable_htlc.sender_intended_value;
35953594
let mut earliest_expiry = claimable_htlc.cltv_expiry;
35963595
for htlc in htlcs.iter() {
@@ -3670,10 +3669,10 @@ where
36703669
fail_htlc!(claimable_htlc, payment_hash);
36713670
}
36723671
}
3673-
check_total_value!(Some(payment_data.payment_secret), payment_preimage, false);
3672+
check_total_value!(Some(payment_data.payment_secret), payment_preimage);
36743673
},
36753674
OnionPayload::Spontaneous(preimage) => {
3676-
check_total_value!(payment_data.as_ref().map(|d| d.payment_secret), Some(preimage), true);
3675+
check_total_value!(payment_data.as_ref().map(|d| d.payment_secret), Some(preimage));
36773676
}
36783677
}
36793678
},
@@ -3691,7 +3690,7 @@ where
36913690
log_bytes!(payment_hash.0), payment_data.total_msat, inbound_payment.get().min_value_msat.unwrap());
36923691
fail_htlc!(claimable_htlc, payment_hash);
36933692
} else {
3694-
let payment_claimable_generated = check_total_value!(Some(payment_data.payment_secret), inbound_payment.get().payment_preimage, false);
3693+
let payment_claimable_generated = check_total_value!(Some(payment_data.payment_secret), inbound_payment.get().payment_preimage);
36953694
if payment_claimable_generated {
36963695
inbound_payment.remove_entry();
36973696
}

0 commit comments

Comments
 (0)