Skip to content

Commit 63497e9

Browse files
authored
Merge pull request #2762 from TheBlueMatt/2023-11-htlc-docs
Improve docs on newly-public structs after #2700
2 parents becdf6f + 63ee03f commit 63497e9

File tree

2 files changed

+93
-35
lines changed

2 files changed

+93
-35
lines changed

lightning/src/ln/channelmanager.rs

+79-28
Original file line numberDiff line numberDiff line change
@@ -109,50 +109,79 @@ use crate::ln::script::ShutdownScript;
109109
// Alternatively, we can fill an outbound HTLC with a HTLCSource::OutboundRoute indicating this is
110110
// our payment, which we can use to decode errors or inform the user that the payment was sent.
111111

112-
/// Routing info for an inbound HTLC onion.
112+
/// Information about where a received HTLC('s onion) has indicated the HTLC should go.
113113
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
114114
pub enum PendingHTLCRouting {
115-
/// A forwarded HTLC.
115+
/// An HTLC which should be forwarded on to another node.
116116
Forward {
117-
/// BOLT 4 onion packet.
117+
/// The onion which should be included in the forwarded HTLC, telling the next hop what to
118+
/// do with the HTLC.
118119
onion_packet: msgs::OnionPacket,
119-
/// The SCID from the onion that we should forward to. This could be a real SCID or a fake one
120-
/// generated using `get_fake_scid` from the scid_utils::fake_scid module.
120+
/// The short channel ID of the channel which we were instructed to forward this HTLC to.
121+
///
122+
/// This could be a real on-chain SCID, an SCID alias, or some other SCID which has meaning
123+
/// to the receiving node, such as one returned from
124+
/// [`ChannelManager::get_intercept_scid`] or [`ChannelManager::get_phantom_scid`].
121125
short_channel_id: u64, // This should be NonZero<u64> eventually when we bump MSRV
122126
/// Set if this HTLC is being forwarded within a blinded path.
123127
blinded: Option<BlindedForward>,
124128
},
125-
/// An HTLC paid to an invoice (supposedly) generated by us.
126-
/// At this point, we have not checked that the invoice being paid was actually generated by us,
127-
/// but rather it's claiming to pay an invoice of ours.
129+
/// The onion indicates that this is a payment for an invoice (supposedly) generated by us.
130+
///
131+
/// Note that at this point, we have not checked that the invoice being paid was actually
132+
/// generated by us, but rather it's claiming to pay an invoice of ours.
128133
Receive {
129-
/// Payment secret and total msat received.
134+
/// Information about the amount the sender intended to pay and (potential) proof that this
135+
/// is a payment for an invoice we generated. This proof of payment is is also used for
136+
/// linking MPP parts of a larger payment.
130137
payment_data: msgs::FinalOnionHopData,
131-
/// See [`RecipientOnionFields::payment_metadata`] for more info.
138+
/// Additional data which we (allegedly) instructed the sender to include in the onion.
139+
///
140+
/// For HTLCs received by LDK, this will ultimately be exposed in
141+
/// [`Event::PaymentClaimable::onion_fields`] as
142+
/// [`RecipientOnionFields::payment_metadata`].
132143
payment_metadata: Option<Vec<u8>>,
133144
/// CLTV expiry of the received HTLC.
145+
///
134146
/// Used to track when we should expire pending HTLCs that go unclaimed.
135147
incoming_cltv_expiry: u32,
136-
/// Shared secret derived using a phantom node secret key. If this field is Some, the
137-
/// payment was sent to a phantom node (one hop beyond the current node), but can be
138-
/// settled by this node.
148+
/// If the onion had forwarding instructions to one of our phantom node SCIDs, this will
149+
/// provide the onion shared secret used to decrypt the next level of forwarding
150+
/// instructions.
139151
phantom_shared_secret: Option<[u8; 32]>,
140-
/// See [`RecipientOnionFields::custom_tlvs`] for more info.
152+
/// Custom TLVs which were set by the sender.
153+
///
154+
/// For HTLCs received by LDK, this will ultimately be exposed in
155+
/// [`Event::PaymentClaimable::onion_fields`] as
156+
/// [`RecipientOnionFields::custom_tlvs`].
141157
custom_tlvs: Vec<(u64, Vec<u8>)>,
142158
},
143-
/// Incoming keysend (sender provided the preimage in a TLV).
159+
/// The onion indicates that this is for payment to us but which contains the preimage for
160+
/// claiming included, and is unrelated to any invoice we'd previously generated (aka a
161+
/// "keysend" or "spontaneous" payment).
144162
ReceiveKeysend {
145-
/// This was added in 0.0.116 and will break deserialization on downgrades.
163+
/// Information about the amount the sender intended to pay and possibly a token to
164+
/// associate MPP parts of a larger payment.
165+
///
166+
/// This will only be filled in if receiving MPP keysend payments is enabled, and it being
167+
/// present will cause deserialization to fail on versions of LDK prior to 0.0.116.
146168
payment_data: Option<msgs::FinalOnionHopData>,
147169
/// Preimage for this onion payment. This preimage is provided by the sender and will be
148170
/// used to settle the spontaneous payment.
149171
payment_preimage: PaymentPreimage,
150-
/// See [`RecipientOnionFields::payment_metadata`] for more info.
172+
/// Additional data which we (allegedly) instructed the sender to include in the onion.
173+
///
174+
/// For HTLCs received by LDK, this will ultimately bubble back up as
175+
/// [`RecipientOnionFields::payment_metadata`].
151176
payment_metadata: Option<Vec<u8>>,
152177
/// CLTV expiry of the received HTLC.
178+
///
153179
/// Used to track when we should expire pending HTLCs that go unclaimed.
154180
incoming_cltv_expiry: u32,
155-
/// See [`RecipientOnionFields::custom_tlvs`] for more info.
181+
/// Custom TLVs which were set by the sender.
182+
///
183+
/// For HTLCs received by LDK, these will ultimately bubble back up as
184+
/// [`RecipientOnionFields::custom_tlvs`].
156185
custom_tlvs: Vec<(u64, Vec<u8>)>,
157186
},
158187
}
@@ -179,25 +208,47 @@ impl PendingHTLCRouting {
179208
}
180209
}
181210

182-
/// Full details of an incoming HTLC, including routing info.
211+
/// Information about an incoming HTLC, including the [`PendingHTLCRouting`] describing where it
212+
/// should go next.
183213
#[derive(Clone)] // See Channel::revoke_and_ack for why, tl;dr: Rust bug
184214
pub struct PendingHTLCInfo {
185215
/// Further routing details based on whether the HTLC is being forwarded or received.
186216
pub routing: PendingHTLCRouting,
187-
/// Shared secret from the previous hop.
188-
/// Used encrypt failure packets in the event that the HTLC needs to be failed backwards.
217+
/// The onion shared secret we build with the sender used to decrypt the onion.
218+
///
219+
/// This is later used to encrypt failure packets in the event that the HTLC is failed.
189220
pub incoming_shared_secret: [u8; 32],
190221
/// Hash of the payment preimage, to lock the payment until the receiver releases the preimage.
191222
pub payment_hash: PaymentHash,
192-
/// Amount offered by this HTLC.
193-
pub incoming_amt_msat: Option<u64>, // Added in 0.0.113
194-
/// Sender intended amount to forward or receive (actual amount received
195-
/// may overshoot this in either case)
223+
/// Amount received in the incoming HTLC.
224+
///
225+
/// This field was added in LDK 0.0.113 and will be `None` for objects written by prior
226+
/// versions.
227+
pub incoming_amt_msat: Option<u64>,
228+
/// The amount the sender indicated should be forwarded on to the next hop or amount the sender
229+
/// intended for us to receive for received payments.
230+
///
231+
/// If the received amount is less than this for received payments, an intermediary hop has
232+
/// attempted to steal some of our funds and we should fail the HTLC (the sender should retry
233+
/// it along another path).
234+
///
235+
/// Because nodes can take less than their required fees, and because senders may wish to
236+
/// improve their own privacy, this amount may be less than [`Self::incoming_amt_msat`] for
237+
/// received payments. In such cases, recipients must handle this HTLC as if it had received
238+
/// [`Self::outgoing_amt_msat`].
196239
pub outgoing_amt_msat: u64,
197-
/// Outgoing timelock expiration blockheight.
240+
/// The CLTV the sender has indicated we should set on the forwarded HTLC (or has indicated
241+
/// should have been set on the received HTLC for received payments).
198242
pub outgoing_cltv_value: u32,
199-
/// The fee being skimmed off the top of this HTLC. If this is a forward, it'll be the fee we are
200-
/// skimming. If we're receiving this HTLC, it's the fee that our counterparty skimmed.
243+
/// The fee taken for this HTLC in addition to the standard protocol HTLC fees.
244+
///
245+
/// If this is a payment for forwarding, this is the fee we are taking before forwarding the
246+
/// HTLC.
247+
///
248+
/// If this is a received payment, this is the fee that our counterparty took.
249+
///
250+
/// This is used to allow LSPs to take fees as a part of payments, without the sender having to
251+
/// shoulder them.
201252
pub skimmed_fee_msat: Option<u64>,
202253
}
203254

lightning/src/ln/onion_payment.rs

+14-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
//! Utilities for channelmanager.rs
1+
//! Utilities to decode payment onions and do contextless validation of incoming payments.
22
//!
3-
//! Includes a public [`peel_payment_onion`] function for use by external projects or libraries.
3+
//! Primarily features [`peel_payment_onion`], which allows the decoding of an onion statelessly
4+
//! and can be used to predict whether we'd accept a payment.
45
56
use bitcoin::hashes::Hash;
67
use bitcoin::hashes::sha256::Hash as Sha256;
@@ -225,7 +226,9 @@ pub(super) fn create_recv_pending_htlc_info(
225226
})
226227
}
227228

228-
/// Peel one layer off an incoming onion, returning [`PendingHTLCInfo`] (either Forward or Receive).
229+
/// Peel one layer off an incoming onion, returning a [`PendingHTLCInfo`] that contains information
230+
/// about the intended next-hop for the HTLC.
231+
///
229232
/// This does all the relevant context-free checks that LDK requires for payment relay or
230233
/// acceptance. If the payment is to be received, and the amount matches the expected amount for
231234
/// a given invoice, this indicates the [`msgs::UpdateAddHTLC`], once fully committed in the
@@ -234,7 +237,7 @@ pub(super) fn create_recv_pending_htlc_info(
234237
/// [`Event::PaymentClaimable`]: crate::events::Event::PaymentClaimable
235238
pub fn peel_payment_onion<NS: Deref, L: Deref, T: secp256k1::Verification>(
236239
msg: &msgs::UpdateAddHTLC, node_signer: &NS, logger: &L, secp_ctx: &Secp256k1<T>,
237-
cur_height: u32, accept_mpp_keysend: bool,
240+
cur_height: u32, accept_mpp_keysend: bool, allow_skimmed_fees: bool,
238241
) -> Result<PendingHTLCInfo, InboundOnionErr>
239242
where
240243
NS::Target: NodeSigner,
@@ -273,6 +276,10 @@ where
273276
err_data: Vec::new(),
274277
});
275278
}
279+
280+
// TODO: If this is potentially a phantom payment we should decode the phantom payment
281+
// onion here and check it.
282+
276283
create_fwd_pending_htlc_info(
277284
msg, next_hop_data, next_hop_hmac, new_packet_bytes, shared_secret,
278285
Some(next_packet_pubkey)
@@ -281,7 +288,7 @@ where
281288
onion_utils::Hop::Receive(received_data) => {
282289
create_recv_pending_htlc_info(
283290
received_data, shared_secret, msg.payment_hash, msg.amount_msat, msg.cltv_expiry,
284-
None, false, msg.skimmed_fee_msat, cur_height, accept_mpp_keysend,
291+
None, allow_skimmed_fees, msg.skimmed_fee_msat, cur_height, accept_mpp_keysend,
285292
)?
286293
}
287294
})
@@ -477,7 +484,7 @@ mod tests {
477484
let msg = make_update_add_msg(amount_msat, cltv_expiry, payment_hash, onion);
478485
let logger = test_utils::TestLogger::with_id("bob".to_string());
479486

480-
let peeled = peel_payment_onion(&msg, &&bob, &&logger, &secp_ctx, cur_height, true)
487+
let peeled = peel_payment_onion(&msg, &&bob, &&logger, &secp_ctx, cur_height, true, false)
481488
.map_err(|e| e.msg).unwrap();
482489

483490
let next_onion = match peeled.routing {
@@ -488,7 +495,7 @@ mod tests {
488495
};
489496

490497
let msg2 = make_update_add_msg(amount_msat, cltv_expiry, payment_hash, next_onion);
491-
let peeled2 = peel_payment_onion(&msg2, &&charlie, &&logger, &secp_ctx, cur_height, true)
498+
let peeled2 = peel_payment_onion(&msg2, &&charlie, &&logger, &secp_ctx, cur_height, true, false)
492499
.map_err(|e| e.msg).unwrap();
493500

494501
match peeled2.routing {

0 commit comments

Comments
 (0)