Skip to content

Commit 250ee32

Browse files
committed
f - get rid of unwraps in check_total_value
1 parent 0b35ab8 commit 250ee32

File tree

1 file changed

+21
-22
lines changed

1 file changed

+21
-22
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3555,21 +3555,11 @@ where
35553555
}
35563556

35573557
macro_rules! check_total_value {
3558-
($payment_secret: expr, $payment_preimage: expr) => {{
3558+
($purpose: expr) => {{
35593559
let mut payment_claimable_generated = false;
3560-
let is_keysend = match claimable_htlc.onion_payload {
3561-
OnionPayload::Spontaneous(_) => true,
3562-
OnionPayload::Invoice { .. } => false,
3563-
};
3564-
let purpose = if is_keysend {
3565-
events::PaymentPurpose::SpontaneousPayment(
3566-
$payment_preimage.expect("Should never call check_total_value with keysend payment but no preimage")
3567-
)
3568-
} else {
3569-
events::PaymentPurpose::InvoicePayment {
3570-
payment_preimage: $payment_preimage,
3571-
payment_secret: $payment_secret.expect("Should never call check_total_value with non-keysend payment but no payment secret"),
3572-
}
3560+
let is_keysend = match $purpose {
3561+
events::PaymentPurpose::SpontaneousPayment(_) => true,
3562+
events::PaymentPurpose::InvoicePayment { .. } => false,
35733563
};
35743564
let mut claimable_payments = self.claimable_payments.lock().unwrap();
35753565
if claimable_payments.pending_claiming_payments.contains_key(&payment_hash) {
@@ -3581,14 +3571,14 @@ where
35813571
.or_insert_with(|| {
35823572
committed_to_claimable = true;
35833573
ClaimablePayment {
3584-
purpose: purpose.clone(), htlcs: Vec::new(), onion_fields: None,
3574+
purpose: $purpose.clone(), htlcs: Vec::new(), onion_fields: None,
35853575
}
35863576
});
35873577
if !self.default_configuration.accept_mpp_keysend && is_keysend && !claimable_payment.htlcs.is_empty() {
35883578
log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} as we already had an existing keysend HTLC with the same payment hash and our config states we don't accept MPP keysend", log_bytes!(payment_hash.0));
35893579
fail_htlc!(claimable_htlc, payment_hash);
35903580
}
3591-
if purpose != claimable_payment.purpose {
3581+
if $purpose != claimable_payment.purpose {
35923582
let log_keysend = |keysend| if keysend { "keysend" } else { "non-keysend" };
35933583
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));
35943584
fail_htlc!(claimable_htlc, payment_hash);
@@ -3632,7 +3622,7 @@ where
36323622
new_events.push_back((events::Event::PaymentClaimable {
36333623
receiver_node_id: Some(receiver_node_id),
36343624
payment_hash,
3635-
purpose,
3625+
purpose: $purpose,
36363626
amount_msat,
36373627
via_channel_id: Some(prev_channel_id),
36383628
via_user_channel_id: Some(prev_user_channel_id),
@@ -3680,18 +3670,23 @@ where
36803670
fail_htlc!(claimable_htlc, payment_hash);
36813671
}
36823672
}
3683-
check_total_value!(Some(payment_data.payment_secret), payment_preimage);
3673+
let purpose = events::PaymentPurpose::InvoicePayment {
3674+
payment_preimage: payment_preimage.clone(),
3675+
payment_secret: payment_data.payment_secret,
3676+
};
3677+
check_total_value!(purpose);
36843678
},
36853679
OnionPayload::Spontaneous(preimage) => {
3686-
check_total_value!(payment_data.as_ref().map(|d| d.payment_secret), Some(preimage));
3680+
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
3681+
check_total_value!(purpose);
36873682
}
36883683
}
36893684
},
36903685
hash_map::Entry::Occupied(inbound_payment) => {
3691-
if payment_data.is_none() {
3686+
if let OnionPayload::Spontaneous(_) = claimable_htlc.onion_payload {
36923687
log_trace!(self.logger, "Failing new keysend HTLC with payment_hash {} because we already have an inbound payment with the same payment hash", log_bytes!(payment_hash.0));
36933688
fail_htlc!(claimable_htlc, payment_hash);
3694-
};
3689+
}
36953690
let payment_data = payment_data.unwrap();
36963691
if inbound_payment.get().payment_secret != payment_data.payment_secret {
36973692
log_trace!(self.logger, "Failing new HTLC with payment_hash {} as it didn't match our expected payment secret.", log_bytes!(payment_hash.0));
@@ -3701,7 +3696,11 @@ where
37013696
log_bytes!(payment_hash.0), payment_data.total_msat, inbound_payment.get().min_value_msat.unwrap());
37023697
fail_htlc!(claimable_htlc, payment_hash);
37033698
} else {
3704-
let payment_claimable_generated = check_total_value!(Some(payment_data.payment_secret), inbound_payment.get().payment_preimage);
3699+
let purpose = events::PaymentPurpose::InvoicePayment {
3700+
payment_preimage: inbound_payment.get().payment_preimage,
3701+
payment_secret: payment_data.payment_secret,
3702+
};
3703+
let payment_claimable_generated = check_total_value!(purpose);
37053704
if payment_claimable_generated {
37063705
inbound_payment.remove_entry();
37073706
}

0 commit comments

Comments
 (0)