Skip to content

Commit d04d54c

Browse files
committed
Send closing_signed when appropriate as pending HTLCs clear
1 parent f1116a9 commit d04d54c

File tree

2 files changed

+37
-12
lines changed

2 files changed

+37
-12
lines changed

src/ln/channel.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1595,13 +1595,16 @@ impl Channel {
15951595
self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))
15961596
}
15971597

1598-
pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>, ChannelMonitor), HandleError> {
1598+
pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned, fee_estimator: &FeeEstimator) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>, Option<msgs::ClosingSigned>, ChannelMonitor), HandleError> {
15991599
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
16001600
return Err(HandleError{err: "Got commitment signed message when channel was not in an operational state", action: None});
16011601
}
16021602
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
16031603
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}})});
16041604
}
1605+
if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.last_sent_closing_fee.is_some() {
1606+
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}})});
1607+
}
16051608

16061609
let funding_script = self.get_funding_redeemscript();
16071610

@@ -1693,19 +1696,22 @@ impl Channel {
16931696
return Err(HandleError{err: "Previous monitor update failure prevented generation of RAA", action: Some(ErrorAction::IgnoreError)});
16941697
}
16951698

1696-
let (our_commitment_signed, monitor_update) = if need_our_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
1699+
let (our_commitment_signed, monitor_update, closing_signed) = if need_our_commitment && (self.channel_state & (ChannelState::AwaitingRemoteRevoke as u32)) == 0 {
16971700
// If we're AwaitingRemoteRevoke we can't send a new commitment here, but that's ok -
16981701
// we'll send one right away when we get the revoke_and_ack when we
16991702
// free_holding_cell_htlcs().
17001703
let (msg, monitor) = self.send_commitment_no_status_check()?;
1701-
(Some(msg), monitor)
1702-
} else { (None, self.channel_monitor.clone()) };
1704+
(Some(msg), monitor, None)
1705+
} else if !need_our_commitment && self.channel_state & (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::AwaitingRemoteRevoke as u32) == BOTH_SIDES_SHUTDOWN_MASK &&
1706+
self.channel_outbound && self.pending_inbound_htlcs.is_empty() && self.pending_outbound_htlcs.is_empty() && self.last_sent_closing_fee.is_none() {
1707+
(None, self.channel_monitor.clone(), Some(self.propose_first_closing_signed(fee_estimator)))
1708+
} else { (None, self.channel_monitor.clone(), None) };
17031709

17041710
Ok((msgs::RevokeAndACK {
17051711
channel_id: self.channel_id,
17061712
per_commitment_secret: per_commitment_secret,
17071713
next_per_commitment_point: next_per_commitment_point,
1708-
}, our_commitment_signed, monitor_update))
1714+
}, our_commitment_signed, closing_signed, monitor_update))
17091715
}
17101716

17111717
/// Used to fulfill holding_cell_htlcs when we get a remote ack (or implicitly get it by them
@@ -1806,13 +1812,16 @@ impl Channel {
18061812
/// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
18071813
/// generating an appropriate error *after* the channel state has been updated based on the
18081814
/// revoke_and_ack message.
1809-
pub fn revoke_and_ack(&mut self, msg: &msgs::RevokeAndACK) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, [u8; 32], HTLCFailReason)>, ChannelMonitor), HandleError> {
1815+
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> {
18101816
if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
18111817
return Err(HandleError{err: "Got revoke/ACK message when channel was not in an operational state", action: None});
18121818
}
18131819
if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
18141820
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}})});
18151821
}
1822+
if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.last_sent_closing_fee.is_some() {
1823+
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}})});
1824+
}
18161825

18171826
if let Some(their_prev_commitment_point) = self.their_prev_commitment_point {
18181827
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 {
@@ -1934,7 +1943,7 @@ impl Channel {
19341943
}
19351944
self.monitor_pending_forwards.append(&mut to_forward_infos);
19361945
self.monitor_pending_failures.append(&mut revoked_htlcs);
1937-
return Ok((None, Vec::new(), Vec::new(), self.channel_monitor.clone()));
1946+
return Ok((None, Vec::new(), Vec::new(), None, self.channel_monitor.clone()));
19381947
}
19391948

19401949
match self.free_holding_cell_htlcs()? {
@@ -1947,7 +1956,7 @@ impl Channel {
19471956
for fail_msg in update_fail_malformed_htlcs.drain(..) {
19481957
commitment_update.0.update_fail_malformed_htlcs.push(fail_msg);
19491958
}
1950-
Ok((Some(commitment_update.0), to_forward_infos, revoked_htlcs, commitment_update.1))
1959+
Ok((Some(commitment_update.0), to_forward_infos, revoked_htlcs, None, commitment_update.1))
19511960
},
19521961
None => {
19531962
if require_commitment {
@@ -1959,9 +1968,13 @@ impl Channel {
19591968
update_fail_malformed_htlcs,
19601969
update_fee: None,
19611970
commitment_signed
1962-
}), to_forward_infos, revoked_htlcs, monitor_update))
1971+
}), to_forward_infos, revoked_htlcs, None, monitor_update))
19631972
} else {
1964-
Ok((None, to_forward_infos, revoked_htlcs, self.channel_monitor.clone()))
1973+
if self.channel_state & BOTH_SIDES_SHUTDOWN_MASK == BOTH_SIDES_SHUTDOWN_MASK && self.channel_outbound && self.pending_inbound_htlcs.is_empty() && self.pending_outbound_htlcs.is_empty() && self.last_sent_closing_fee.is_none() {
1974+
Ok((None, to_forward_infos, revoked_htlcs, Some(self.propose_first_closing_signed(fee_estimator)), self.channel_monitor.clone()))
1975+
} else {
1976+
Ok((None, to_forward_infos, revoked_htlcs, None, self.channel_monitor.clone()))
1977+
}
19651978
}
19661979
}
19671980
}

src/ln/channelmanager.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ impl ChannelManager {
22022202
//TODO: here and below MsgHandleErrInternal, #153 case
22032203
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
22042204
}
2205-
let (revoke_and_ack, commitment_signed, chan_monitor) = chan.commitment_signed(&msg).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?;
2205+
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))?;
22062206
if let Err(_e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
22072207
unimplemented!();
22082208
}
@@ -2223,6 +2223,12 @@ impl ChannelManager {
22232223
},
22242224
});
22252225
}
2226+
if let Some(msg) = closing_signed {
2227+
channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
2228+
node_id: their_node_id.clone(),
2229+
msg,
2230+
});
2231+
}
22262232
Ok(())
22272233
},
22282234
None => Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
@@ -2272,7 +2278,7 @@ impl ChannelManager {
22722278
//TODO: here and below MsgHandleErrInternal, #153 case
22732279
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
22742280
}
2275-
let (commitment_update, pending_forwards, pending_failures, chan_monitor) = chan.revoke_and_ack(&msg).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?;
2281+
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))?;
22762282
if let Err(_e) = self.monitor.add_update_monitor(chan_monitor.get_funding_txo().unwrap(), chan_monitor) {
22772283
unimplemented!();
22782284
}
@@ -2282,6 +2288,12 @@ impl ChannelManager {
22822288
updates,
22832289
});
22842290
}
2291+
if let Some(msg) = closing_signed {
2292+
channel_state.pending_msg_events.push(events::MessageSendEvent::SendClosingSigned {
2293+
node_id: their_node_id.clone(),
2294+
msg,
2295+
});
2296+
}
22852297
(pending_forwards, pending_failures, chan.get_short_channel_id().expect("RAA should only work on a short-id-available channel"))
22862298
},
22872299
None => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))

0 commit comments

Comments
 (0)