Skip to content

Commit b9f4ebd

Browse files
committed
Add total_value_received to ClaimableHTLC for claim validation
This is pre-work for allowing nodes to overshoot onion values and changing validation for MPP completion. This adds a field to `ClaimableHTLC` that is separate from the onion values, which represents the actual received amount reported in `PaymentClaimable` which is what we want to validate against when a user goes to claim.
1 parent 31e78ff commit b9f4ebd

File tree

1 file changed

+28
-7
lines changed

1 file changed

+28
-7
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ struct ClaimableHTLC {
194194
value: u64,
195195
onion_payload: OnionPayload,
196196
timer_ticks: u8,
197-
/// The sum total of all MPP parts
197+
/// The total value received for a payment (sum of all MPP parts if the payment is a MPP).
198+
/// Gets set to the amount reported when pushing [`Event::PaymentClaimable`].
199+
total_value_received: Option<u64>,
200+
/// The sender intended sum total of all MPP parts specified in the onion
198201
total_msat: u64,
199202
}
200203

@@ -3272,7 +3275,7 @@ where
32723275
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
32733276
}
32743277
};
3275-
let claimable_htlc = ClaimableHTLC {
3278+
let mut claimable_htlc = ClaimableHTLC {
32763279
prev_hop: HTLCPreviousHopData {
32773280
short_channel_id: prev_short_channel_id,
32783281
outpoint: prev_funding_outpoint,
@@ -3282,6 +3285,7 @@ where
32823285
},
32833286
value: outgoing_amt_msat,
32843287
timer_ticks: 0,
3288+
total_value_received: None,
32853289
total_msat: if let Some(data) = &payment_data { data.total_msat } else { outgoing_amt_msat },
32863290
cltv_expiry,
32873291
onion_payload,
@@ -3326,7 +3330,7 @@ where
33263330
fail_htlc!(claimable_htlc, payment_hash);
33273331
continue
33283332
}
3329-
let (_, htlcs) = claimable_payments.claimable_htlcs.entry(payment_hash)
3333+
let (_, ref mut htlcs) = claimable_payments.claimable_htlcs.entry(payment_hash)
33303334
.or_insert_with(|| (purpose(), Vec::new()));
33313335
if htlcs.len() == 1 {
33323336
if let OnionPayload::Spontaneous(_) = htlcs[0].onion_payload {
@@ -3357,11 +3361,13 @@ where
33573361
} else if total_value == $payment_data.total_msat {
33583362
let prev_channel_id = prev_funding_outpoint.to_channel_id();
33593363
htlcs.push(claimable_htlc);
3364+
let amount_msat = htlcs.iter().map(|htlc| htlc.value).sum();
3365+
htlcs.iter_mut().for_each(|htlc| htlc.total_value_received = Some(amount_msat));
33603366
new_events.push(events::Event::PaymentClaimable {
33613367
receiver_node_id: Some(receiver_node_id),
33623368
payment_hash,
33633369
purpose: purpose(),
3364-
amount_msat: total_value,
3370+
amount_msat,
33653371
via_channel_id: Some(prev_channel_id),
33663372
via_user_channel_id: Some(prev_user_channel_id),
33673373
});
@@ -3415,6 +3421,8 @@ where
34153421
}
34163422
match claimable_payments.claimable_htlcs.entry(payment_hash) {
34173423
hash_map::Entry::Vacant(e) => {
3424+
let amount_msat = claimable_htlc.value;
3425+
claimable_htlc.total_value_received = Some(amount_msat);
34183426
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
34193427
e.insert((purpose.clone(), vec![claimable_htlc]));
34203428
let prev_channel_id = prev_funding_outpoint.to_channel_id();
@@ -3960,6 +3968,7 @@ where
39603968
// provide the preimage, so worrying too much about the optimal handling isn't worth
39613969
// it.
39623970
let mut claimable_amt_msat = 0;
3971+
let mut prev_total_msat = None;
39633972
let mut expected_amt_msat = None;
39643973
let mut valid_mpp = true;
39653974
let mut errs = Vec::new();
@@ -3987,14 +3996,22 @@ where
39873996
break;
39883997
}
39893998

3990-
if expected_amt_msat.is_some() && expected_amt_msat != Some(htlc.total_msat) {
3991-
log_error!(self.logger, "Somehow ended up with an MPP payment with different total amounts - this should not be reachable!");
3999+
if prev_total_msat.is_some() && prev_total_msat != Some(htlc.total_msat) {
4000+
log_error!(self.logger, "Somehow ended up with an MPP payment with different expected total amounts - this should not be reachable!");
39924001
debug_assert!(false);
39934002
valid_mpp = false;
39944003
break;
39954004
}
4005+
prev_total_msat = Some(htlc.total_msat);
4006+
4007+
if expected_amt_msat.is_some() && expected_amt_msat != htlc.total_value_received {
4008+
log_error!(self.logger, "Somehow ended up with an MPP payment with different received total amounts - this should not be reachable!");
4009+
debug_assert!(false);
4010+
valid_mpp = false;
4011+
break;
4012+
}
4013+
expected_amt_msat = htlc.total_value_received;
39964014

3997-
expected_amt_msat = Some(htlc.total_msat);
39984015
if let OnionPayload::Spontaneous(_) = &htlc.onion_payload {
39994016
// We don't currently support MPP for spontaneous payments, so just check
40004017
// that there's one payment here and move on.
@@ -6795,6 +6812,7 @@ impl Writeable for ClaimableHTLC {
67956812
(1, self.total_msat, required),
67966813
(2, self.value, required),
67976814
(4, payment_data, option),
6815+
(5, self.total_value_received, option),
67986816
(6, self.cltv_expiry, required),
67996817
(8, keysend_preimage, option),
68006818
});
@@ -6808,13 +6826,15 @@ impl Readable for ClaimableHTLC {
68086826
let mut value = 0;
68096827
let mut payment_data: Option<msgs::FinalOnionHopData> = None;
68106828
let mut cltv_expiry = 0;
6829+
let mut total_value_received = None;
68116830
let mut total_msat = None;
68126831
let mut keysend_preimage: Option<PaymentPreimage> = None;
68136832
read_tlv_fields!(reader, {
68146833
(0, prev_hop, required),
68156834
(1, total_msat, option),
68166835
(2, value, required),
68176836
(4, payment_data, option),
6837+
(5, total_value_received, option),
68186838
(6, cltv_expiry, required),
68196839
(8, keysend_preimage, option)
68206840
});
@@ -6842,6 +6862,7 @@ impl Readable for ClaimableHTLC {
68426862
prev_hop: prev_hop.0.unwrap(),
68436863
timer_ticks: 0,
68446864
value,
6865+
total_value_received,
68456866
total_msat: total_msat.unwrap(),
68466867
onion_payload,
68476868
cltv_expiry,

0 commit comments

Comments
 (0)