Skip to content

Commit 41fac75

Browse files
authored
Merge pull request #3092 from TheBlueMatt/2024-06-channel_state-module-123
[0.0.123-bindings] Move the public channel state API into a new module
2 parents 9b523d9 + 36c6586 commit 41fac75

File tree

16 files changed

+739
-684
lines changed

16 files changed

+739
-684
lines changed

fuzz/src/chanmon_consistency.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ use lightning::sign::{KeyMaterial, InMemorySigner, Recipient, EntropySource, Nod
4141
use lightning::events;
4242
use lightning::events::MessageSendEventsProvider;
4343
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
44-
use lightning::ln::channelmanager::{ChainParameters, ChannelDetails, ChannelManager, ChannelManagerReadArgs, PaymentId};
44+
use lightning::ln::channel_state::ChannelDetails;
45+
use lightning::ln::channelmanager::{ChainParameters, ChannelManager, ChannelManagerReadArgs, PaymentId};
4546
use lightning::ln::outbound_payment::{RecipientOnionFields, PaymentSendFailure};
4647
use lightning::ln::channel::FEE_SPIKE_BUFFER_FEE_INCREASE_MULTIPLE;
4748
use lightning::ln::msgs::{self, CommitmentUpdate, ChannelMessageHandler, DecodeError, UpdateAddHTLC, Init};

fuzz/src/full_stack.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ use lightning::chain::transaction::OutPoint;
3737
use lightning::sign::{InMemorySigner, Recipient, KeyMaterial, EntropySource, NodeSigner, SignerProvider};
3838
use lightning::events::Event;
3939
use lightning::ln::{ChannelId, PaymentHash, PaymentPreimage, PaymentSecret};
40-
use lightning::ln::channelmanager::{ChainParameters, ChannelDetails, ChannelManager, PaymentId, InterceptId};
40+
use lightning::ln::channel_state::ChannelDetails;
41+
use lightning::ln::channelmanager::{ChainParameters, ChannelManager, PaymentId, InterceptId};
4142
use lightning::ln::outbound_payment::{RecipientOnionFields, Retry};
4243
use lightning::ln::peer_handler::{MessageHandler,PeerManager,SocketDescriptor,IgnoringMessageHandler};
4344
use lightning::ln::msgs::{self, DecodeError};

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
@@ -14,7 +14,8 @@ use bitcoin::blockdata::transaction::TxOut;
1414
use lightning::blinded_path::{BlindedHop, BlindedPath, IntroductionNode};
1515
use lightning::chain::transaction::OutPoint;
1616
use lightning::ln::ChannelId;
17-
use lightning::ln::channelmanager::{self, ChannelDetails, ChannelCounterparty};
17+
use lightning::ln::channel_state::{ChannelDetails, ChannelCounterparty, ChannelShutdownState};
18+
use lightning::ln::channelmanager;
1819
use lightning::ln::features::{BlindedHopFeatures, Bolt12InvoiceFeatures};
1920
use lightning::ln::msgs;
2021
use lightning::offers::invoice::BlindedPayInfo;
@@ -243,7 +244,7 @@ pub fn do_test<Out: test_logger::Output>(data: &[u8], out: Out) {
243244
inbound_htlc_maximum_msat: None,
244245
config: None,
245246
feerate_sat_per_1000_weight: None,
246-
channel_shutdown_state: Some(channelmanager::ChannelShutdownState::NotShuttingDown),
247+
channel_shutdown_state: Some(ChannelShutdownState::NotShuttingDown),
247248
pending_inbound_htlcs: Vec::new(),
248249
pending_outbound_htlcs: Vec::new(),
249250
});

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
@@ -8,7 +8,7 @@ use crate::blinded_path::BlindedHop;
88
use crate::blinded_path::utils;
99
use crate::io;
1010
use crate::ln::types::PaymentSecret;
11-
use crate::ln::channelmanager::CounterpartyForwardingInfo;
11+
use crate::ln::channel_state::CounterpartyForwardingInfo;
1212
use crate::ln::features::BlindedHopFeatures;
1313
use crate::ln::msgs::DecodeError;
1414
use crate::offers::invoice::BlindedPayInfo;

lightning/src/chain/chainmonitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use crate::util::atomic_counter::AtomicCounter;
3939
use crate::util::logger::{Logger, WithContext};
4040
use crate::util::errors::APIError;
4141
use crate::util::wakers::{Future, Notifier};
42-
use crate::ln::channelmanager::ChannelDetails;
42+
use crate::ln::channel_state::ChannelDetails;
4343

4444
use crate::prelude::*;
4545
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
@@ -29,7 +29,8 @@ use crate::ln::features::{ChannelTypeFeatures, InitFeatures};
2929
use crate::ln::msgs;
3030
use crate::ln::msgs::DecodeError;
3131
use crate::ln::script::{self, ShutdownScript};
32-
use crate::ln::channelmanager::{self, CounterpartyForwardingInfo, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT, ChannelShutdownState};
32+
use crate::ln::channel_state::{ChannelShutdownState, CounterpartyForwardingInfo, InboundHTLCDetails, InboundHTLCStateDetails, OutboundHTLCDetails, OutboundHTLCStateDetails};
33+
use crate::ln::channelmanager::{self, PendingHTLCStatus, HTLCSource, SentHTLCId, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
3334
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};
3435
use crate::ln::chan_utils;
3536
use crate::ln::onion_utils::HTLCFailReason;
@@ -185,45 +186,6 @@ enum InboundHTLCState {
185186
LocalRemoved(InboundHTLCRemovalReason),
186187
}
187188

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

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

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

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

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

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

0 commit comments

Comments
 (0)