@@ -106,31 +106,31 @@ use crate::ln::script::ShutdownScript;
106
106
// Alternatively, we can fill an outbound HTLC with a HTLCSource::OutboundRoute indicating this is
107
107
// our payment, which we can use to decode errors or inform the user that the payment was sent.
108
108
109
- /// Routing info for a received HTLC onion.
109
+ /// Routing info for an inbound HTLC onion.
110
110
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
111
111
pub enum PendingHTLCRouting {
112
112
/// A forwarded HTLC.
113
113
Forward {
114
- /// Full Bolt04 onion packet.
114
+ /// BOLT 4 onion packet.
115
115
onion_packet: msgs::OnionPacket,
116
116
/// The SCID from the onion that we should forward to. This could be a real SCID or a fake one
117
117
/// generated using `get_fake_scid` from the scid_utils::fake_scid module.
118
118
short_channel_id: u64, // This should be NonZero<u64> eventually when we bump MSRV
119
119
},
120
- /// A paid invoice.
120
+ /// An HTLC paid to an invoice we generated .
121
121
Receive {
122
122
/// Payment secret and total msat received.
123
123
payment_data: msgs::FinalOnionHopData,
124
- /// Metadata for this payment .
124
+ /// See [`RecipientOnionFields::payment_metadata`] for more info .
125
125
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.
127
127
incoming_cltv_expiry: u32,
128
128
/// Optional shared secret for phantom node.
129
129
phantom_shared_secret: Option<[u8; 32]>,
130
130
/// See [`RecipientOnionFields::custom_tlvs`] for more info.
131
131
custom_tlvs: Vec<(u64, Vec<u8>)>,
132
132
},
133
- /// Incoming keysend (sender included the preimage in a TLV).
133
+ /// Incoming keysend (sender provided the preimage in a TLV).
134
134
ReceiveKeysend {
135
135
/// This was added in 0.0.116 and will break deserialization on downgrades.
136
136
payment_data: Option<msgs::FinalOnionHopData>,
@@ -148,7 +148,7 @@ pub enum PendingHTLCRouting {
148
148
/// Full details of an incoming HTLC, including routing info.
149
149
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
150
150
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 .
152
152
pub routing: PendingHTLCRouting,
153
153
/// Shared secret from the previous hop.
154
154
pub incoming_shared_secret: [u8; 32],
@@ -2920,19 +2920,6 @@ where
2920
2920
}
2921
2921
}
2922
2922
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
-
2936
2923
fn decode_update_add_htlc_onion(
2937
2924
&self, msg: &msgs::UpdateAddHTLC
2938
2925
) -> Result<
@@ -2957,12 +2944,11 @@ where
2957
2944
}
2958
2945
2959
2946
// 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() {
2961
2948
return Ok((next_hop, shared_secret, None));
2962
2949
}
2963
2950
let (next_packet_pk, outgoing_scid, outgoing_amt_msat, outgoing_cltv_value) =
2964
2951
next_packet_details_opt.unwrap();
2965
- let next_packet_pk_opt = Some(next_packet_pk);
2966
2952
2967
2953
// Perform outbound checks here instead of in [`Self::construct_pending_htlc_info`] because we
2968
2954
// can't hold the outbound peer state lock at the same time as the inbound peer state lock.
@@ -2983,7 +2969,7 @@ where
2983
2969
},
2984
2970
Some((cp_id, id)) => Some((cp_id.clone(), id.clone())),
2985
2971
};
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 {
2987
2973
let per_peer_state = self.per_peer_state.read().unwrap();
2988
2974
let peer_state_mutex_opt = per_peer_state.get(&counterparty_node_id);
2989
2975
if peer_state_mutex_opt.is_none() {
@@ -3037,15 +3023,15 @@ where
3037
3023
break Some((err, code, chan_update_opt));
3038
3024
}
3039
3025
// min_cltv_expiry_delta is already checked above in htlc_satisfies_config
3040
- ( chan_update_opt, false)
3026
+ chan_update_opt
3041
3027
} else {
3042
- ( None, true)
3028
+ None
3043
3029
};
3044
3030
3045
3031
let cur_height = self.best_block.read().unwrap().height() + 1;
3046
3032
3047
3033
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
3049
3035
) {
3050
3036
let chan_update_opt = if include_chan_update_opt { chan_update_opt } else { None };
3051
3037
break Some((err_msg, code, chan_update_opt));
@@ -3078,7 +3064,7 @@ where
3078
3064
}
3079
3065
return_err!(err, code, &res.0[..]);
3080
3066
}
3081
- Ok((next_hop, shared_secret, next_packet_pk_opt ))
3067
+ Ok((next_hop, shared_secret, Some(next_packet_pk) ))
3082
3068
}
3083
3069
3084
3070
fn construct_pending_htlc_status<'a>(
@@ -3101,8 +3087,10 @@ where
3101
3087
match decoded_hop {
3102
3088
onion_utils::Hop::Receive(next_hop_data) => {
3103
3089
// 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)
3106
3094
{
3107
3095
Ok(info) => {
3108
3096
// Note that we could obviously respond immediately with an update_fulfill_htlc
@@ -4141,9 +4129,11 @@ where
4141
4129
};
4142
4130
match next_hop {
4143
4131
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,
4145
4134
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)
4147
4137
{
4148
4138
Ok(info) => phantom_receives.push((prev_short_channel_id, prev_funding_outpoint, prev_user_channel_id, vec![(info, prev_htlc_id)])),
4149
4139
Err(InboundOnionErr { err_code, err_data, msg }) => failed_payment!(msg, err_code, err_data, Some(phantom_shared_secret))
@@ -7891,7 +7881,7 @@ where
7891
7881
})?;
7892
7882
Ok(match hop {
7893
7883
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() {
7895
7885
// Forward should always include the next hop details
7896
7886
return Err(InboundOnionErr {
7897
7887
msg: "Failed to decode update add htlc onion",
@@ -7902,7 +7892,7 @@ where
7902
7892
let (next_packet_pubkey, _, _, outgoing_cltv_value) = next_packet_details_opt.unwrap();
7903
7893
7904
7894
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
7906
7896
) {
7907
7897
return Err(InboundOnionErr {
7908
7898
msg: err_msg,
@@ -8019,19 +8009,17 @@ where
8019
8009
}
8020
8010
8021
8011
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
8023
8013
) -> 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
+ }
8035
8023
// Theoretically, channel counterparty shouldn't send us a HTLC expiring now,
8036
8024
// but we want to be robust wrt to counterparty packet sanitization (see
8037
8025
// HTLC_FAIL_BACK_BUFFER rationale).
@@ -10920,7 +10908,7 @@ mod tests {
10920
10908
use crate::events::{Event, HTLCDestination, MessageSendEvent, MessageSendEventsProvider, ClosureReason};
10921
10909
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
10922
10910
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};
10924
10912
use crate::ln::functional_test_utils::*;
10925
10913
use crate::ln::msgs::{self, ErrorAction};
10926
10914
use crate::ln::msgs::ChannelMessageHandler;
@@ -11922,9 +11910,11 @@ mod tests {
11922
11910
};
11923
11911
// Check that if the amount we received + the penultimate hop extra fee is less than the sender
11924
11912
// intended amount, we fail the payment.
11913
+ let current_height: u32 = node[0].node.best_block.read().unwrap().height();
11925
11914
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)
11928
11918
{
11929
11919
assert_eq!(err_code, 19);
11930
11920
} else { panic!(); }
@@ -11940,8 +11930,10 @@ mod tests {
11940
11930
}),
11941
11931
custom_tlvs: Vec::new(),
11942
11932
};
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());
11945
11937
}
11946
11938
11947
11939
#[test]
@@ -11951,7 +11943,8 @@ mod tests {
11951
11943
let node_chanmgr = create_node_chanmgrs(1, &node_cfg, &[None]);
11952
11944
let node = create_network(1, &node_cfg, &node_chanmgr);
11953
11945
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 {
11955
11948
amt_msat: 100,
11956
11949
outgoing_cltv_value: 22,
11957
11950
payment_metadata: None,
@@ -11960,7 +11953,8 @@ mod tests {
11960
11953
payment_secret: PaymentSecret([0; 32]), total_msat: 100,
11961
11954
}),
11962
11955
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);
11964
11958
11965
11959
// Should not return an error as this condition:
11966
11960
// https://github.com/lightning/bolts/blob/4dcc377209509b13cf89a4b91fde7d478f5b46d8/04-onion-routing.md?plain=1#L334
@@ -12219,7 +12213,7 @@ mod tests {
12219
12213
payment_hash, Some(preimage), prng_seed
12220
12214
).unwrap();
12221
12215
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);
12223
12217
let logger = test_utils::TestLogger::with_id("bob".to_string());
12224
12218
12225
12219
let peeled = peel_payment_onion(&msg, &&bob, &&logger, &secp_ctx, cur_height, true)
@@ -12232,7 +12226,7 @@ mod tests {
12232
12226
_ => panic!("expected a forwarded onion"),
12233
12227
};
12234
12228
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);
12236
12230
let peeled2 = peel_payment_onion(&msg2, &&charlie, &&logger, &secp_ctx, cur_height, true)
12237
12231
.map_err(|e| e.msg).unwrap();
12238
12232
@@ -12249,7 +12243,7 @@ mod tests {
12249
12243
};
12250
12244
}
12251
12245
12252
- fn make_udpate_add_msg (
12246
+ fn make_update_add_msg (
12253
12247
amount_msat: u64, cltv_expiry: u32, payment_hash: PaymentHash,
12254
12248
onion_routing_packet: msgs::OnionPacket
12255
12249
) -> msgs::UpdateAddHTLC {
0 commit comments