Skip to content

Commit 9a2b6ff

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 e73434f commit 9a2b6ff

File tree

2 files changed

+32
-35
lines changed

2 files changed

+32
-35
lines changed

lightning/src/ln/channel.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8500,7 +8500,7 @@ where
85008500
pub fn maybe_propose_closing_signed<F: Deref, L: Deref>(
85018501
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
85028502
) -> Result<
8503-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
8503+
(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>),
85048504
ChannelError,
85058505
>
85068506
where
@@ -8512,20 +8512,20 @@ where
85128512
// initiate `closing_signed` negotiation until we're clear of all pending messages. Note
85138513
// that closing_negotiation_ready checks this case (as well as a few others).
85148514
if self.context.last_sent_closing_fee.is_some() || !self.closing_negotiation_ready() {
8515-
return Ok((None, None, None));
8515+
return Ok((None, None));
85168516
}
85178517

85188518
if !self.funding.is_outbound() {
85198519
if let Some(msg) = &self.context.pending_counterparty_closing_signed.take() {
85208520
return self.closing_signed(fee_estimator, &msg, logger);
85218521
}
8522-
return Ok((None, None, None));
8522+
return Ok((None, None));
85238523
}
85248524

85258525
// If we're waiting on a counterparty `commitment_signed` to clear some updates from our
85268526
// local commitment transaction, we can't yet initiate `closing_signed` negotiation.
85278527
if self.context.expecting_peer_commitment_signed {
8528-
return Ok((None, None, None));
8528+
return Ok((None, None));
85298529
}
85308530

85318531
let (our_min_fee, our_max_fee) = self.calculate_closing_fee_limits(fee_estimator);
@@ -8544,7 +8544,7 @@ where
85448544
our_max_fee,
85458545
logger,
85468546
);
8547-
Ok((closing_signed, None, None))
8547+
Ok((closing_signed, None))
85488548
}
85498549

85508550
fn mark_response_received(&mut self) {
@@ -8808,7 +8808,7 @@ where
88088808
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, msg: &msgs::ClosingSigned,
88098809
logger: &L,
88108810
) -> Result<
8811-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
8811+
(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>),
88128812
ChannelError,
88138813
>
88148814
where
@@ -8850,7 +8850,7 @@ where
88508850

88518851
if self.context.channel_state.is_monitor_update_in_progress() {
88528852
self.context.pending_counterparty_closing_signed = Some(msg.clone());
8853-
return Ok((None, None, None));
8853+
return Ok((None, None));
88548854
}
88558855

88568856
let funding_redeemscript = self.funding.get_funding_redeemscript();
@@ -8904,7 +8904,7 @@ where
89048904
self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
89058905
self.context.channel_state = ChannelState::ShutdownComplete;
89068906
self.context.update_time_counter += 1;
8907-
return Ok((None, Some(tx), Some(shutdown_result)));
8907+
return Ok((None, Some((tx, shutdown_result))));
89088908
}
89098909
}
89108910

@@ -8927,26 +8927,25 @@ where
89278927
our_max_fee,
89288928
logger,
89298929
);
8930-
let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
8931-
let shutdown_result =
8932-
closing_signed.as_ref().map(|_| self.shutdown_result_coop_close());
8933-
if closing_signed.is_some() {
8934-
self.context.channel_state = ChannelState::ShutdownComplete;
8935-
}
8930+
let signed_tx_shutdown = if $new_fee == msg.fee_satoshis {
89368931
self.context.update_time_counter += 1;
89378932
self.context.last_received_closing_sig = Some(msg.signature.clone());
8938-
let tx = closing_signed.as_ref().map(|ClosingSigned { signature, .. }| {
8939-
self.build_signed_closing_transaction(
8933+
if let Some(ClosingSigned { signature, .. }) = &closing_signed {
8934+
let shutdown_result = self.shutdown_result_coop_close();
8935+
self.context.channel_state = ChannelState::ShutdownComplete;
8936+
let tx = self.build_signed_closing_transaction(
89408937
&closing_tx,
89418938
&msg.signature,
89428939
signature,
8943-
)
8944-
});
8945-
(tx, shutdown_result)
8940+
);
8941+
Some((tx, shutdown_result))
8942+
} else {
8943+
None
8944+
}
89468945
} else {
8947-
(None, None)
8946+
None
89488947
};
8949-
return Ok((closing_signed, signed_tx, shutdown_result))
8948+
return Ok((closing_signed, signed_tx_shutdown))
89508949
};
89518950
}
89528951

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9261,26 +9261,26 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
92619261
hash_map::Entry::Occupied(mut chan_entry) => {
92629262
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
92639263
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
9264-
let (closing_signed, tx, shutdown_result) = try_channel_entry!(self, peer_state, chan.closing_signed(&self.fee_estimator, &msg, &&logger), chan_entry);
9265-
debug_assert_eq!(shutdown_result.is_some(), chan.is_shutdown());
9264+
let res = chan.closing_signed(&self.fee_estimator, &msg, &&logger);
9265+
let (closing_signed, tx_shutdown_result) =
9266+
try_channel_entry!(self, peer_state, res, chan_entry);
9267+
debug_assert_eq!(tx_shutdown_result.is_some(), chan.is_shutdown());
92669268
if let Some(msg) = closing_signed {
92679269
peer_state.pending_msg_events.push(MessageSendEvent::SendClosingSigned {
92689270
node_id: counterparty_node_id.clone(),
92699271
msg,
92709272
});
92719273
}
9272-
if let Some(mut close_res) = shutdown_result {
9274+
if let Some((tx, mut close_res)) = tx_shutdown_result {
92739275
// We're done with this channel, we've got a signed closing transaction and
92749276
// will send the closing_signed back to the remote peer upon return. This
92759277
// also implies there are no pending HTLCs left on the channel, so we can
92769278
// fully delete it from tracking (the channel monitor is still around to
92779279
// watch for old state broadcasts)!
9278-
debug_assert!(tx.is_some());
92799280
locked_close_channel!(self, peer_state, chan, close_res, FUNDED);
9280-
(tx, Some(chan_entry.remove()), Some(close_res))
9281+
(Some(tx), Some(chan_entry.remove()), Some(close_res))
92819282
} else {
9282-
debug_assert!(tx.is_none());
9283-
(tx, None, None)
9283+
(None, None, None)
92849284
}
92859285
} else {
92869286
return try_channel_entry!(self, peer_state, Err(ChannelError::close(
@@ -10480,19 +10480,17 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1048010480
Some(funded_chan) => {
1048110481
let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
1048210482
match funded_chan.maybe_propose_closing_signed(&self.fee_estimator, &&logger) {
10483-
Ok((msg_opt, tx_opt, shutdown_result_opt)) => {
10483+
Ok((msg_opt, tx_shutdown_result_opt)) => {
1048410484
if let Some(msg) = msg_opt {
1048510485
has_update = true;
1048610486
pending_msg_events.push(MessageSendEvent::SendClosingSigned {
1048710487
node_id: funded_chan.context.get_counterparty_node_id(), msg,
1048810488
});
1048910489
}
10490-
debug_assert_eq!(shutdown_result_opt.is_some(), funded_chan.is_shutdown());
10491-
if let Some(mut shutdown_result) = shutdown_result_opt {
10492-
locked_close_channel!(self, peer_state, funded_chan, shutdown_result, FUNDED);
10493-
shutdown_results.push(shutdown_result);
10494-
}
10495-
if let Some(tx) = tx_opt {
10490+
debug_assert_eq!(tx_shutdown_result_opt.is_some(), funded_chan.is_shutdown());
10491+
if let Some((tx, mut shutdown_res)) = tx_shutdown_result_opt {
10492+
locked_close_channel!(self, peer_state, funded_chan, shutdown_res, FUNDED);
10493+
shutdown_results.push(shutdown_res);
1049610494
// We're done with this channel. We got a closing_signed and sent back
1049710495
// a closing_signed with a closing transaction to broadcast.
1049810496
if let Ok(update) = self.get_channel_update_for_broadcast(&funded_chan) {

0 commit comments

Comments
 (0)