Skip to content

Commit 3d6a77a

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 86e94c4 commit 3d6a77a

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
@@ -193,7 +193,10 @@ struct ClaimableHTLC {
193193
value: u64,
194194
onion_payload: OnionPayload,
195195
timer_ticks: u8,
196-
/// The sum total of all MPP parts
196+
/// The total value received for a payment (sum of all MPP parts if the payment is a MPP).
197+
/// Gets set to the amount reported when pushing [`Event::PaymentClaimable`].
198+
total_value_received: Option<u64>,
199+
/// The sender intended sum total of all MPP parts specified in the onion
197200
total_msat: u64,
198201
}
199202

@@ -3246,7 +3249,7 @@ where
32463249
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
32473250
}
32483251
};
3249-
let claimable_htlc = ClaimableHTLC {
3252+
let mut claimable_htlc = ClaimableHTLC {
32503253
prev_hop: HTLCPreviousHopData {
32513254
short_channel_id: prev_short_channel_id,
32523255
outpoint: prev_funding_outpoint,
@@ -3256,6 +3259,7 @@ where
32563259
},
32573260
value: outgoing_amt_msat,
32583261
timer_ticks: 0,
3262+
total_value_received: None,
32593263
total_msat: if let Some(data) = &payment_data { data.total_msat } else { outgoing_amt_msat },
32603264
cltv_expiry,
32613265
onion_payload,
@@ -3300,7 +3304,7 @@ where
33003304
fail_htlc!(claimable_htlc, payment_hash);
33013305
continue
33023306
}
3303-
let (_, htlcs) = claimable_payments.claimable_htlcs.entry(payment_hash)
3307+
let (_, ref mut htlcs) = claimable_payments.claimable_htlcs.entry(payment_hash)
33043308
.or_insert_with(|| (purpose(), Vec::new()));
33053309
if htlcs.len() == 1 {
33063310
if let OnionPayload::Spontaneous(_) = htlcs[0].onion_payload {
@@ -3331,11 +3335,13 @@ where
33313335
} else if total_value == $payment_data.total_msat {
33323336
let prev_channel_id = prev_funding_outpoint.to_channel_id();
33333337
htlcs.push(claimable_htlc);
3338+
let amount_msat = htlcs.iter().map(|htlc| htlc.value).sum();
3339+
htlcs.iter_mut().for_each(|htlc| htlc.total_value_received = Some(amount_msat));
33343340
new_events.push(events::Event::PaymentClaimable {
33353341
receiver_node_id: Some(receiver_node_id),
33363342
payment_hash,
33373343
purpose: purpose(),
3338-
amount_msat: total_value,
3344+
amount_msat,
33393345
via_channel_id: Some(prev_channel_id),
33403346
via_user_channel_id: Some(prev_user_channel_id),
33413347
});
@@ -3389,6 +3395,8 @@ where
33893395
}
33903396
match claimable_payments.claimable_htlcs.entry(payment_hash) {
33913397
hash_map::Entry::Vacant(e) => {
3398+
let amount_msat = claimable_htlc.value;
3399+
claimable_htlc.total_value_received = Some(amount_msat);
33923400
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
33933401
e.insert((purpose.clone(), vec![claimable_htlc]));
33943402
let prev_channel_id = prev_funding_outpoint.to_channel_id();
@@ -3931,6 +3939,7 @@ where
39313939
// provide the preimage, so worrying too much about the optimal handling isn't worth
39323940
// it.
39333941
let mut claimable_amt_msat = 0;
3942+
let mut prev_total_msat = None;
39343943
let mut expected_amt_msat = None;
39353944
let mut valid_mpp = true;
39363945
let mut errs = Vec::new();
@@ -3958,14 +3967,22 @@ where
39583967
break;
39593968
}
39603969

3961-
if expected_amt_msat.is_some() && expected_amt_msat != Some(htlc.total_msat) {
3962-
log_error!(self.logger, "Somehow ended up with an MPP payment with different total amounts - this should not be reachable!");
3970+
if prev_total_msat.is_some() && prev_total_msat != Some(htlc.total_msat) {
3971+
log_error!(self.logger, "Somehow ended up with an MPP payment with different expected total amounts - this should not be reachable!");
39633972
debug_assert!(false);
39643973
valid_mpp = false;
39653974
break;
39663975
}
3976+
prev_total_msat = Some(htlc.total_msat);
3977+
3978+
if expected_amt_msat.is_some() && expected_amt_msat != htlc.total_value_received {
3979+
log_error!(self.logger, "Somehow ended up with an MPP payment with different received total amounts - this should not be reachable!");
3980+
debug_assert!(false);
3981+
valid_mpp = false;
3982+
break;
3983+
}
3984+
expected_amt_msat = htlc.total_value_received;
39673985

3968-
expected_amt_msat = Some(htlc.total_msat);
39693986
if let OnionPayload::Spontaneous(_) = &htlc.onion_payload {
39703987
// We don't currently support MPP for spontaneous payments, so just check
39713988
// that there's one payment here and move on.
@@ -6766,6 +6783,7 @@ impl Writeable for ClaimableHTLC {
67666783
(1, self.total_msat, required),
67676784
(2, self.value, required),
67686785
(4, payment_data, option),
6786+
(5, self.total_value_received, option),
67696787
(6, self.cltv_expiry, required),
67706788
(8, keysend_preimage, option),
67716789
});
@@ -6779,13 +6797,15 @@ impl Readable for ClaimableHTLC {
67796797
let mut value = 0;
67806798
let mut payment_data: Option<msgs::FinalOnionHopData> = None;
67816799
let mut cltv_expiry = 0;
6800+
let mut total_value_received = None;
67826801
let mut total_msat = None;
67836802
let mut keysend_preimage: Option<PaymentPreimage> = None;
67846803
read_tlv_fields!(reader, {
67856804
(0, prev_hop, required),
67866805
(1, total_msat, option),
67876806
(2, value, required),
67886807
(4, payment_data, option),
6808+
(5, total_value_received, option),
67896809
(6, cltv_expiry, required),
67906810
(8, keysend_preimage, option)
67916811
});
@@ -6813,6 +6833,7 @@ impl Readable for ClaimableHTLC {
68136833
prev_hop: prev_hop.0.unwrap(),
68146834
timer_ticks: 0,
68156835
value,
6836+
total_value_received,
68166837
total_msat: total_msat.unwrap(),
68176838
onion_payload,
68186839
cltv_expiry,

0 commit comments

Comments
 (0)