Skip to content

Commit 7a44af3

Browse files
committed
Include pending HTLCs in ChannelDetails
This exposes details around pending HTLCs in ChannelDetails. The state of the HTLC in the state machine is also included, so it can be determined which protocol message the HTLC is waiting for to advance.
1 parent 6b0ba8c commit 7a44af3

File tree

4 files changed

+352
-0
lines changed

4 files changed

+352
-0
lines changed

fuzz/src/router.rs

+2
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,8 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
242242
config: None,
243243
feerate_sat_per_1000_weight: None,
244244
channel_shutdown_state: Some(channelmanager::ChannelShutdownState::NotShuttingDown),
245+
pending_inbound_htlcs: Vec::new(),
246+
pending_outbound_htlcs: Vec::new(),
245247
});
246248
}
247249
Some(&$first_hops_vec[..])

lightning/src/ln/channel.rs

+329
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,104 @@ enum InboundHTLCState {
158158
LocalRemoved(InboundHTLCRemovalReason),
159159
}
160160

161+
/// Exposes the state of pending inbound HTLCs.
162+
///
163+
/// At a high level, an HTLC being forwarded from one Lightning node to another Lightning node goes
164+
/// through the following states in the state machine:
165+
/// - Announced for addition by the originating node through the update_add_htlc message.
166+
/// - Added to the commitment transaction of the receiving node and originating node in turn
167+
/// through the exchange of commitment_signed and revoke_and_ack messages.
168+
/// - Announced for resolution (fulfillment or failure) by the receiving node through either one of
169+
/// the update_fulfill_htlc, update_fail_htlc, and update_fail_malformed_htlc messages.
170+
/// - Removed from the commitment transaction of the originating node and receiving node in turn
171+
/// through the exchange of commitment_signed and revoke_and_ack messages.
172+
///
173+
/// This can be used to inspect what next message an HTLC is waiting for to advance its state.
174+
#[derive(Clone, Debug, PartialEq)]
175+
pub enum InboundHTLCStateDetails {
176+
/// The remote node announced the HTLC with update_add_htlc but the HTLC is not added to any
177+
/// commitment transactions yet.
178+
///
179+
/// We intend to forward the HTLC as it is correctly formed and is forwardable to the next hop.
180+
RemoteAnnouncedForward,
181+
/// The remote node announced the HTLC with update_add_htlc but the HTLC is not added to any
182+
/// commitment transactions yet.
183+
///
184+
/// We intend to fail the HTLC as it is malformed or we are unable to forward to the next hop,
185+
/// for example if the peer is disconnected or not enough capacity is available.
186+
RemoteAnnouncedFail,
187+
/// We have added this HTLC in our commitment transaction by receiving commitment_signed and
188+
/// returning revoke_and_ack. We are awaiting the appropriate revoke_and_ack's from the remote
189+
/// before this HTLC is included on the remote commitment transaction.
190+
///
191+
/// We intend to forward the HTLC as it is correctly formed and is forwardable to the next hop.
192+
AwaitingRemoteRevokeToAddForward,
193+
/// We have added this HTLC in our commitment transaction by receiving commitment_signed and
194+
/// returning revoke_and_ack. We are awaiting the appropriate revoke_and_ack's from the remote
195+
/// before this HTLC is included on the remote commitment transaction.
196+
///
197+
/// We intend to fail the HTLC as it is malformed or we are unable to forward to the next hop,
198+
/// for example if the peer is disconnected or not enough capacity is available.
199+
AwaitingRemoteRevokeToAddFail,
200+
/// This HTLC has been included in the commitment_signed and revoke_and_ack messages on both sides
201+
/// and is included in both commitment transactions.
202+
///
203+
/// This HTLC is now safe to either forward or be claimed by us. The HTLC will remain in this
204+
/// state until the forwarded upstream HTLC has been resolved, or until it is claimed,
205+
/// potentially together with other HTLCs as part of a multipath payment.
206+
Committed,
207+
/// This HTLC is still on both commitment transactions, but we are awaiting revoke_and_ack from
208+
/// the remote for previous changes before we can announce to remove this HTLC from the remote
209+
/// commitment transaction.
210+
///
211+
/// We have received the preimage for this HTLC and it will be removed by fulfilling it with
212+
/// update_fulfill_htlc.
213+
AwaitingRemoteRevokeToRemoveFulfill,
214+
/// This HTLC is still on both commitment transactions, but we are awaiting revoke_and_ack from
215+
/// the remote for previous changes before we can announce to remove this HTLC from the remote
216+
/// commitment transaction.
217+
///
218+
/// The HTLC will be removed by failing it with update_fail_htlc or update_fail_malformed_htlc.
219+
AwaitingRemoteRevokeToRemoveFail,
220+
}
221+
222+
impl From<&InboundHTLCState> for InboundHTLCStateDetails {
223+
fn from(state: &InboundHTLCState) -> InboundHTLCStateDetails {
224+
match state {
225+
InboundHTLCState::RemoteAnnounced(PendingHTLCStatus::Forward(_)) =>
226+
InboundHTLCStateDetails::RemoteAnnouncedForward,
227+
InboundHTLCState::RemoteAnnounced(PendingHTLCStatus::Fail(_)) =>
228+
InboundHTLCStateDetails::RemoteAnnouncedFail,
229+
InboundHTLCState::AwaitingRemoteRevokeToAnnounce(PendingHTLCStatus::Forward(_)) =>
230+
InboundHTLCStateDetails::AwaitingRemoteRevokeToAddForward,
231+
InboundHTLCState::AwaitingRemoteRevokeToAnnounce(PendingHTLCStatus::Fail(_)) =>
232+
InboundHTLCStateDetails::AwaitingRemoteRevokeToAddFail,
233+
InboundHTLCState::AwaitingAnnouncedRemoteRevoke(PendingHTLCStatus::Forward(_)) =>
234+
InboundHTLCStateDetails::AwaitingRemoteRevokeToAddForward,
235+
InboundHTLCState::AwaitingAnnouncedRemoteRevoke(PendingHTLCStatus::Fail(_)) =>
236+
InboundHTLCStateDetails::AwaitingRemoteRevokeToAddFail,
237+
InboundHTLCState::Committed =>
238+
InboundHTLCStateDetails::Committed,
239+
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailRelay(_)) =>
240+
InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFail,
241+
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::FailMalformed(_)) =>
242+
InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFail,
243+
InboundHTLCState::LocalRemoved(InboundHTLCRemovalReason::Fulfill(_)) =>
244+
InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill,
245+
}
246+
}
247+
}
248+
249+
impl_writeable_tlv_based_enum!(InboundHTLCStateDetails,
250+
(0, RemoteAnnouncedForward) => {},
251+
(2, RemoteAnnouncedFail) => {},
252+
(4, AwaitingRemoteRevokeToAddForward) => {},
253+
(6, AwaitingRemoteRevokeToAddFail) => {},
254+
(8, Committed) => {},
255+
(10, AwaitingRemoteRevokeToRemoveFulfill) => {},
256+
(12, AwaitingRemoteRevokeToRemoveFail) => {};
257+
);
258+
161259
struct InboundHTLCOutput {
162260
htlc_id: u64,
163261
amount_msat: u64,
@@ -166,6 +264,44 @@ struct InboundHTLCOutput {
166264
state: InboundHTLCState,
167265
}
168266

267+
/// Exposes details around pending inbound HTLCs.
268+
#[derive(Clone, Debug, PartialEq)]
269+
pub struct InboundHTLCDetails {
270+
/// The HTLC ID.
271+
/// The IDs are incremented by 1 starting from 0 for each offered HTLC.
272+
/// They are unique per channel and inbound/outbound direction, unless an HTLC was only announced
273+
/// and not part of any commitment transaction.
274+
pub htlc_id: u64,
275+
/// The amount in msat.
276+
pub amount_msat: u64,
277+
/// The block height at which this HTLC expires.
278+
pub cltv_expiry: u32,
279+
/// The payment hash.
280+
pub payment_hash: PaymentHash,
281+
/// The state of the HTLC in the state machine.
282+
/// Determines on which commitment transactions the HTLC is included and what message the HTLC is
283+
/// waiting for to advance to the next state.
284+
/// See [InboundHTLCStateDetails] for information on the specific states.
285+
pub state: InboundHTLCStateDetails,
286+
/// Whether the HTLC has an output below the local dust limit. If so, the output will be trimmed
287+
/// from the local commitment transaction and added to the commitment transaction fee.
288+
/// This takes into account the second-stage HTLC transactions as well.
289+
///
290+
/// When the local commitment transaction is broadcasted as part of a unilateral closure,
291+
/// the value of this HTLC will therefore not be claimable but instead burned as a transaction
292+
/// fee.
293+
pub is_dust: bool,
294+
}
295+
296+
impl_writeable_tlv_based!(InboundHTLCDetails, {
297+
(0, htlc_id, required),
298+
(2, amount_msat, required),
299+
(4, cltv_expiry, required),
300+
(6, payment_hash, required),
301+
(8, state, required),
302+
(10, is_dust, required),
303+
});
304+
169305
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
170306
enum OutboundHTLCState {
171307
/// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
@@ -199,6 +335,72 @@ enum OutboundHTLCState {
199335
AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome),
200336
}
201337

338+
/// Exposes the state of pending outbound HTLCs.
339+
///
340+
/// At a high level, an HTLC being forwarded from one Lightning node to another Lightning node goes
341+
/// through the following states in the state machine:
342+
/// - Announced for addition by the originating node through the update_add_htlc message.
343+
/// - Added to the commitment transaction of the receiving node and originating node in turn
344+
/// through the exchange of commitment_signed and revoke_and_ack messages.
345+
/// - Announced for resolution (fulfillment or failure) by the receiving node through either one of
346+
/// the update_fulfill_htlc, update_fail_htlc, and update_fail_malformed_htlc messages.
347+
/// - Removed from the commitment transaction of the originating node and receiving node in turn
348+
/// through the exchange of commitment_signed and revoke_and_ack messages.
349+
///
350+
/// This can be used to inspect what next message an HTLC is waiting for to advance its state.
351+
#[derive(Clone, Debug, PartialEq)]
352+
pub enum OutboundHTLCStateDetails {
353+
/// We are awaiting revoke_and_ack from the remote for previous changes before the HTLC can be
354+
/// announced for addition with update_add_htlc on the remote's commitment transaction.
355+
AwaitingRemoteRevokeToAdd,
356+
/// The HTLC is included on the remote's commitment transaction through a commitment_signed and
357+
/// revoke_and_ack exchange.
358+
///
359+
/// The HTLC will remain in this state until the remote node resolves the HTLC, or until we
360+
/// unilaterally close the channel due to a timeout with an uncooperative remote node.
361+
Committed,
362+
/// The HTLC has been fulfilled succesfully by the remote with a preimage in update_fulfill_htlc,
363+
/// and we removed the HTLC from our commitment transaction through a commitment_signed and
364+
/// revoke_and_ack exchange. We are awaiting the appropriate revoke_and_ack's from the remote for
365+
/// the removal from its commitment transaction.
366+
AwaitingRemoteRevokeToRemoveSuccess,
367+
/// The HTLC has been failed by the remote with update_fail_htlc or update_fail_malformed_htlc,
368+
/// and we removed the HTLC from our commitment transaction through a commitment_signed and
369+
/// revoke_and_ack exchange. We are awaiting the appropriate revoke_and_ack's from the remote for
370+
/// the removal from its commitment transaction.
371+
AwaitingRemoteRevokeToRemoveFailure,
372+
}
373+
374+
impl From<&OutboundHTLCState> for OutboundHTLCStateDetails {
375+
fn from(state: &OutboundHTLCState) -> OutboundHTLCStateDetails {
376+
match state {
377+
OutboundHTLCState::LocalAnnounced(_) =>
378+
OutboundHTLCStateDetails::AwaitingRemoteRevokeToAdd,
379+
OutboundHTLCState::Committed =>
380+
OutboundHTLCStateDetails::Committed,
381+
// RemoteRemoved states are ignored as the state is transient and the remote has not committed to
382+
// the state yet.
383+
OutboundHTLCState::RemoteRemoved(_) =>
384+
OutboundHTLCStateDetails::Committed,
385+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Success(_)) =>
386+
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess,
387+
OutboundHTLCState::AwaitingRemoteRevokeToRemove(OutboundHTLCOutcome::Failure(_)) =>
388+
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFailure,
389+
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Success(_)) =>
390+
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveSuccess,
391+
OutboundHTLCState::AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome::Failure(_)) =>
392+
OutboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFailure,
393+
}
394+
}
395+
}
396+
397+
impl_writeable_tlv_based_enum!(OutboundHTLCStateDetails,
398+
(0, AwaitingRemoteRevokeToAdd) => {},
399+
(2, Committed) => {},
400+
(4, AwaitingRemoteRevokeToRemoveSuccess) => {},
401+
(6, AwaitingRemoteRevokeToRemoveFailure) => {};
402+
);
403+
202404
#[derive(Clone)]
203405
#[cfg_attr(test, derive(Debug, PartialEq))]
204406
enum OutboundHTLCOutcome {
@@ -237,6 +439,49 @@ struct OutboundHTLCOutput {
237439
skimmed_fee_msat: Option<u64>,
238440
}
239441

442+
/// Exposes details around pending outbound HTLCs.
443+
#[derive(Clone, Debug, PartialEq)]
444+
pub struct OutboundHTLCDetails {
445+
/// The HTLC ID.
446+
/// The IDs are incremented by 1 starting from 0 for each offered HTLC.
447+
/// They are unique per channel and inbound/outbound direction, unless an HTLC was only announced
448+
/// and not part of any commitment transaction.
449+
///
450+
/// Not present when we are awaiting a remote revocation and the HTLC is not added yet.
451+
pub htlc_id: Option<u64>,
452+
/// The amount in msat.
453+
pub amount_msat: u64,
454+
/// The block height at which this HTLC expires.
455+
pub cltv_expiry: u32,
456+
/// The payment hash.
457+
pub payment_hash: PaymentHash,
458+
/// The state of the HTLC in the state machine.
459+
/// Determines on which commitment transactions the HTLC is included and what message the HTLC is
460+
/// waiting for to advance to the next state.
461+
/// See [OutboundHTLCStateDetails] for information on the specific states.
462+
pub state: OutboundHTLCStateDetails,
463+
/// The extra fee being skimmed off the top of this HTLC.
464+
pub skimmed_fee_msat: Option<u64>,
465+
/// Whether the HTLC has an output below the local dust limit. If so, the output will be trimmed
466+
/// from the local commitment transaction and added to the commitment transaction fee.
467+
/// This takes into account the second-stage HTLC transactions as well.
468+
///
469+
/// When the local commitment transaction is broadcasted as part of a unilateral closure,
470+
/// the value of this HTLC will therefore not be claimable but instead burned as a transaction
471+
/// fee.
472+
pub is_dust: bool,
473+
}
474+
475+
impl_writeable_tlv_based!(OutboundHTLCDetails, {
476+
(0, htlc_id, required),
477+
(2, amount_msat, required),
478+
(4, cltv_expiry, required),
479+
(6, payment_hash, required),
480+
(8, state, required),
481+
(10, skimmed_fee_msat, required),
482+
(12, is_dust, required),
483+
});
484+
240485
/// See AwaitingRemoteRevoke ChannelState for more info
241486
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
242487
enum HTLCUpdateAwaitingACK {
@@ -1966,6 +2211,90 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
19662211
stats
19672212
}
19682213

2214+
/// Returns information on all pending inbound HTLCs.
2215+
pub fn get_pending_inbound_htlc_details(&self) -> Vec<InboundHTLCDetails> {
2216+
let mut holding_cell_states = HashMap::new();
2217+
for holding_cell_update in self.holding_cell_htlc_updates.iter() {
2218+
match holding_cell_update {
2219+
HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
2220+
holding_cell_states.insert(
2221+
htlc_id,
2222+
InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFulfill,
2223+
);
2224+
},
2225+
HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
2226+
holding_cell_states.insert(
2227+
htlc_id,
2228+
InboundHTLCStateDetails::AwaitingRemoteRevokeToRemoveFail,
2229+
);
2230+
},
2231+
_ => {},
2232+
}
2233+
}
2234+
let mut inbound_details = Vec::new();
2235+
let htlc_success_dust_limit = if self.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
2236+
0
2237+
} else {
2238+
let dust_buffer_feerate = self.get_dust_buffer_feerate(None) as u64;
2239+
dust_buffer_feerate * htlc_success_tx_weight(self.get_channel_type()) / 1000
2240+
};
2241+
let holder_dust_limit_success_sat = htlc_success_dust_limit + self.holder_dust_limit_satoshis;
2242+
for htlc in self.pending_inbound_htlcs.iter() {
2243+
inbound_details.push(InboundHTLCDetails{
2244+
htlc_id: htlc.htlc_id,
2245+
amount_msat: htlc.amount_msat,
2246+
cltv_expiry: htlc.cltv_expiry,
2247+
payment_hash: htlc.payment_hash,
2248+
state: holding_cell_states.remove(&htlc.htlc_id).unwrap_or((&htlc.state).into()),
2249+
is_dust: htlc.amount_msat / 1000 < holder_dust_limit_success_sat,
2250+
});
2251+
}
2252+
inbound_details
2253+
}
2254+
2255+
/// Returns information on all pending outbound HTLCs.
2256+
pub fn get_pending_outbound_htlc_details(&self) -> Vec<OutboundHTLCDetails> {
2257+
let mut outbound_details = Vec::new();
2258+
let htlc_timeout_dust_limit = if self.get_channel_type().supports_anchors_zero_fee_htlc_tx() {
2259+
0
2260+
} else {
2261+
let dust_buffer_feerate = self.get_dust_buffer_feerate(None) as u64;
2262+
dust_buffer_feerate * htlc_success_tx_weight(self.get_channel_type()) / 1000
2263+
};
2264+
let holder_dust_limit_timeout_sat = htlc_timeout_dust_limit + self.holder_dust_limit_satoshis;
2265+
for htlc in self.pending_outbound_htlcs.iter() {
2266+
outbound_details.push(OutboundHTLCDetails{
2267+
htlc_id: Some(htlc.htlc_id),
2268+
amount_msat: htlc.amount_msat,
2269+
cltv_expiry: htlc.cltv_expiry,
2270+
payment_hash: htlc.payment_hash,
2271+
skimmed_fee_msat: htlc.skimmed_fee_msat,
2272+
state: (&htlc.state).into(),
2273+
is_dust: htlc.amount_msat / 1000 < holder_dust_limit_timeout_sat,
2274+
});
2275+
}
2276+
for holding_cell_update in self.holding_cell_htlc_updates.iter() {
2277+
if let HTLCUpdateAwaitingACK::AddHTLC {
2278+
amount_msat,
2279+
cltv_expiry,
2280+
payment_hash,
2281+
skimmed_fee_msat,
2282+
..
2283+
} = *holding_cell_update {
2284+
outbound_details.push(OutboundHTLCDetails{
2285+
htlc_id: None,
2286+
amount_msat: amount_msat,
2287+
cltv_expiry: cltv_expiry,
2288+
payment_hash: payment_hash,
2289+
skimmed_fee_msat: skimmed_fee_msat,
2290+
state: OutboundHTLCStateDetails::AwaitingRemoteRevokeToAdd,
2291+
is_dust: amount_msat / 1000 < holder_dust_limit_timeout_sat,
2292+
});
2293+
}
2294+
}
2295+
outbound_details
2296+
}
2297+
19692298
/// Get the available balances, see [`AvailableBalances`]'s fields for more info.
19702299
/// Doesn't bother handling the
19712300
/// if-we-removed-it-already-but-haven't-fully-resolved-they-can-still-send-an-inbound-HTLC

0 commit comments

Comments
 (0)