Skip to content

Commit 17949bc

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 3ae86c0 commit 17949bc

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
@@ -8643,7 +8643,7 @@ where
86438643
pub fn maybe_propose_closing_signed<F: Deref, L: Deref>(
86448644
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
86458645
) -> Result<
8646-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
8646+
(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>),
86478647
ChannelError,
86488648
>
86498649
where
@@ -8655,20 +8655,20 @@ where
86558655
// initiate `closing_signed` negotiation until we're clear of all pending messages. Note
86568656
// that closing_negotiation_ready checks this case (as well as a few others).
86578657
if self.context.last_sent_closing_fee.is_some() || !self.closing_negotiation_ready() {
8658-
return Ok((None, None, None));
8658+
return Ok((None, None));
86598659
}
86608660

86618661
if !self.funding.is_outbound() {
86628662
if let Some(msg) = &self.context.pending_counterparty_closing_signed.take() {
86638663
return self.closing_signed(fee_estimator, &msg, logger);
86648664
}
8665-
return Ok((None, None, None));
8665+
return Ok((None, None));
86668666
}
86678667

86688668
// If we're waiting on a counterparty `commitment_signed` to clear some updates from our
86698669
// local commitment transaction, we can't yet initiate `closing_signed` negotiation.
86708670
if self.context.expecting_peer_commitment_signed {
8671-
return Ok((None, None, None));
8671+
return Ok((None, None));
86728672
}
86738673

86748674
let (our_min_fee, our_max_fee) = self.calculate_closing_fee_limits(fee_estimator);
@@ -8687,7 +8687,7 @@ where
86878687
our_max_fee,
86888688
logger,
86898689
);
8690-
Ok((closing_signed, None, None))
8690+
Ok((closing_signed, None))
86918691
}
86928692

86938693
fn mark_response_received(&mut self) {
@@ -8951,7 +8951,7 @@ where
89518951
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, msg: &msgs::ClosingSigned,
89528952
logger: &L,
89538953
) -> Result<
8954-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
8954+
(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>),
89558955
ChannelError,
89568956
>
89578957
where
@@ -8993,7 +8993,7 @@ where
89938993

89948994
if self.context.channel_state.is_monitor_update_in_progress() {
89958995
self.context.pending_counterparty_closing_signed = Some(msg.clone());
8996-
return Ok((None, None, None));
8996+
return Ok((None, None));
89978997
}
89988998

89998999
let funding_redeemscript = self.funding.get_funding_redeemscript();
@@ -9047,7 +9047,7 @@ where
90479047
self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
90489048
self.context.channel_state = ChannelState::ShutdownComplete;
90499049
self.context.update_time_counter += 1;
9050-
return Ok((None, Some(tx), Some(shutdown_result)));
9050+
return Ok((None, Some((tx, shutdown_result))));
90519051
}
90529052
}
90539053

@@ -9070,26 +9070,25 @@ where
90709070
our_max_fee,
90719071
logger,
90729072
);
9073-
let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
9074-
let shutdown_result =
9075-
closing_signed.as_ref().map(|_| self.shutdown_result_coop_close());
9076-
if closing_signed.is_some() {
9077-
self.context.channel_state = ChannelState::ShutdownComplete;
9078-
}
9073+
let signed_tx_shutdown = if $new_fee == msg.fee_satoshis {
90799074
self.context.update_time_counter += 1;
90809075
self.context.last_received_closing_sig = Some(msg.signature.clone());
9081-
let tx = closing_signed.as_ref().map(|ClosingSigned { signature, .. }| {
9082-
self.build_signed_closing_transaction(
9076+
if let Some(ClosingSigned { signature, .. }) = &closing_signed {
9077+
let shutdown_result = self.shutdown_result_coop_close();
9078+
self.context.channel_state = ChannelState::ShutdownComplete;
9079+
let tx = self.build_signed_closing_transaction(
90839080
&closing_tx,
90849081
&msg.signature,
90859082
signature,
9086-
)
9087-
});
9088-
(tx, shutdown_result)
9083+
);
9084+
Some((tx, shutdown_result))
9085+
} else {
9086+
None
9087+
}
90899088
} else {
9090-
(None, None)
9089+
None
90919090
};
9092-
return Ok((closing_signed, signed_tx, shutdown_result))
9091+
return Ok((closing_signed, signed_tx_shutdown))
90939092
};
90949093
}
90959094

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9319,26 +9319,26 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
93199319
hash_map::Entry::Occupied(mut chan_entry) => {
93209320
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
93219321
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
9322-
let (closing_signed, tx, shutdown_result) = try_channel_entry!(self, peer_state, chan.closing_signed(&self.fee_estimator, &msg, &&logger), chan_entry);
9323-
debug_assert_eq!(shutdown_result.is_some(), chan.is_shutdown());
9322+
let res = chan.closing_signed(&self.fee_estimator, &msg, &&logger);
9323+
let (closing_signed, tx_shutdown_result) =
9324+
try_channel_entry!(self, peer_state, res, chan_entry);
9325+
debug_assert_eq!(tx_shutdown_result.is_some(), chan.is_shutdown());
93249326
if let Some(msg) = closing_signed {
93259327
peer_state.pending_msg_events.push(MessageSendEvent::SendClosingSigned {
93269328
node_id: counterparty_node_id.clone(),
93279329
msg,
93289330
});
93299331
}
9330-
if let Some(mut close_res) = shutdown_result {
9332+
if let Some((tx, mut close_res)) = tx_shutdown_result {
93319333
// We're done with this channel, we've got a signed closing transaction and
93329334
// will send the closing_signed back to the remote peer upon return. This
93339335
// also implies there are no pending HTLCs left on the channel, so we can
93349336
// fully delete it from tracking (the channel monitor is still around to
93359337
// watch for old state broadcasts)!
9336-
debug_assert!(tx.is_some());
93379338
locked_close_channel!(self, peer_state, chan, close_res, FUNDED);
9338-
(tx, Some(chan_entry.remove()), Some(close_res))
9339+
(Some(tx), Some(chan_entry.remove()), Some(close_res))
93399340
} else {
9340-
debug_assert!(tx.is_none());
9341-
(tx, None, None)
9341+
(None, None, None)
93429342
}
93439343
} else {
93449344
return try_channel_entry!(self, peer_state, Err(ChannelError::close(
@@ -10538,19 +10538,17 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1053810538
Some(funded_chan) => {
1053910539
let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
1054010540
match funded_chan.maybe_propose_closing_signed(&self.fee_estimator, &&logger) {
10541-
Ok((msg_opt, tx_opt, shutdown_result_opt)) => {
10541+
Ok((msg_opt, tx_shutdown_result_opt)) => {
1054210542
if let Some(msg) = msg_opt {
1054310543
has_update = true;
1054410544
pending_msg_events.push(MessageSendEvent::SendClosingSigned {
1054510545
node_id: funded_chan.context.get_counterparty_node_id(), msg,
1054610546
});
1054710547
}
10548-
debug_assert_eq!(shutdown_result_opt.is_some(), funded_chan.is_shutdown());
10549-
if let Some(mut shutdown_result) = shutdown_result_opt {
10550-
locked_close_channel!(self, peer_state, funded_chan, shutdown_result, FUNDED);
10551-
shutdown_results.push(shutdown_result);
10552-
}
10553-
if let Some(tx) = tx_opt {
10548+
debug_assert_eq!(tx_shutdown_result_opt.is_some(), funded_chan.is_shutdown());
10549+
if let Some((tx, mut shutdown_res)) = tx_shutdown_result_opt {
10550+
locked_close_channel!(self, peer_state, funded_chan, shutdown_res, FUNDED);
10551+
shutdown_results.push(shutdown_res);
1055410552
// We're done with this channel. We got a closing_signed and sent back
1055510553
// a closing_signed with a closing transaction to broadcast.
1055610554
if let Ok(update) = self.get_channel_update_for_broadcast(&funded_chan) {

0 commit comments

Comments
 (0)