Skip to content

Commit 15c709d

Browse files
authored
Merge pull request #3089 from TheBlueMatt/2024-06-channel_state-module
Move the public channel state API into a new module
2 parents 7f47d67 + 2d6d5cc commit 15c709d

File tree

16 files changed

+594
-542
lines changed

16 files changed

+594
-542
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ use lightning::sign::{KeyMaterial, InMemorySigner, Recipient, EntropySource, Nod
4545
use lightning::events;
4646
use lightning::events::MessageSendEventsProvider;
4747
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
48-
use lightning::ln::channelmanager::{ChainParameters, ChannelDetails, ChannelManager, PaymentSendFailure, ChannelManagerReadArgs, PaymentId, RecipientOnionFields};
48+
use lightning::ln::channel_state::ChannelDetails;
49+
use lightning::ln::channelmanager::{ChainParameters,ChannelManager, PaymentSendFailure, ChannelManagerReadArgs, PaymentId, RecipientOnionFields};
4950
use lightning::ln::channel::FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
5051
use lightning::ln::msgs::{self, CommitmentUpdate, ChannelMessageHandler, DecodeError, UpdateAddHTLC, Init};
5152
use lightning::ln::script::ShutdownScript;

fuzz/src/full_stack.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ use lightning::chain::transaction::OutPoint;
4141
use lightning::sign::{InMemorySigner, Recipient, KeyMaterial, EntropySource, NodeSigner, SignerProvider};
4242
use lightning::events::Event;
4343
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
44-
use lightning::ln::channelmanager::{ChainParameters, ChannelDetails, ChannelManager, PaymentId, RecipientOnionFields, Retry, InterceptId};
44+
use lightning::ln::channel_state::ChannelDetails;
45+
use lightning::ln::channelmanager::{ChainParameters, ChannelManager, PaymentId, RecipientOnionFields, Retry, InterceptId};
4546
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor,IgnoringMessageHandler};
4647
use lightning::ln::msgs::{self, DecodeError};
4748
use lightning::ln::script::ShutdownScript;

fuzz/src/msg_targets/gen_target.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ GEN_TEST lightning::ln::msgs::ErrorMessage test_msg_hole ", 32, 2"
4545
GEN_TEST lightning::ln::msgs::WarningMessage test_msg_hole ", 32, 2"
4646
GEN_TEST lightning::ln::msgs::ChannelUpdate test_msg_hole ", 108, 1"
4747

48-
GEN_TEST lightning::ln::channelmanager::ChannelDetails test_msg_simple ""
48+
GEN_TEST lightning::ln::channel_state::ChannelDetails test_msg_simple ""
4949

5050
GEN_TEST lightning::ln::msgs::OpenChannelV2 test_msg_simple ""
5151
GEN_TEST lightning::ln::msgs::AcceptChannelV2 test_msg_simple ""

fuzz/src/msg_targets/msg_channel_details.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ use crate::utils::test_logger;
1515

1616
#[inline]
1717
pub fn msg_channel_details_test<Out: test_logger::Output>(data: &[u8], _out: Out) {
18-
test_msg_simple!(lightning::ln::channelmanager::ChannelDetails, data);
18+
test_msg_simple!(lightning::ln::channel_state::ChannelDetails, data);
1919
}
2020

2121
#[no_mangle]
2222
pub extern "C" fn msg_channel_details_run(data: *const u8, datalen: usize) {
2323
let data = unsafe { std::slice::from_raw_parts(data, datalen) };
24-
test_msg_simple!(lightning::ln::channelmanager::ChannelDetails, data);
24+
test_msg_simple!(lightning::ln::channel_state::ChannelDetails, data);
2525
}

fuzz/src/router.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ use bitcoin::blockdata::transaction::TxOut;
1515
use lightning::blinded_path::{BlindedHop, BlindedPath, IntroductionNode};
1616
use lightning::chain::transaction::OutPoint;
1717
use lightning::ln::ChannelId;
18-
use lightning::ln::channelmanager::{self, ChannelDetails, ChannelCounterparty};
18+
use lightning::ln::channel_state::{ChannelDetails, ChannelCounterparty, ChannelShutdownState};
19+
use lightning::ln::channelmanager;
1920
use lightning::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
2021
use lightning::ln::msgs;
2122
use lightning::offers::invoice::BlindedPayInfo;
@@ -244,7 +245,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
244245
inbound_htlc_maximum_msat: None,
245246
config: None,
246247
feerate_sat_per_1000_weight: None,
247-
channel_shutdown_state: Some(channelmanager::ChannelShutdownState::NotShuttingDown),
248+
channel_shutdown_state: Some(ChannelShutdownState::NotShuttingDown),
248249
pending_inbound_htlcs: Vec::new(),
249250
pending_outbound_htlcs: Vec::new(),
250251
});

lightning-invoice/src/utils.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ use lightning::chain;
99
use lightning::chain::chaininterface::{BroadcasterInterface, FeeEstimator};
1010
use lightning::sign::{Recipient, NodeSigner, SignerProvider, EntropySource};
1111
use lightning::ln::types::{PaymentHash, PaymentSecret};
12-
use lightning::ln::channelmanager::{ChannelDetails, ChannelManager, MIN_FINAL_CLTV_EXPIRY_DELTA};
12+
use lightning::ln::channel_state::ChannelDetails;
13+
use lightning::ln::channelmanager::{ChannelManager, MIN_FINAL_CLTV_EXPIRY_DELTA};
1314
use lightning::ln::channelmanager::{PhantomRouteHints, MIN_CLTV_EXPIRY_DELTA};
1415
use lightning::ln::inbound_payment::{create, create_from_hash, ExpandedKey};
1516
use lightning::routing::gossip::RoutingFees;

lightning/src/blinded_path/payment.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::blinded_path::BlindedHop;
1717
use crate::blinded_path::utils;
1818
use crate::io;
1919
use crate::ln::types::PaymentSecret;
20-
use crate::ln::channelmanager::CounterpartyForwardingInfo;
20+
use crate::ln::channel_state::CounterpartyForwardingInfo;
2121
use crate::ln::features::BlindedHopFeatures;
2222
use crate::ln::msgs::DecodeError;
2323
use crate::offers::invoice::BlindedPayInfo;

lightning/src/chain/chainmonitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ use crate::events::{Event, EventHandler};
3838
use crate::util::logger::{Logger, WithContext};
3939
use crate::util::errors::APIError;
4040
use crate::util::wakers::{Future, Notifier};
41-
use crate::ln::channelmanager::ChannelDetails;
41+
use crate::ln::channel_state::ChannelDetails;
4242

4343
use crate::prelude::*;
4444
use crate::sync::{RwLock, RwLockReadGuard, Mutex, MutexGuard};

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::{ChannelShutdownState, 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};
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)