Skip to content

Commit b60215b

Browse files
committed
Introduce interactive signing state flags and session persistence
This intoduces the INTERACTIVE_SIGNING, THEIR_TX_SIGNATURES_SENT, and OUR_TX_SIGNATURES_SENT funded state flags. A top-level state flag for INTERACTIVE_SIGNING was avoided so that this work is compatible with splicing as well as V2 channel establishment (dual-funding).
1 parent 2ab7da1 commit b60215b

File tree

3 files changed

+185
-28
lines changed

3 files changed

+185
-28
lines changed

lightning/src/ln/channel.rs

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,12 @@ 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);
437443

438444
impl core::ops::BitOr<FundedStateFlags> for $flag_type {
439445
type Output = Self;
@@ -479,6 +485,9 @@ mod state_flags {
479485
pub const LOCAL_STFU_SENT: u32 = 1 << 15;
480486
pub const REMOTE_STFU_SENT: u32 = 1 << 16;
481487
pub const QUIESCENT: u32 = 1 << 17;
488+
pub const INTERACTIVE_SIGNING: u32 = 1 << 18;
489+
pub const OUR_TX_SIGNATURES_SENT: u32 = 1 << 19;
490+
pub const THEIR_TX_SIGNATURES_SENT: u32 = 1 << 20;
482491
}
483492

484493
define_state_flags!(
@@ -497,7 +506,16 @@ define_state_flags!(
497506
is_remote_shutdown_sent, set_remote_shutdown_sent, clear_remote_shutdown_sent),
498507
("Indicates we sent a `shutdown` message. At this point, we may not add any new HTLCs to \
499508
the channel.", LOCAL_SHUTDOWN_SENT, state_flags::LOCAL_SHUTDOWN_SENT,
500-
is_local_shutdown_sent, set_local_shutdown_sent, clear_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)
501519
]
502520
);
503521

@@ -568,7 +586,8 @@ enum ChannelState {
568586
NegotiatingFunding(NegotiatingFundingFlags),
569587
/// We have sent `funding_created` and are awaiting a `funding_signed` to advance to
570588
/// `AwaitingChannelReady`. Note that this is nonsense for an inbound channel as we immediately generate
571-
/// `funding_signed` upon receipt of `funding_created`, so simply skip this state.
589+
/// `funding_signed` upon receipt of `funding_created`, so simply skip this state. For dual-funded (V2)
590+
/// channels, this state is also skipped.
572591
FundingNegotiated,
573592
/// We've received/sent `funding_created` and `funding_signed` and are thus now waiting on the
574593
/// funding transaction to confirm.
@@ -685,6 +704,9 @@ impl ChannelState {
685704
impl_state_flag!(is_monitor_update_in_progress, set_monitor_update_in_progress, clear_monitor_update_in_progress, FUNDED_STATES);
686705
impl_state_flag!(is_local_shutdown_sent, set_local_shutdown_sent, clear_local_shutdown_sent, FUNDED_STATES);
687706
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);
688710
impl_state_flag!(is_our_channel_ready, set_our_channel_ready, clear_our_channel_ready, AwaitingChannelReady);
689711
impl_state_flag!(is_their_channel_ready, set_their_channel_ready, clear_their_channel_ready, AwaitingChannelReady);
690712
impl_state_flag!(is_waiting_for_batch, set_waiting_for_batch, clear_waiting_for_batch, AwaitingChannelReady);
@@ -2094,10 +2116,12 @@ trait InitialRemoteCommitmentReceiver<SP: Deref> where SP::Target: SignerProvide
20942116
context.channel_id = channel_id;
20952117

20962118
assert!(!context.channel_state.is_monitor_update_in_progress()); // We have not had any monitor(s) yet to fail update!
2097-
if context.is_batch_funding() {
2098-
context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::WAITING_FOR_BATCH);
2099-
} else {
2100-
context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
2119+
if !context.channel_state.is_interactive_signing() {
2120+
if context.is_batch_funding() {
2121+
context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::WAITING_FOR_BATCH);
2122+
} else {
2123+
context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
2124+
}
21012125
}
21022126
if holder_commitment_point.advance(&context.holder_signer, &context.secp_ctx, logger).is_err() {
21032127
// 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();
@@ -4435,7 +4461,8 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
44354461
ChannelState::FundingNegotiated => f(),
44364462
ChannelState::AwaitingChannelReady(flags) =>
44374463
if flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH) ||
4438-
flags.is_set(FundedStateFlags::MONITOR_UPDATE_IN_PROGRESS.into())
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()))
44394466
{
44404467
f()
44414468
} else {
@@ -5642,7 +5669,7 @@ impl<SP: Deref> FundedChannel<SP> where
56425669
) -> Result<ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>, ChannelError>
56435670
where L::Target: Logger
56445671
{
5645-
if !matches!(self.context.channel_state, ChannelState::FundingNegotiated) {
5672+
if !matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(_)) {
56465673
return Err(ChannelError::Close(
56475674
(
56485675
"Received initial commitment_signed before funding transaction constructed!".to_owned(),
@@ -5659,7 +5686,6 @@ impl<SP: Deref> FundedChannel<SP> where
56595686
log_info!(logger, "Received initial commitment_signed from peer for channel {}", &self.context.channel_id());
56605687

56615688
let need_channel_ready = self.check_get_channel_ready(0, logger).is_some();
5662-
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
56635689
self.monitor_updating_paused(false, false, need_channel_ready, Vec::new(), Vec::new(), Vec::new());
56645690

56655691
if let Some(tx_signatures) = self.interactive_tx_signing_session.as_mut().and_then(
@@ -6249,7 +6275,7 @@ impl<SP: Deref> FundedChannel<SP> where
62496275
pub fn tx_signatures<L: Deref>(&mut self, msg: &msgs::TxSignatures, logger: &L) -> Result<Option<msgs::TxSignatures>, ChannelError>
62506276
where L::Target: Logger
62516277
{
6252-
if !matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(_)) {
6278+
if !matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if flags.is_interactive_signing()) {
62536279
return Err(ChannelError::close("Received tx_signatures in strange state!".to_owned()));
62546280
}
62556281

@@ -6286,6 +6312,8 @@ impl<SP: Deref> FundedChannel<SP> where
62866312
let (holder_tx_signatures_opt, funding_tx_opt) = signing_session.received_tx_signatures(msg.clone())
62876313
.map_err(|_| ChannelError::Warn("Witness count did not match contributed input count".to_string()))?;
62886314

6315+
// Set `THEIR_TX_SIGNATURES_SENT` flag after all potential errors.
6316+
self.context.channel_state.set_their_tx_signatures_sent();
62896317

62906318
if funding_tx_opt.is_some() {
62916319
// We have a finalized funding transaction, so we can set the funding transaction and reset the
@@ -6300,6 +6328,10 @@ impl<SP: Deref> FundedChannel<SP> where
63006328
return Ok(None);
63016329
}
63026330

6331+
if holder_tx_signatures_opt.is_some() {
6332+
self.context.channel_state.set_our_tx_signatures_sent();
6333+
}
6334+
63036335
Ok(holder_tx_signatures_opt)
63046336
} else {
63056337
Err(ChannelError::Close((
@@ -6522,7 +6554,7 @@ impl<SP: Deref> FundedChannel<SP> where
65226554
// first received the funding_signed.
65236555
let mut funding_broadcastable = None;
65246556
if let Some(funding_transaction) = &self.funding.funding_transaction {
6525-
if (self.funding.is_outbound() || self.is_v2_established()) &&
6557+
if (self.funding.is_outbound() || (self.is_v2_established() && self.context.channel_state.is_their_tx_signatures_sent())) &&
65266558
(matches!(self.context.channel_state, ChannelState::AwaitingChannelReady(flags) if !flags.is_set(AwaitingChannelReadyFlags::WAITING_FOR_BATCH)) ||
65276559
matches!(self.context.channel_state, ChannelState::ChannelReady(_)))
65286560
{
@@ -6560,6 +6592,9 @@ impl<SP: Deref> FundedChannel<SP> where
65606592
// MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
65616593
// transaction and waits for us to do it).
65626594
let tx_signatures = self.context.monitor_pending_tx_signatures.take();
6595+
if tx_signatures.is_some() {
6596+
self.context.channel_state.set_our_tx_signatures_sent();
6597+
}
65636598

65646599
if self.context.channel_state.is_peer_disconnected() {
65656600
self.context.monitor_pending_revoke_and_ack = false;
@@ -7028,7 +7063,7 @@ impl<SP: Deref> FundedChannel<SP> where
70287063
if session.unsigned_tx().compute_txid() == next_funding_txid {
70297064
debug_assert_eq!(session.unsigned_tx().compute_txid(), self.maybe_get_next_funding_txid().unwrap());
70307065

7031-
let commitment_update = if !session.counterparty_sent_tx_signatures() && msg.next_local_commitment_number == 0 {
7066+
let commitment_update = if !self.context.channel_state.is_their_tx_signatures_sent() && msg.next_local_commitment_number == 0 {
70327067
// if it has not received tx_signatures for that funding transaction AND
70337068
// if next_commitment_number is zero:
70347069
// MUST retransmit its commitment_signed for that funding transaction.
@@ -7048,9 +7083,9 @@ impl<SP: Deref> FundedChannel<SP> where
70487083
// else if it HAS received commitment_signed AND has received tx_signatures for that funding transaction:
70497084
// MUST send its tx_signatures for that funding transaction.
70507085
let tx_signatures = if session.has_received_commitment_signed() && ((
7051-
!session.counterparty_sent_tx_signatures() &&
7086+
!self.context.channel_state.is_their_tx_signatures_sent() &&
70527087
session.holder_sends_tx_signatures_first()
7053-
) || session.counterparty_sent_tx_signatures()) {
7088+
) || self.context.channel_state.is_their_tx_signatures_sent()) {
70547089
// This should have already been set in `commitment_signed_initial_v2`, but check again
70557090
// just in case.
70567091
if self.context.channel_state.is_monitor_update_in_progress() {
@@ -8422,9 +8457,9 @@ impl<SP: Deref> FundedChannel<SP> where
84228457
// If we've sent `commtiment_signed` for an interactively constructed transaction
84238458
// during a signing session, but have not received `tx_signatures` we MUST set `next_funding_txid`
84248459
// to the txid of that interactive transaction, else we MUST NOT set it.
8425-
if let Some(signing_session) = &self.interactive_tx_signing_session {
8460+
if self.context.channel_state.is_interactive_signing() {
84268461
// Since we have a signing_session, this implies we've sent an initial `commitment_signed`...
8427-
if !signing_session.counterparty_sent_tx_signatures() {
8462+
if !self.context.channel_state.is_their_tx_signatures_sent() {
84288463
// ...but we didn't receive a `tx_signatures` from the counterparty yet.
84298464
Some(self.funding_outpoint().txid)
84308465
} else {
@@ -10612,6 +10647,7 @@ impl<SP: Deref> Writeable for FundedChannel<SP> where SP::Target: SignerProvider
1061210647
(49, self.context.local_initiated_shutdown, option), // Added in 0.0.122
1061310648
(51, is_manual_broadcast, option), // Added in 0.0.124
1061410649
(53, funding_tx_broadcast_safe_event_emitted, option), // Added in 0.0.124
10650+
(55, self.interactive_tx_signing_session, option) // Added in 0.2
1061510651
});
1061610652

1061710653
Ok(())
@@ -10903,6 +10939,8 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1090310939
let mut next_holder_commitment_point_opt: Option<PublicKey> = None;
1090410940
let mut is_manual_broadcast = None;
1090510941

10942+
let mut interactive_tx_signing_session: Option<InteractiveTxSigningSession> = None;
10943+
1090610944
read_tlv_fields!(reader, {
1090710945
(0, announcement_sigs, option),
1090810946
(1, minimum_depth, option),
@@ -10938,6 +10976,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1093810976
(49, local_initiated_shutdown, option),
1093910977
(51, is_manual_broadcast, option),
1094010978
(53, funding_tx_broadcast_safe_event_emitted, option),
10979+
(55, interactive_tx_signing_session, option),
1094110980
});
1094210981

1094310982
let holder_signer = signer_provider.derive_channel_signer(channel_keys_id);
@@ -11060,6 +11099,15 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1106011099
&channel_parameters.counterparty_parameters.as_ref()
1106111100
.expect("Persisted channel must have counterparty parameters").pubkeys.revocation_basepoint);
1106211101

11102+
let interactive_tx_signing_session = if channel_state.is_interactive_signing() {
11103+
interactive_tx_signing_session.zip(funding_transaction.as_ref()).map(|(mut session, funding_tx)| {
11104+
session.update_from_funding_tx_after_read(funding_tx);
11105+
session
11106+
})
11107+
} else {
11108+
None
11109+
};
11110+
1106311111
Ok(FundedChannel {
1106411112
funding: FundingScope {
1106511113
value_to_self_msat,
@@ -11192,7 +11240,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, &'c Channel
1119211240

1119311241
is_holder_quiescence_initiator: None,
1119411242
},
11195-
interactive_tx_signing_session: None,
11243+
interactive_tx_signing_session,
1119611244
is_v2_established,
1119711245
holder_commitment_point,
1119811246
#[cfg(splicing)]

0 commit comments

Comments
 (0)