Skip to content

Commit f7cbaf1

Browse files
committed
Move the public channel state API into a new module
Our "what is the channel's current state" structs have slowly grown to be rather nontrivial, and now include eight structs with many fields. Thus, it makes sense to pull them out of `ln::channelmanager` and into their own module. This also makes things easier for the C bindings which don't support `pub use` from a private module.
1 parent 7326334 commit f7cbaf1

File tree

4 files changed

+555
-522
lines changed

4 files changed

+555
-522
lines changed

lightning/src/ln/channel.rs

Lines changed: 2 additions & 189 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@ use crate::ln::features::{ChannelTypeFeatures, InitFeatures};
3030
use crate::ln::msgs;
3131
use crate::ln::msgs::DecodeError;
3232
use crate::ln::script::{self, ShutdownScript};
33-
use crate::ln::channelmanager::{self, CounterpartyForwardingInfo, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT, ChannelShutdownState};
33+
use crate::ln::channel_state::{CounterpartyForwardingInfo, InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails, OutboundHTLCStateDetails};
34+
use crate::ln::channelmanager::{self, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT, ChannelShutdownState};
3435
use crate::ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, htlc_success_tx_weight, htlc_timeout_tx_weight, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor, ClosingTransaction};
3536
use crate::ln::chan_utils;
3637
use crate::ln::onion_utils::HTLCFailReason;
@@ -186,45 +187,6 @@ enum InboundHTLCState {
186187
LocalRemoved(InboundHTLCRemovalReason),
187188
}
188189

189-
/// Exposes the state of pending inbound HTLCs.
190-
///
191-
/// At a high level, an HTLC being forwarded from one Lightning node to another Lightning node goes
192-
/// through the following states in the state machine:
193-
/// - Announced for addition by the originating node through the update_add_htlc message.
194-
/// - Added to the commitment transaction of the receiving node and originating node in turn
195-
/// through the exchange of commitment_signed and revoke_and_ack messages.
196-
/// - Announced for resolution (fulfillment or failure) by the receiving node through either one of
197-
/// the update_fulfill_htlc, update_fail_htlc, and update_fail_malformed_htlc messages.
198-
/// - Removed from the commitment transaction of the originating node and receiving node in turn
199-
/// through the exchange of commitment_signed and revoke_and_ack messages.
200-
///
201-
/// This can be used to inspect what next message an HTLC is waiting for to advance its state.
202-
#[derive(Clone, Debug, PartialEq)]
203-
pub enum InboundHTLCStateDetails {
204-
/// We have added this HTLC in our commitment transaction by receiving commitment_signed and
205-
/// returning revoke_and_ack. We are awaiting the appropriate revoke_and_ack's from the remote
206-
/// before this HTLC is included on the remote commitment transaction.
207-
AwaitingRemoteRevokeToAdd,
208-
/// This HTLC has been included in the commitment_signed and revoke_and_ack messages on both sides
209-
/// and is included in both commitment transactions.
210-
///
211-
/// This HTLC is now safe to either forward or be claimed as a payment by us. The HTLC will
212-
/// remain in this state until the forwarded upstream HTLC has been resolved and we resolve this
213-
/// HTLC correspondingly, or until we claim it as a payment. If it is part of a multipart
214-
/// payment, it will only be claimed together with other required parts.
215-
Committed,
216-
/// We have received the preimage for this HTLC and it is being removed by fulfilling it with
217-
/// update_fulfill_htlc. This HTLC is still on both commitment transactions, but we are awaiting
218-
/// the appropriate revoke_and_ack's from the remote before this HTLC is removed from the remote
219-
/// commitment transaction after update_fulfill_htlc.
220-
AwaitingRemoteRevokeToRemoveFulfill,
221-
/// The HTLC is being removed by failing it with update_fail_htlc or update_fail_malformed_htlc.
222-
/// This HTLC is still on both commitment transactions, but we are awaiting the appropriate
223-
/// revoke_and_ack's from the remote before this HTLC is removed from the remote commitment
224-
/// transaction.
225-
AwaitingRemoteRevokeToRemoveFail,
226-
}
227-
228190
impl From<&InboundHTLCState> for Option<InboundHTLCStateDetails> {
229191
fn from(state: &InboundHTLCState) -> Option<InboundHTLCStateDetails> {
230192
match state {
@@ -245,13 +207,6 @@ impl From<&InboundHTLCState> for Option<InboundHTLCStateDetails> {
245207
}
246208
}
247209

248-
impl_writeable_tlv_based_enum_upgradable!(InboundHTLCStateDetails,
249-
(0, AwaitingRemoteRevokeToAdd) => {},
250-
(2, Committed) => {},
251-
(4, AwaitingRemoteRevokeToRemoveFulfill) => {},
252-
(6, AwaitingRemoteRevokeToRemoveFail) => {};
253-
);
254-
255210
struct InboundHTLCOutput {
256211
htlc_id: u64,
257212
amount_msat: u64,
@@ -260,53 +215,6 @@ struct InboundHTLCOutput {
260215
state: InboundHTLCState,
261216
}
262217

263-
/// Exposes details around pending inbound HTLCs.
264-
#[derive(Clone, Debug, PartialEq)]
265-
pub struct InboundHTLCDetails {
266-
/// The HTLC ID.
267-
/// The IDs are incremented by 1 starting from 0 for each offered HTLC.
268-
/// They are unique per channel and inbound/outbound direction, unless an HTLC was only announced
269-
/// and not part of any commitment transaction.
270-
pub htlc_id: u64,
271-
/// The amount in msat.
272-
pub amount_msat: u64,
273-
/// The block height at which this HTLC expires.
274-
pub cltv_expiry: u32,
275-
/// The payment hash.
276-
pub payment_hash: PaymentHash,
277-
/// The state of the HTLC in the state machine.
278-
///
279-
/// Determines on which commitment transactions the HTLC is included and what message the HTLC is
280-
/// waiting for to advance to the next state.
281-
///
282-
/// See [`InboundHTLCStateDetails`] for information on the specific states.
283-
///
284-
/// LDK will always fill this field in, but when downgrading to prior versions of LDK, new
285-
/// states may result in `None` here.
286-
pub state: Option<InboundHTLCStateDetails>,
287-
/// Whether the HTLC has an output below the local dust limit. If so, the output will be trimmed
288-
/// from the local commitment transaction and added to the commitment transaction fee.
289-
/// For non-anchor channels, this takes into account the cost of the second-stage HTLC
290-
/// transactions as well.
291-
///
292-
/// When the local commitment transaction is broadcasted as part of a unilateral closure,
293-
/// the value of this HTLC will therefore not be claimable but instead burned as a transaction
294-
/// fee.
295-
///
296-
/// Note that dust limits are specific to each party. An HTLC can be dust for the local
297-
/// commitment transaction but not for the counterparty's commitment transaction and vice versa.
298-
pub is_dust: bool,
299-
}
300-
301-
impl_writeable_tlv_based!(InboundHTLCDetails, {
302-
(0, htlc_id, required),
303-
(2, amount_msat, required),
304-
(4, cltv_expiry, required),
305-
(6, payment_hash, required),
306-
(7, state, upgradable_option),
307-
(8, is_dust, required),
308-
});
309-
310218
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
311219
enum OutboundHTLCState {
312220
/// Added by us and included in a commitment_signed (if we were AwaitingRemoteRevoke when we
@@ -340,42 +248,6 @@ enum OutboundHTLCState {
340248
AwaitingRemovedRemoteRevoke(OutboundHTLCOutcome),
341249
}
342250

343-
/// Exposes the state of pending outbound HTLCs.
344-
///
345-
/// At a high level, an HTLC being forwarded from one Lightning node to another Lightning node goes
346-
/// through the following states in the state machine:
347-
/// - Announced for addition by the originating node through the update_add_htlc message.
348-
/// - Added to the commitment transaction of the receiving node and originating node in turn
349-
/// through the exchange of commitment_signed and revoke_and_ack messages.
350-
/// - Announced for resolution (fulfillment or failure) by the receiving node through either one of
351-
/// the update_fulfill_htlc, update_fail_htlc, and update_fail_malformed_htlc messages.
352-
/// - Removed from the commitment transaction of the originating node and receiving node in turn
353-
/// through the exchange of commitment_signed and revoke_and_ack messages.
354-
///
355-
/// This can be used to inspect what next message an HTLC is waiting for to advance its state.
356-
#[derive(Clone, Debug, PartialEq)]
357-
pub enum OutboundHTLCStateDetails {
358-
/// We are awaiting the appropriate revoke_and_ack's from the remote before the HTLC is added
359-
/// on the remote's commitment transaction after update_add_htlc.
360-
AwaitingRemoteRevokeToAdd,
361-
/// The HTLC has been added to the remote's commitment transaction by sending commitment_signed
362-
/// and receiving revoke_and_ack in return.
363-
///
364-
/// The HTLC will remain in this state until the remote node resolves the HTLC, or until we
365-
/// unilaterally close the channel due to a timeout with an uncooperative remote node.
366-
Committed,
367-
/// The HTLC has been fulfilled successfully by the remote with a preimage in update_fulfill_htlc,
368-
/// and we removed the HTLC from our commitment transaction by receiving commitment_signed and
369-
/// returning revoke_and_ack. We are awaiting the appropriate revoke_and_ack's from the remote
370-
/// for the removal from its commitment transaction.
371-
AwaitingRemoteRevokeToRemoveSuccess,
372-
/// The HTLC has been failed by the remote with update_fail_htlc or update_fail_malformed_htlc,
373-
/// and we removed the HTLC from our commitment transaction by receiving commitment_signed and
374-
/// returning revoke_and_ack. We are awaiting the appropriate revoke_and_ack's from the remote
375-
/// for the removal from its commitment transaction.
376-
AwaitingRemoteRevokeToRemoveFailure,
377-
}
378-
379251
impl From<&OutboundHTLCState> for OutboundHTLCStateDetails {
380252
fn from(state: &OutboundHTLCState) -> OutboundHTLCStateDetails {
381253
match state {
@@ -399,13 +271,6 @@ impl From<&OutboundHTLCState> for OutboundHTLCStateDetails {
399271
}
400272
}
401273

402-
impl_writeable_tlv_based_enum_upgradable!(OutboundHTLCStateDetails,
403-
(0, AwaitingRemoteRevokeToAdd) => {},
404-
(2, Committed) => {},
405-
(4, AwaitingRemoteRevokeToRemoveSuccess) => {},
406-
(6, AwaitingRemoteRevokeToRemoveFailure) => {};
407-
);
408-
409274
#[derive(Clone)]
410275
#[cfg_attr(test, derive(Debug, PartialEq))]
411276
enum OutboundHTLCOutcome {
@@ -444,58 +309,6 @@ struct OutboundHTLCOutput {
444309
skimmed_fee_msat: Option<u64>,
445310
}
446311

447-
/// Exposes details around pending outbound HTLCs.
448-
#[derive(Clone, Debug, PartialEq)]
449-
pub struct OutboundHTLCDetails {
450-
/// The HTLC ID.
451-
/// The IDs are incremented by 1 starting from 0 for each offered HTLC.
452-
/// They are unique per channel and inbound/outbound direction, unless an HTLC was only announced
453-
/// and not part of any commitment transaction.
454-
///
455-
/// Not present when we are awaiting a remote revocation and the HTLC is not added yet.
456-
pub htlc_id: Option<u64>,
457-
/// The amount in msat.
458-
pub amount_msat: u64,
459-
/// The block height at which this HTLC expires.
460-
pub cltv_expiry: u32,
461-
/// The payment hash.
462-
pub payment_hash: PaymentHash,
463-
/// The state of the HTLC in the state machine.
464-
///
465-
/// Determines on which commitment transactions the HTLC is included and what message the HTLC is
466-
/// waiting for to advance to the next state.
467-
///
468-
/// See [`OutboundHTLCStateDetails`] for information on the specific states.
469-
///
470-
/// LDK will always fill this field in, but when downgrading to prior versions of LDK, new
471-
/// states may result in `None` here.
472-
pub state: Option<OutboundHTLCStateDetails>,
473-
/// The extra fee being skimmed off the top of this HTLC.
474-
pub skimmed_fee_msat: Option<u64>,
475-
/// Whether the HTLC has an output below the local dust limit. If so, the output will be trimmed
476-
/// from the local commitment transaction and added to the commitment transaction fee.
477-
/// For non-anchor channels, this takes into account the cost of the second-stage HTLC
478-
/// transactions as well.
479-
///
480-
/// When the local commitment transaction is broadcasted as part of a unilateral closure,
481-
/// the value of this HTLC will therefore not be claimable but instead burned as a transaction
482-
/// fee.
483-
///
484-
/// Note that dust limits are specific to each party. An HTLC can be dust for the local
485-
/// commitment transaction but not for the counterparty's commitment transaction and vice versa.
486-
pub is_dust: bool,
487-
}
488-
489-
impl_writeable_tlv_based!(OutboundHTLCDetails, {
490-
(0, htlc_id, required),
491-
(2, amount_msat, required),
492-
(4, cltv_expiry, required),
493-
(6, payment_hash, required),
494-
(7, state, upgradable_option),
495-
(8, skimmed_fee_msat, required),
496-
(10, is_dust, required),
497-
});
498-
499312
/// See AwaitingRemoteRevoke ChannelState for more info
500313
#[cfg_attr(test, derive(Clone, Debug, PartialEq))]
501314
enum HTLCUpdateAwaitingACK {

0 commit comments

Comments
 (0)