Skip to content

Commit ab09472

Browse files
committed
f wip: move flags to FundingNegotiated state and make FundingNegotiated a Funded state
1 parent b60215b commit ab09472

File tree

2 files changed

+50
-44
lines changed

2 files changed

+50
-44
lines changed

lightning/src/ln/channel.rs

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -434,12 +434,6 @@ macro_rules! define_state_flags {
434434
is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent);
435435
define_state_flags!($flag_type, FundedStateFlags::LOCAL_SHUTDOWN_SENT,
436436
is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent);
437-
define_state_flags!($flag_type, FundedStateFlags::INTERACTIVE_SIGNING,
438-
is_interactive_signing, set_interactive_signing, clear_interactive_signing);
439-
define_state_flags!($flag_type, FundedStateFlags::THEIR_TX_SIGNATURES_SENT,
440-
is_their_tx_signatures_sent, set_their_tx_signatures_sent, clear_their_tx_signatures_sent);
441-
define_state_flags!($flag_type, FundedStateFlags::OUR_TX_SIGNATURES_SENT,
442-
is_our_tx_signatures_sent, set_our_tx_signatures_sent, clear_our_tx_signatures_sent);
443437

444438
impl core::ops::BitOr<FundedStateFlags> for $flag_type {
445439
type Output = Self;
@@ -506,16 +500,7 @@ define_state_flags!(
506500
is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent),
507501
("Indicates we sent a `shutdown` message. At this point, we may not add any new HTLCs to \
508502
the channel.", LOCAL_SHUTDOWN_SENT, state_flags::LOCAL_SHUTDOWN_SENT,
509-
is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent),
510-
("Indicates we have an active interactive signing session for an interactive transaction",
511-
INTERACTIVE_SIGNING, state_flags::INTERACTIVE_SIGNING,
512-
is_interactive_signing, set_interactive_signing, clear_interactive_singing),
513-
("Indicates they sent us a `tx_signatures` message.",
514-
THEIR_TX_SIGNATURES_SENT, state_flags::THEIR_TX_SIGNATURES_SENT,
515-
is_their_tx_signatures_sent, set_their_tx_signatures_sent, clear_their_tx_signatures_sent),
516-
("Indicates we sent them a `tx_signatures` message.",
517-
OUR_TX_SIGNATURES_SENT, state_flags::OUR_TX_SIGNATURES_SENT,
518-
is_our_tx_signatures_sent, set_our_tx_signatures_sent, clear_our_tx_signatures_sent)
503+
is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent)
519504
]
520505
);
521506

@@ -529,6 +514,21 @@ define_state_flags!(
529514
]
530515
);
531516

517+
define_state_flags!(
518+
"Flags that only apply to [`ChannelState::FundingNegotiated`].",
519+
FUNDED_STATE, FundingNegotiatedFlags, [
520+
("Indicates we have an active interactive signing session for an interactive transaction",
521+
INTERACTIVE_SIGNING, state_flags::INTERACTIVE_SIGNING,
522+
is_interactive_signing, set_interactive_signing, clear_interactive_signing),
523+
("Indicates they sent us a `tx_signatures` message.",
524+
THEIR_TX_SIGNATURES_SENT, state_flags::THEIR_TX_SIGNATURES_SENT,
525+
is_their_tx_signatures_sent, set_their_tx_signatures_sent, clear_their_tx_signatures_sent),
526+
("Indicates we sent them a `tx_signatures` message.",
527+
OUR_TX_SIGNATURES_SENT, state_flags::OUR_TX_SIGNATURES_SENT,
528+
is_our_tx_signatures_sent, set_our_tx_signatures_sent, clear_our_tx_signatures_sent)
529+
]
530+
);
531+
532532
define_state_flags!(
533533
"Flags that only apply to [`ChannelState::AwaitingChannelReady`].",
534534
FUNDED_STATE, AwaitingChannelReadyFlags, [
@@ -588,7 +588,7 @@ enum ChannelState {
588588
/// `AwaitingChannelReady`. Note that this is nonsense for an inbound channel as we immediately generate
589589
/// `funding_signed` upon receipt of `funding_created`, so simply skip this state. For dual-funded (V2)
590590
/// channels, this state is also skipped.
591-
FundingNegotiated,
591+
FundingNegotiated(FundingNegotiatedFlags),
592592
/// We've received/sent `funding_created` and `funding_signed` and are thus now waiting on the
593593
/// funding transaction to confirm.
594594
AwaitingChannelReady(AwaitingChannelReadyFlags),
@@ -631,7 +631,7 @@ macro_rules! impl_state_flag {
631631
}
632632
};
633633
($get: ident, $set: ident, $clear: ident, FUNDED_STATES) => {
634-
impl_state_flag!($get, $set, $clear, [AwaitingChannelReady, ChannelReady]);
634+
impl_state_flag!($get, $set, $clear, [FundingNegotiated, AwaitingChannelReady, ChannelReady]);
635635
};
636636
($get: ident, $set: ident, $clear: ident, $state: ident) => {
637637
impl_state_flag!($get, $set, $clear, [$state]);
@@ -641,10 +641,12 @@ macro_rules! impl_state_flag {
641641
impl ChannelState {
642642
fn from_u32(state: u32) -> Result<Self, ()> {
643643
match state {
644-
state_flags::FUNDING_NEGOTIATED => Ok(ChannelState::FundingNegotiated),
645644
state_flags::SHUTDOWN_COMPLETE => Ok(ChannelState::ShutdownComplete),
646645
val => {
647-
if val & state_flags::AWAITING_CHANNEL_READY == state_flags::AWAITING_CHANNEL_READY {
646+
if val & state_flags::FUNDING_NEGOTIATED == state_flags::FUNDING_NEGOTIATED {
647+
FundingNegotiatedFlags::from_u32(val & !state_flags::FUNDING_NEGOTIATED)
648+
.map(|flags| ChannelState::FundingNegotiated(flags))
649+
} else if val & state_flags::AWAITING_CHANNEL_READY == state_flags::AWAITING_CHANNEL_READY {
648650
AwaitingChannelReadyFlags::from_u32(val & !state_flags::AWAITING_CHANNEL_READY)
649651
.map(|flags| ChannelState::AwaitingChannelReady(flags))
650652
} else if val & state_flags::CHANNEL_READY == state_flags::CHANNEL_READY {
@@ -662,15 +664,15 @@ impl ChannelState {
662664
fn to_u32(self) -> u32 {
663665
match self {
664666
ChannelState::NegotiatingFunding(flags) => flags.0,
665-
ChannelState::FundingNegotiated => state_flags::FUNDING_NEGOTIATED,
667+
ChannelState::FundingNegotiated(flags) => state_flags::FUNDING_NEGOTIATED | flags.0,
666668
ChannelState::AwaitingChannelReady(flags) => state_flags::AWAITING_CHANNEL_READY | flags.0,
667669
ChannelState::ChannelReady(flags) => state_flags::CHANNEL_READY | flags.0,
668670
ChannelState::ShutdownComplete => state_flags::SHUTDOWN_COMPLETE,
669671
}
670672
}
671673

672674
fn is_pre_funded_state(&self) -> bool {
673-
matches!(self, ChannelState::NegotiatingFunding(_)|ChannelState::FundingNegotiated)
675+
matches!(self, ChannelState::NegotiatingFunding(_)|ChannelState::FundingNegotiated(_))
674676
}
675677

676678
fn is_both_sides_shutdown(&self) -> bool {
@@ -704,9 +706,9 @@ impl ChannelState {
704706
impl_state_flag!(is_monitor_update_in_progress, set_monitor_update_in_progress, clear_monitor_update_in_progress, FUNDED_STATES);
705707
impl_state_flag!(is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent, FUNDED_STATES);
706708
impl_state_flag!(is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent, FUNDED_STATES);
707-
impl_state_flag!(is_interactive_signing, set_interactive_signing, clear_interactive_signing, FUNDED_STATES);
708-
impl_state_flag!(is_our_tx_signatures_sent, set_our_tx_signatures_sent, clear_our_tx_signatures_sent, FUNDED_STATES);
709-
impl_state_flag!(is_their_tx_signatures_sent, set_their_tx_signatures_sent, clear_their_tx_signatures_sent, FUNDED_STATES);
709+
impl_state_flag!(is_interactive_signing, set_interactive_signing, clear_interactive_signing, FundingNegotiated);
710+
impl_state_flag!(is_our_tx_signatures_sent, set_our_tx_signatures_sent, clear_our_tx_signatures_sent, FundingNegotiated);
711+
impl_state_flag!(is_their_tx_signatures_sent, set_their_tx_signatures_sent, clear_their_tx_signatures_sent, FundingNegotiated);
710712
impl_state_flag!(is_our_channel_ready, set_our_channel_ready, clear_our_channel_ready, AwaitingChannelReady);
711713
impl_state_flag!(is_their_channel_ready, set_their_channel_ready, clear_their_channel_ready, AwaitingChannelReady);
712714
impl_state_flag!(is_waiting_for_batch, set_waiting_for_batch, clear_waiting_for_batch, AwaitingChannelReady);
@@ -2373,7 +2375,7 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23732375
)));
23742376
};
23752377

2376-
let mut channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
2378+
let mut channel_state = ChannelState::FundingNegotiated(FundingNegotiatedFlags::new());
23772379
channel_state.set_interactive_signing();
23782380
self.context.channel_state = channel_state;
23792381

@@ -3466,7 +3468,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
34663468

34673469
fn unset_funding_info(&mut self, funding: &mut FundingScope) {
34683470
debug_assert!(
3469-
matches!(self.channel_state, ChannelState::FundingNegotiated)
3471+
matches!(self.channel_state, ChannelState::FundingNegotiated(_))
34703472
|| matches!(self.channel_state, ChannelState::AwaitingChannelReady(_))
34713473
);
34723474
funding.channel_transaction_parameters.funding_outpoint = None;
@@ -4458,11 +4460,10 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
44584460

44594461
fn if_unbroadcasted_funding<F, O>(&self, f: F) -> Option<O> where F: Fn() -> Option<O> {
44604462
match self.channel_state {
4461-
ChannelState::FundingNegotiated => f(),
4463+
ChannelState::FundingNegotiated(_) => f(),
44624464
ChannelState::AwaitingChannelReady(flags) =>
44634465
if flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH) ||
4464-
flags.is_set(FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS.into()) ||
4465-
(flags.is_set(FundedStateFlags::THEIR_TX_SIGNATURES_SENT.into()) && flags.is_set(FundedStateFlags::OUR_TX_SIGNATURES_SENT.into()))
4466+
flags.is_set(FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS.into())
44664467
{
44674468
f()
44684469
} else {
@@ -5669,7 +5670,7 @@ impl<SP: Deref> FundedChannel<SP> where
56695670
) -> Result<ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>, ChannelError>
56705671
where L::Target: Logger
56715672
{
5672-
if !matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(_)) {
5673+
if !matches!(self.context.channel_state, ChannelState::FundingNegotiated(_)) {
56735674
return Err(ChannelError::Close(
56745675
(
56755676
"Received initial commitment_signed before funding transaction constructed!".to_owned(),
@@ -6275,7 +6276,7 @@ impl<SP: Deref> FundedChannel<SP> where
62756276
pub fn tx_signatures<L: Deref>(&mut self, msg: &msgs::TxSignatures, logger: &L) -> Result<Option<msgs::TxSignatures>, ChannelError>
62766277
where L::Target: Logger
62776278
{
6278-
if !matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if flags.is_interactive_signing()) {
6279+
if !matches!(self.context.channel_state, ChannelState::FundingNegotiated(flags) if flags.is_interactive_signing()) {
62796280
return Err(ChannelError::close("Received tx_signatures in strange state!".to_owned()));
62806281
}
62816282

@@ -6332,6 +6333,10 @@ impl<SP: Deref> FundedChannel<SP> where
63326333
self.context.channel_state.set_our_tx_signatures_sent();
63336334
}
63346335

6336+
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
6337+
let need_channel_ready = self.check_get_channel_ready(0, logger).is_some()
6338+
|| self.context.signer_pending_channel_ready;
6339+
self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
63356340
Ok(holder_tx_signatures_opt)
63366341
} else {
63376342
Err(ChannelError::Close((
@@ -6554,7 +6559,7 @@ impl<SP: Deref> FundedChannel<SP> where
65546559
// first received the funding_signed.
65556560
let mut funding_broadcastable = None;
65566561
if let Some(funding_transaction) = &self.funding.funding_transaction {
6557-
if (self.funding.is_outbound() || (self.is_v2_established() && self.context.channel_state.is_their_tx_signatures_sent())) &&
6562+
if (self.funding.is_outbound() || self.is_v2_established()) &&
65586563
(matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if !flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH)) ||
65596564
matches!(self.context.channel_state, ChannelState::ChannelReady(_)))
65606565
{
@@ -9439,7 +9444,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
94399444

94409445
// Now that we're past error-generating stuff, update our local state:
94419446

9442-
self.context.channel_state = ChannelState::FundingNegotiated;
9447+
self.context.channel_state = ChannelState::FundingNegotiated(FundingNegotiatedFlags::new());
94439448
self.context.channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
94449449

94459450
// If the funding transaction is a coinbase transaction, we need to set the minimum depth to 100.
@@ -9556,7 +9561,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
95569561
if !self.funding.is_outbound() {
95579562
return Err((self, ChannelError::close("Received funding_signed for an inbound channel?".to_owned())));
95589563
}
9559-
if !matches!(self.context.channel_state, ChannelState::FundingNegotiated) {
9564+
if !matches!(self.context.channel_state, ChannelState::FundingNegotiated(_)) {
95609565
return Err((self, ChannelError::close("Received funding_signed in strange state!".to_owned())));
95619566
}
95629567
let mut holder_commitment_point = match self.unfunded_context.holder_commitment_point {
@@ -11297,11 +11302,12 @@ mod tests {
1129711302
#[test]
1129811303
fn test_channel_state_order() {
1129911304
use crate::ln::channel::NegotiatingFundingFlags;
11305+
use crate::ln::channel::FundingNegotiatedFlags;
1130011306
use crate::ln::channel::AwaitingChannelReadyFlags;
1130111307
use crate::ln::channel::ChannelReadyFlags;
1130211308

11303-
assert!(ChannelState::NegotiatingFunding(NegotiatingFundingFlags::new()) < ChannelState::FundingNegotiated);
11304-
assert!(ChannelState::FundingNegotiated < ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new()));
11309+
assert!(ChannelState::NegotiatingFunding(NegotiatingFundingFlags::new()) < ChannelState::FundingNegotiated(FundingNegotiatedFlags::new()));
11310+
assert!(ChannelState::FundingNegotiated(FundingNegotiatedFlags::new()) < ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new()));
1130511311
assert!(ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new()) < ChannelState::ChannelReady(ChannelReadyFlags::new()));
1130611312
assert!(ChannelState::ChannelReady(ChannelReadyFlags::new()) < ChannelState::ShutdownComplete);
1130711313
}

lightning/src/ln/dual_funding_tests.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,6 @@ fn do_test_v2_channel_establishment(session: V2ChannelEstablishmentTestSession)
208208
assert!(events.is_empty());
209209
nodes[1].chain_monitor.complete_sole_pending_chan_update(&channel_id);
210210

211-
let events = nodes[1].node.get_and_clear_pending_events();
212-
assert_eq!(events.len(), 1);
213-
match events[0] {
214-
Event::ChannelPending { channel_id: chan_id, .. } => assert_eq!(chan_id, channel_id),
215-
_ => panic!("Unexpected event"),
216-
};
217-
218211
let tx_signatures_msg = get_event_msg!(
219212
nodes[1],
220213
MessageSendEvent::SendTxSignatures,
@@ -236,6 +229,13 @@ fn do_test_v2_channel_establishment(session: V2ChannelEstablishmentTestSession)
236229
},
237230
);
238231

232+
let events = nodes[1].node.get_and_clear_pending_events();
233+
assert_eq!(events.len(), 1);
234+
match events[0] {
235+
Event::ChannelPending { channel_id: chan_id, .. } => assert_eq!(chan_id, channel_id),
236+
_ => panic!("Unexpected event"),
237+
};
238+
239239
// For an inbound channel V2 channel the transaction should be broadcast once receiving a
240240
// tx_signature and applying local tx_signatures:
241241
let broadcasted_txs = nodes[1].tx_broadcaster.txn_broadcast();

0 commit comments

Comments
 (0)