Skip to content

Commit 500582e

Browse files
committed
Move announcement_signatures handling into new force-close macro
Because we've separated out channel closure from ErrorMessage returning we can return error messages in a few additional cases, like if the peer sent us a message for a channel they didn't own.
1 parent aa60a93 commit 500582e

File tree

1 file changed

+62
-36
lines changed

1 file changed

+62
-36
lines changed

src/ln/channelmanager.rs

Lines changed: 62 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,25 @@ impl MsgHandleErrInternal {
141141
}
142142
}
143143
#[inline]
144+
fn send_err_msg_close_chan(err: &'static str, channel_id: [u8; 32]) -> Self {
145+
Self {
146+
err: HandleError {
147+
err,
148+
action: Some(msgs::ErrorAction::SendErrorMessage {
149+
msg: msgs::ErrorMessage {
150+
channel_id,
151+
data: err.to_string()
152+
},
153+
}),
154+
},
155+
needs_channel_force_close: true,
156+
}
157+
}
158+
#[inline]
159+
fn from_maybe_close(err: msgs::HandleError) -> Self {
160+
Self { err, needs_channel_force_close: true }
161+
}
162+
#[inline]
144163
fn from_no_close(err: msgs::HandleError) -> Self {
145164
Self { err, needs_channel_force_close: false }
146165
}
@@ -1419,6 +1438,48 @@ impl ChannelManager {
14191438
channel_state.by_id.insert(channel.channel_id(), channel);
14201439
Ok(accept_msg)
14211440
}
1441+
1442+
fn internal_announcement_signatures(&self, their_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), MsgHandleErrInternal> {
1443+
let (chan_announcement, chan_update) = {
1444+
let mut channel_state = self.channel_state.lock().unwrap();
1445+
match channel_state.by_id.get_mut(&msg.channel_id) {
1446+
Some(chan) => {
1447+
if chan.get_their_node_id() != *their_node_id {
1448+
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
1449+
}
1450+
if !chan.is_usable() {
1451+
return Err(MsgHandleErrInternal::from_no_close(HandleError{err: "Got an announcement_signatures before we were ready for it", action: Some(msgs::ErrorAction::IgnoreError)}));
1452+
}
1453+
1454+
let our_node_id = self.get_our_node_id();
1455+
let (announcement, our_bitcoin_sig) = chan.get_channel_announcement(our_node_id.clone(), self.genesis_hash.clone())
1456+
.map_err(|e| MsgHandleErrInternal::from_maybe_close(e))?;
1457+
1458+
let were_node_one = announcement.node_id_1 == our_node_id;
1459+
let msghash = Message::from_slice(&Sha256dHash::from_data(&announcement.encode()[..])[..]).unwrap();
1460+
let bad_sig_action = MsgHandleErrInternal::send_err_msg_close_chan("Bad announcement_signatures node_signature", msg.channel_id);
1461+
secp_call!(self.secp_ctx.verify(&msghash, &msg.node_signature, if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 }), bad_sig_action);
1462+
secp_call!(self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 }), bad_sig_action);
1463+
1464+
let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
1465+
1466+
(msgs::ChannelAnnouncement {
1467+
node_signature_1: if were_node_one { our_node_sig } else { msg.node_signature },
1468+
node_signature_2: if were_node_one { msg.node_signature } else { our_node_sig },
1469+
bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { msg.bitcoin_signature },
1470+
bitcoin_signature_2: if were_node_one { msg.bitcoin_signature } else { our_bitcoin_sig },
1471+
contents: announcement,
1472+
}, self.get_channel_update(chan).unwrap()) // can only fail if we're not in a ready state
1473+
},
1474+
None => return Err(MsgHandleErrInternal::send_err_msg_no_close("Failed to find corresponding channel", msg.channel_id))
1475+
}
1476+
};
1477+
let mut pending_events = self.pending_events.lock().unwrap();
1478+
pending_events.push(events::Event::BroadcastChannelAnnouncement { msg: chan_announcement, update_msg: chan_update });
1479+
Ok(())
1480+
}
1481+
1482+
14221483
}
14231484

14241485
impl events::EventsProvider for ChannelManager {
@@ -2041,42 +2102,7 @@ impl ChannelMessageHandler for ChannelManager {
20412102
}
20422103

20432104
fn handle_announcement_signatures(&self, their_node_id: &PublicKey, msg: &msgs::AnnouncementSignatures) -> Result<(), HandleError> {
2044-
let (chan_announcement, chan_update) = {
2045-
let mut channel_state = self.channel_state.lock().unwrap();
2046-
match channel_state.by_id.get_mut(&msg.channel_id) {
2047-
Some(chan) => {
2048-
if chan.get_their_node_id() != *their_node_id {
2049-
return Err(HandleError{err: "Got a message for a channel from the wrong node!", action: Some(msgs::ErrorAction::IgnoreError) })
2050-
}
2051-
if !chan.is_usable() {
2052-
return Err(HandleError{err: "Got an announcement_signatures before we were ready for it", action: Some(msgs::ErrorAction::IgnoreError) });
2053-
}
2054-
2055-
let our_node_id = self.get_our_node_id();
2056-
let (announcement, our_bitcoin_sig) = chan.get_channel_announcement(our_node_id.clone(), self.genesis_hash.clone())?;
2057-
2058-
let were_node_one = announcement.node_id_1 == our_node_id;
2059-
let msghash = Message::from_slice(&Sha256dHash::from_data(&announcement.encode()[..])[..]).unwrap();
2060-
let bad_sig_action = msgs::HandleError {err: "Invalid signature in announcement_signatures", action: msgs::ErrorAction::SendErrorMessage {msg: msgs::ErrorMessage {channel_id: msg.channel_id.clone(), data: "Invalid signature in announcement_signatures".to_string()}}};
2061-
secp_call!(self.secp_ctx.verify(&msghash, &msg.node_signature, if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 }), bad_sig_action);
2062-
secp_call!(self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 }), bad_sig_action);
2063-
2064-
let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
2065-
2066-
(msgs::ChannelAnnouncement {
2067-
node_signature_1: if were_node_one { our_node_sig } else { msg.node_signature },
2068-
node_signature_2: if were_node_one { msg.node_signature } else { our_node_sig },
2069-
bitcoin_signature_1: if were_node_one { our_bitcoin_sig } else { msg.bitcoin_signature },
2070-
bitcoin_signature_2: if were_node_one { msg.bitcoin_signature } else { our_bitcoin_sig },
2071-
contents: announcement,
2072-
}, self.get_channel_update(chan).unwrap()) // can only fail if we're not in a ready state
2073-
},
2074-
None => return Err(HandleError{err: "Failed to find corresponding channel", action: Some(msgs::ErrorAction::IgnoreError)})
2075-
}
2076-
};
2077-
let mut pending_events = self.pending_events.lock().unwrap();
2078-
pending_events.push(events::Event::BroadcastChannelAnnouncement { msg: chan_announcement, update_msg: chan_update });
2079-
Ok(())
2105+
handle_error!(self, self.internal_announcement_signatures(their_node_id, msg), their_node_id)
20802106
}
20812107

20822108
fn peer_disconnected(&self, their_node_id: &PublicKey, no_connection_possible: bool) {

0 commit comments

Comments
 (0)