Skip to content

Commit a11e27d

Browse files
committed
Send closing_signed when appropriate as pending HTLCs clear
1 parent 1993ec0 commit a11e27d

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

src/ln/channel.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,13 +1632,16 @@ impl Channel {
16321632
self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))
16331633
}
16341634

1635-
pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>, ChannelMonitor), HandleError> {
1635+
pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned, fee_estimator: &FeeEstimator) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>, Option<msgs::ClosingSigned>, ChannelMonitor), HandleError> {
16361636
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
16371637
return Err(HandleError{err: "Got commitment signed message when channel was not in an operational state", action: None});
16381638
}
16391639
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
16401640
return Err(HandleError{err: "Peer sent commitment_signed when we needed a channel_reestablish", action: Some(msgs::ErrorAction::SendErrorMessage{msg: msgs::ErrorMessage{data: "Peer sent commitment_signed when we needed a channel_reestablish".to_string(), channel_id: msg.channel_id}})});
16411641
}
1642+
if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.last_sent_closing_fee.is_some() {
1643+
return Err(HandleError{err: "Peer sent commitment_signed after we'd started exchanging closing_signeds", action: Some(msgs::ErrorAction::SendErrorMessage{msg: msgs::ErrorMessage{data: "Peer sent commitment_signed after we'd started exchanging closing_signeds".to_string(), channel_id: msg.channel_id}})});
1644+
}
16421645

16431646
let funding_script = self.get_funding_redeemscript();
16441647

@@ -1730,19 +1733,21 @@ impl Channel {
17301733
return Err(HandleError{err: "Previous monitor update failure prevented generation of RAA", action: Some(ErrorAction::IgnoreError)});
17311734
}
17321735

1733-
let (our_commitment_signed, monitor_update) = if need_our_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
1736+
let (our_commitment_signed, monitor_update, closing_signed) = if need_our_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
17341737
// If we're AwaitingRemoteRevoke we can't send a new commitment here, but that's ok -
17351738
// we'll send one right away when we get the revoke_and_ack when we
17361739
// free_holding_cell_htlcs().
17371740
let (msg, monitor) = self.send_commitment_no_status_check()?;
1738-
(Some(msg), monitor)
1739-
} else { (None, self.channel_monitor.clone()) };
1741+
(Some(msg), monitor, None)
1742+
} else if !need_our_commitment {
1743+
(None, self.channel_monitor.clone(), self.maybe_propose_first_closing_signed(fee_estimator))
1744+
} else { (None, self.channel_monitor.clone(), None) };
17401745

17411746
Ok((msgs::RevokeAndACK {
17421747
channel_id: self.channel_id,
17431748
per_commitment_secret: per_commitment_secret,
17441749
next_per_commitment_point: next_per_commitment_point,
1745-
}, our_commitment_signed, monitor_update))
1750+
}, our_commitment_signed, closing_signed, monitor_update))
17461751
}
17471752

17481753
/// Used to fulfill holding_cell_htlcs when we get a remote ack (or implicitly get it by them
@@ -1843,13 +1848,16 @@ impl Channel {
18431848
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
18441849
/// generating an appropriate error *after* the channel state has been updated based on the
18451850
/// revoke_and_ack message.
1846-
pub fn revoke_and_ack(&mut self, msg: &msgs::RevokeAndACK) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, [u8; 32], HTLCFailReason)>, ChannelMonitor), HandleError> {
1851+
pub fn revoke_and_ack(&mut self, msg: &msgs::RevokeAndACK, fee_estimator: &FeeEstimator) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, [u8; 32], HTLCFailReason)>, Option<msgs::ClosingSigned>, ChannelMonitor), HandleError> {
18471852
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
18481853
return Err(HandleError{err: "Got revoke/ACK message when channel was not in an operational state", action: None});
18491854
}
18501855
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
18511856
return Err(HandleError{err: "Peer sent revoke_and_ack when we needed a channel_reestablish", action: Some(msgs::ErrorAction::SendErrorMessage{msg: msgs::ErrorMessage{data: "Peer sent revoke_and_ack when we needed a channel_reestablish".to_string(), channel_id: msg.channel_id}})});
18521857
}
1858+
if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.last_sent_closing_fee.is_some() {
1859+
return Err(HandleError{err: "Peer sent revoke_and_ack after we'd started exchanging closing_signeds", action: Some(msgs::ErrorAction::SendErrorMessage{msg: msgs::ErrorMessage{data: "Peer sent revoke_and_ack after we'd started exchanging closing_signeds".to_string(), channel_id: msg.channel_id}})});
1860+
}
18531861

18541862
if let Some(their_prev_commitment_point) = self.their_prev_commitment_point {
18551863
if PublicKey::from_secret_key(&self.secp_ctx, &secp_call!(SecretKey::from_slice(&self.secp_ctx, &msg.per_commitment_secret), "Peer provided an invalid per_commitment_secret", self.channel_id())) != their_prev_commitment_point {
@@ -1971,7 +1979,7 @@ impl Channel {
19711979
}
19721980
self.monitor_pending_forwards.append(&mut to_forward_infos);
19731981
self.monitor_pending_failures.append(&mut revoked_htlcs);
1974-
return Ok((None, Vec::new(), Vec::new(), self.channel_monitor.clone()));
1982+
return Ok((None, Vec::new(), Vec::new(), None, self.channel_monitor.clone()));
19751983
}
19761984

19771985
match self.free_holding_cell_htlcs()? {
@@ -1984,7 +1992,7 @@ impl Channel {
19841992
for fail_msg in update_fail_malformed_htlcs.drain(..) {
19851993
commitment_update.0.update_fail_malformed_htlcs.push(fail_msg);
19861994
}
1987-
Ok((Some(commitment_update.0), to_forward_infos, revoked_htlcs, commitment_update.1))
1995+
Ok((Some(commitment_update.0), to_forward_infos, revoked_htlcs, None, commitment_update.1))
19881996
},
19891997
None => {
19901998
if require_commitment {
@@ -1996,9 +2004,9 @@ impl Channel {
19962004
update_fail_malformed_htlcs,
19972005
update_fee: None,
19982006
commitment_signed
1999-
}), to_forward_infos, revoked_htlcs, monitor_update))
2007+
}), to_forward_infos, revoked_htlcs, None, monitor_update))
20002008
} else {
2001-
Ok((None, to_forward_infos, revoked_htlcs, self.channel_monitor.clone()))
2009+
Ok((None, to_forward_infos, revoked_htlcs, self.maybe_propose_first_closing_signed(fee_estimator), self.channel_monitor.clone()))
20022010
}
20032011
}
20042012
}
@@ -2366,7 +2374,9 @@ impl Channel {
23662374
}
23672375

23682376
fn maybe_propose_first_closing_signed(&mut self, fee_estimator: &FeeEstimator) -> Option<msgs::ClosingSigned> {
2369-
if !self.channel_outbound || !self.pending_inbound_htlcs.is_empty() || !self.pending_outbound_htlcs.is_empty() {
2377+
if !self.channel_outbound || !self.pending_inbound_htlcs.is_empty() || !self.pending_outbound_htlcs.is_empty() ||
2378+
self.channel_state & (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::AwaitingRemoteRevoke as u32) != BOTH_SIDES_SHUTDOWN_MASK ||
2379+
self.last_sent_closing_fee.is_some() {
23702380
return None;
23712381
}
23722382

src/ln/channelmanager.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,7 +2205,7 @@ impl ChannelManager {
22052205
//TODO: here and below MsgHandleErrInternal, #153 case
22062206
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
22072207
}
2208-
let (revoke_and_ack, commitment_signed, chan_monitor) = chan.commitment_signed(&msg).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?;
2208+
let (revoke_and_ack, commitment_signed, closing_signed, chan_monitor) = chan.commitment_signed(&msg, &*self.fee_estimator).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?;
22092209
if let Err(_e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
22102210
unimplemented!();
22112211
}
@@ -2226,6 +2226,12 @@ impl ChannelManager {
22262226
},
22272227
});
22282228
}
2229+
if let Some(msg) = closing_signed {
2230+
channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
2231+
node_id: their_node_id.clone(),
2232+
msg,
2233+
});
2234+
}
22292235
Ok(())
22302236
},
22312237
None => Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
@@ -2275,7 +2281,7 @@ impl ChannelManager {
22752281
//TODO: here and below MsgHandleErrInternal, #153 case
22762282
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
22772283
}
2278-
let (commitment_update, pending_forwards, pending_failures, chan_monitor) = chan.revoke_and_ack(&msg).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?;
2284+
let (commitment_update, pending_forwards, pending_failures, closing_signed, chan_monitor) = chan.revoke_and_ack(&msg, &*self.fee_estimator).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?;
22792285
if let Err(_e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
22802286
unimplemented!();
22812287
}
@@ -2285,6 +2291,12 @@ impl ChannelManager {
22852291
updates,
22862292
});
22872293
}
2294+
if let Some(msg) = closing_signed {
2295+
channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
2296+
node_id: their_node_id.clone(),
2297+
msg,
2298+
});
2299+
}
22882300
(pending_forwards, pending_failures, chan.get_short_channel_id().expect("RAA should only work on a short-id-available channel"))
22892301
},
22902302
None => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))

0 commit comments

Comments
 (0)