Skip to content

Commit d3cc8a3

Browse files
committed
feedback from val
1 parent f168203 commit d3cc8a3

File tree

1 file changed

+26
-23
lines changed

1 file changed

+26
-23
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ pub enum PendingHTLCRouting {
136136
payment_data: Option<msgs::FinalOnionHopData>,
137137
/// Preimage for this onion payment.
138138
payment_preimage: PaymentPreimage,
139-
/// Metadata associated with this payment.
139+
/// See [`RecipientOnionFields::payment_metadata`] for more info.
140140
payment_metadata: Option<Vec<u8>>,
141141
/// CLTV expiry of the incoming HTLC.
142142
incoming_cltv_expiry: u32, // Used to track when we should expire pending HTLCs that go unclaimed
@@ -3030,10 +3030,16 @@ where
30303030

30313031
let cur_height = self.best_block.read().unwrap().height() + 1;
30323032

3033-
if let Err((err_msg, code, include_chan_update_opt)) = check_incoming_htlc_cltv(
3033+
if let Err((err_msg, code)) = check_incoming_htlc_cltv(
30343034
cur_height, outgoing_cltv_value, msg.cltv_expiry
30353035
) {
3036-
let chan_update_opt = if include_chan_update_opt { chan_update_opt } else { None };
3036+
if code & 0x1000 != 0 && chan_update_opt.is_none() {
3037+
// We really should set `incorrect_cltv_expiry` here but as we're not
3038+
// forwarding over a real channel we can't generate a channel_update
3039+
// for it. Instead we just return a generic temporary_node_failure.
3040+
break Some((err_msg, 0x2000 | 2, None))
3041+
}
3042+
let chan_update_opt = if code & 0x1000 != 0 { chan_update_opt } else { None };
30373043
break Some((err_msg, code, chan_update_opt));
30383044
}
30393045

@@ -7892,7 +7898,7 @@ where
78927898
}),
78937899
};
78947900

7895-
if let Err((err_msg, code, _)) = check_incoming_htlc_cltv(
7901+
if let Err((err_msg, code)) = check_incoming_htlc_cltv(
78967902
cur_height, outgoing_cltv_value, msg.cltv_expiry
78977903
) {
78987904
return Err(InboundOnionErr {
@@ -7924,9 +7930,7 @@ struct NextPacketDetails {
79247930

79257931
fn decode_incoming_update_add_htlc_onion<NS: Deref, L: Deref, T: secp256k1::Verification>(
79267932
msg: &msgs::UpdateAddHTLC, node_signer: &NS, logger: &L, secp_ctx: &Secp256k1<T>,
7927-
) -> Result<(
7928-
onion_utils::Hop, [u8; 32], Option<NextPacketDetails>
7929-
), HTLCFailureMsg>
7933+
) -> Result<(onion_utils::Hop, [u8; 32], Option<NextPacketDetails>), HTLCFailureMsg>
79307934
where
79317935
NS::Target: NodeSigner,
79327936
L::Target: Logger,
@@ -8002,6 +8006,8 @@ where
80028006
outgoing_amt_msat: amt_to_forward, outgoing_cltv_value
80038007
}
80048008
},
8009+
// We'll do receive checks in [`Self::construct_pending_htlc_info`] so we have access to the
8010+
// inbound channel's state.
80058011
onion_utils::Hop::Receive { .. } => return Ok((next_hop, shared_secret, None)),
80068012
onion_utils::Hop::Forward { next_hop_data: msgs::InboundOnionPayload::Receive { .. }, .. } |
80078013
onion_utils::Hop::Forward { next_hop_data: msgs::InboundOnionPayload::BlindedReceive { .. }, .. } =>
@@ -8015,24 +8021,21 @@ where
80158021

80168022
fn check_incoming_htlc_cltv(
80178023
cur_height: u32, outgoing_cltv_value: u32, cltv_expiry: u32
8018-
) -> Result<(), (&'static str, u16, bool)> {
8024+
) -> Result<(), (&'static str, u16)> {
80198025
if (cltv_expiry as u64) < (outgoing_cltv_value) as u64 + MIN_CLTV_EXPIRY_DELTA as u64 {
8020-
// We really should set `incorrect_cltv_expiry` here but as we're not
8021-
// forwarding over a real channel we can't generate a channel_update
8022-
// for it. Instead we just return a generic temporary_node_failure.
80238026
return Err((
8024-
"Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
8025-
0x2000 | 2, false,
8027+
"Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
8028+
0x1000 | 13, // incorrect_cltv_expiry
80268029
));
80278030
}
80288031
// Theoretically, channel counterparty shouldn't send us a HTLC expiring now,
80298032
// but we want to be robust wrt to counterparty packet sanitization (see
80308033
// HTLC_FAIL_BACK_BUFFER rationale).
80318034
if cltv_expiry <= cur_height + HTLC_FAIL_BACK_BUFFER as u32 { // expiry_too_soon
8032-
return Err(("CLTV expiry is too close", 0x1000 | 14, true));
8035+
return Err(("CLTV expiry is too close", 0x1000 | 14));
80338036
}
80348037
if cltv_expiry > cur_height + CLTV_FAR_FAR_AWAY as u32 { // expiry_too_far
8035-
return Err(("CLTV expiry is too far in the future", 21, false));
8038+
return Err(("CLTV expiry is too far in the future", 21));
80368039
}
80378040
// If the HTLC expires ~now, don't bother trying to forward it to our
80388041
// counterparty. They should fail it anyway, but we don't want to bother with
@@ -8043,7 +8046,7 @@ fn check_incoming_htlc_cltv(
80438046
// but there is no need to do that, and since we're a bit conservative with our
80448047
// risk threshold it just results in failing to forward payments.
80458048
if (outgoing_cltv_value) as u64 <= (cur_height + LATENCY_GRACE_PERIOD_BLOCKS) as u64 {
8046-
return Err(("Outgoing CLTV value is too soon", 0x1000 | 14, true));
8049+
return Err(("Outgoing CLTV value is too soon", 0x1000 | 14));
80478050
}
80488051

80498052
Ok(())
@@ -12205,7 +12208,7 @@ mod tests {
1220512208
let charlie = crate::sign::KeysManager::new(&[3; 32], 42, 42);
1220612209
let charlie_pk = PublicKey::from_secret_key(&secp_ctx, &charlie.get_node_secret_key());
1220712210

12208-
let (session_priv, total_amt_msat, cur_height, recipient_onion, preimage, payment_hash,
12211+
let (session_priv, total_amt_msat, cur_height, recipient_onion, preimage, payment_hash,
1220912212
prng_seed, hops, recipient_amount, pay_secret) = payment_onion_args(bob_pk, charlie_pk);
1221012213

1221112214
let path = Path {
@@ -12220,9 +12223,9 @@ mod tests {
1222012223

1222112224
let msg = make_update_add_msg(amount_msat, cltv_expiry, payment_hash, onion);
1222212225
let logger = test_utils::TestLogger::with_id("bob".to_string());
12223-
12226+
1222412227
let peeled = peel_payment_onion(&msg, &&bob, &&logger, &secp_ctx, cur_height, true)
12225-
.map_err(|e| e.msg).unwrap();
12228+
.map_err(|e| e.msg).unwrap();
1222612229

1222712230
let next_onion = match peeled.routing {
1222812231
PendingHTLCRouting::Forward { onion_packet, short_channel_id: _ } => {
@@ -12233,8 +12236,8 @@ mod tests {
1223312236

1223412237
let msg2 = make_update_add_msg(amount_msat, cltv_expiry, payment_hash, next_onion);
1223512238
let peeled2 = peel_payment_onion(&msg2, &&charlie, &&logger, &secp_ctx, cur_height, true)
12236-
.map_err(|e| e.msg).unwrap();
12237-
12239+
.map_err(|e| e.msg).unwrap();
12240+
1223812241
match peeled2.routing {
1223912242
PendingHTLCRouting::ReceiveKeysend { payment_preimage, payment_data, incoming_cltv_expiry, .. } => {
1224012243
assert_eq!(payment_preimage, preimage);
@@ -12249,7 +12252,7 @@ mod tests {
1224912252
}
1225012253

1225112254
fn make_update_add_msg(
12252-
amount_msat: u64, cltv_expiry: u32, payment_hash: PaymentHash,
12255+
amount_msat: u64, cltv_expiry: u32, payment_hash: PaymentHash,
1225312256
onion_routing_packet: msgs::OnionPacket
1225412257
) -> msgs::UpdateAddHTLC {
1225512258
msgs::UpdateAddHTLC {
@@ -12308,7 +12311,7 @@ mod tests {
1230812311
}
1230912312

1231012313
pub fn create_payment_onion<T: bitcoin::secp256k1::Signing>(
12311-
secp_ctx: &Secp256k1<T>, path: &Path, session_priv: &SecretKey, total_msat: u64,
12314+
secp_ctx: &Secp256k1<T>, path: &Path, session_priv: &SecretKey, total_msat: u64,
1231212315
recipient_onion: RecipientOnionFields, best_block_height: u32, payment_hash: PaymentHash,
1231312316
keysend_preimage: Option<PaymentPreimage>, prng_seed: [u8; 32]
1231412317
) -> Result<(u64, u32, msgs::OnionPacket), ()> {

0 commit comments

Comments
 (0)