Skip to content

Commit 1c6c08f

Browse files
committed
Generate FundingTransactionReadyForSigning on channel resumption
The `handle_channel_resumption` method handles resumption from both a channel re-establish and a monitor update. When the corresponding monitor update for the commitment_signed message completes, we will push the event here. We can thus only ever provide holder signatures after a monitor update has completed. We can also get rid of the reestablish code involved with `monitor_pending_tx_signatures` and remove that field too.
1 parent 89f93b9 commit 1c6c08f

File tree

3 files changed

+54
-111
lines changed

3 files changed

+54
-111
lines changed

lightning/src/events/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,10 +2161,8 @@ impl Writeable for Event {
21612161
},
21622162
&Event::FundingTransactionReadyForSigning { .. } => {
21632163
49u8.write(writer)?;
2164-
// We never write out FundingTransactionReadyForSigning events as, upon disconnection, peers
2165-
// drop any V2-established/spliced channels which have not yet exchanged the initial `commitment_signed`.
2166-
// We only exhange the initial `commitment_signed` after the client calls
2167-
// `ChannelManager::funding_transaction_signed` and ALWAYS before we send a `tx_signatures`
2164+
// We never write out FundingTransactionReadyForSigning events as they will be regenerated when
2165+
// necessary.
21682166
},
21692167
// Note that, going forward, all new events must only write data inside of
21702168
// `write_tlv_fields`. Versions 0.0.101+ will ignore odd-numbered events that write

lightning/src/ln/channel.rs

Lines changed: 30 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,7 +1188,6 @@ pub(super) struct MonitorRestoreUpdates {
11881188
pub funding_broadcastable: Option<Transaction>,
11891189
pub channel_ready: Option<msgs::ChannelReady>,
11901190
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
1191-
pub tx_signatures: Option<msgs::TxSignatures>,
11921191
}
11931192

11941193
/// The return value of `signer_maybe_unblocked`
@@ -1214,7 +1213,6 @@ pub(super) struct ReestablishResponses {
12141213
pub order: RAACommitmentOrder,
12151214
pub announcement_sigs: Option<msgs::AnnouncementSignatures>,
12161215
pub shutdown_msg: Option<msgs::Shutdown>,
1217-
pub tx_signatures: Option<msgs::TxSignatures>,
12181216
pub tx_abort: Option<msgs::TxAbort>,
12191217
}
12201218

@@ -1766,7 +1764,7 @@ where
17661764

17671765
pub fn funding_tx_constructed<L: Deref>(
17681766
&mut self, signing_session: InteractiveTxSigningSession, logger: &L,
1769-
) -> Result<(msgs::CommitmentSigned, Option<Transaction>), ChannelError>
1767+
) -> Result<msgs::CommitmentSigned, ChannelError>
17701768
where
17711769
L::Target: Logger,
17721770
{
@@ -1786,7 +1784,7 @@ where
17861784
#[rustfmt::skip]
17871785
pub fn commitment_signed<L: Deref>(
17881786
&mut self, msg: &msgs::CommitmentSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
1789-
) -> Result<(Option<ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>), ChannelError>
1787+
) -> Result<(Option<ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>>, Option<ChannelMonitorUpdate>, Option<Transaction>), ChannelError>
17901788
where
17911789
L::Target: Logger
17921790
{
@@ -1813,7 +1811,7 @@ where
18131811
pending_splice: None,
18141812
};
18151813
let res = funded_channel.commitment_signed_initial_v2(msg, best_block, signer_provider, logger)
1816-
.map(|monitor| (Some(monitor), None))
1814+
.map(|(monitor, funding_tx_opt)| (Some(monitor), None, funding_tx_opt))
18171815
// TODO: Change to `inspect_err` when MSRV is high enough.
18181816
.map_err(|err| {
18191817
// We always expect a `ChannelError` close.
@@ -1840,15 +1838,15 @@ where
18401838
let res = if has_negotiated_pending_splice && !session_received_commitment_signed {
18411839
funded_channel
18421840
.splice_initial_commitment_signed(msg, logger)
1843-
.map(|monitor_update_opt| (None, monitor_update_opt))
1841+
.map(|monitor_update_opt| (None, monitor_update_opt, None))
18441842
} else {
18451843
funded_channel.commitment_signed(msg, logger)
1846-
.map(|monitor_update_opt| (None, monitor_update_opt))
1844+
.map(|monitor_update_opt| (None, monitor_update_opt, None))
18471845
};
18481846

18491847
#[cfg(not(splicing))]
18501848
let res = funded_channel.commitment_signed(msg, logger)
1851-
.map(|monitor_update_opt| (None, monitor_update_opt));
1849+
.map(|monitor_update_opt| (None, monitor_update_opt, None));
18521850

18531851
self.phase = ChannelPhase::Funded(funded_channel);
18541852
res
@@ -2315,7 +2313,6 @@ where
23152313
monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
23162314
monitor_pending_finalized_fulfills: Vec<HTLCSource>,
23172315
monitor_pending_update_adds: Vec<msgs::UpdateAddHTLC>,
2318-
monitor_pending_tx_signatures: Option<msgs::TxSignatures>,
23192316

23202317
/// If we went to send a revoke_and_ack but our signer was unable to give us a signature,
23212318
/// we should retry at some point in the future when the signer indicates it may have a
@@ -2916,7 +2913,7 @@ where
29162913
#[rustfmt::skip]
29172914
pub fn funding_tx_constructed<L: Deref>(
29182915
&mut self, mut signing_session: InteractiveTxSigningSession, logger: &L
2919-
) -> Result<(msgs::CommitmentSigned, Option<Transaction>), ChannelError>
2916+
) -> Result<msgs::CommitmentSigned, ChannelError>
29202917
where
29212918
L::Target: Logger
29222919
{
@@ -2954,7 +2951,8 @@ where
29542951
},
29552952
};
29562953

2957-
let funding_tx_opt = if signing_session.local_inputs_count() == 0 {
2954+
// Check that we have the expected number of local inputs
2955+
if signing_session.local_inputs_count() == 0 {
29582956
debug_assert_eq!(our_funding_satoshis, 0);
29592957
if signing_session.provide_holder_witnesses(self.context.channel_id, Vec::new()).is_err() {
29602958
debug_assert!(
@@ -2965,10 +2963,7 @@ where
29652963
let reason = ClosureReason::ProcessingError { err: msg.to_owned() };
29662964
return Err(ChannelError::Close((msg.to_owned(), reason)));
29672965
}
2968-
None
2969-
} else {
2970-
Some(signing_session.unsigned_tx().build_unsigned_tx())
2971-
};
2966+
}
29722967

29732968
let mut channel_state = ChannelState::FundingNegotiated(FundingNegotiatedFlags::new());
29742969
channel_state.set_interactive_signing();
@@ -2978,7 +2973,7 @@ where
29782973
self.interactive_tx_constructor.take();
29792974
self.interactive_tx_signing_session = Some(signing_session);
29802975

2981-
Ok((commitment_signed, funding_tx_opt))
2976+
Ok(commitment_signed)
29822977
}
29832978
}
29842979

@@ -3260,7 +3255,6 @@ where
32603255
monitor_pending_failures: Vec::new(),
32613256
monitor_pending_finalized_fulfills: Vec::new(),
32623257
monitor_pending_update_adds: Vec::new(),
3263-
monitor_pending_tx_signatures: None,
32643258

32653259
signer_pending_revoke_and_ack: false,
32663260
signer_pending_commitment_update: false,
@@ -3506,7 +3500,6 @@ where
35063500
monitor_pending_failures: Vec::new(),
35073501
monitor_pending_finalized_fulfills: Vec::new(),
35083502
monitor_pending_update_adds: Vec::new(),
3509-
monitor_pending_tx_signatures: None,
35103503

35113504
signer_pending_revoke_and_ack: false,
35123505
signer_pending_commitment_update: false,
@@ -6628,7 +6621,7 @@ where
66286621
#[rustfmt::skip]
66296622
pub fn commitment_signed_initial_v2<L: Deref>(
66306623
&mut self, msg: &msgs::CommitmentSigned, best_block: BestBlock, signer_provider: &SP, logger: &L
6631-
) -> Result<ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>, ChannelError>
6624+
) -> Result<(ChannelMonitor<<SP::Target as SignerProvider>::EcdsaSigner>, Option<Transaction>), ChannelError>
66326625
where L::Target: Logger
66336626
{
66346627
if !self.context.channel_state.is_interactive_signing()
@@ -6650,15 +6643,12 @@ where
66506643

66516644
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
66526645

6653-
if let Some(tx_signatures) = self.interactive_tx_signing_session.as_mut().and_then(
6654-
|session| session.received_commitment_signed()
6655-
) {
6656-
// We're up first for submitting our tx_signatures, but our monitor has not persisted yet
6657-
// so they'll be sent as soon as that's done.
6658-
self.context.monitor_pending_tx_signatures = Some(tx_signatures);
6659-
}
6646+
let _ = self.interactive_tx_signing_session.as_mut().and_then(|session| session.received_commitment_signed());
6647+
// Only build the unsigned transaction for signing if there are any holder inputs to actually sign
6648+
let funding_tx_opt = self.interactive_tx_signing_session.as_ref().and_then(|session|
6649+
session.local_inputs_count().gt(&0).then_some(session.unsigned_tx().build_unsigned_tx()));
66606650

6661-
Ok(channel_monitor)
6651+
Ok((channel_monitor, funding_tx_opt))
66626652
}
66636653

66646654
/// Handles an incoming `commitment_signed` message for the first commitment transaction of the
@@ -6739,13 +6729,12 @@ where
67396729
channel_id: Some(self.context.channel_id()),
67406730
};
67416731

6742-
let tx_signatures = self
6732+
let _ = self
67436733
.interactive_tx_signing_session
67446734
.as_mut()
67456735
.expect("Signing session must exist for negotiated pending splice")
67466736
.received_commitment_signed();
67476737
self.monitor_updating_paused(false, false, false, Vec::new(), Vec::new(), Vec::new());
6748-
self.context.monitor_pending_tx_signatures = tx_signatures;
67496738

67506739
Ok(self.push_ret_blockable_mon_update(monitor_update))
67516740
}
@@ -7606,8 +7595,10 @@ where
76067595
.map_err(|err| APIError::APIMisuseError { err })?
76077596
{
76087597
if self.is_awaiting_initial_mon_persist() {
7609-
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
7610-
self.context.monitor_pending_tx_signatures = Some(holder_tx_signatures);
7598+
log_debug!(
7599+
logger,
7600+
"Not sending tx_signatures: a monitor update is in progress."
7601+
);
76117602
return Ok(None);
76127603
}
76137604
return Ok(Some(holder_tx_signatures));
@@ -7684,8 +7675,7 @@ where
76847675
// case checks if there is a monitor persist in progress when we need to respond with our `tx_signatures`
76857676
// and sets it as pending.
76867677
if holder_tx_signatures_opt.is_some() && self.is_awaiting_initial_mon_persist() {
7687-
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
7688-
self.context.monitor_pending_tx_signatures = holder_tx_signatures_opt;
7678+
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress.");
76897679
return Ok((None, None));
76907680
}
76917681

@@ -7941,25 +7931,14 @@ where
79417931
mem::swap(&mut finalized_claimed_htlcs, &mut self.context.monitor_pending_finalized_fulfills);
79427932
let mut pending_update_adds = Vec::new();
79437933
mem::swap(&mut pending_update_adds, &mut self.context.monitor_pending_update_adds);
7944-
// For channels established with V2 establishment we won't send a `tx_signatures` when we're in
7945-
// MonitorUpdateInProgress (and we assume the user will never directly broadcast the funding
7946-
// transaction and waits for us to do it).
7947-
let tx_signatures = self.context.monitor_pending_tx_signatures.take();
7948-
if tx_signatures.is_some() {
7949-
if self.context.channel_state.is_their_tx_signatures_sent() {
7950-
self.context.channel_state = ChannelState::AwaitingChannelReady(AwaitingChannelReadyFlags::new());
7951-
} else {
7952-
self.context.channel_state.set_our_tx_signatures_ready();
7953-
}
7954-
}
79557934

79567935
if self.context.channel_state.is_peer_disconnected() {
79577936
self.context.monitor_pending_revoke_and_ack = false;
79587937
self.context.monitor_pending_commitment_signed = false;
79597938
return MonitorRestoreUpdates {
79607939
raa: None, commitment_update: None, order: RAACommitmentOrder::RevokeAndACKFirst,
79617940
accepted_htlcs, failed_htlcs, finalized_claimed_htlcs, pending_update_adds,
7962-
funding_broadcastable, channel_ready, announcement_sigs, tx_signatures
7941+
funding_broadcastable, channel_ready, announcement_sigs
79637942
};
79647943
}
79657944

@@ -7989,7 +7968,7 @@ where
79897968
match order { RAACommitmentOrder::CommitmentFirst => "commitment", RAACommitmentOrder::RevokeAndACKFirst => "RAA"});
79907969
MonitorRestoreUpdates {
79917970
raa, commitment_update, order, accepted_htlcs, failed_htlcs, finalized_claimed_htlcs,
7992-
pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs, tx_signatures
7971+
pending_update_adds, funding_broadcastable, channel_ready, announcement_sigs
79937972
}
79947973
}
79957974

@@ -8358,7 +8337,6 @@ where
83588337
raa: None, commitment_update: None,
83598338
order: RAACommitmentOrder::CommitmentFirst,
83608339
shutdown_msg, announcement_sigs,
8361-
tx_signatures: None,
83628340
tx_abort: None,
83638341
});
83648342
}
@@ -8369,7 +8347,6 @@ where
83698347
raa: None, commitment_update: None,
83708348
order: RAACommitmentOrder::CommitmentFirst,
83718349
shutdown_msg, announcement_sigs,
8372-
tx_signatures: None,
83738350
tx_abort: None,
83748351
});
83758352
}
@@ -8414,7 +8391,7 @@ where
84148391
}
84158392

84168393
// if next_funding_txid is set:
8417-
let (commitment_update, tx_signatures, tx_abort) = if let Some(next_funding_txid) = msg.next_funding_txid {
8394+
let (commitment_update, tx_abort) = if let Some(next_funding_txid) = msg.next_funding_txid {
84188395
if let Some(session) = &self.interactive_tx_signing_session {
84198396
// if next_funding_txid matches the latest interactive funding transaction:
84208397
let our_next_funding_txid = session.unsigned_tx().compute_txid();
@@ -8435,41 +8412,14 @@ where
84358412
update_fee: None,
84368413
})
84378414
} else { None };
8438-
// TODO(dual_funding): For async signing support we need to hold back `tx_signatures` until the `commitment_signed` is ready.
8439-
let tx_signatures = if (
8440-
// if it has not received tx_signatures for that funding transaction AND
8441-
// if it has already received commitment_signed AND it should sign first, as specified in the tx_signatures requirements:
8442-
// MUST send its tx_signatures for that funding transaction.
8443-
!self.context.channel_state.is_their_tx_signatures_sent() && session.has_received_commitment_signed() && session.holder_sends_tx_signatures_first()
8444-
// else if it has already received tx_signatures for that funding transaction:
8445-
// MUST send its tx_signatures for that funding transaction.
8446-
) || self.context.channel_state.is_their_tx_signatures_sent() {
8447-
if self.context.channel_state.is_monitor_update_in_progress() {
8448-
// The `monitor_pending_tx_signatures` field should have already been set in `commitment_signed_initial_v2`
8449-
// if we were up first for signing and had a monitor update in progress, but check again just in case.
8450-
debug_assert!(self.context.monitor_pending_tx_signatures.is_some(), "monitor_pending_tx_signatures should already be set");
8451-
log_debug!(logger, "Not sending tx_signatures: a monitor update is in progress. Setting monitor_pending_tx_signatures.");
8452-
if self.context.monitor_pending_tx_signatures.is_none() {
8453-
self.context.monitor_pending_tx_signatures = session.holder_tx_signatures().clone();
8454-
}
8455-
None
8456-
} else {
8457-
// If `holder_tx_signatures` is `None` here, the `tx_signatures` message will be sent
8458-
// when the holder provides their witnesses as this will queue a `tx_signatures` if the
8459-
// holder must send one.
8460-
session.holder_tx_signatures().clone()
8461-
}
8462-
} else {
8463-
None
8464-
};
84658415
if !session.has_received_commitment_signed() {
84668416
self.context.expecting_peer_commitment_signed = true;
84678417
}
8468-
(commitment_update, tx_signatures, None)
8418+
(commitment_update, None)
84698419
} else {
84708420
// The `next_funding_txid` does not match the latest interactive funding transaction so we
84718421
// MUST send tx_abort to let the remote know that they can forget this funding transaction.
8472-
(None, None, Some(msgs::TxAbort {
8422+
(None, Some(msgs::TxAbort {
84738423
channel_id: self.context.channel_id(),
84748424
data: format!(
84758425
"next_funding_txid {} does match our latest interactive funding txid {}",
@@ -8480,22 +8430,21 @@ where
84808430
// We'll just send a `tx_abort` here if we don't have a signing session for this channel
84818431
// on reestablish and tell our peer to just forget about it.
84828432
// Our peer is doing something strange, but it doesn't warrant closing the channel.
8483-
(None, None, Some(msgs::TxAbort {
8433+
(None, Some(msgs::TxAbort {
84848434
channel_id: self.context.channel_id(),
84858435
data:
84868436
"No active signing session. The associated funding transaction may have already been broadcast.".as_bytes().to_vec() }))
84878437
}
84888438
} else {
84898439
// Don't send anything related to interactive signing if `next_funding_txid` is not set.
8490-
(None, None, None)
8440+
(None, None)
84918441
};
84928442

84938443
Ok(ReestablishResponses {
84948444
channel_ready, shutdown_msg, announcement_sigs,
84958445
raa: required_revoke,
84968446
commitment_update,
84978447
order: self.context.resend_order.clone(),
8498-
tx_signatures,
84998448
tx_abort,
85008449
})
85018450
} else if msg.next_local_commitment_number == next_counterparty_commitment_number - 1 {
@@ -8511,7 +8460,6 @@ where
85118460
channel_ready, shutdown_msg, announcement_sigs,
85128461
commitment_update: None, raa: None,
85138462
order: self.context.resend_order.clone(),
8514-
tx_signatures: None,
85158463
tx_abort: None,
85168464
})
85178465
} else {
@@ -8535,7 +8483,6 @@ where
85358483
channel_ready, shutdown_msg, announcement_sigs,
85368484
raa, commitment_update,
85378485
order: self.context.resend_order.clone(),
8538-
tx_signatures: None,
85398486
tx_abort: None,
85408487
})
85418488
}
@@ -13211,7 +13158,6 @@ where
1321113158
monitor_pending_failures,
1321213159
monitor_pending_finalized_fulfills: monitor_pending_finalized_fulfills.unwrap(),
1321313160
monitor_pending_update_adds: monitor_pending_update_adds.unwrap_or_default(),
13214-
monitor_pending_tx_signatures: None,
1321513161

1321613162
signer_pending_revoke_and_ack: false,
1321713163
signer_pending_commitment_update: false,

0 commit comments

Comments
 (0)