Skip to content

Commit 9d7a7e0

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 eae2bb1 commit 9d7a7e0

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
@@ -8787,10 +8787,7 @@ where
87878787

87888788
pub fn maybe_propose_closing_signed<F: Deref, L: Deref>(
87898789
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
8790-
) -> Result<
8791-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
8792-
ChannelError,
8793-
>
8790+
) -> Result<(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>), ChannelError>
87948791
where
87958792
F::Target: FeeEstimator,
87968793
L::Target: Logger,
@@ -8800,20 +8797,20 @@ where
88008797
// initiate `closing_signed` negotiation until we're clear of all pending messages. Note
88018798
// that closing_negotiation_ready checks this case (as well as a few others).
88028799
if self.context.last_sent_closing_fee.is_some() || !self.closing_negotiation_ready() {
8803-
return Ok((None, None, None));
8800+
return Ok((None, None));
88048801
}
88058802

88068803
if !self.funding.is_outbound() {
88078804
if let Some(msg) = &self.context.pending_counterparty_closing_signed.take() {
88088805
return self.closing_signed(fee_estimator, &msg, logger);
88098806
}
8810-
return Ok((None, None, None));
8807+
return Ok((None, None));
88118808
}
88128809

88138810
// If we're waiting on a counterparty `commitment_signed` to clear some updates from our
88148811
// local commitment transaction, we can't yet initiate `closing_signed` negotiation.
88158812
if self.context.expecting_peer_commitment_signed {
8816-
return Ok((None, None, None));
8813+
return Ok((None, None));
88178814
}
88188815

88198816
let (our_min_fee, our_max_fee) = self.calculate_closing_fee_limits(fee_estimator);
@@ -8832,7 +8829,7 @@ where
88328829
our_max_fee,
88338830
logger,
88348831
);
8835-
Ok((closing_signed, None, None))
8832+
Ok((closing_signed, None))
88368833
}
88378834

88388835
fn mark_response_received(&mut self) {
@@ -9095,10 +9092,7 @@ where
90959092
pub fn closing_signed<F: Deref, L: Deref>(
90969093
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, msg: &msgs::ClosingSigned,
90979094
logger: &L,
9098-
) -> Result<
9099-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
9100-
ChannelError,
9101-
>
9095+
) -> Result<(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>), ChannelError>
91029096
where
91039097
F::Target: FeeEstimator,
91049098
L::Target: Logger,
@@ -9138,7 +9132,7 @@ where
91389132

91399133
if self.context.channel_state.is_monitor_update_in_progress() {
91409134
self.context.pending_counterparty_closing_signed = Some(msg.clone());
9141-
return Ok((None, None, None));
9135+
return Ok((None, None));
91429136
}
91439137

91449138
let funding_redeemscript = self.funding.get_funding_redeemscript();
@@ -9192,7 +9186,7 @@ where
91929186
self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
91939187
self.context.channel_state = ChannelState::ShutdownComplete;
91949188
self.context.update_time_counter += 1;
9195-
return Ok((None, Some(tx), Some(shutdown_result)));
9189+
return Ok((None, Some((tx, shutdown_result))));
91969190
}
91979191
}
91989192

@@ -9215,26 +9209,25 @@ where
92159209
our_max_fee,
92169210
logger,
92179211
);
9218-
let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
9219-
let shutdown_result =
9220-
closing_signed.as_ref().map(|_| self.shutdown_result_coop_close());
9221-
if closing_signed.is_some() {
9222-
self.context.channel_state = ChannelState::ShutdownComplete;
9223-
}
9212+
let signed_tx_shutdown = if $new_fee == msg.fee_satoshis {
92249213
self.context.update_time_counter += 1;
92259214
self.context.last_received_closing_sig = Some(msg.signature.clone());
9226-
let tx = closing_signed.as_ref().map(|ClosingSigned { signature, .. }| {
9227-
self.build_signed_closing_transaction(
9215+
if let Some(ClosingSigned { signature, .. }) = &closing_signed {
9216+
let shutdown_result = self.shutdown_result_coop_close();
9217+
self.context.channel_state = ChannelState::ShutdownComplete;
9218+
let tx = self.build_signed_closing_transaction(
92289219
&closing_tx,
92299220
&msg.signature,
92309221
signature,
9231-
)
9232-
});
9233-
(tx, shutdown_result)
9222+
);
9223+
Some((tx, shutdown_result))
9224+
} else {
9225+
None
9226+
}
92349227
} else {
9235-
(None, None)
9228+
None
92369229
};
9237-
return Ok((closing_signed, signed_tx, shutdown_result))
9230+
return Ok((closing_signed, signed_tx_shutdown))
92389231
};
92399232
}
92409233

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9733,26 +9733,26 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
97339733
hash_map::Entry::Occupied(mut chan_entry) => {
97349734
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
97359735
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
9736-
let (closing_signed, tx, shutdown_result) = try_channel_entry!(self, peer_state, chan.closing_signed(&self.fee_estimator, &msg, &&logger), chan_entry);
9737-
debug_assert_eq!(shutdown_result.is_some(), chan.is_shutdown());
9736+
let res = chan.closing_signed(&self.fee_estimator, &msg, &&logger);
9737+
let (closing_signed, tx_shutdown_result) =
9738+
try_channel_entry!(self, peer_state, res, chan_entry);
9739+
debug_assert_eq!(tx_shutdown_result.is_some(), chan.is_shutdown());
97389740
if let Some(msg) = closing_signed {
97399741
peer_state.pending_msg_events.push(MessageSendEvent::SendClosingSigned {
97409742
node_id: counterparty_node_id.clone(),
97419743
msg,
97429744
});
97439745
}
9744-
if let Some(mut close_res) = shutdown_result {
9746+
if let Some((tx, mut close_res)) = tx_shutdown_result {
97459747
// We're done with this channel, we've got a signed closing transaction and
97469748
// will send the closing_signed back to the remote peer upon return. This
97479749
// also implies there are no pending HTLCs left on the channel, so we can
97489750
// fully delete it from tracking (the channel monitor is still around to
97499751
// watch for old state broadcasts)!
9750-
debug_assert!(tx.is_some());
97519752
locked_close_channel!(self, peer_state, chan, close_res, FUNDED);
9752-
(tx, Some(chan_entry.remove()), Some(close_res))
9753+
(Some(tx), Some(chan_entry.remove()), Some(close_res))
97539754
} else {
9754-
debug_assert!(tx.is_none());
9755-
(tx, None, None)
9755+
(None, None, None)
97569756
}
97579757
} else {
97589758
return try_channel_entry!(self, peer_state, Err(ChannelError::close(
@@ -11097,19 +11097,17 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1109711097
Some(funded_chan) => {
1109811098
let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
1109911099
match funded_chan.maybe_propose_closing_signed(&self.fee_estimator, &&logger) {
11100-
Ok((msg_opt, tx_opt, shutdown_result_opt)) => {
11100+
Ok((msg_opt, tx_shutdown_result_opt)) => {
1110111101
if let Some(msg) = msg_opt {
1110211102
has_update = true;
1110311103
pending_msg_events.push(MessageSendEvent::SendClosingSigned {
1110411104
node_id: funded_chan.context.get_counterparty_node_id(), msg,
1110511105
});
1110611106
}
11107-
debug_assert_eq!(shutdown_result_opt.is_some(), funded_chan.is_shutdown());
11108-
if let Some(mut shutdown_result) = shutdown_result_opt {
11109-
locked_close_channel!(self, peer_state, funded_chan, shutdown_result, FUNDED);
11110-
shutdown_results.push(shutdown_result);
11111-
}
11112-
if let Some(tx) = tx_opt {
11107+
debug_assert_eq!(tx_shutdown_result_opt.is_some(), funded_chan.is_shutdown());
11108+
if let Some((tx, mut shutdown_res)) = tx_shutdown_result_opt {
11109+
locked_close_channel!(self, peer_state, funded_chan, shutdown_res, FUNDED);
11110+
shutdown_results.push(shutdown_res);
1111311111
// We're done with this channel. We got a closing_signed and sent back
1111411112
// a closing_signed with a closing transaction to broadcast.
1111511113
if let Ok(update) = self.get_channel_update_for_broadcast(&funded_chan) {

0 commit comments

Comments
 (0)