Skip to content

Commit e238ac0

Browse files
committed
Limit return value of coop close methods to possible states
Our coop close methods `Channel::maybe_propose_closing_signed` and `Channel::closing_signed` may return a `Transaction` to broadcast as well as a `ShutdownResult` to provide the post-shutdown handling fields. However, it only does either both of them or neither - we only and always broadcast when we're done closing. Here we tweak the return values to match the possible states, combining the two fields in the return value into a single `Option`.
1 parent 4e4f128 commit e238ac0

File tree

2 files changed

+32
-41
lines changed

2 files changed

+32
-41
lines changed

lightning/src/ln/channel.rs

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8835,10 +8835,7 @@ where
88358835

88368836
pub fn maybe_propose_closing_signed<F: Deref, L: Deref>(
88378837
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
8838-
) -> Result<
8839-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
8840-
ChannelError,
8841-
>
8838+
) -> Result<(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>), ChannelError>
88428839
where
88438840
F::Target: FeeEstimator,
88448841
L::Target: Logger,
@@ -8848,20 +8845,20 @@ where
88488845
// initiate `closing_signed` negotiation until we're clear of all pending messages. Note
88498846
// that closing_negotiation_ready checks this case (as well as a few others).
88508847
if self.context.last_sent_closing_fee.is_some() || !self.closing_negotiation_ready() {
8851-
return Ok((None, None, None));
8848+
return Ok((None, None));
88528849
}
88538850

88548851
if !self.funding.is_outbound() {
88558852
if let Some(msg) = &self.context.pending_counterparty_closing_signed.take() {
88568853
return self.closing_signed(fee_estimator, &msg, logger);
88578854
}
8858-
return Ok((None, None, None));
8855+
return Ok((None, None));
88598856
}
88608857

88618858
// If we're waiting on a counterparty `commitment_signed` to clear some updates from our
88628859
// local commitment transaction, we can't yet initiate `closing_signed` negotiation.
88638860
if self.context.expecting_peer_commitment_signed {
8864-
return Ok((None, None, None));
8861+
return Ok((None, None));
88658862
}
88668863

88678864
let (our_min_fee, our_max_fee) = self.calculate_closing_fee_limits(fee_estimator);
@@ -8880,7 +8877,7 @@ where
88808877
our_max_fee,
88818878
logger,
88828879
);
8883-
Ok((closing_signed, None, None))
8880+
Ok((closing_signed, None))
88848881
}
88858882

88868883
fn mark_response_received(&mut self) {
@@ -9143,10 +9140,7 @@ where
91439140
pub fn closing_signed<F: Deref, L: Deref>(
91449141
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, msg: &msgs::ClosingSigned,
91459142
logger: &L,
9146-
) -> Result<
9147-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
9148-
ChannelError,
9149-
>
9143+
) -> Result<(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>), ChannelError>
91509144
where
91519145
F::Target: FeeEstimator,
91529146
L::Target: Logger,
@@ -9186,7 +9180,7 @@ where
91869180

91879181
if self.context.channel_state.is_monitor_update_in_progress() {
91889182
self.context.pending_counterparty_closing_signed = Some(msg.clone());
9189-
return Ok((None, None, None));
9183+
return Ok((None, None));
91909184
}
91919185

91929186
let funding_redeemscript = self.funding.get_funding_redeemscript();
@@ -9240,7 +9234,7 @@ where
92409234
self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
92419235
self.context.channel_state = ChannelState::ShutdownComplete;
92429236
self.context.update_time_counter += 1;
9243-
return Ok((None, Some(tx), Some(shutdown_result)));
9237+
return Ok((None, Some((tx, shutdown_result))));
92449238
}
92459239
}
92469240

@@ -9263,26 +9257,25 @@ where
92639257
our_max_fee,
92649258
logger,
92659259
);
9266-
let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
9267-
let shutdown_result =
9268-
closing_signed.as_ref().map(|_| self.shutdown_result_coop_close());
9269-
if closing_signed.is_some() {
9270-
self.context.channel_state = ChannelState::ShutdownComplete;
9271-
}
9260+
let signed_tx_shutdown = if $new_fee == msg.fee_satoshis {
92729261
self.context.update_time_counter += 1;
92739262
self.context.last_received_closing_sig = Some(msg.signature.clone());
9274-
let tx = closing_signed.as_ref().map(|ClosingSigned { signature, .. }| {
9275-
self.build_signed_closing_transaction(
9263+
if let Some(ClosingSigned { signature, .. }) = &closing_signed {
9264+
let shutdown_result = self.shutdown_result_coop_close();
9265+
self.context.channel_state = ChannelState::ShutdownComplete;
9266+
let tx = self.build_signed_closing_transaction(
92769267
&closing_tx,
92779268
&msg.signature,
92789269
signature,
9279-
)
9280-
});
9281-
(tx, shutdown_result)
9270+
);
9271+
Some((tx, shutdown_result))
9272+
} else {
9273+
None
9274+
}
92829275
} else {
9283-
(None, None)
9276+
None
92849277
};
9285-
return Ok((closing_signed, signed_tx, shutdown_result))
9278+
return Ok((closing_signed, signed_tx_shutdown))
92869279
};
92879280
}
92889281

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9795,26 +9795,26 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
97959795
hash_map::Entry::Occupied(mut chan_entry) => {
97969796
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
97979797
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
9798-
let (closing_signed, tx, shutdown_result) = try_channel_entry!(self, peer_state, chan.closing_signed(&self.fee_estimator, &msg, &&logger), chan_entry);
9799-
debug_assert_eq!(shutdown_result.is_some(), chan.is_shutdown());
9798+
let res = chan.closing_signed(&self.fee_estimator, &msg, &&logger);
9799+
let (closing_signed, tx_shutdown_result) =
9800+
try_channel_entry!(self, peer_state, res, chan_entry);
9801+
debug_assert_eq!(tx_shutdown_result.is_some(), chan.is_shutdown());
98009802
if let Some(msg) = closing_signed {
98019803
peer_state.pending_msg_events.push(MessageSendEvent::SendClosingSigned {
98029804
node_id: counterparty_node_id.clone(),
98039805
msg,
98049806
});
98059807
}
9806-
if let Some(mut close_res) = shutdown_result {
9808+
if let Some((tx, mut close_res)) = tx_shutdown_result {
98079809
// We're done with this channel, we've got a signed closing transaction and
98089810
// will send the closing_signed back to the remote peer upon return. This
98099811
// also implies there are no pending HTLCs left on the channel, so we can
98109812
// fully delete it from tracking (the channel monitor is still around to
98119813
// watch for old state broadcasts)!
9812-
debug_assert!(tx.is_some());
98139814
locked_close_channel!(self, peer_state, chan, close_res, FUNDED);
9814-
(tx, Some(chan_entry.remove()), Some(close_res))
9815+
(Some(tx), Some(chan_entry.remove()), Some(close_res))
98159816
} else {
9816-
debug_assert!(tx.is_none());
9817-
(tx, None, None)
9817+
(None, None, None)
98189818
}
98199819
} else {
98209820
return try_channel_entry!(self, peer_state, Err(ChannelError::close(
@@ -11121,19 +11121,17 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1112111121
Some(funded_chan) => {
1112211122
let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
1112311123
match funded_chan.maybe_propose_closing_signed(&self.fee_estimator, &&logger) {
11124-
Ok((msg_opt, tx_opt, shutdown_result_opt)) => {
11124+
Ok((msg_opt, tx_shutdown_result_opt)) => {
1112511125
if let Some(msg) = msg_opt {
1112611126
has_update = true;
1112711127
pending_msg_events.push(MessageSendEvent::SendClosingSigned {
1112811128
node_id: funded_chan.context.get_counterparty_node_id(), msg,
1112911129
});
1113011130
}
11131-
debug_assert_eq!(shutdown_result_opt.is_some(), funded_chan.is_shutdown());
11132-
if let Some(mut shutdown_result) = shutdown_result_opt {
11133-
locked_close_channel!(self, peer_state, funded_chan, shutdown_result, FUNDED);
11134-
shutdown_results.push(shutdown_result);
11135-
}
11136-
if let Some(tx) = tx_opt {
11131+
debug_assert_eq!(tx_shutdown_result_opt.is_some(), funded_chan.is_shutdown());
11132+
if let Some((tx, mut shutdown_res)) = tx_shutdown_result_opt {
11133+
locked_close_channel!(self, peer_state, funded_chan, shutdown_res, FUNDED);
11134+
shutdown_results.push(shutdown_res);
1113711135
// We're done with this channel. We got a closing_signed and sent back
1113811136
// a closing_signed with a closing transaction to broadcast.
1113911137
if let Ok(update) = self.get_channel_update_for_broadcast(&funded_chan) {

0 commit comments

Comments
 (0)