Skip to content

Commit 8791605

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 2f9898c commit 8791605

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
@@ -8819,10 +8819,7 @@ where
88198819

88208820
pub fn maybe_propose_closing_signed<F: Deref, L: Deref>(
88218821
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, logger: &L,
8822-
) -> Result<
8823-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
8824-
ChannelError,
8825-
>
8822+
) -> Result<(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>), ChannelError>
88268823
where
88278824
F::Target: FeeEstimator,
88288825
L::Target: Logger,
@@ -8832,20 +8829,20 @@ where
88328829
// initiate `closing_signed` negotiation until we're clear of all pending messages. Note
88338830
// that closing_negotiation_ready checks this case (as well as a few others).
88348831
if self.context.last_sent_closing_fee.is_some() || !self.closing_negotiation_ready() {
8835-
return Ok((None, None, None));
8832+
return Ok((None, None));
88368833
}
88378834

88388835
if !self.funding.is_outbound() {
88398836
if let Some(msg) = &self.context.pending_counterparty_closing_signed.take() {
88408837
return self.closing_signed(fee_estimator, &msg, logger);
88418838
}
8842-
return Ok((None, None, None));
8839+
return Ok((None, None));
88438840
}
88448841

88458842
// If we're waiting on a counterparty `commitment_signed` to clear some updates from our
88468843
// local commitment transaction, we can't yet initiate `closing_signed` negotiation.
88478844
if self.context.expecting_peer_commitment_signed {
8848-
return Ok((None, None, None));
8845+
return Ok((None, None));
88498846
}
88508847

88518848
let (our_min_fee, our_max_fee) = self.calculate_closing_fee_limits(fee_estimator);
@@ -8864,7 +8861,7 @@ where
88648861
our_max_fee,
88658862
logger,
88668863
);
8867-
Ok((closing_signed, None, None))
8864+
Ok((closing_signed, None))
88688865
}
88698866

88708867
fn mark_response_received(&mut self) {
@@ -9127,10 +9124,7 @@ where
91279124
pub fn closing_signed<F: Deref, L: Deref>(
91289125
&mut self, fee_estimator: &LowerBoundedFeeEstimator<F>, msg: &msgs::ClosingSigned,
91299126
logger: &L,
9130-
) -> Result<
9131-
(Option<msgs::ClosingSigned>, Option<Transaction>, Option<ShutdownResult>),
9132-
ChannelError,
9133-
>
9127+
) -> Result<(Option<msgs::ClosingSigned>, Option<(Transaction, ShutdownResult)>), ChannelError>
91349128
where
91359129
F::Target: FeeEstimator,
91369130
L::Target: Logger,
@@ -9170,7 +9164,7 @@ where
91709164

91719165
if self.context.channel_state.is_monitor_update_in_progress() {
91729166
self.context.pending_counterparty_closing_signed = Some(msg.clone());
9173-
return Ok((None, None, None));
9167+
return Ok((None, None));
91749168
}
91759169

91769170
let funding_redeemscript = self.funding.get_funding_redeemscript();
@@ -9224,7 +9218,7 @@ where
92249218
self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
92259219
self.context.channel_state = ChannelState::ShutdownComplete;
92269220
self.context.update_time_counter += 1;
9227-
return Ok((None, Some(tx), Some(shutdown_result)));
9221+
return Ok((None, Some((tx, shutdown_result))));
92289222
}
92299223
}
92309224

@@ -9247,26 +9241,25 @@ where
92479241
our_max_fee,
92489242
logger,
92499243
);
9250-
let (signed_tx, shutdown_result) = if $new_fee == msg.fee_satoshis {
9251-
let shutdown_result =
9252-
closing_signed.as_ref().map(|_| self.shutdown_result_coop_close());
9253-
if closing_signed.is_some() {
9254-
self.context.channel_state = ChannelState::ShutdownComplete;
9255-
}
9244+
let signed_tx_shutdown = if $new_fee == msg.fee_satoshis {
92569245
self.context.update_time_counter += 1;
92579246
self.context.last_received_closing_sig = Some(msg.signature.clone());
9258-
let tx = closing_signed.as_ref().map(|ClosingSigned { signature, .. }| {
9259-
self.build_signed_closing_transaction(
9247+
if let Some(ClosingSigned { signature, .. }) = &closing_signed {
9248+
let shutdown_result = self.shutdown_result_coop_close();
9249+
self.context.channel_state = ChannelState::ShutdownComplete;
9250+
let tx = self.build_signed_closing_transaction(
92609251
&closing_tx,
92619252
&msg.signature,
92629253
signature,
9263-
)
9264-
});
9265-
(tx, shutdown_result)
9254+
);
9255+
Some((tx, shutdown_result))
9256+
} else {
9257+
None
9258+
}
92669259
} else {
9267-
(None, None)
9260+
None
92689261
};
9269-
return Ok((closing_signed, signed_tx, shutdown_result))
9262+
return Ok((closing_signed, signed_tx_shutdown))
92709263
};
92719264
}
92729265

lightning/src/ln/channelmanager.rs

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9766,26 +9766,26 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
97669766
hash_map::Entry::Occupied(mut chan_entry) => {
97679767
if let Some(chan) = chan_entry.get_mut().as_funded_mut() {
97689768
let logger = WithChannelContext::from(&self.logger, &chan.context, None);
9769-
let (closing_signed, tx, shutdown_result) = try_channel_entry!(self, peer_state, chan.closing_signed(&self.fee_estimator, &msg, &&logger), chan_entry);
9770-
debug_assert_eq!(shutdown_result.is_some(), chan.is_shutdown());
9769+
let res = chan.closing_signed(&self.fee_estimator, &msg, &&logger);
9770+
let (closing_signed, tx_shutdown_result) =
9771+
try_channel_entry!(self, peer_state, res, chan_entry);
9772+
debug_assert_eq!(tx_shutdown_result.is_some(), chan.is_shutdown());
97719773
if let Some(msg) = closing_signed {
97729774
peer_state.pending_msg_events.push(MessageSendEvent::SendClosingSigned {
97739775
node_id: counterparty_node_id.clone(),
97749776
msg,
97759777
});
97769778
}
9777-
if let Some(mut close_res) = shutdown_result {
9779+
if let Some((tx, mut close_res)) = tx_shutdown_result {
97789780
// We're done with this channel, we've got a signed closing transaction and
97799781
// will send the closing_signed back to the remote peer upon return. This
97809782
// also implies there are no pending HTLCs left on the channel, so we can
97819783
// fully delete it from tracking (the channel monitor is still around to
97829784
// watch for old state broadcasts)!
9783-
debug_assert!(tx.is_some());
97849785
locked_close_channel!(self, peer_state, chan, close_res, FUNDED);
9785-
(tx, Some(chan_entry.remove()), Some(close_res))
9786+
(Some(tx), Some(chan_entry.remove()), Some(close_res))
97869787
} else {
9787-
debug_assert!(tx.is_none());
9788-
(tx, None, None)
9788+
(None, None, None)
97899789
}
97909790
} else {
97919791
return try_channel_entry!(self, peer_state, Err(ChannelError::close(
@@ -11136,19 +11136,17 @@ This indicates a bug inside LDK. Please report this error at https://github.com/
1113611136
Some(funded_chan) => {
1113711137
let logger = WithChannelContext::from(&self.logger, &funded_chan.context, None);
1113811138
match funded_chan.maybe_propose_closing_signed(&self.fee_estimator, &&logger) {
11139-
Ok((msg_opt, tx_opt, shutdown_result_opt)) => {
11139+
Ok((msg_opt, tx_shutdown_result_opt)) => {
1114011140
if let Some(msg) = msg_opt {
1114111141
has_update = true;
1114211142
pending_msg_events.push(MessageSendEvent::SendClosingSigned {
1114311143
node_id: funded_chan.context.get_counterparty_node_id(), msg,
1114411144
});
1114511145
}
11146-
debug_assert_eq!(shutdown_result_opt.is_some(), funded_chan.is_shutdown());
11147-
if let Some(mut shutdown_result) = shutdown_result_opt {
11148-
locked_close_channel!(self, peer_state, funded_chan, shutdown_result, FUNDED);
11149-
shutdown_results.push(shutdown_result);
11150-
}
11151-
if let Some(tx) = tx_opt {
11146+
debug_assert_eq!(tx_shutdown_result_opt.is_some(), funded_chan.is_shutdown());
11147+
if let Some((tx, mut shutdown_res)) = tx_shutdown_result_opt {
11148+
locked_close_channel!(self, peer_state, funded_chan, shutdown_res, FUNDED);
11149+
shutdown_results.push(shutdown_res);
1115211150
// We're done with this channel. We got a closing_signed and sent back
1115311151
// a closing_signed with a closing transaction to broadcast.
1115411152
if let Ok(update) = self.get_channel_update_for_broadcast(&funded_chan) {

0 commit comments

Comments
 (0)