Skip to content

Commit 04381b6

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 fda5a04 commit 04381b6

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ struct ClaimableHTLC {
173173
value: u64,
174174
onion_payload: OnionPayload,
175175
timer_ticks: u8,
176+
total_msat: u64,
176177
}
177178

178179
/// A payment identifier used to uniquely identify a payment to LDK.
@@ -3091,11 +3092,11 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
30913092
HTLCForwardInfo::AddHTLC { prev_short_channel_id, prev_htlc_id, forward_info: PendingHTLCInfo {
30923093
routing, incoming_shared_secret, payment_hash, amt_to_forward, .. },
30933094
prev_funding_outpoint } => {
3094-
let (cltv_expiry, onion_payload, phantom_shared_secret) = match routing {
3095+
let (cltv_expiry, total_msat, onion_payload, phantom_shared_secret) = match routing {
30953096
PendingHTLCRouting::Receive { payment_data, incoming_cltv_expiry, phantom_shared_secret } =>
3096-
(incoming_cltv_expiry, OnionPayload::Invoice(payment_data), phantom_shared_secret),
3097+
(incoming_cltv_expiry, payment_data.total_msat, OnionPayload::Invoice(payment_data), phantom_shared_secret),
30973098
PendingHTLCRouting::ReceiveKeysend { payment_preimage, incoming_cltv_expiry } =>
3098-
(incoming_cltv_expiry, OnionPayload::Spontaneous(payment_preimage), None),
3099+
(incoming_cltv_expiry, amt_to_forward, OnionPayload::Spontaneous(payment_preimage), None),
30993100
_ => {
31003101
panic!("short_channel_id == 0 should imply any pending_forward entries are of type Receive");
31013102
}
@@ -3110,6 +3111,7 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31103111
},
31113112
value: amt_to_forward,
31123113
timer_ticks: 0,
3114+
total_msat,
31133115
cltv_expiry,
31143116
onion_payload,
31153117
};
@@ -3148,23 +3150,21 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31483150
for htlc in htlcs.iter() {
31493151
total_value += htlc.value;
31503152
match &htlc.onion_payload {
3151-
OnionPayload::Invoice(htlc_payment_data) => {
3152-
if htlc_payment_data.total_msat != $payment_data_total_msat {
3153+
OnionPayload::Invoice(_) => {
3154+
if htlc.total_msat != claimable_htlc.total_msat {
31533155
log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the HTLCs had inconsistent total values (eg {} and {})",
3154-
log_bytes!(payment_hash.0), $payment_data_total_msat, htlc_payment_data.total_msat);
3156+
log_bytes!(payment_hash.0), claimable_htlc.total_msat, htlc.total_msat);
31553157
total_value = msgs::MAX_VALUE_MSAT;
31563158
}
31573159
if total_value >= msgs::MAX_VALUE_MSAT { break; }
31583160
},
31593161
_ => unreachable!(),
31603162
}
31613163
}
3162-
if total_value >= msgs::MAX_VALUE_MSAT || total_value > $payment_data_total_msat {
3164+
if total_value >= msgs::MAX_VALUE_MSAT || total_value > claimable_htlc.total_msat {
31633165
log_trace!(self.logger, "Failing HTLCs with payment_hash {} as the total value {} ran over expected value {} (or HTLCs were inconsistent)",
3164-
log_bytes!(payment_hash.0), total_value, $payment_data_total_msat);
3165-
fail_htlc!(claimable_htlc);
3166-
} else if total_value == $payment_data_total_msat {
3167-
htlcs.push(claimable_htlc);
3166+
log_bytes!(payment_hash.0), total_value, claimable_htlc.total_msat);
3167+
} else if total_value == claimable_htlc.total_msat {
31683168
new_events.push(events::Event::PaymentReceived {
31693169
payment_hash,
31703170
purpose: events::PaymentPurpose::InvoicePayment {
@@ -3178,8 +3178,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
31783178
// Nothing to do - we haven't reached the total
31793179
// payment value yet, wait until we receive more
31803180
// MPP parts.
3181-
htlcs.push(claimable_htlc);
31823181
}
3182+
htlcs.push(claimable_htlc);
31833183
payment_received_generated
31843184
}}
31853185
}
@@ -3202,9 +3202,8 @@ impl<Signer: Sign, M: Deref, T: Deref, K: Deref, F: Deref, L: Deref> ChannelMana
32023202
continue
32033203
}
32043204
};
3205-
let payment_data_total_msat = payment_data.total_msat;
32063205
let payment_secret = payment_data.payment_secret.clone();
3207-
check_total_value!(payment_data_total_msat, payment_secret, payment_preimage);
3206+
check_total_value!(payment_data.total_msat, payment_secret, payment_preimage);
32083207
},
32093208
OnionPayload::Spontaneous(preimage) => {
32103209
match channel_state.claimable_htlcs.entry(payment_hash) {
@@ -6069,13 +6068,14 @@ impl Writeable for ClaimableHTLC {
60696068
OnionPayload::Invoice(_) => None,
60706069
OnionPayload::Spontaneous(preimage) => Some(preimage.clone()),
60716070
};
6072-
write_tlv_fields!
6073-
(writer,
6074-
{
6075-
(0, self.prev_hop, required), (2, self.value, required),
6076-
(4, payment_data, option), (6, self.cltv_expiry, required),
6077-
(8, keysend_preimage, option),
6078-
});
6071+
write_tlv_fields!(writer, {
6072+
(0, self.prev_hop, required),
6073+
(1, self.total_msat, required),
6074+
(2, self.value, required),
6075+
(4, payment_data, option),
6076+
(6, self.cltv_expiry, required),
6077+
(8, keysend_preimage, option),
6078+
});
60796079
Ok(())
60806080
}
60816081
}
@@ -6086,32 +6086,41 @@ impl Readable for ClaimableHTLC {
60866086
let mut value = 0;
60876087
let mut payment_data: Option<msgs::FinalOnionHopData> = None;
60886088
let mut cltv_expiry = 0;
6089+
let mut total_msat = None;
60896090
let mut keysend_preimage: Option<PaymentPreimage> = None;
6090-
read_tlv_fields!
6091-
(reader,
6092-
{
6093-
(0, prev_hop, required), (2, value, required),
6094-
(4, payment_data, option), (6, cltv_expiry, required),
6095-
(8, keysend_preimage, option)
6096-
});
6091+
read_tlv_fields!(reader, {
6092+
(0, prev_hop, required),
6093+
(1, total_msat, option),
6094+
(2, value, required),
6095+
(4, payment_data, option),
6096+
(6, cltv_expiry, required),
6097+
(8, keysend_preimage, option)
6098+
});
60976099
let onion_payload = match keysend_preimage {
60986100
Some(p) => {
60996101
if payment_data.is_some() {
61006102
return Err(DecodeError::InvalidValue)
61016103
}
6104+
if total_msat.is_none() {
6105+
total_msat = Some(value);
6106+
}
61026107
OnionPayload::Spontaneous(p)
61036108
},
61046109
None => {
61056110
if payment_data.is_none() {
61066111
return Err(DecodeError::InvalidValue)
61076112
}
6113+
if total_msat.is_none() {
6114+
total_msat = Some(payment_data.as_ref().unwrap().total_msat);
6115+
}
61086116
OnionPayload::Invoice(payment_data.unwrap())
61096117
},
61106118
};
61116119
Ok(Self {
61126120
prev_hop: prev_hop.0.unwrap(),
61136121
timer_ticks: 0,
61146122
value,
6123+
total_msat: total_msat.unwrap(),
61156124
onion_payload,
61166125
cltv_expiry,
61176126
})

0 commit comments

Comments
 (0)