Skip to content

Commit 1f5a5f7

Browse files
committed
feedback from val
1 parent 729e5a7 commit 1f5a5f7

File tree

1 file changed

+49
-55
lines changed

1 file changed

+49
-55
lines changed

lightning/src/ln/channelmanager.rs

Lines changed: 49 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,31 @@ use crate::ln::script::ShutdownScript;
106106
// Alternatively, we can fill an outbound HTLC with a HTLCSource::OutboundRoute indicating this is
107107
// our payment, which we can use to decode errors or inform the user that the payment was sent.
108108

109-
/// Routing info for a received HTLC onion.
109+
/// Routing info for an inbound HTLC onion.
110110
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
111111
pub enum PendingHTLCRouting {
112112
/// A forwarded HTLC.
113113
Forward {
114-
/// Full Bolt04 onion packet.
114+
/// BOLT 4 onion packet.
115115
onion_packet: msgs::OnionPacket,
116116
/// The SCID from the onion that we should forward to. This could be a real SCID or a fake one
117117
/// generated using `get_fake_scid` from the scid_utils::fake_scid module.
118118
short_channel_id: u64, // This should be NonZero<u64> eventually when we bump MSRV
119119
},
120-
/// A paid invoice.
120+
/// An HTLC paid to an invoice we generated.
121121
Receive {
122122
/// Payment secret and total msat received.
123123
payment_data: msgs::FinalOnionHopData,
124-
/// Metadata for this payment.
124+
/// See [`RecipientOnionFields::payment_metadata`] for more info.
125125
payment_metadata: Option<Vec<u8>>,
126-
/// Used to track when we should expire pending HTLCs that go unclaimed
126+
/// Used to track when we should expire pending HTLCs that go unclaimed.
127127
incoming_cltv_expiry: u32,
128128
/// Optional shared secret for phantom node.
129129
phantom_shared_secret: Option<[u8; 32]>,
130130
/// See [`RecipientOnionFields::custom_tlvs`] for more info.
131131
custom_tlvs: Vec<(u64, Vec<u8>)>,
132132
},
133-
/// Incoming keysend (sender included the preimage in a TLV).
133+
/// Incoming keysend (sender provided the preimage in a TLV).
134134
ReceiveKeysend {
135135
/// This was added in 0.0.116 and will break deserialization on downgrades.
136136
payment_data: Option<msgs::FinalOnionHopData>,
@@ -148,7 +148,7 @@ pub enum PendingHTLCRouting {
148148
/// Full details of an incoming HTLC, including routing info.
149149
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
150150
pub struct PendingHTLCInfo {
151-
/// Routing struct of either Forward, Receive, or ReceiveKeysend.
151+
/// Further routing details based on whether the HTLC is being forwarded or received.
152152
pub routing: PendingHTLCRouting,
153153
/// Shared secret from the previous hop.
154154
pub incoming_shared_secret: [u8; 32],
@@ -2920,19 +2920,6 @@ where
29202920
}
29212921
}
29222922

2923-
fn construct_recv_pending_htlc_info(
2924-
&self, hop_data: msgs::InboundOnionPayload, shared_secret: [u8; 32], payment_hash: PaymentHash,
2925-
amt_msat: u64, cltv_expiry: u32, phantom_shared_secret: Option<[u8; 32]>, allow_underpay: bool,
2926-
counterparty_skimmed_fee_msat: Option<u64>,
2927-
) -> Result<PendingHTLCInfo, InboundOnionErr> {
2928-
let current_height: u32 = self.best_block.read().unwrap().height();
2929-
create_recv_pending_htlc_info(
2930-
hop_data, shared_secret, payment_hash, amt_msat, cltv_expiry, phantom_shared_secret,
2931-
allow_underpay, counterparty_skimmed_fee_msat, current_height,
2932-
self.default_configuration.accept_mpp_keysend,
2933-
)
2934-
}
2935-
29362923
fn decode_update_add_htlc_onion(
29372924
&self, msg: &msgs::UpdateAddHTLC
29382925
) -> Result<
@@ -2957,12 +2944,11 @@ where
29572944
}
29582945

29592946
// it is a receive, so no need for outbound checks
2960-
if let None = next_packet_details_opt {
2947+
if next_packet_details_opt.is_none() {
29612948
return Ok((next_hop, shared_secret, None));
29622949
}
29632950
let (next_packet_pk, outgoing_scid, outgoing_amt_msat, outgoing_cltv_value) =
29642951
next_packet_details_opt.unwrap();
2965-
let next_packet_pk_opt = Some(next_packet_pk);
29662952

29672953
// Perform outbound checks here instead of in [`Self::construct_pending_htlc_info`] because we
29682954
// can't hold the outbound peer state lock at the same time as the inbound peer state lock.
@@ -2983,7 +2969,7 @@ where
29832969
},
29842970
Some((cp_id, id)) => Some((cp_id.clone(), id.clone())),
29852971
};
2986-
let (chan_update_opt, check_min_cltv_expiry_delta) = if let Some((counterparty_node_id, forwarding_id)) = forwarding_chan_info_opt {
2972+
let chan_update_opt = if let Some((counterparty_node_id, forwarding_id)) = forwarding_chan_info_opt {
29872973
let per_peer_state = self.per_peer_state.read().unwrap();
29882974
let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
29892975
if peer_state_mutex_opt.is_none() {
@@ -3037,15 +3023,15 @@ where
30373023
break Some((err, code, chan_update_opt));
30383024
}
30393025
// min_cltv_expiry_delta is already checked above in htlc_satisfies_config
3040-
(chan_update_opt, false)
3026+
chan_update_opt
30413027
} else {
3042-
(None, true)
3028+
None
30433029
};
30443030

30453031
let cur_height = self.best_block.read().unwrap().height() + 1;
30463032

30473033
if let Err((err_msg, code, include_chan_update_opt)) = check_incoming_htlc_cltv(
3048-
cur_height, outgoing_cltv_value, msg.cltv_expiry, check_min_cltv_expiry_delta
3034+
cur_height, outgoing_cltv_value, msg.cltv_expiry
30493035
) {
30503036
let chan_update_opt = if include_chan_update_opt { chan_update_opt } else { None };
30513037
break Some((err_msg, code, chan_update_opt));
@@ -3078,7 +3064,7 @@ where
30783064
}
30793065
return_err!(err, code, &res.0[..]);
30803066
}
3081-
Ok((next_hop, shared_secret, next_packet_pk_opt))
3067+
Ok((next_hop, shared_secret, Some(next_packet_pk)))
30823068
}
30833069

30843070
fn construct_pending_htlc_status<'a>(
@@ -3101,8 +3087,10 @@ where
31013087
match decoded_hop {
31023088
onion_utils::Hop::Receive(next_hop_data) => {
31033089
// OUR PAYMENT!
3104-
match self.construct_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash,
3105-
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat)
3090+
let current_height: u32 = self.best_block.read().unwrap().height();
3091+
match create_recv_pending_htlc_info(next_hop_data, shared_secret, msg.payment_hash,
3092+
msg.amount_msat, msg.cltv_expiry, None, allow_underpay, msg.skimmed_fee_msat,
3093+
current_height, self.default_configuration.accept_mpp_keysend)
31063094
{
31073095
Ok(info) => {
31083096
// Note that we could obviously respond immediately with an update_fulfill_htlc
@@ -4141,9 +4129,11 @@ where
41414129
};
41424130
match next_hop {
41434131
onion_utils::Hop::Receive(hop_data) => {
4144-
match self.construct_recv_pending_htlc_info(hop_data,
4132+
let current_height: u32 = self.best_block.read().unwrap().height();
4133+
match create_recv_pending_htlc_info(hop_data,
41454134
incoming_shared_secret, payment_hash, outgoing_amt_msat,
4146-
outgoing_cltv_value, Some(phantom_shared_secret), false, None)
4135+
outgoing_cltv_value, Some(phantom_shared_secret), false, None,
4136+
current_height, self.default_configuration.accept_mpp_keysend)
41474137
{
41484138
Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, prev_user_channel_id, vec![(info, prev_htlc_id)])),
41494139
Err(InboundOnionErr { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
@@ -7891,7 +7881,7 @@ where
78917881
})?;
78927882
Ok(match hop {
78937883
onion_utils::Hop::Forward{ next_hop_data, next_hop_hmac, new_packet_bytes } => {
7894-
if let None = next_packet_details_opt {
7884+
if next_packet_details_opt.is_none() {
78957885
// Forward should always include the next hop details
78967886
return Err(InboundOnionErr {
78977887
msg: "Failed to decode update add htlc onion",
@@ -7902,7 +7892,7 @@ where
79027892
let (next_packet_pubkey, _, _, outgoing_cltv_value) = next_packet_details_opt.unwrap();
79037893

79047894
if let Err((err_msg, code, _)) = check_incoming_htlc_cltv(
7905-
cur_height, outgoing_cltv_value, msg.cltv_expiry, true
7895+
cur_height, outgoing_cltv_value, msg.cltv_expiry
79067896
) {
79077897
return Err(InboundOnionErr {
79087898
msg: err_msg,
@@ -8019,19 +8009,17 @@ where
80198009
}
80208010

80218011
fn check_incoming_htlc_cltv(
8022-
cur_height: u32, outgoing_cltv_value: u32, cltv_expiry: u32, check_min_cltv_expiry_delta: bool
8012+
cur_height: u32, outgoing_cltv_value: u32, cltv_expiry: u32
80238013
) -> Result<(), (&'static str, u16, bool)> {
8024-
if check_min_cltv_expiry_delta {
8025-
if (cltv_expiry as u64) < (outgoing_cltv_value) as u64 + MIN_CLTV_EXPIRY_DELTA as u64 {
8026-
// We really should set `incorrect_cltv_expiry` here but as we're not
8027-
// forwarding over a real channel we can't generate a channel_update
8028-
// for it. Instead we just return a generic temporary_node_failure.
8029-
return Err((
8030-
"Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
8031-
0x2000 | 2, false,
8032-
));
8033-
}
8034-
};
8014+
if (cltv_expiry as u64) < (outgoing_cltv_value) as u64 + MIN_CLTV_EXPIRY_DELTA as u64 {
8015+
// We really should set `incorrect_cltv_expiry` here but as we're not
8016+
// forwarding over a real channel we can't generate a channel_update
8017+
// for it. Instead we just return a generic temporary_node_failure.
8018+
return Err((
8019+
"Forwarding node has tampered with the intended HTLC values or origin node has an obsolete cltv_expiry_delta",
8020+
0x2000 | 2, false,
8021+
));
8022+
}
80358023
// Theoretically, channel counterparty shouldn't send us a HTLC expiring now,
80368024
// but we want to be robust wrt to counterparty packet sanitization (see
80378025
// HTLC_FAIL_BACK_BUFFER rationale).
@@ -10920,7 +10908,7 @@ mod tests {
1092010908
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
1092110909
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
1092210910
use crate::ln::ChannelId;
10923-
use crate::ln::channelmanager::{inbound_payment, PaymentId, PaymentSendFailure, RecipientOnionFields, InterceptId};
10911+
use crate::ln::channelmanager::{create_recv_pending_htlc_info, inbound_payment, PaymentId, PaymentSendFailure, RecipientOnionFields, InterceptId};
1092410912
use crate::ln::functional_test_utils::*;
1092510913
use crate::ln::msgs::{self, ErrorAction};
1092610914
use crate::ln::msgs::ChannelMessageHandler;
@@ -11922,9 +11910,11 @@ mod tests {
1192211910
};
1192311911
// Check that if the amount we received + the penultimate hop extra fee is less than the sender
1192411912
// intended amount, we fail the payment.
11913+
let current_height: u32 = node[0].node.best_block.read().unwrap().height();
1192511914
if let Err(crate::ln::channelmanager::InboundOnionErr { err_code, .. }) =
11926-
node[0].node.construct_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
11927-
sender_intended_amt_msat - extra_fee_msat - 1, 42, None, true, Some(extra_fee_msat))
11915+
create_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
11916+
sender_intended_amt_msat - extra_fee_msat - 1, 42, None, true, Some(extra_fee_msat),
11917+
current_height, node[0].node.default_configuration.accept_mpp_keysend)
1192811918
{
1192911919
assert_eq!(err_code, 19);
1193011920
} else { panic!(); }
@@ -11940,8 +11930,10 @@ mod tests {
1194011930
}),
1194111931
custom_tlvs: Vec::new(),
1194211932
};
11943-
assert!(node[0].node.construct_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
11944-
sender_intended_amt_msat - extra_fee_msat, 42, None, true, Some(extra_fee_msat)).is_ok());
11933+
let current_height: u32 = node[0].node.best_block.read().unwrap().height();
11934+
assert!(create_recv_pending_htlc_info(hop_data, [0; 32], PaymentHash([0; 32]),
11935+
sender_intended_amt_msat - extra_fee_msat, 42, None, true, Some(extra_fee_msat),
11936+
current_height, node[0].node.default_configuration.accept_mpp_keysend).is_ok());
1194511937
}
1194611938

1194711939
#[test]
@@ -11951,7 +11943,8 @@ mod tests {
1195111943
let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
1195211944
let node = create_network(1, &node_cfg, &node_chanmgr);
1195311945

11954-
let result = node[0].node.construct_recv_pending_htlc_info(msgs::InboundOnionPayload::Receive {
11946+
let current_height: u32 = node[0].node.best_block.read().unwrap().height();
11947+
let result = create_recv_pending_htlc_info(msgs::InboundOnionPayload::Receive {
1195511948
amt_msat: 100,
1195611949
outgoing_cltv_value: 22,
1195711950
payment_metadata: None,
@@ -11960,7 +11953,8 @@ mod tests {
1196011953
payment_secret: PaymentSecret([0; 32]), total_msat: 100,
1196111954
}),
1196211955
custom_tlvs: Vec::new(),
11963-
}, [0; 32], PaymentHash([0; 32]), 100, 23, None, true, None);
11956+
}, [0; 32], PaymentHash([0; 32]), 100, 23, None, true, None, current_height,
11957+
node[0].node.default_configuration.accept_mpp_keysend);
1196411958

1196511959
// Should not return an error as this condition:
1196611960
// https://github.com/lightning/bolts/blob/4dcc377209509b13cf89a4b91fde7d478f5b46d8/04-onion-routing.md?plain=1#L334
@@ -12219,7 +12213,7 @@ mod tests {
1221912213
payment_hash, Some(preimage), prng_seed
1222012214
).unwrap();
1222112215

12222-
let msg = make_udpate_add_msg(amount_msat, cltv_expiry, payment_hash, onion);
12216+
let msg = make_update_add_msg(amount_msat, cltv_expiry, payment_hash, onion);
1222312217
let logger = test_utils::TestLogger::with_id("bob".to_string());
1222412218

1222512219
let peeled = peel_payment_onion(&msg, &&bob, &&logger, &secp_ctx, cur_height, true)
@@ -12232,7 +12226,7 @@ mod tests {
1223212226
_ => panic!("expected a forwarded onion"),
1223312227
};
1223412228

12235-
let msg2 = make_udpate_add_msg(amount_msat, cltv_expiry, payment_hash, next_onion);
12229+
let msg2 = make_update_add_msg(amount_msat, cltv_expiry, payment_hash, next_onion);
1223612230
let peeled2 = peel_payment_onion(&msg2, &&charlie, &&logger, &secp_ctx, cur_height, true)
1223712231
.map_err(|e| e.msg).unwrap();
1223812232

@@ -12249,7 +12243,7 @@ mod tests {
1224912243
};
1225012244
}
1225112245

12252-
fn make_udpate_add_msg(
12246+
fn make_update_add_msg(
1225312247
amount_msat: u64, cltv_expiry: u32, payment_hash: PaymentHash,
1225412248
onion_routing_packet: msgs::OnionPacket
1225512249
) -> msgs::UpdateAddHTLC {

0 commit comments

Comments
 (0)