Skip to content

Commit 466d0f6

Browse files
committed
Simplify + document the ChannelManager Err flow a bit
This removes all the channel-closure stuff from handle_error!() and MsgHandleErrInternal, making all the Err handling consistent by closing the channel before releasing the channel_state lock and then calling handle_error!() outside of the lock.
1 parent fe3d706 commit 466d0f6

File tree

1 file changed

+12
-47
lines changed

1 file changed

+12
-47
lines changed

src/ln/channelmanager.rs

Lines changed: 12 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,13 @@ pub(super) use self::channel_held_info::*;
135135

136136
type ShutdownResult = (Vec<Transaction>, Vec<(HTLCSource, [u8; 32])>);
137137

138+
/// Error type returned across the channel_state mutex boundary. When an Err is generated for a
139+
/// Channel, we generally end up with a ChannelError::Close for which we have to close the channel
140+
/// immediately (ie with no further calls on it made). Thus, this step happens inside a
141+
/// channel_state lock. We then return the set of things that need to be done outside the lock in
142+
/// this struct and call handle_error!() on it.
138143
struct MsgHandleErrInternal {
139144
err: msgs::HandleError,
140-
needs_channel_force_close: bool,
141145
shutdown_finish: Option<(ShutdownResult, Option<msgs::ChannelUpdate>)>,
142146
}
143147
impl MsgHandleErrInternal {
@@ -153,29 +157,12 @@ impl MsgHandleErrInternal {
153157
},
154158
}),
155159
},
156-
needs_channel_force_close: false,
157-
shutdown_finish: None,
158-
}
159-
}
160-
#[inline]
161-
fn send_err_msg_close_chan(err: &'static str, channel_id: [u8; 32]) -> Self {
162-
Self {
163-
err: HandleError {
164-
err,
165-
action: Some(msgs::ErrorAction::SendErrorMessage {
166-
msg: msgs::ErrorMessage {
167-
channel_id,
168-
data: err.to_string()
169-
},
170-
}),
171-
},
172-
needs_channel_force_close: true,
173160
shutdown_finish: None,
174161
}
175162
}
176163
#[inline]
177164
fn from_no_close(err: msgs::HandleError) -> Self {
178-
Self { err, needs_channel_force_close: false, shutdown_finish: None }
165+
Self { err, shutdown_finish: None }
179166
}
180167
#[inline]
181168
fn from_finish_shutdown(err: &'static str, channel_id: [u8; 32], shutdown_res: ShutdownResult, channel_update: Option<msgs::ChannelUpdate>) -> Self {
@@ -189,7 +176,6 @@ impl MsgHandleErrInternal {
189176
},
190177
}),
191178
},
192-
needs_channel_force_close: false,
193179
shutdown_finish: Some((shutdown_res, channel_update)),
194180
}
195181
}
@@ -211,7 +197,6 @@ impl MsgHandleErrInternal {
211197
}),
212198
},
213199
},
214-
needs_channel_force_close: false,
215200
shutdown_finish: None,
216201
}
217202
}
@@ -405,28 +390,7 @@ macro_rules! handle_error {
405390
($self: ident, $internal: expr, $their_node_id: expr) => {
406391
match $internal {
407392
Ok(msg) => Ok(msg),
408-
Err(MsgHandleErrInternal { err, needs_channel_force_close, shutdown_finish }) => {
409-
if needs_channel_force_close {
410-
match &err.action {
411-
&Some(msgs::ErrorAction::DisconnectPeer { msg: Some(ref msg) }) => {
412-
if msg.channel_id == [0; 32] {
413-
$self.peer_disconnected(&$their_node_id, true);
414-
} else {
415-
$self.force_close_channel(&msg.channel_id);
416-
}
417-
},
418-
&Some(msgs::ErrorAction::DisconnectPeer { msg: None }) => {},
419-
&Some(msgs::ErrorAction::IgnoreError) => {},
420-
&Some(msgs::ErrorAction::SendErrorMessage { ref msg }) => {
421-
if msg.channel_id == [0; 32] {
422-
$self.peer_disconnected(&$their_node_id, true);
423-
} else {
424-
$self.force_close_channel(&msg.channel_id);
425-
}
426-
},
427-
&None => {},
428-
}
429-
}
393+
Err(MsgHandleErrInternal { err, shutdown_finish }) => {
430394
if let Some((shutdown_res, update_option)) = shutdown_finish {
431395
$self.finish_force_close_channel(shutdown_res);
432396
if let Some(update) = update_option {
@@ -2301,7 +2265,7 @@ impl ChannelManager {
23012265
return Err(MsgHandleErrInternal::send_err_msg_no_close("Got a message for a channel from the wrong node!", msg.channel_id));
23022266
}
23032267
if (msg.failure_code & 0x8000) == 0 {
2304-
return Err(MsgHandleErrInternal::send_err_msg_close_chan("Got update_fail_malformed_htlc with BADONION not set", msg.channel_id));
2268+
try_chan_entry!(self, Err(ChannelError::Close("Got update_fail_malformed_htlc with BADONION not set")), channel_state, chan);
23052269
}
23062270
try_chan_entry!(self, chan.get_mut().update_fail_malformed_htlc(&msg, HTLCFailReason::Reason { failure_code: msg.failure_code, data: Vec::new() }), channel_state, chan);
23072271
Ok(())
@@ -2461,9 +2425,10 @@ impl ChannelManager {
24612425

24622426
let were_node_one = announcement.node_id_1 == our_node_id;
24632427
let msghash = Message::from_slice(&Sha256dHash::from_data(&announcement.encode()[..])[..]).unwrap();
2464-
let bad_sig_action = MsgHandleErrInternal::send_err_msg_close_chan("Bad announcement_signatures node_signature", msg.channel_id);
2465-
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);
2466-
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);
2428+
if self.secp_ctx.verify(&msghash, &msg.node_signature, if were_node_one { &announcement.node_id_2 } else { &announcement.node_id_1 }).is_err() ||
2429+
self.secp_ctx.verify(&msghash, &msg.bitcoin_signature, if were_node_one { &announcement.bitcoin_key_2 } else { &announcement.bitcoin_key_1 }).is_err() {
2430+
try_chan_entry!(self, Err(ChannelError::Close("Bad announcement_signatures node_signature")), channel_state, chan);
2431+
}
24672432

24682433
let our_node_sig = self.secp_ctx.sign(&msghash, &self.our_network_key);
24692434

0 commit comments

Comments
 (0)