@@ -433,6 +433,12 @@ macro_rules! define_state_flags {
433433 is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent);
434434 define_state_flags!($flag_type, FundedStateFlags::LOCAL_SHUTDOWN_SENT,
435435 is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent);
436+ define_state_flags!($flag_type, FundedStateFlags::INTERACTIVE_SIGNING,
437+ is_interactive_signing, set_interactive_signing, clear_interactive_signing);
438+ define_state_flags!($flag_type, FundedStateFlags::THEIR_TX_SIGNATURES_SENT,
439+ is_their_tx_signatures_sent, set_their_tx_signatures_sent, clear_their_tx_signatures_sent);
440+ define_state_flags!($flag_type, FundedStateFlags::OUR_TX_SIGNATURES_SENT,
441+ is_our_tx_signatures_sent, set_our_tx_signatures_sent, clear_our_tx_signatures_sent);
436442
437443 impl core::ops::BitOr<FundedStateFlags> for $flag_type {
438444 type Output = Self;
@@ -478,6 +484,9 @@ mod state_flags {
478484 pub const LOCAL_STFU_SENT: u32 = 1 << 15;
479485 pub const REMOTE_STFU_SENT: u32 = 1 << 16;
480486 pub const QUIESCENT: u32 = 1 << 17;
487+ pub const INTERACTIVE_SIGNING: u32 = 1 << 18;
488+ pub const OUR_TX_SIGNATURES_SENT: u32 = 1 << 19;
489+ pub const THEIR_TX_SIGNATURES_SENT: u32 = 1 << 20;
481490}
482491
483492define_state_flags!(
@@ -496,7 +505,16 @@ define_state_flags!(
496505 is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent),
497506 ("Indicates we sent a `shutdown` message. At this point, we may not add any new HTLCs to \
498507 the channel.", LOCAL_SHUTDOWN_SENT, state_flags::LOCAL_SHUTDOWN_SENT,
499- is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent)
508+ is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent),
509+ ("Indicates we have an active interactive signing session for an interactive transaction",
510+ INTERACTIVE_SIGNING, state_flags::INTERACTIVE_SIGNING,
511+ is_interactive_signing, set_interactive_signing, clear_interactive_singing),
512+ ("Indicates they sent us a `tx_signatures` message.",
513+ THEIR_TX_SIGNATURES_SENT, state_flags::THEIR_TX_SIGNATURES_SENT,
514+ is_their_tx_signatures_sent, set_their_tx_signatures_sent, clear_their_tx_signatures_sent),
515+ ("Indicates we sent them a `tx_signatures` message.",
516+ OUR_TX_SIGNATURES_SENT, state_flags::OUR_TX_SIGNATURES_SENT,
517+ is_our_tx_signatures_sent, set_our_tx_signatures_sent, clear_our_tx_signatures_sent)
500518 ]
501519);
502520
@@ -567,7 +585,8 @@ enum ChannelState {
567585 NegotiatingFunding(NegotiatingFundingFlags),
568586 /// We have sent `funding_created` and are awaiting a `funding_signed` to advance to
569587 /// `AwaitingChannelReady`. Note that this is nonsense for an inbound channel as we immediately generate
570- /// `funding_signed` upon receipt of `funding_created`, so simply skip this state.
588+ /// `funding_signed` upon receipt of `funding_created`, so simply skip this state. For dual-funded (V2)
589+ /// channels, this state is also skipped.
571590 FundingNegotiated,
572591 /// We've received/sent `funding_created` and `funding_signed` and are thus now waiting on the
573592 /// funding transaction to confirm.
@@ -684,6 +703,9 @@ impl ChannelState {
684703 impl_state_flag!(is_monitor_update_in_progress, set_monitor_update_in_progress, clear_monitor_update_in_progress, FUNDED_STATES);
685704 impl_state_flag!(is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent, FUNDED_STATES);
686705 impl_state_flag!(is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent, FUNDED_STATES);
706+ impl_state_flag!(is_interactive_signing, set_interactive_signing, clear_interactive_signing, FUNDED_STATES);
707+ impl_state_flag!(is_our_tx_signatures_sent, set_our_tx_signatures_sent, clear_our_tx_signatures_sent, FUNDED_STATES);
708+ impl_state_flag!(is_their_tx_signatures_sent, set_their_tx_signatures_sent, clear_their_tx_signatures_sent, FUNDED_STATES);
687709 impl_state_flag!(is_our_channel_ready, set_our_channel_ready, clear_our_channel_ready, AwaitingChannelReady);
688710 impl_state_flag!(is_their_channel_ready, set_their_channel_ready, clear_their_channel_ready, AwaitingChannelReady);
689711 impl_state_flag!(is_waiting_for_batch, set_waiting_for_batch, clear_waiting_for_batch, AwaitingChannelReady);
@@ -2085,10 +2107,12 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
20852107 context.channel_id = channel_id;
20862108
20872109 assert!(!context.channel_state.is_monitor_update_in_progress()); // We have not had any monitor(s) yet to fail update!
2088- if context.is_batch_funding() {
2089- context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::WAITING_FOR_BATCH);
2090- } else {
2091- context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
2110+ if !context.channel_state.is_interactive_signing() {
2111+ if context.is_batch_funding() {
2112+ context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::WAITING_FOR_BATCH);
2113+ } else {
2114+ context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
2115+ }
20922116 }
20932117 if holder_commitment_point.advance(&context.holder_signer, &context.secp_ctx, logger).is_err() {
20942118 // We only fail to advance our commitment point/number if we're currently
@@ -2349,7 +2373,9 @@ impl<SP: Deref> PendingV2Channel<SP> where SP::Target: SignerProvider {
23492373 )));
23502374 };
23512375
2352- self.context.channel_state = ChannelState::FundingNegotiated;
2376+ let mut channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
2377+ channel_state.set_interactive_signing();
2378+ self.context.channel_state = channel_state;
23532379
23542380 // Clear the interactive transaction constructor
23552381 self.interactive_tx_constructor.take();
@@ -4294,7 +4320,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
42944320 ChannelState::FundingNegotiated => f(),
42954321 ChannelState::AwaitingChannelReady(flags) =>
42964322 if flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH) ||
4297- flags.is_set(FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS.into())
4323+ flags.is_set(FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS.into()) ||
4324+ (flags.is_set(FundedStateFlags::THEIR_TX_SIGNATURES_SENT.into()) && flags.is_set(FundedStateFlags::OUR_TX_SIGNATURES_SENT.into()))
42984325 {
42994326 f()
43004327 } else {
@@ -5445,7 +5472,7 @@ impl<SP: Deref> FundedChannel<SP> where
54455472 ) -> Result<ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>, ChannelError>
54465473 where L::Target: Logger
54475474 {
5448- if !matches!(self.context.channel_state, ChannelState::FundingNegotiated ) {
5475+ if !matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(_) ) {
54495476 return Err(ChannelError::Close(
54505477 (
54515478 "Received initial commitment_signed before funding transaction constructed!".to_owned(),
@@ -5463,7 +5490,6 @@ impl<SP: Deref> FundedChannel<SP> where
54635490 log_info!(logger, "Received initial commitment_signed from peer for channel {}", &self.context.channel_id());
54645491
54655492 let need_channel_ready = self.check_get_channel_ready(0, logger).is_some();
5466- self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
54675493 self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
54685494
54695495 if let Some(tx_signatures) = self.interactive_tx_signing_session.as_mut().and_then(
@@ -6161,7 +6187,7 @@ impl<SP: Deref> FundedChannel<SP> where
61616187 pub fn tx_signatures<L: Deref>(&mut self, msg: &msgs::TxSignatures, logger: &L) -> Result<Option<msgs::TxSignatures>, ChannelError>
61626188 where L::Target: Logger
61636189 {
6164- if !matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(_ )) {
6190+ if !matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if flags.is_interactive_signing( )) {
61656191 return Err(ChannelError::close("Received tx_signatures in strange state!".to_owned()));
61666192 }
61676193
@@ -6198,6 +6224,8 @@ impl<SP: Deref> FundedChannel<SP> where
61986224 let (holder_tx_signatures_opt, funding_tx_opt) = signing_session.received_tx_signatures(msg.clone())
61996225 .map_err(|_| ChannelError::Warn("Witness count did not match contributed input count".to_string()))?;
62006226
6227+ // Set `THEIR_TX_SIGNATURES_SENT` flag after all potential errors.
6228+ self.context.channel_state.set_their_tx_signatures_sent();
62016229
62026230 if funding_tx_opt.is_some() {
62036231 // We have a finalized funding transaction, so we can set the funding transaction and reset the
@@ -6212,6 +6240,10 @@ impl<SP: Deref> FundedChannel<SP> where
62126240 return Ok(None);
62136241 }
62146242
6243+ if holder_tx_signatures_opt.is_some() {
6244+ self.context.channel_state.set_our_tx_signatures_sent();
6245+ }
6246+
62156247 Ok(holder_tx_signatures_opt)
62166248 } else {
62176249 Err(ChannelError::Close((
@@ -6434,7 +6466,7 @@ impl<SP: Deref> FundedChannel<SP> where
64346466 // first received the funding_signed.
64356467 let mut funding_broadcastable = None;
64366468 if let Some(funding_transaction) = &self.funding.funding_transaction {
6437- if (self.funding.is_outbound() || self.is_v2_established()) &&
6469+ if (self.funding.is_outbound() || ( self.is_v2_established() && self.context.channel_state.is_their_tx_signatures_sent() )) &&
64386470 (matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if !flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH)) ||
64396471 matches!(self.context.channel_state, ChannelState::ChannelReady(_)))
64406472 {
@@ -6472,6 +6504,9 @@ impl<SP: Deref> FundedChannel<SP> where
64726504 // MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
64736505 // transaction and waits for us to do it).
64746506 let tx_signatures = self.context.monitor_pending_tx_signatures.take();
6507+ if tx_signatures.is_some() {
6508+ self.context.channel_state.set_our_tx_signatures_sent();
6509+ }
64756510
64766511 if self.context.channel_state.is_peer_disconnected() {
64776512 self.context.monitor_pending_revoke_and_ack = false;
@@ -6939,7 +6974,7 @@ impl<SP: Deref> FundedChannel<SP> where
69396974 // if next_funding_txid matches the latest interactive funding transaction:
69406975 if session.unsigned_tx().compute_txid() == next_funding_txid {
69416976 // if it has not received tx_signatures for that funding transaction:
6942- if !session.counterparty_sent_tx_signatures () {
6977+ if !self.context.channel_state.is_their_tx_signatures_sent () {
69436978 // if next_commitment_number is zero:
69446979 let commitment_update = if msg.next_local_commitment_number == 0 {
69456980 // MUST retransmit its commitment_signed for that funding transaction.
@@ -6953,8 +6988,8 @@ impl<SP: Deref> FundedChannel<SP> where
69536988 update_fee: None,
69546989 })
69556990 } else { None };
6956- // if it has already received commitment_signed and it should sign first, as specified in the tx_signatures requirements:
6957- if session.has_received_commitment_signed() && session.holder_sends_tx_signatures_first() {
6991+ // if it has already received initial commitment_signed and it should sign first, as specified in the tx_signatures requirements:
6992+ if (our_commitment_transaction == INITIAL_COMMITMENT_NUMBER - 1) && session.holder_sends_tx_signatures_first() {
69586993 // MUST send its tx_signatures for that funding transaction.
69596994 if self.context.channel_state.is_monitor_update_in_progress() {
69606995 log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
@@ -8331,9 +8366,9 @@ impl<SP: Deref> FundedChannel<SP> where
83318366 // If we've sent `commtiment_signed` for an interactively constructed transaction
83328367 // during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
83338368 // to the txid of that interactive transaction, else we MUST NOT set it.
8334- if let Some(signing_session) = & self.interactive_tx_signing_session {
8369+ if self.context.channel_state.is_interactive_signing() {
83358370 // Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
8336- if !signing_session.counterparty_sent_tx_signatures () {
8371+ if !self.context.channel_state.is_their_tx_signatures_sent () {
83378372 // ...but we didn't receive a `tx_signatures` from the counterparty yet.
83388373 Some(self.funding_outpoint().txid)
83398374 } else {
@@ -10369,6 +10404,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1036910404 (49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
1037010405 (51, is_manual_broadcast, option), // Added in 0.0.124
1037110406 (53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124
10407+ (55, self.interactive_tx_signing_session, option) // Added in 0.2
1037210408 });
1037310409
1037410410 Ok(())
@@ -10648,6 +10684,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1064810684 let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
1064910685 let mut is_manual_broadcast = None;
1065010686
10687+ let mut interactive_tx_signing_session: Option<InteractiveTxSigningSession> = None;
10688+
1065110689 read_tlv_fields!(reader, {
1065210690 (0, announcement_sigs, option),
1065310691 (1, minimum_depth, option),
@@ -10683,6 +10721,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1068310721 (49, local_initiated_shutdown, option),
1068410722 (51, is_manual_broadcast, option),
1068510723 (53, funding_tx_broadcast_safe_event_emitted, option),
10724+ (55, interactive_tx_signing_session, option),
1068610725 });
1068710726
1068810727 let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
@@ -10805,6 +10844,15 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1080510844 &channel_parameters.counterparty_parameters.as_ref()
1080610845 .expect("Persisted channel must have counterparty parameters").pubkeys.revocation_basepoint);
1080710846
10847+ let interactive_tx_signing_session = if channel_state.is_interactive_signing() {
10848+ interactive_tx_signing_session.zip(funding_transaction.as_ref()).map(|(mut session, funding_tx)| {
10849+ session.update_from_funding_tx_after_read(funding_tx);
10850+ session
10851+ })
10852+ } else {
10853+ None
10854+ };
10855+
1080810856 Ok(FundedChannel {
1080910857 funding: FundingScope {
1081010858 value_to_self_msat,
@@ -10937,7 +10985,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1093710985
1093810986 is_holder_quiescence_initiator: None,
1093910987 },
10940- interactive_tx_signing_session: None ,
10988+ interactive_tx_signing_session,
1094110989 is_v2_established,
1094210990 holder_commitment_point,
1094310991 })
0 commit comments