Skip to content

Commit 6132a8e

Browse files
committed
Merge pull request #154 from ariard/funding_created_refactor
Refactor handle_funding_created to wrapper error handling function
2 parents 0c8d402 + e67b715 commit 6132a8e

File tree

1 file changed

+40
-39
lines changed

1 file changed

+40
-39
lines changed

src/ln/channelmanager.rs

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,45 @@ impl ChannelManager {
14721472
Ok(())
14731473
}
14741474

1475+
fn internal_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, MsgHandleErrInternal> {
1476+
let (chan, funding_msg, monitor_update) = {
1477+
let mut channel_state = self.channel_state.lock().unwrap();
1478+
match channel_state.by_id.entry(msg.temporary_channel_id.clone()) {
1479+
hash_map::Entry::Occupied(mut chan) => {
1480+
if chan.get().get_their_node_id() != *their_node_id {
1481+
//TODO: here and below MsgHandleErrInternal, #153 case
1482+
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.temporary_channel_id));
1483+
}
1484+
match chan.get_mut().funding_created(msg) {
1485+
Ok((funding_msg, monitor_update)) => {
1486+
(chan.remove(), funding_msg, monitor_update)
1487+
},
1488+
Err(e) => {
1489+
return Err(e).map_err(|e| MsgHandleErrInternal::from_maybe_close(e))
1490+
}
1491+
}
1492+
},
1493+
hash_map::Entry::Vacant(_) => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.temporary_channel_id))
1494+
}
1495+
}; // Release channel lock for install_watch_outpoint call,
1496+
// note that this means if the remote end is misbehaving and sends a message for the same
1497+
// channel back-to-back with funding_created, we'll end up thinking they sent a message
1498+
// for a bogus channel.
1499+
if let Err(_e) = self.monitor.add_update_monitor(monitor_update.get_funding_txo().unwrap(), monitor_update) {
1500+
unimplemented!();
1501+
}
1502+
let mut channel_state = self.channel_state.lock().unwrap();
1503+
match channel_state.by_id.entry(funding_msg.channel_id) {
1504+
hash_map::Entry::Occupied(_) => {
1505+
return Err(MsgHandleErrInternal::send_err_msg_no_close("Already had channel with the new channel_id", funding_msg.channel_id))
1506+
},
1507+
hash_map::Entry::Vacant(e) => {
1508+
e.insert(chan);
1509+
}
1510+
}
1511+
Ok(funding_msg)
1512+
}
1513+
14751514
fn internal_announcement_signatures(&self, their_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), MsgHandleErrInternal> {
14761515
let (chan_announcement, chan_update) = {
14771516
let mut channel_state = self.channel_state.lock().unwrap();
@@ -1682,45 +1721,7 @@ impl ChannelMessageHandler for ChannelManager {
16821721
}
16831722

16841723
fn handle_funding_created(&self, their_node_id: &PublicKey, msg: &msgs::FundingCreated) -> Result<msgs::FundingSigned, HandleError> {
1685-
let (chan, funding_msg, monitor_update) = {
1686-
let mut channel_state = self.channel_state.lock().unwrap();
1687-
match channel_state.by_id.entry(msg.temporary_channel_id.clone()) {
1688-
hash_map::Entry::Occupied(mut chan) => {
1689-
if chan.get().get_their_node_id() != *their_node_id {
1690-
return Err(HandleError{err: "Got a message for a channel from the wrong node!", action: None})
1691-
}
1692-
match chan.get_mut().funding_created(msg) {
1693-
Ok((funding_msg, monitor_update)) => {
1694-
(chan.remove(), funding_msg, monitor_update)
1695-
},
1696-
Err(e) => {
1697-
//TODO: Possibly remove the channel depending on e.action
1698-
return Err(e);
1699-
}
1700-
}
1701-
},
1702-
hash_map::Entry::Vacant(_) => return Err(HandleError{err: "Failed to find corresponding channel", action: None})
1703-
}
1704-
}; // Release channel lock for install_watch_outpoint call,
1705-
// note that this means if the remote end is misbehaving and sends a message for the same
1706-
// channel back-to-back with funding_created, we'll end up thinking they sent a message
1707-
// for a bogus channel.
1708-
if let Err(_e) = self.monitor.add_update_monitor(monitor_update.get_funding_txo().unwrap(), monitor_update) {
1709-
unimplemented!();
1710-
}
1711-
let mut channel_state = self.channel_state.lock().unwrap();
1712-
match channel_state.by_id.entry(funding_msg.channel_id) {
1713-
hash_map::Entry::Occupied(_) => {
1714-
return Err(HandleError {
1715-
err: "Duplicate channel_id!",
1716-
action: Some(msgs::ErrorAction::SendErrorMessage { msg: msgs::ErrorMessage { channel_id: funding_msg.channel_id, data: "Already had channel with the new channel_id".to_owned() } })
1717-
});
1718-
},
1719-
hash_map::Entry::Vacant(e) => {
1720-
e.insert(chan);
1721-
}
1722-
}
1723-
Ok(funding_msg)
1724+
handle_error!(self, self.internal_funding_created(their_node_id, msg), their_node_id)
17241725
}
17251726

17261727
fn handle_funding_signed(&self, their_node_id: &PublicKey, msg: &msgs::FundingSigned) -> Result<(), HandleError> {

0 commit comments

Comments
 (0)