Skip to content

Commit 4b5b48c

Browse files
author
Antoine Riard
committed
Refactor handle_shutdown to wrapper error handling function
1 parent 8dfc3c3 commit 4b5b48c

File tree

1 file changed

+38
-33
lines changed

1 file changed

+38
-33
lines changed

src/ln/channelmanager.rs

Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1552,6 +1552,43 @@ impl ChannelManager {
15521552
};
15531553
}
15541554

1555+
fn internal_shutdown(&self, their_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>), MsgHandleErrInternal> {
1556+
let (res, chan_option) = {
1557+
let mut channel_state_lock = self.channel_state.lock().unwrap();
1558+
let channel_state = channel_state_lock.borrow_parts();
1559+
1560+
match channel_state.by_id.entry(msg.channel_id.clone()) {
1561+
hash_map::Entry::Occupied(mut chan_entry) => {
1562+
if chan_entry.get().get_their_node_id() != *their_node_id {
1563+
//TODO: here and below MsgHandleErrInternal, #153 case
1564+
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
1565+
}
1566+
let res = chan_entry.get_mut().shutdown(&*self.fee_estimator, &msg).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?;
1567+
if chan_entry.get().is_shutdown() {
1568+
if let Some(short_id) = chan_entry.get().get_short_channel_id() {
1569+
channel_state.short_to_id.remove(&short_id);
1570+
}
1571+
(res, Some(chan_entry.remove_entry().1))
1572+
} else { (res, None) }
1573+
},
1574+
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
1575+
}
1576+
};
1577+
for payment_hash in res.2 {
1578+
// unknown_next_peer...I dunno who that is anymore....
1579+
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() });
1580+
}
1581+
if let Some(chan) = chan_option {
1582+
if let Ok(update) = self.get_channel_update(&chan) {
1583+
let mut events = self.pending_events.lock().unwrap();
1584+
events.push(events::Event::BroadcastChannelUpdate {
1585+
msg: update
1586+
});
1587+
}
1588+
}
1589+
Ok((res.0, res.1))
1590+
}
1591+
15551592
fn internal_announcement_signatures(&self, their_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), MsgHandleErrInternal> {
15561593
let (chan_announcement, chan_update) = {
15571594
let mut channel_state = self.channel_state.lock().unwrap();
@@ -1774,39 +1811,7 @@ impl ChannelMessageHandler for ChannelManager {
17741811
}
17751812

17761813
fn handle_shutdown(&self, their_node_id: &PublicKey, msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>), HandleError> {
1777-
let (res, chan_option) = {
1778-
let mut channel_state_lock = self.channel_state.lock().unwrap();
1779-
let channel_state = channel_state_lock.borrow_parts();
1780-
1781-
match channel_state.by_id.entry(msg.channel_id.clone()) {
1782-
hash_map::Entry::Occupied(mut chan_entry) => {
1783-
if chan_entry.get().get_their_node_id() != *their_node_id {
1784-
return Err(HandleError{err: "Got a message for a channel from the wrong node!", action: None})
1785-
}
1786-
let res = chan_entry.get_mut().shutdown(&*self.fee_estimator, &msg)?;
1787-
if chan_entry.get().is_shutdown() {
1788-
if let Some(short_id) = chan_entry.get().get_short_channel_id() {
1789-
channel_state.short_to_id.remove(&short_id);
1790-
}
1791-
(res, Some(chan_entry.remove_entry().1))
1792-
} else { (res, None) }
1793-
},
1794-
hash_map::Entry::Vacant(_) => return Err(HandleError{err: "Failed to find corresponding channel", action: None})
1795-
}
1796-
};
1797-
for payment_hash in res.2 {
1798-
// unknown_next_peer...I dunno who that is anymore....
1799-
self.fail_htlc_backwards_internal(self.channel_state.lock().unwrap(), &payment_hash, HTLCFailReason::Reason { failure_code: 0x4000 | 10, data: Vec::new() });
1800-
}
1801-
if let Some(chan) = chan_option {
1802-
if let Ok(update) = self.get_channel_update(&chan) {
1803-
let mut events = self.pending_events.lock().unwrap();
1804-
events.push(events::Event::BroadcastChannelUpdate {
1805-
msg: update
1806-
});
1807-
}
1808-
}
1809-
Ok((res.0, res.1))
1814+
handle_error!(self, self.internal_shutdown(their_node_id, msg), their_node_id)
18101815
}
18111816

18121817
fn handle_closing_signed(&self, their_node_id: &PublicKey, msg: &msgs::ClosingSigned) -> Result<Option<msgs::ClosingSigned>, HandleError> {

0 commit comments

Comments
 (0)