Skip to content

Commit 84685b9

Browse files
committed
f - make PaymentClaimable report actual received amount not sender intended amount
1 parent 28a0d2c commit 84685b9

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
@@ -191,6 +191,9 @@ struct ClaimableHTLC {
191191
cltv_expiry: u32,
192192
/// The amount (in msats) of this MPP part
193193
value: u64,
194+
/// The amount (in msats) that the sender intended to be sent (used for
195+
/// validating total MPP amount)
196+
intended_value: u64,
194197
onion_payload: OnionPayload,
195198
timer_ticks: u8,
196199
/// The sum total of all MPP parts
@@ -3232,7 +3235,7 @@ where
32323235
HTLCForwardInfo::AddHTLC(PendingAddHTLCInfo {
32333236
prev_short_channel_id, prev_htlc_id, prev_funding_outpoint, prev_user_channel_id,
32343237
forward_info: PendingHTLCInfo {
3235-
routing, incoming_shared_secret, payment_hash, outgoing_amt_msat, ..
3238+
routing, incoming_shared_secret, payment_hash, incoming_amt_msat, outgoing_amt_msat, ..
32363239
}
32373240
}) => {
32383241
let (cltv_expiry, onion_payload, payment_data, phantom_shared_secret) = match routing {
@@ -3254,7 +3257,8 @@ where
32543257
incoming_packet_shared_secret: incoming_shared_secret,
32553258
phantom_shared_secret,
32563259
},
3257-
value: outgoing_amt_msat,
3260+
value: incoming_amt_msat.unwrap_or(outgoing_amt_msat),
3261+
intended_value: outgoing_amt_msat,
32583262
timer_ticks: 0,
32593263
total_msat: if let Some(data) = &payment_data { data.total_msat } else { outgoing_amt_msat },
32603264
cltv_expiry,
@@ -3309,9 +3313,9 @@ where
33093313
continue
33103314
}
33113315
}
3312-
let mut total_value = claimable_htlc.value;
3316+
let mut total_value = claimable_htlc.intended_value;
33133317
for htlc in htlcs.iter() {
3314-
total_value += htlc.value;
3318+
total_value += htlc.intended_value;
33153319
match &htlc.onion_payload {
33163320
OnionPayload::Invoice { .. } => {
33173321
if htlc.total_msat != $payment_data.total_msat {
@@ -3328,7 +3332,7 @@ where
33283332
log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the total value {} ran over the maximum possible msats {} (or HTLCs were inconsistent)",
33293333
log_bytes!(payment_hash.0), total_value, msgs::MAX_VALUE_MSAT);
33303334
fail_htlc!(claimable_htlc, payment_hash);
3331-
} else if total_value - claimable_htlc.value >= $payment_data.total_msat {
3335+
} else if total_value - claimable_htlc.intended_value >= $payment_data.total_msat {
33323336
log_trace!(self.logger, "Failing HTLC with payment_hash {} as payment is already claimable",
33333337
log_bytes!(payment_hash.0));
33343338
fail_htlc!(claimable_htlc, payment_hash);
@@ -3339,7 +3343,7 @@ where
33393343
receiver_node_id: Some(receiver_node_id),
33403344
payment_hash,
33413345
purpose: purpose(),
3342-
amount_msat: total_value,
3346+
amount_msat: htlcs.iter().map(|htlc| htlc.value).sum(),
33433347
via_channel_id: Some(prev_channel_id),
33443348
via_user_channel_id: Some(prev_user_channel_id),
33453349
});
@@ -3393,13 +3397,14 @@ where
33933397
}
33943398
match claimable_payments.claimable_htlcs.entry(payment_hash) {
33953399
hash_map::Entry::Vacant(e) => {
3400+
let amount_msat = claimable_htlc.value;
33963401
let purpose = events::PaymentPurpose::SpontaneousPayment(preimage);
33973402
e.insert((purpose.clone(), vec![claimable_htlc]));
33983403
let prev_channel_id = prev_funding_outpoint.to_channel_id();
33993404
new_events.push(events::Event::PaymentClaimable {
34003405
receiver_node_id: Some(receiver_node_id),
34013406
payment_hash,
3402-
amount_msat: outgoing_amt_msat,
3407+
amount_msat,
34033408
purpose,
34043409
via_channel_id: Some(prev_channel_id),
34053410
via_user_channel_id: Some(prev_user_channel_id),
@@ -6769,6 +6774,7 @@ impl Writeable for ClaimableHTLC {
67696774
(0, self.prev_hop, required),
67706775
(1, self.total_msat, required),
67716776
(2, self.value, required),
6777+
(3, self.intended_value, required),
67726778
(4, payment_data, option),
67736779
(6, self.cltv_expiry, required),
67746780
(8, keysend_preimage, option),
@@ -6781,6 +6787,7 @@ impl Readable for ClaimableHTLC {
67816787
fn read<R: Read>(reader: &mut R) -> Result<Self, DecodeError> {
67826788
let mut prev_hop = crate::util::ser::RequiredWrapper(None);
67836789
let mut value = 0;
6790+
let mut intended_value = None;
67846791
let mut payment_data: Option<msgs::FinalOnionHopData> = None;
67856792
let mut cltv_expiry = 0;
67866793
let mut total_msat = None;
@@ -6789,6 +6796,7 @@ impl Readable for ClaimableHTLC {
67896796
(0, prev_hop, required),
67906797
(1, total_msat, option),
67916798
(2, value, required),
6799+
(3, intended_value, option),
67926800
(4, payment_data, option),
67936801
(6, cltv_expiry, required),
67946802
(8, keysend_preimage, option)
@@ -6817,6 +6825,7 @@ impl Readable for ClaimableHTLC {
68176825
prev_hop: prev_hop.0.unwrap(),
68186826
timer_ticks: 0,
68196827
value,
6828+
intended_value: intended_value.unwrap_or(value),
68206829
total_msat: total_msat.unwrap(),
68216830
onion_payload,
68226831
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)