@@ -909,6 +909,8 @@ pub(super) struct SignerResumeUpdates {
909
909
pub commitment_update: Option<msgs::CommitmentUpdate>,
910
910
pub funding_signed: Option<msgs::FundingSigned>,
911
911
pub channel_ready: Option<msgs::ChannelReady>,
912
+ pub closing_signed: Option<msgs::ClosingSigned>,
913
+ pub signed_closing_tx: Option<Transaction>,
912
914
}
913
915
914
916
/// The return value of `channel_reestablish`
@@ -1227,6 +1229,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
1227
1229
/// [`msgs::FundingCreated`] or [`msgs::FundingSigned`] depending on if this channel is
1228
1230
/// outbound or inbound.
1229
1231
signer_pending_funding: bool,
1232
+ /// If we attempted to sign a cooperative close transaction but the signer wasn't ready, then this
1233
+ /// will be set to `true`.
1234
+ signer_pending_closing: bool,
1230
1235
1231
1236
// pending_update_fee is filled when sending and receiving update_fee.
1232
1237
//
@@ -1258,7 +1263,9 @@ pub(super) struct ChannelContext<SP: Deref> where SP::Target: SignerProvider {
1258
1263
/// Max to_local and to_remote outputs in a remote-generated commitment transaction
1259
1264
counterparty_max_commitment_tx_output: Mutex<(u64, u64)>,
1260
1265
1261
- last_sent_closing_fee: Option<(u64, Signature)>, // (fee, holder_sig)
1266
+ // (fee, skip_remote_output, fee_range, holder_sig)
1267
+ last_sent_closing_fee: Option<(u64, bool, ClosingSignedFeeRange, Option<Signature>)>,
1268
+ last_received_closing_sig: Option<Signature>,
1262
1269
target_closing_feerate_sats_per_kw: Option<u32>,
1263
1270
1264
1271
/// If our counterparty sent us a closing_signed while we were waiting for a `ChannelMonitor`
@@ -1686,6 +1693,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1686
1693
1687
1694
signer_pending_commitment_update: false,
1688
1695
signer_pending_funding: false,
1696
+ signer_pending_closing: false,
1689
1697
1690
1698
1691
1699
#[cfg(debug_assertions)]
@@ -1694,6 +1702,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1694
1702
counterparty_max_commitment_tx_output: Mutex::new((value_to_self_msat, (channel_value_satoshis * 1000 - msg_push_msat).saturating_sub(value_to_self_msat))),
1695
1703
1696
1704
last_sent_closing_fee: None,
1705
+ last_received_closing_sig: None,
1697
1706
pending_counterparty_closing_signed: None,
1698
1707
expecting_peer_commitment_signed: false,
1699
1708
closing_fee_limits: None,
@@ -1911,6 +1920,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1911
1920
1912
1921
signer_pending_commitment_update: false,
1913
1922
signer_pending_funding: false,
1923
+ signer_pending_closing: false,
1914
1924
1915
1925
// We'll add our counterparty's `funding_satoshis` to these max commitment output assertions
1916
1926
// when we receive `accept_channel2`.
@@ -1920,6 +1930,7 @@ impl<SP: Deref> ChannelContext<SP> where SP::Target: SignerProvider {
1920
1930
counterparty_max_commitment_tx_output: Mutex::new((channel_value_satoshis * 1000 - push_msat, push_msat)),
1921
1931
1922
1932
last_sent_closing_fee: None,
1933
+ last_received_closing_sig: None,
1923
1934
pending_counterparty_closing_signed: None,
1924
1935
expecting_peer_commitment_signed: false,
1925
1936
closing_fee_limits: None,
@@ -5387,15 +5398,35 @@ impl<SP: Deref> Channel<SP> where
5387
5398
self.check_get_channel_ready(0, logger)
5388
5399
} else { None };
5389
5400
5390
- log_trace!(logger, "Signer unblocked with {} commitment_update, {} funding_signed and {} channel_ready",
5401
+ let (closing_signed, signed_closing_tx) = if self.context.signer_pending_closing {
5402
+ debug_assert!(self.context.last_sent_closing_fee.is_some());
5403
+ if let Some((fee, skip_remote_output, fee_range, holder_sig)) = self.context.last_sent_closing_fee.clone() {
5404
+ debug_assert!(holder_sig.is_none());
5405
+ log_trace!(logger, "Attempting to generate pending closing_signed...");
5406
+ let (closing_tx, fee) = self.build_closing_transaction(fee, skip_remote_output);
5407
+ let closing_signed = self.get_closing_signed_msg(&closing_tx, skip_remote_output,
5408
+ fee, fee_range.min_fee_satoshis, fee_range.max_fee_satoshis, logger);
5409
+ let signed_tx = if let (Some(ClosingSigned { signature, .. }), Some(counterparty_sig)) =
5410
+ (closing_signed.as_ref(), self.context.last_received_closing_sig) {
5411
+ Some(self.build_signed_closing_transaction(&closing_tx, &counterparty_sig, signature))
5412
+ } else { None };
5413
+ (closing_signed, signed_tx)
5414
+ } else { (None, None) }
5415
+ } else { (None, None) };
5416
+
5417
+ log_trace!(logger, "Signer unblocked with {} commitment_update, {} funding_signed, {} channel_ready, {} closing_signed, and {} signed_closing_tx",
5391
5418
if commitment_update.is_some() { "a" } else { "no" },
5392
5419
if funding_signed.is_some() { "a" } else { "no" },
5393
- if channel_ready.is_some() { "a" } else { "no" });
5420
+ if channel_ready.is_some() { "a" } else { "no" },
5421
+ if closing_signed.is_some() { "a" } else { "no" },
5422
+ if signed_closing_tx.is_some() { "a" } else { "no" });
5394
5423
5395
5424
SignerResumeUpdates {
5396
5425
commitment_update,
5397
5426
funding_signed,
5398
5427
channel_ready,
5428
+ closing_signed,
5429
+ signed_closing_tx,
5399
5430
}
5400
5431
}
5401
5432
@@ -5801,9 +5832,6 @@ impl<SP: Deref> Channel<SP> where
5801
5832
our_min_fee, our_max_fee, total_fee_satoshis);
5802
5833
5803
5834
let closing_signed = self.get_closing_signed_msg(&closing_tx, false, total_fee_satoshis, our_min_fee, our_max_fee, logger);
5804
- if closing_signed.is_none() {
5805
- return Err(ChannelError::close("Failed to get signature for closing transaction.".to_owned()));
5806
- }
5807
5835
Ok((closing_signed, None, None))
5808
5836
}
5809
5837
@@ -5957,26 +5985,26 @@ impl<SP: Deref> Channel<SP> where
5957
5985
) -> Option<msgs::ClosingSigned>
5958
5986
where L::Target: Logger
5959
5987
{
5960
- match &self.context.holder_signer {
5961
- ChannelSignerType::Ecdsa(ecdsa) => {
5962
- let fee_range = msgs::ClosingSignedFeeRange {
5963
- min_fee_satoshis,
5964
- max_fee_satoshis,
5965
- };
5966
- let sig = ecdsa.sign_closing_transaction(closing_tx, &self.context.secp_ctx).ok()?;
5967
-
5968
- self.context.last_sent_closing_fee = Some((fee_satoshis, sig.clone()));
5969
- Some(msgs::ClosingSigned {
5970
- channel_id: self.context.channel_id,
5971
- fee_satoshis,
5972
- signature: sig,
5973
- fee_range: Some(fee_range),
5974
- })
5975
- },
5988
+ let sig = match &self.context.holder_signer {
5989
+ ChannelSignerType::Ecdsa(ecdsa) => ecdsa.sign_closing_transaction(closing_tx, &self.context.secp_ctx).ok(),
5976
5990
// TODO (taproot|arik)
5977
5991
#[cfg(taproot)]
5978
5992
_ => todo!()
5993
+ };
5994
+ if sig.is_none() {
5995
+ log_trace!(logger, "Closing transaction signature unavailable, waiting on signer");
5996
+ self.context.signer_pending_closing = true;
5997
+ } else {
5998
+ self.context.signer_pending_closing = false;
5979
5999
}
6000
+ let fee_range = msgs::ClosingSignedFeeRange { min_fee_satoshis, max_fee_satoshis };
6001
+ self.context.last_sent_closing_fee = Some((fee_satoshis, skip_remote_output, fee_range.clone(), sig.clone()));
6002
+ sig.map(|signature| msgs::ClosingSigned {
6003
+ channel_id: self.context.channel_id,
6004
+ fee_satoshis,
6005
+ signature,
6006
+ fee_range: Some(fee_range),
6007
+ })
5980
6008
}
5981
6009
5982
6010
pub fn closing_signed<F: Deref, L: Deref>(
@@ -6039,7 +6067,7 @@ impl<SP: Deref> Channel<SP> where
6039
6067
};
6040
6068
6041
6069
assert!(self.context.shutdown_scriptpubkey.is_some());
6042
- if let Some((last_fee, sig)) = self.context.last_sent_closing_fee {
6070
+ if let Some((last_fee, _, _, Some( sig) )) = self.context.last_sent_closing_fee {
6043
6071
if last_fee == msg.fee_satoshis {
6044
6072
let shutdown_result = ShutdownResult {
6045
6073
closure_reason,
@@ -6072,9 +6100,6 @@ impl<SP: Deref> Channel<SP> where
6072
6100
};
6073
6101
6074
6102
let closing_signed = self.get_closing_signed_msg(&closing_tx, skip_remote_output, used_fee, our_min_fee, our_max_fee, logger);
6075
- if closing_signed.is_none() {
6076
- return Err(ChannelError::close("Failed to get signature for closing transaction.".to_owned()));
6077
- }
6078
6103
let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
6079
6104
let shutdown_result = ShutdownResult {
6080
6105
closure_reason,
@@ -6090,6 +6115,7 @@ impl<SP: Deref> Channel<SP> where
6090
6115
};
6091
6116
self.context.channel_state = ChannelState::ShutdownComplete;
6092
6117
self.context.update_time_counter += 1;
6118
+ self.context.last_received_closing_sig = Some(msg.signature.clone());
6093
6119
let tx = closing_signed.as_ref().map(|ClosingSigned { signature, .. }|
6094
6120
self.build_signed_closing_transaction(&closing_tx, &msg.signature, signature));
6095
6121
(tx, Some(shutdown_result))
@@ -6127,7 +6153,7 @@ impl<SP: Deref> Channel<SP> where
6127
6153
} else {
6128
6154
// Old fee style negotiation. We don't bother to enforce whether they are complying
6129
6155
// with the "making progress" requirements, we just comply and hope for the best.
6130
- if let Some((last_fee, _)) = self.context.last_sent_closing_fee {
6156
+ if let Some((last_fee, _, _, _ )) = self.context.last_sent_closing_fee {
6131
6157
if msg.fee_satoshis > last_fee {
6132
6158
if msg.fee_satoshis < our_max_fee {
6133
6159
propose_fee!(msg.fee_satoshis);
@@ -9282,6 +9308,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9282
9308
9283
9309
signer_pending_commitment_update: false,
9284
9310
signer_pending_funding: false,
9311
+ signer_pending_closing: false,
9285
9312
9286
9313
pending_update_fee,
9287
9314
holding_cell_update_fee,
@@ -9296,6 +9323,7 @@ impl<'a, 'b, 'c, ES: Deref, SP: Deref> ReadableArgs<(&'a ES, &'b SP, u32, &'c Ch
9296
9323
counterparty_max_commitment_tx_output: Mutex::new((0, 0)),
9297
9324
9298
9325
last_sent_closing_fee: None,
9326
+ last_received_closing_sig: None,
9299
9327
pending_counterparty_closing_signed: None,
9300
9328
expecting_peer_commitment_signed: false,
9301
9329
closing_fee_limits: None,
0 commit comments