Skip to content

Commit 9072ebb

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

File tree

3 files changed

+53
-50
lines changed

3 files changed

+53
-50
lines changed

lightning/src/ln/channel.rs

Lines changed: 44 additions & 41 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(),
@@ -6272,10 +6273,10 @@ impl<SP: Deref> FundedChannel<SP> where
62726273
}
62736274
}
62746275

6275-
pub fn tx_signatures<L: Deref>(&mut self, msg: &msgs::TxSignatures, logger: &L) -> Result<Option<msgs::TxSignatures>, ChannelError>
6276+
pub fn tx_signatures<L: Deref>(&mut self, msg: &msgs::TxSignatures, logger: &L) -> Result<(Option<Transaction>, 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

@@ -6318,21 +6319,22 @@ impl<SP: Deref> FundedChannel<SP> where
63186319
if funding_tx_opt.is_some() {
63196320
// We have a finalized funding transaction, so we can set the funding transaction and reset the
63206321
// signing session fields.
6321-
self.funding.funding_transaction = funding_tx_opt;
6322+
self.funding.funding_transaction = funding_tx_opt.clone();
63226323
self.interactive_tx_signing_session = None;
63236324
}
63246325

63256326
if holder_tx_signatures_opt.is_some() && self.is_awaiting_initial_mon_persist() {
63266327
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
63276328
self.context.monitor_pending_tx_signatures = holder_tx_signatures_opt;
6328-
return Ok(None);
6329+
return Ok((None, None));
63296330
}
63306331

63316332
if holder_tx_signatures_opt.is_some() {
63326333
self.context.channel_state.set_our_tx_signatures_sent();
63336334
}
63346335

6335-
Ok(holder_tx_signatures_opt)
6336+
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
6337+
Ok((funding_tx_opt, holder_tx_signatures_opt))
63366338
} else {
63376339
Err(ChannelError::Close((
63386340
"Unexpected tx_signatures. No funding transaction awaiting signatures".to_string(),
@@ -6554,7 +6556,7 @@ impl<SP: Deref> FundedChannel<SP> where
65546556
// first received the funding_signed.
65556557
let mut funding_broadcastable = None;
65566558
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())) &&
6559+
if (self.funding.is_outbound() || self.is_v2_established()) &&
65586560
(matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if !flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH)) ||
65596561
matches!(self.context.channel_state, ChannelState::ChannelReady(_)))
65606562
{
@@ -9439,7 +9441,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
94399441

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

9442-
self.context.channel_state = ChannelState::FundingNegotiated;
9444+
self.context.channel_state = ChannelState::FundingNegotiated(FundingNegotiatedFlags::new());
94439445
self.context.channel_id = ChannelId::v1_from_funding_outpoint(funding_txo);
94449446

94459447
// If the funding transaction is a coinbase transaction, we need to set the minimum depth to 100.
@@ -9556,7 +9558,7 @@ impl<SP: Deref> OutboundV1Channel<SP> where SP::Target: SignerProvider {
95569558
if !self.funding.is_outbound() {
95579559
return Err((self, ChannelError::close("Received funding_signed for an inbound channel?".to_owned())));
95589560
}
9559-
if !matches!(self.context.channel_state, ChannelState::FundingNegotiated) {
9561+
if !matches!(self.context.channel_state, ChannelState::FundingNegotiated(_)) {
95609562
return Err((self, ChannelError::close("Received funding_signed in strange state!".to_owned())));
95619563
}
95629564
let mut holder_commitment_point = match self.unfunded_context.holder_commitment_point {
@@ -11297,11 +11299,12 @@ mod tests {
1129711299
#[test]
1129811300
fn test_channel_state_order() {
1129911301
use crate::ln::channel::NegotiatingFundingFlags;
11302+
use crate::ln::channel::FundingNegotiatedFlags;
1130011303
use crate::ln::channel::AwaitingChannelReadyFlags;
1130111304
use crate::ln::channel::ChannelReadyFlags;
1130211305

11303-
assert!(ChannelState::NegotiatingFunding(NegotiatingFundingFlags::new()) < ChannelState::FundingNegotiated);
11304-
assert!(ChannelState::FundingNegotiated < ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new()));
11306+
assert!(ChannelState::NegotiatingFunding(NegotiatingFundingFlags::new()) < ChannelState::FundingNegotiated(FundingNegotiatedFlags::new()));
11307+
assert!(ChannelState::FundingNegotiated(FundingNegotiatedFlags::new()) < ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new()));
1130511308
assert!(ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new()) < ChannelState::ChannelReady(ChannelReadyFlags::new()));
1130611309
assert!(ChannelState::ChannelReady(ChannelReadyFlags::new()) < ChannelState::ShutdownComplete);
1130711310
}

lightning/src/ln/channelmanager.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8586,14 +8586,14 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
85868586
match chan_entry.get_mut().as_funded_mut() {
85878587
Some(chan) => {
85888588
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
8589-
let tx_signatures_opt = try_channel_entry!(self, peer_state, chan.tx_signatures(msg, &&logger), chan_entry);
8589+
let (funding_tx_opt, tx_signatures_opt) = try_channel_entry!(self, peer_state, chan.tx_signatures(msg, &&logger), chan_entry);
85908590
if let Some(tx_signatures) = tx_signatures_opt {
85918591
peer_state.pending_msg_events.push(MessageSendEvent::SendTxSignatures {
85928592
node_id: *counterparty_node_id,
85938593
msg: tx_signatures,
85948594
});
85958595
}
8596-
if let Some(ref funding_tx) = chan.context.unbroadcasted_funding(&chan.funding) {
8596+
if let Some(ref funding_tx) = funding_tx_opt {
85978597
self.tx_broadcaster.broadcast_transactions(&[funding_tx]);
85988598
{
85998599
let mut pending_events = self.pending_events.lock().unwrap();

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)