Skip to content

Commit 62f8df5

Browse files
TheBlueMattandozw
authored andcommitted
Store total payment amount in ClaimableHTLC explicitly
...instead of accessing it via the `OnionPayload::Invoice` form. This may be useful if we add MPP keysend support, but is directly useful to allow us to drop `FinalOnionHopData` from `OnionPayload`.
1 parent 26c0150 commit 62f8df5

File tree

1 file changed

+36
-22
lines changed

1 file changed

+36
-22
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 36 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,16 @@ enum OnionPayload {
167167
Spontaneous(PaymentPreimage),
168168
}
169169

170+
/// HTLCs that are to us and can be failed/claimed by the user
170171
struct ClaimableHTLC {
171172
prev_hop: HTLCPreviousHopData,
172173
cltv_expiry: u32,
174+
/// The amount (in msats) of this MPP part
173175
value: u64,
174176
onion_payload: OnionPayload,
175177
timer_ticks: u8,
178+
/// The sum total of all MPP parts
179+
total_msat: u64,
176180
}
177181

178182
/// A payment identifier used to uniquely identify a payment to LDK.
@@ -3096,11 +3100,11 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30963100
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
30973101
routing, incoming_shared_secret, payment_hash, amt_to_forward, .. },
30983102
prev_funding_outpoint } => {
3099-
let (cltv_expiry, onion_payload, phantom_shared_secret) = match routing {
3103+
let (cltv_expiry, total_msat, onion_payload, phantom_shared_secret) = match routing {
31003104
PendingHTLCRouting::Receive { payment_data, incoming_cltv_expiry, phantom_shared_secret } =>
3101-
(incoming_cltv_expiry, OnionPayload::Invoice(payment_data), phantom_shared_secret),
3105+
(incoming_cltv_expiry, payment_data.total_msat, OnionPayload::Invoice(payment_data), phantom_shared_secret),
31023106
PendingHTLCRouting::ReceiveKeysend { payment_preimage, incoming_cltv_expiry } =>
3103-
(incoming_cltv_expiry, OnionPayload::Spontaneous(payment_preimage), None),
3107+
(incoming_cltv_expiry, amt_to_forward, OnionPayload::Spontaneous(payment_preimage), None),
31043108
_ => {
31053109
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
31063110
}
@@ -3115,6 +3119,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31153119
},
31163120
value: amt_to_forward,
31173121
timer_ticks: 0,
3122+
total_msat,
31183123
cltv_expiry,
31193124
onion_payload,
31203125
};
@@ -3153,10 +3158,10 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31533158
for htlc in htlcs.iter() {
31543159
total_value += htlc.value;
31553160
match &htlc.onion_payload {
3156-
OnionPayload::Invoice(htlc_payment_data) => {
3157-
if htlc_payment_data.total_msat != $payment_data_total_msat {
3161+
OnionPayload::Invoice { .. } => {
3162+
if htlc.total_msat != $payment_data_total_msat {
31583163
log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the HTLCs had inconsistent total values (eg {} and {})",
3159-
log_bytes!(payment_hash.0), $payment_data_total_msat, htlc_payment_data.total_msat);
3164+
log_bytes!(payment_hash.0), $payment_data_total_msat, htlc.total_msat);
31603165
total_value = msgs::MAX_VALUE_MSAT;
31613166
}
31623167
if total_value >= msgs::MAX_VALUE_MSAT { break; }
@@ -3207,9 +3212,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32073212
continue
32083213
}
32093214
};
3210-
let payment_data_total_msat = payment_data.total_msat;
32113215
let payment_secret = payment_data.payment_secret.clone();
3212-
check_total_value!(payment_data_total_msat, payment_secret, payment_preimage);
3216+
check_total_value!(payment_data.total_msat, payment_secret, payment_preimage);
32133217
},
32143218
OnionPayload::Spontaneous(preimage) => {
32153219
match channel_state.claimable_htlcs.entry(payment_hash) {
@@ -6076,13 +6080,14 @@ impl Writeable for ClaimableHTLC {
60766080
OnionPayload::Invoice(_) => None,
60776081
OnionPayload::Spontaneous(preimage) => Some(preimage.clone()),
60786082
};
6079-
write_tlv_fields!
6080-
(writer,
6081-
{
6082-
(0, self.prev_hop, required), (2, self.value, required),
6083-
(4, payment_data, option), (6, self.cltv_expiry, required),
6084-
(8, keysend_preimage, option),
6085-
});
6083+
write_tlv_fields!(writer, {
6084+
(0, self.prev_hop, required),
6085+
(1, self.total_msat, required),
6086+
(2, self.value, required),
6087+
(4, payment_data, option),
6088+
(6, self.cltv_expiry, required),
6089+
(8, keysend_preimage, option),
6090+
});
60866091
Ok(())
60876092
}
60886093
}
@@ -6093,32 +6098,41 @@ impl Readable for ClaimableHTLC {
60936098
let mut value = 0;
60946099
let mut payment_data: Option<msgs::FinalOnionHopData> = None;
60956100
let mut cltv_expiry = 0;
6101+
let mut total_msat = None;
60966102
let mut keysend_preimage: Option<PaymentPreimage> = None;
6097-
read_tlv_fields!
6098-
(reader,
6099-
{
6100-
(0, prev_hop, required), (2, value, required),
6101-
(4, payment_data, option), (6, cltv_expiry, required),
6102-
(8, keysend_preimage, option)
6103-
});
6103+
read_tlv_fields!(reader, {
6104+
(0, prev_hop, required),
6105+
(1, total_msat, option),
6106+
(2, value, required),
6107+
(4, payment_data, option),
6108+
(6, cltv_expiry, required),
6109+
(8, keysend_preimage, option)
6110+
});
61046111
let onion_payload = match keysend_preimage {
61056112
Some(p) => {
61066113
if payment_data.is_some() {
61076114
return Err(DecodeError::InvalidValue)
61086115
}
6116+
if total_msat.is_none() {
6117+
total_msat = Some(value);
6118+
}
61096119
OnionPayload::Spontaneous(p)
61106120
},
61116121
None => {
61126122
if payment_data.is_none() {
61136123
return Err(DecodeError::InvalidValue)
61146124
}
6125+
if total_msat.is_none() {
6126+
total_msat = Some(payment_data.as_ref().unwrap().total_msat);
6127+
}
61156128
OnionPayload::Invoice(payment_data.unwrap())
61166129
},
61176130
};
61186131
Ok(Self {
61196132
prev_hop: prev_hop.0.unwrap(),
61206133
timer_ticks: 0,
61216134
value,
6135+
total_msat: total_msat.unwrap(),
61226136
onion_payload,
61236137
cltv_expiry,
61246138
})

0 commit comments

Comments
 (0)