Skip to content

Commit b917107

Browse files
committed
A few trivial cleanups of peer_handler.rs
In e620310 we prep'd `peer_handler.rs` for `rustfmt`, but left a few misc things kinda ugly, which we clean up here.
1 parent 9db0fb2 commit b917107

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,13 +1666,15 @@ where
16661666
/// Append a message to a peer's pending outbound/write buffer
16671667
fn enqueue_message<M: wire::Type>(&self, peer: &mut Peer, message: &M) {
16681668
let their_node_id = peer.their_node_id.map(|p| p.0);
1669-
let logger = WithContext::from(&self.logger, their_node_id, None, None);
1670-
// `unwrap` SAFETY: `their_node_id` is guaranteed to be `Some` after the handshake
1671-
let node_id = peer.their_node_id.unwrap().0;
1672-
if is_gossip_msg(message.type_id()) {
1673-
log_gossip!(logger, "Enqueueing message {:?} to {}", message, log_pubkey!(node_id));
1669+
if let Some(node_id) = their_node_id {
1670+
let logger = WithContext::from(&self.logger, their_node_id, None, None);
1671+
if is_gossip_msg(message.type_id()) {
1672+
log_gossip!(logger, "Enqueueing message {:?} to {}", message, log_pubkey!(node_id));
1673+
} else {
1674+
log_trace!(logger, "Enqueueing message {:?} to {}", message, log_pubkey!(node_id));
1675+
}
16741676
} else {
1675-
log_trace!(logger, "Enqueueing message {:?} to {}", message, log_pubkey!(node_id));
1677+
debug_assert!(false, "node_id should be set by the time we send a message");
16761678
}
16771679
peer.msgs_sent_since_pong += 1;
16781680
peer.pending_outbound_buffer.push_back(peer.channel_encryptor.encrypt_message(message));
@@ -1775,21 +1777,26 @@ where
17751777

17761778
macro_rules! insert_node_id {
17771779
() => {
1778-
let their_node_id = peer.their_node_id.map(|p| p.0);
1779-
let logger = WithContext::from(&self.logger, their_node_id, None, None);
1780-
match self.node_id_to_descriptor.lock().unwrap().entry(peer.their_node_id.unwrap().0) {
1780+
let their_node_id = if let Some((node_id, _)) = peer.their_node_id {
1781+
node_id
1782+
} else {
1783+
debug_assert!(false, "Should have a node_id to insert");
1784+
return Err(PeerHandleError {});
1785+
};
1786+
let logger = WithContext::from(&self.logger, Some(their_node_id), None, None);
1787+
match self.node_id_to_descriptor.lock().unwrap().entry(their_node_id) {
17811788
hash_map::Entry::Occupied(e) => {
1782-
log_trace!(logger, "Got second connection with {}, closing", log_pubkey!(peer.their_node_id.unwrap().0));
1789+
log_trace!(logger, "Got second connection with {}, closing", log_pubkey!(their_node_id));
17831790
// Unset `their_node_id` so that we don't generate a peer_disconnected event
1791+
peer.their_node_id = None;
17841792
// Check that the peers map is consistent with the
17851793
// node_id_to_descriptor map, as this has been broken
17861794
// before.
1787-
peer.their_node_id = None;
17881795
debug_assert!(peers.get(e.get()).is_some());
17891796
return Err(PeerHandleError { })
17901797
},
17911798
hash_map::Entry::Vacant(entry) => {
1792-
log_debug!(logger, "Finished noise handshake for connection with {}", log_pubkey!(peer.their_node_id.unwrap().0));
1799+
log_debug!(logger, "Finished noise handshake for connection with {}", log_pubkey!(their_node_id));
17931800
entry.insert(peer_descriptor.clone())
17941801
},
17951802
};
@@ -2115,9 +2122,9 @@ where
21152122
peer_lock.sync_status = InitSyncTracker::ChannelsSyncing(0);
21162123
}
21172124

2118-
let connection = peer_lock.inbound_connection;
2125+
let inbound = peer_lock.inbound_connection;
21192126
let route_handler = &self.message_handler.route_handler;
2120-
if let Err(()) = route_handler.peer_connected(their_node_id, &msg, connection) {
2127+
if let Err(()) = route_handler.peer_connected(their_node_id, &msg, inbound) {
21212128
log_debug!(
21222129
logger,
21232130
"Route Handler decided we couldn't communicate with peer {}",
@@ -2126,7 +2133,7 @@ where
21262133
return Err(PeerHandleError {}.into());
21272134
}
21282135
let chan_handler = &self.message_handler.chan_handler;
2129-
if let Err(()) = chan_handler.peer_connected(their_node_id, &msg, connection) {
2136+
if let Err(()) = chan_handler.peer_connected(their_node_id, &msg, inbound) {
21302137
log_debug!(
21312138
logger,
21322139
"Channel Handler decided we couldn't communicate with peer {}",
@@ -2136,7 +2143,7 @@ where
21362143
return Err(PeerHandleError {}.into());
21372144
}
21382145
let onion_message_handler = &self.message_handler.onion_message_handler;
2139-
if let Err(()) = onion_message_handler.peer_connected(their_node_id, &msg, connection) {
2146+
if let Err(()) = onion_message_handler.peer_connected(their_node_id, &msg, inbound) {
21402147
log_debug!(
21412148
logger,
21422149
"Onion Message Handler decided we couldn't communicate with peer {}",
@@ -2147,7 +2154,7 @@ where
21472154
return Err(PeerHandleError {}.into());
21482155
}
21492156
let custom_handler = &self.message_handler.custom_message_handler;
2150-
if let Err(()) = custom_handler.peer_connected(their_node_id, &msg, connection) {
2157+
if let Err(()) = custom_handler.peer_connected(their_node_id, &msg, inbound) {
21512158
log_debug!(
21522159
logger,
21532160
"Custom Message Handler decided we couldn't communicate with peer {}",
@@ -3480,8 +3487,8 @@ where
34803487
debug_assert!(peer.channel_encryptor.is_ready_for_encryption());
34813488
debug_assert!(peer.their_node_id.is_some());
34823489

3490+
// We use a loop as a `goto` to skip writing the Ping message:
34833491
loop {
3484-
// Used as a `goto` to skip writing a Ping message.
34853492
if peer.awaiting_pong_timer_tick_intervals == -1 {
34863493
// Magic value set in `maybe_send_extra_ping`.
34873494
peer.awaiting_pong_timer_tick_intervals = 1;
@@ -3814,20 +3821,7 @@ mod tests {
38143821
cfgs
38153822
}
38163823

3817-
fn create_network<'a>(
3818-
peer_count: usize, cfgs: &'a Vec<PeerManagerCfg>,
3819-
) -> Vec<
3820-
PeerManager<
3821-
FileDescriptor,
3822-
&'a test_utils::TestChannelMessageHandler,
3823-
&'a test_utils::TestRoutingMessageHandler,
3824-
IgnoringMessageHandler,
3825-
&'a test_utils::TestLogger,
3826-
&'a TestCustomMessageHandler,
3827-
&'a test_utils::TestNodeSigner,
3828-
IgnoringMessageHandler,
3829-
>,
3830-
> {
3824+
fn create_network<'a>(peer_count: usize, cfgs: &'a Vec<PeerManagerCfg>) -> Vec<TestPeer<'a>> {
38313825
let mut peers = Vec::new();
38323826
for i in 0..peer_count {
38333827
let ephemeral_bytes = [i as u8; 32];

0 commit comments

Comments
 (0)