Skip to content

Commit 4bb74e3

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 99ec2e3 commit 4bb74e3

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.
@@ -3172,11 +3173,11 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31723173
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
31733174
routing, incoming_shared_secret, payment_hash, amt_to_forward, .. },
31743175
prev_funding_outpoint } => {
3175-
let (cltv_expiry, onion_payload) = match routing {
3176+
let (cltv_expiry, total_msat, onion_payload) = match routing {
31763177
PendingHTLCRouting::Receive { payment_data, incoming_cltv_expiry } =>
3177-
(incoming_cltv_expiry, OnionPayload::Invoice(payment_data)),
3178+
(incoming_cltv_expiry, payment_data.total_msat, OnionPayload::Invoice(payment_data)),
31783179
PendingHTLCRouting::ReceiveKeysend { payment_preimage, incoming_cltv_expiry } =>
3179-
(incoming_cltv_expiry, OnionPayload::Spontaneous(payment_preimage)),
3180+
(incoming_cltv_expiry, amt_to_forward, OnionPayload::Spontaneous(payment_preimage)),
31803181
_ => {
31813182
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
31823183
}
@@ -3189,6 +3190,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31893190
incoming_packet_shared_secret: incoming_shared_secret,
31903191
},
31913192
value: amt_to_forward,
3193+
total_msat,
31923194
cltv_expiry,
31933195
onion_payload,
31943196
};
@@ -3212,7 +3214,6 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32123214

32133215
macro_rules! check_total_value {
32143216
($payment_data_total_msat: expr, $payment_secret: expr, $payment_preimage: expr) => {{
3215-
let mut total_value = 0;
32163217
let mut payment_received_generated = false;
32173218
let htlcs = channel_state.claimable_htlcs.entry(payment_hash)
32183219
.or_insert(Vec::new());
@@ -3223,28 +3224,28 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32233224
continue
32243225
}
32253226
}
3226-
htlcs.push(claimable_htlc);
3227+
let mut total_value = claimable_htlc.value;
32273228
for htlc in htlcs.iter() {
32283229
total_value += htlc.value;
32293230
match &htlc.onion_payload {
3230-
OnionPayload::Invoice(htlc_payment_data) => {
3231-
if htlc_payment_data.total_msat != $payment_data_total_msat {
3231+
OnionPayload::Invoice(_) => {
3232+
if htlc.total_msat != claimable_htlc.total_msat {
32323233
log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the HTLCs had inconsistent total values (eg {} and {})",
3233-
log_bytes!(payment_hash.0), $payment_data_total_msat, htlc_payment_data.total_msat);
3234+
log_bytes!(payment_hash.0), claimable_htlc.total_msat, htlc.total_msat);
32343235
total_value = msgs::MAX_VALUE_MSAT;
32353236
}
32363237
if total_value >= msgs::MAX_VALUE_MSAT { break; }
32373238
},
32383239
_ => unreachable!(),
32393240
}
32403241
}
3241-
if total_value >= msgs::MAX_VALUE_MSAT || total_value > $payment_data_total_msat {
3242+
if total_value >= msgs::MAX_VALUE_MSAT || total_value > claimable_htlc.total_msat {
32423243
log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the total value {} ran over expected value {} (or HTLCs were inconsistent)",
3243-
log_bytes!(payment_hash.0), total_value, $payment_data_total_msat);
3244+
log_bytes!(payment_hash.0), total_value, claimable_htlc.total_msat);
32443245
for htlc in htlcs.iter() {
32453246
fail_htlc!(htlc);
32463247
}
3247-
} else if total_value == $payment_data_total_msat {
3248+
} else if total_value == claimable_htlc.total_msat {
32483249
new_events.push(events::Event::PaymentReceived {
32493250
payment_hash,
32503251
purpose: events::PaymentPurpose::InvoicePayment {
@@ -3259,6 +3260,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32593260
// payment value yet, wait until we receive more
32603261
// MPP parts.
32613262
}
3263+
htlcs.push(claimable_htlc);
32623264
payment_received_generated
32633265
}}
32643266
}
@@ -5927,13 +5929,14 @@ impl Writeable for ClaimableHTLC {
59275929
OnionPayload::Invoice(_) => None,
59285930
OnionPayload::Spontaneous(preimage) => Some(preimage.clone()),
59295931
};
5930-
write_tlv_fields!
5931-
(writer,
5932-
{
5933-
(0, self.prev_hop, required), (2, self.value, required),
5934-
(4, payment_data, option), (6, self.cltv_expiry, required),
5935-
(8, keysend_preimage, option),
5936-
});
5932+
write_tlv_fields!(writer, {
5933+
(0, self.prev_hop, required),
5934+
(1, self.total_msat, required),
5935+
(2, self.value, required),
5936+
(4, payment_data, option),
5937+
(6, self.cltv_expiry, required),
5938+
(8, keysend_preimage, option),
5939+
});
59375940
Ok(())
59385941
}
59395942
}
@@ -5944,31 +5947,40 @@ impl Readable for ClaimableHTLC {
59445947
let mut value = 0;
59455948
let mut payment_data: Option<msgs::FinalOnionHopData> = None;
59465949
let mut cltv_expiry = 0;
5950+
let mut total_msat = None;
59475951
let mut keysend_preimage: Option<PaymentPreimage> = None;
5948-
read_tlv_fields!
5949-
(reader,
5950-
{
5951-
(0, prev_hop, required), (2, value, required),
5952-
(4, payment_data, option), (6, cltv_expiry, required),
5953-
(8, keysend_preimage, option)
5954-
});
5952+
read_tlv_fields!(reader, {
5953+
(0, prev_hop, required),
5954+
(1, total_msat, option),
5955+
(2, value, required),
5956+
(4, payment_data, option),
5957+
(6, cltv_expiry, required),
5958+
(8, keysend_preimage, option)
5959+
});
59555960
let onion_payload = match keysend_preimage {
59565961
Some(p) => {
59575962
if payment_data.is_some() {
59585963
return Err(DecodeError::InvalidValue)
59595964
}
5965+
if total_msat.is_none() {
5966+
total_msat = Some(value);
5967+
}
59605968
OnionPayload::Spontaneous(p)
59615969
},
59625970
None => {
59635971
if payment_data.is_none() {
59645972
return Err(DecodeError::InvalidValue)
59655973
}
5974+
if total_msat.is_none() {
5975+
total_msat = Some(payment_data.as_ref().unwrap().total_msat);
5976+
}
59665977
OnionPayload::Invoice(payment_data.unwrap())
59675978
},
59685979
};
59695980
Ok(Self {
59705981
prev_hop: prev_hop.0.unwrap(),
59715982
value,
5983+
total_msat: total_msat.unwrap(),
59725984
onion_payload,
59735985
cltv_expiry,
59745986
})

0 commit comments

Comments
 (0)