Skip to content

Commit 782069a

Browse files
committed
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 98daab4 commit 782069a

File tree

1 file changed

+37
-25
lines changed

1 file changed

+37
-25
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 37 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ struct ClaimableHTLC {
459459
cltv_expiry: u32,
460460
value: u64,
461461
onion_payload: OnionPayload,
462+
total_msat: u64,
462463
}
463464

464465
/// A payment identifier used to uniquely identify a payment to LDK.
@@ -3168,11 +3169,11 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31683169
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
31693170
routing, incoming_shared_secret, payment_hash, amt_to_forward, .. },
31703171
prev_funding_outpoint } => {
3171-
let (cltv_expiry, onion_payload) = match routing {
3172+
let (cltv_expiry, total_msat, onion_payload) = match routing {
31723173
PendingHTLCRouting::Receive { payment_data, incoming_cltv_expiry } =>
3173-
(incoming_cltv_expiry, OnionPayload::Invoice(payment_data)),
3174+
(incoming_cltv_expiry, payment_data.total_msat, OnionPayload::Invoice(payment_data)),
31743175
PendingHTLCRouting::ReceiveKeysend { payment_preimage, incoming_cltv_expiry } =>
3175-
(incoming_cltv_expiry, OnionPayload::Spontaneous(payment_preimage)),
3176+
(incoming_cltv_expiry, amt_to_forward, OnionPayload::Spontaneous(payment_preimage)),
31763177
_ => {
31773178
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
31783179
}
@@ -3185,6 +3186,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31853186
incoming_packet_shared_secret: incoming_shared_secret,
31863187
},
31873188
value: amt_to_forward,
3189+
total_msat,
31883190
cltv_expiry,
31893191
onion_payload,
31903192
};
@@ -3208,7 +3210,6 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32083210

32093211
macro_rules! check_total_value {
32103212
($payment_data_total_msat: expr, $payment_secret: expr, $payment_preimage: expr) => {{
3211-
let mut total_value = 0;
32123213
let mut payment_received_generated = false;
32133214
let htlcs = channel_state.claimable_htlcs.entry(payment_hash)
32143215
.or_insert(Vec::new());
@@ -3219,28 +3220,28 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32193220
continue
32203221
}
32213222
}
3222-
htlcs.push(claimable_htlc);
3223+
let mut total_value = claimable_htlc.value;
32233224
for htlc in htlcs.iter() {
32243225
total_value += htlc.value;
32253226
match &htlc.onion_payload {
3226-
OnionPayload::Invoice(htlc_payment_data) => {
3227-
if htlc_payment_data.total_msat != $payment_data_total_msat {
3227+
OnionPayload::Invoice(_) => {
3228+
if htlc.total_msat != claimable_htlc.total_msat {
32283229
log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the HTLCs had inconsistent total values (eg {} and {})",
3229-
log_bytes!(payment_hash.0), $payment_data_total_msat, htlc_payment_data.total_msat);
3230+
log_bytes!(payment_hash.0), claimable_htlc.total_msat, htlc.total_msat);
32303231
total_value = msgs::MAX_VALUE_MSAT;
32313232
}
32323233
if total_value >= msgs::MAX_VALUE_MSAT { break; }
32333234
},
32343235
_ => unreachable!(),
32353236
}
32363237
}
3237-
if total_value >= msgs::MAX_VALUE_MSAT || total_value > $payment_data_total_msat {
3238+
if total_value >= msgs::MAX_VALUE_MSAT || total_value > claimable_htlc.total_msat {
32383239
log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the total value {} ran over expected value {} (or HTLCs were inconsistent)",
3239-
log_bytes!(payment_hash.0), total_value, $payment_data_total_msat);
3240+
log_bytes!(payment_hash.0), total_value, claimable_htlc.total_msat);
32403241
for htlc in htlcs.iter() {
32413242
fail_htlc!(htlc);
32423243
}
3243-
} else if total_value == $payment_data_total_msat {
3244+
} else if total_value == claimable_htlc.total_msat {
32443245
new_events.push(events::Event::PaymentReceived {
32453246
payment_hash,
32463247
purpose: events::PaymentPurpose::InvoicePayment {
@@ -3255,6 +3256,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32553256
// payment value yet, wait until we receive more
32563257
// MPP parts.
32573258
}
3259+
htlcs.push(claimable_htlc);
32583260
payment_received_generated
32593261
}}
32603262
}
@@ -5923,13 +5925,14 @@ impl Writeable for ClaimableHTLC {
59235925
OnionPayload::Invoice(_) => None,
59245926
OnionPayload::Spontaneous(preimage) => Some(preimage.clone()),
59255927
};
5926-
write_tlv_fields!
5927-
(writer,
5928-
{
5929-
(0, self.prev_hop, required), (2, self.value, required),
5930-
(4, payment_data, option), (6, self.cltv_expiry, required),
5931-
(8, keysend_preimage, option),
5932-
});
5928+
write_tlv_fields!(writer, {
5929+
(0, self.prev_hop, required),
5930+
(1, self.total_msat, required),
5931+
(2, self.value, required),
5932+
(4, payment_data, option),
5933+
(6, self.cltv_expiry, required),
5934+
(8, keysend_preimage, option),
5935+
});
59335936
Ok(())
59345937
}
59355938
}
@@ -5940,31 +5943,40 @@ impl Readable for ClaimableHTLC {
59405943
let mut value = 0;
59415944
let mut payment_data: Option<msgs::FinalOnionHopData> = None;
59425945
let mut cltv_expiry = 0;
5946+
let mut total_msat = None;
59435947
let mut keysend_preimage: Option<PaymentPreimage> = None;
5944-
read_tlv_fields!
5945-
(reader,
5946-
{
5947-
(0, prev_hop, required), (2, value, required),
5948-
(4, payment_data, option), (6, cltv_expiry, required),
5949-
(8, keysend_preimage, option)
5950-
});
5948+
read_tlv_fields!(reader, {
5949+
(0, prev_hop, required),
5950+
(1, total_msat, option),
5951+
(2, value, required),
5952+
(4, payment_data, option),
5953+
(6, cltv_expiry, required),
5954+
(8, keysend_preimage, option)
5955+
});
59515956
let onion_payload = match keysend_preimage {
59525957
Some(p) => {
59535958
if payment_data.is_some() {
59545959
return Err(DecodeError::InvalidValue)
59555960
}
5961+
if total_msat.is_none() {
5962+
total_msat = Some(value);
5963+
}
59565964
OnionPayload::Spontaneous(p)
59575965
},
59585966
None => {
59595967
if payment_data.is_none() {
59605968
return Err(DecodeError::InvalidValue)
59615969
}
5970+
if total_msat.is_none() {
5971+
total_msat = Some(payment_data.as_ref().unwrap().total_msat);
5972+
}
59625973
OnionPayload::Invoice(payment_data.unwrap())
59635974
},
59645975
};
59655976
Ok(Self {
59665977
prev_hop: prev_hop.0.unwrap(),
59675978
value,
5979+
total_msat: total_msat.unwrap(),
59685980
onion_payload,
59695981
cltv_expiry,
59705982
})

0 commit comments

Comments
 (0)