Skip to content

Commit e3cf4b0

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 544175b commit e3cf4b0

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
@@ -8735,7 +8735,7 @@ where
87358735
pub fn maybe_propose_closing_signed<F: Deref, L: Deref>(
87368736
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
87378737
) -> Result<
8738-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
8738+
(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>),
87398739
ChannelError,
87408740
>
87418741
where
@@ -8747,20 +8747,20 @@ where
87478747
// initiate `closing_signed` negotiation until we're clear of all pending messages. Note
87488748
// that closing_negotiation_ready checks this case (as well as a few others).
87498749
if self.context.last_sent_closing_fee.is_some() || !self.closing_negotiation_ready() {
8750-
return Ok((None, None, None));
8750+
return Ok((None, None));
87518751
}
87528752

87538753
if !self.funding.is_outbound() {
87548754
if let Some(msg) = &self.context.pending_counterparty_closing_signed.take() {
87558755
return self.closing_signed(fee_estimator, &msg, logger);
87568756
}
8757-
return Ok((None, None, None));
8757+
return Ok((None, None));
87588758
}
87598759

87608760
// If we're waiting on a counterparty `commitment_signed` to clear some updates from our
87618761
// local commitment transaction, we can't yet initiate `closing_signed` negotiation.
87628762
if self.context.expecting_peer_commitment_signed {
8763-
return Ok((None, None, None));
8763+
return Ok((None, None));
87648764
}
87658765

87668766
let (our_min_fee, our_max_fee) = self.calculate_closing_fee_limits(fee_estimator);
@@ -8779,7 +8779,7 @@ where
87798779
our_max_fee,
87808780
logger,
87818781
);
8782-
Ok((closing_signed, None, None))
8782+
Ok((closing_signed, None))
87838783
}
87848784

87858785
fn mark_response_received(&mut self) {
@@ -9043,7 +9043,7 @@ where
90439043
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, msg: &msgs::ClosingSigned,
90449044
logger: &L,
90459045
) -> Result<
9046-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
9046+
(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>),
90479047
ChannelError,
90489048
>
90499049
where
@@ -9085,7 +9085,7 @@ where
90859085

90869086
if self.context.channel_state.is_monitor_update_in_progress() {
90879087
self.context.pending_counterparty_closing_signed = Some(msg.clone());
9088-
return Ok((None, None, None));
9088+
return Ok((None, None));
90899089
}
90909090

90919091
let funding_redeemscript = self.funding.get_funding_redeemscript();
@@ -9139,7 +9139,7 @@ where
91399139
self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
91409140
self.context.channel_state = ChannelState::ShutdownComplete;
91419141
self.context.update_time_counter += 1;
9142-
return Ok((None, Some(tx), Some(shutdown_result)));
9142+
return Ok((None, Some((tx, shutdown_result))));
91439143
}
91449144
}
91459145

@@ -9162,26 +9162,25 @@ where
91629162
our_max_fee,
91639163
logger,
91649164
);
9165-
let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
9166-
let shutdown_result =
9167-
closing_signed.as_ref().map(|_| self.shutdown_result_coop_close());
9168-
if closing_signed.is_some() {
9169-
self.context.channel_state = ChannelState::ShutdownComplete;
9170-
}
9165+
let signed_tx_shutdown = if $new_fee == msg.fee_satoshis {
91719166
self.context.update_time_counter += 1;
91729167
self.context.last_received_closing_sig = Some(msg.signature.clone());
9173-
let tx = closing_signed.as_ref().map(|ClosingSigned { signature, .. }| {
9174-
self.build_signed_closing_transaction(
9168+
if let Some(ClosingSigned { signature, .. }) = &closing_signed {
9169+
let shutdown_result = self.shutdown_result_coop_close();
9170+
self.context.channel_state = ChannelState::ShutdownComplete;
9171+
let tx = self.build_signed_closing_transaction(
91759172
&closing_tx,
91769173
&msg.signature,
91779174
signature,
9178-
)
9179-
});
9180-
(tx, shutdown_result)
9175+
);
9176+
Some((tx, shutdown_result))
9177+
} else {
9178+
None
9179+
}
91819180
} else {
9182-
(None, None)
9181+
None
91839182
};
9184-
return Ok((closing_signed, signed_tx, shutdown_result))
9183+
return Ok((closing_signed, signed_tx_shutdown))
91859184
};
91869185
}
91879186

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9290,26 +9290,26 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
92909290
hash_map::Entry::Occupied(mut chan_entry) => {
92919291
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
92929292
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
9293-
let (closing_signed, tx, shutdown_result) = try_channel_entry!(self, peer_state, chan.closing_signed(&self.fee_estimator, &msg, &&logger), chan_entry);
9294-
debug_assert_eq!(shutdown_result.is_some(), chan.is_shutdown());
9293+
let res = chan.closing_signed(&self.fee_estimator, &msg, &&logger);
9294+
let (closing_signed, tx_shutdown_result) =
9295+
try_channel_entry!(self, peer_state, res, chan_entry);
9296+
debug_assert_eq!(tx_shutdown_result.is_some(), chan.is_shutdown());
92959297
if let Some(msg) = closing_signed {
92969298
peer_state.pending_msg_events.push(MessageSendEvent::SendClosingSigned {
92979299
node_id: counterparty_node_id.clone(),
92989300
msg,
92999301
});
93009302
}
9301-
if let Some(mut close_res) = shutdown_result {
9303+
if let Some((tx, mut close_res)) = tx_shutdown_result {
93029304
// We're done with this channel, we've got a signed closing transaction and
93039305
// will send the closing_signed back to the remote peer upon return. This
93049306
// also implies there are no pending HTLCs left on the channel, so we can
93059307
// fully delete it from tracking (the channel monitor is still around to
93069308
// watch for old state broadcasts)!
9307-
debug_assert!(tx.is_some());
93089309
locked_close_channel!(self, peer_state, chan, close_res, FUNDED);
9309-
(tx, Some(chan_entry.remove()), Some(close_res))
9310+
(Some(tx), Some(chan_entry.remove()), Some(close_res))
93109311
} else {
9311-
debug_assert!(tx.is_none());
9312-
(tx, None, None)
9312+
(None, None, None)
93139313
}
93149314
} else {
93159315
return try_channel_entry!(self, peer_state, Err(ChannelError::close(
@@ -10509,19 +10509,17 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1050910509
Some(funded_chan) => {
1051010510
let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
1051110511
match funded_chan.maybe_propose_closing_signed(&self.fee_estimator, &&logger) {
10512-
Ok((msg_opt, tx_opt, shutdown_result_opt)) => {
10512+
Ok((msg_opt, tx_shutdown_result_opt)) => {
1051310513
if let Some(msg) = msg_opt {
1051410514
has_update = true;
1051510515
pending_msg_events.push(MessageSendEvent::SendClosingSigned {
1051610516
node_id: funded_chan.context.get_counterparty_node_id(), msg,
1051710517
});
1051810518
}
10519-
debug_assert_eq!(shutdown_result_opt.is_some(), funded_chan.is_shutdown());
10520-
if let Some(mut shutdown_result) = shutdown_result_opt {
10521-
locked_close_channel!(self, peer_state, funded_chan, shutdown_result, FUNDED);
10522-
shutdown_results.push(shutdown_result);
10523-
}
10524-
if let Some(tx) = tx_opt {
10519+
debug_assert_eq!(tx_shutdown_result_opt.is_some(), funded_chan.is_shutdown());
10520+
if let Some((tx, mut shutdown_res)) = tx_shutdown_result_opt {
10521+
locked_close_channel!(self, peer_state, funded_chan, shutdown_res, FUNDED);
10522+
shutdown_results.push(shutdown_res);
1052510523
// We're done with this channel. We got a closing_signed and sent back
1052610524
// a closing_signed with a closing transaction to broadcast.
1052710525
if let Ok(update) = self.get_channel_update_for_broadcast(&funded_chan) {

0 commit comments

Comments
 (0)