Skip to content

Commit ea8c945

Browse files
committed
f - make PaymentClaimable report actual received amount not sender intended amount
1 parent c0c4835 commit ea8c945

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@ struct ClaimableHTLC {
193193
cltv_expiry: u32,
194194
/// The amount (in msats) of this MPP part
195195
value: u64,
196+
/// The amount (in msats) that the sender intended to be sent (used for
197+
/// validating total MPP amount)
198+
intended_value: u64,
196199
onion_payload: OnionPayload,
197200
timer_ticks: u8,
198201
/// The sum total of all MPP parts
@@ -3182,7 +3185,7 @@ where
31823185
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
31833186
prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id,
31843187
forward_info: PendingHTLCInfo {
3185-
routing, incoming_shared_secret, payment_hash, outgoing_amt_msat, ..
3188+
routing, incoming_shared_secret, payment_hash, incoming_amt_msat, outgoing_amt_msat, ..
31863189
}
31873190
}) => {
31883191
let (cltv_expiry, onion_payload, payment_data, phantom_shared_secret) = match routing {
@@ -3204,7 +3207,8 @@ where
32043207
incoming_packet_shared_secret: incoming_shared_secret,
32053208
phantom_shared_secret,
32063209
},
3207-
value: outgoing_amt_msat,
3210+
value: incoming_amt_msat.unwrap_or(outgoing_amt_msat),
3211+
intended_value: outgoing_amt_msat,
32083212
timer_ticks: 0,
32093213
total_msat: if let Some(data) = &payment_data { data.total_msat } else { outgoing_amt_msat },
32103214
cltv_expiry,
@@ -3259,9 +3263,9 @@ where
32593263
continue
32603264
}
32613265
}
3262-
let mut total_value = claimable_htlc.value;
3266+
let mut total_value = claimable_htlc.intended_value;
32633267
for htlc in htlcs.iter() {
3264-
total_value += htlc.value;
3268+
total_value += htlc.intended_value;
32653269
match &htlc.onion_payload {
32663270
OnionPayload::Invoice { .. } => {
32673271
if htlc.total_msat != $payment_data.total_msat {
@@ -3278,7 +3282,7 @@ where
32783282
log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the total value {} ran over the maximum possible msats {} (or HTLCs were inconsistent)",
32793283
log_bytes!(payment_hash.0), total_value, msgs::MAX_VALUE_MSAT);
32803284
fail_htlc!(claimable_htlc, payment_hash);
3281-
} else if total_value - claimable_htlc.value >= $payment_data.total_msat {
3285+
} else if total_value - claimable_htlc.intended_value >= $payment_data.total_msat {
32823286
log_trace!(self.logger, "Failing HTLC with payment_hash {} as payment is already claimable",
32833287
log_bytes!(payment_hash.0));
32843288
fail_htlc!(claimable_htlc, payment_hash);
@@ -3289,7 +3293,7 @@ where
32893293
receiver_node_id: Some(receiver_node_id),
32903294
payment_hash,
32913295
purpose: purpose(),
3292-
amount_msat: total_value,
3296+
amount_msat: htlcs.iter().map(|htlc| htlc.value).sum(),
32933297
via_channel_id: Some(prev_channel_id),
32943298
via_user_channel_id: Some(prev_user_channel_id),
32953299
});
@@ -3343,13 +3347,14 @@ where
33433347
}
33443348
match claimable_payments.claimable_htlcs.entry(payment_hash) {
33453349
hash_map::Entry::Vacant(e) => {
3350+
let amount_msat = claimable_htlc.value;
33463351
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
33473352
e.insert((purpose.clone(), vec![claimable_htlc]));
33483353
let prev_channel_id = prev_funding_outpoint.to_channel_id();
33493354
new_events.push(events::Event::PaymentClaimable {
33503355
receiver_node_id: Some(receiver_node_id),
33513356
payment_hash,
3352-
amount_msat: outgoing_amt_msat,
3357+
amount_msat,
33533358
purpose,
33543359
via_channel_id: Some(prev_channel_id),
33553360
via_user_channel_id: Some(prev_user_channel_id),
@@ -6716,6 +6721,7 @@ impl Writeable for ClaimableHTLC {
67166721
(0, self.prev_hop, required),
67176722
(1, self.total_msat, required),
67186723
(2, self.value, required),
6724+
(3, self.intended_value, required),
67196725
(4, payment_data, option),
67206726
(6, self.cltv_expiry, required),
67216727
(8, keysend_preimage, option),
@@ -6728,6 +6734,7 @@ impl Readable for ClaimableHTLC {
67286734
fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
67296735
let mut prev_hop = crate::util::ser::RequiredWrapper(None);
67306736
let mut value = 0;
6737+
let mut intended_value = None;
67316738
let mut payment_data: Option<msgs::FinalOnionHopData> = None;
67326739
let mut cltv_expiry = 0;
67336740
let mut total_msat = None;
@@ -6736,6 +6743,7 @@ impl Readable for ClaimableHTLC {
67366743
(0, prev_hop, required),
67376744
(1, total_msat, option),
67386745
(2, value, required),
6746+
(3, intended_value, option),
67396747
(4, payment_data, option),
67406748
(6, cltv_expiry, required),
67416749
(8, keysend_preimage, option)
@@ -6764,6 +6772,7 @@ impl Readable for ClaimableHTLC {
67646772
prev_hop: prev_hop.0.unwrap(),
67656773
timer_ticks: 0,
67666774
value,
6775+
intended_value: intended_value.unwrap_or(value),
67676776
total_msat: total_msat.unwrap(),
67686777
onion_payload,
67696778
cltv_expiry,

lightning/src/ln/functional_tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8016,7 +8016,7 @@ fn test_onion_value_mpp_set_calculation() {
80168016

80178017
// Second path
80188018
let ev = remove_first_msg_event_to_node(&expected_paths[1][0].node.get_our_node_id(), &mut events);
8019-
pass_along_path(&nodes[0], expected_paths[1], total_msat, our_payment_hash.clone(), Some(our_payment_secret), ev, true, None);
8019+
pass_along_path(&nodes[0], expected_paths[1], 101_000, our_payment_hash.clone(), Some(our_payment_secret), ev, true, None);
80208020

80218021
claim_payment_along_route(&nodes[0], expected_paths.as_slice(), false, our_payment_preimage);
80228022
}

0 commit comments

Comments
 (0)