Skip to content

Commit 8c15b50

Browse files
committed
simplify review by means of 3 scopes
1 parent a128bc5 commit 8c15b50

File tree

1 file changed

+180
-178
lines changed

1 file changed

+180
-178
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 180 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,6 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
506506
}
507507

508508
if let &mut PeerState::Connected(ref mut conduit) = &mut peer.encryptor {
509-
510509
macro_rules! encode_and_send_msg {
511510
($msg: expr) => {
512511
{
@@ -550,206 +549,209 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
550549
if offset == 0 {
551550
// nothing got read
552551
break;
553-
}else{
552+
} else {
554553
peer.pending_read_buffer.drain(0..offset);
555554
}
556555

557556
// something got read, so we definitely have a message
558557
let msg_data = next_message_result.0.unwrap();
559558

560-
let mut reader = ::std::io::Cursor::new(&msg_data[..]);
561-
let message_result = wire::read(&mut reader);
562-
let message = match message_result {
563-
Ok(x) => x,
564-
Err(e) => {
565-
match e {
566-
msgs::DecodeError::UnknownVersion => return Err(PeerHandleError { no_connection_possible: false }),
567-
msgs::DecodeError::UnknownRequiredFeature => {
568-
log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to update!");
569-
continue;
570-
}
571-
msgs::DecodeError::InvalidValue => {
572-
log_debug!(self, "Got an invalid value while deserializing message");
573-
return Err(PeerHandleError { no_connection_possible: false });
574-
}
575-
msgs::DecodeError::ShortRead => {
576-
log_debug!(self, "Deserialization failed due to shortness of message");
577-
return Err(PeerHandleError { no_connection_possible: false });
578-
}
579-
msgs::DecodeError::ExtraAddressesPerType => {
580-
log_debug!(self, "Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407");
581-
continue;
559+
{{{
560+
561+
let mut reader = ::std::io::Cursor::new(&msg_data[..]);
562+
let message_result = wire::read(&mut reader);
563+
let message = match message_result {
564+
Ok(x) => x,
565+
Err(e) => {
566+
match e {
567+
msgs::DecodeError::UnknownVersion => return Err(PeerHandleError { no_connection_possible: false }),
568+
msgs::DecodeError::UnknownRequiredFeature => {
569+
log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to update!");
570+
continue;
571+
}
572+
msgs::DecodeError::InvalidValue => {
573+
log_debug!(self, "Got an invalid value while deserializing message");
574+
return Err(PeerHandleError { no_connection_possible: false });
575+
}
576+
msgs::DecodeError::ShortRead => {
577+
log_debug!(self, "Deserialization failed due to shortness of message");
578+
return Err(PeerHandleError { no_connection_possible: false });
579+
}
580+
msgs::DecodeError::ExtraAddressesPerType => {
581+
log_debug!(self, "Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407");
582+
continue;
583+
}
584+
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError { no_connection_possible: false }),
585+
msgs::DecodeError::Io(_) => return Err(PeerHandleError { no_connection_possible: false }),
582586
}
583-
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError { no_connection_possible: false }),
584-
msgs::DecodeError::Io(_) => return Err(PeerHandleError { no_connection_possible: false }),
585-
}
586-
}
587-
};
588-
589-
log_trace!(self, "Received message of type {} from {}", message.type_id(), log_pubkey!(peer.their_node_id.unwrap()));
590-
591-
// Need an Init as first message
592-
if let wire::Message::Init(_) = message {
593-
} else if peer.their_features.is_none() {
594-
log_trace!(self, "Peer {} sent non-Init first message", log_pubkey!(peer.their_node_id.unwrap()));
595-
return Err(PeerHandleError{ no_connection_possible: false });
596-
}
597-
598-
match message {
599-
// Setup and Control messages:
600-
wire::Message::Init(msg) => {
601-
if msg.features.requires_unknown_bits() {
602-
log_info!(self, "Peer global features required unknown version bits");
603-
return Err(PeerHandleError{ no_connection_possible: true });
604-
}
605-
if msg.features.requires_unknown_bits() {
606-
log_info!(self, "Peer local features required unknown version bits");
607-
return Err(PeerHandleError{ no_connection_possible: true });
608-
}
609-
if peer.their_features.is_some() {
610-
return Err(PeerHandleError{ no_connection_possible: false });
611587
}
588+
};
612589

613-
log_info!(self, "Received peer Init message: data_loss_protect: {}, initial_routing_sync: {}, upfront_shutdown_script: {}, unkown local flags: {}, unknown global flags: {}",
614-
if msg.features.supports_data_loss_protect() { "supported" } else { "not supported"},
615-
if msg.features.initial_routing_sync() { "requested" } else { "not requested" },
616-
if msg.features.supports_upfront_shutdown_script() { "supported" } else { "not supported"},
617-
if msg.features.supports_unknown_bits() { "present" } else { "none" },
618-
if msg.features.supports_unknown_bits() { "present" } else { "none" });
590+
log_trace!(self, "Received message of type {} from {}", message.type_id(), log_pubkey!(peer.their_node_id.unwrap()));
619591

620-
if msg.features.initial_routing_sync() {
621-
peer.sync_status = InitSyncTracker::ChannelsSyncing(0);
622-
peers.peers_needing_send.insert(peer_descriptor.clone());
623-
}
592+
// Need an Init as first message
593+
if let wire::Message::Init(_) = message {} else if peer.their_features.is_none() {
594+
log_trace!(self, "Peer {} sent non-Init first message", log_pubkey!(peer.their_node_id.unwrap()));
595+
return Err(PeerHandleError { no_connection_possible: false });
596+
}
624597

625-
if !peer.outbound {
626-
let mut features = InitFeatures::supported();
627-
if self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) {
628-
features.set_initial_routing_sync();
598+
match message {
599+
// Setup and Control messages:
600+
wire::Message::Init(msg) => {
601+
if msg.features.requires_unknown_bits() {
602+
log_info!(self, "Peer global features required unknown version bits");
603+
return Err(PeerHandleError { no_connection_possible: true });
629604
}
630-
631-
let resp = msgs::Init { features };
632-
encode_and_send_msg!(resp);
633-
}
634-
635-
self.message_handler.chan_handler.peer_connected(&peer.their_node_id.unwrap(), &msg);
636-
peer.their_features = Some(msg.features);
637-
},
638-
wire::Message::Error(msg) => {
639-
let mut data_is_printable = true;
640-
for b in msg.data.bytes() {
641-
if b < 32 || b > 126 {
642-
data_is_printable = false;
643-
break;
605+
if msg.features.requires_unknown_bits() {
606+
log_info!(self, "Peer local features required unknown version bits");
607+
return Err(PeerHandleError { no_connection_possible: true });
608+
}
609+
if peer.their_features.is_some() {
610+
return Err(PeerHandleError { no_connection_possible: false });
644611
}
645-
}
646-
647-
if data_is_printable {
648-
log_debug!(self, "Got Err message from {}: {}", log_pubkey!(peer.their_node_id.unwrap()), msg.data);
649-
} else {
650-
log_debug!(self, "Got Err message from {} with non-ASCII error message", log_pubkey!(peer.their_node_id.unwrap()));
651-
}
652-
self.message_handler.chan_handler.handle_error(&peer.their_node_id.unwrap(), &msg);
653-
if msg.channel_id == [0; 32] {
654-
return Err(PeerHandleError{ no_connection_possible: true });
655-
}
656-
},
657-
658-
wire::Message::Ping(msg) => {
659-
if msg.ponglen < 65532 {
660-
let resp = msgs::Pong { byteslen: msg.ponglen };
661-
encode_and_send_msg!(resp);
662-
}
663-
},
664-
wire::Message::Pong(_msg) => {
665-
peer.awaiting_pong = false;
666-
},
667612

668-
// Channel messages:
669-
wire::Message::OpenChannel(msg) => {
670-
self.message_handler.chan_handler.handle_open_channel(&peer.their_node_id.unwrap(), peer.their_features.clone().unwrap(), &msg);
671-
},
672-
wire::Message::AcceptChannel(msg) => {
673-
self.message_handler.chan_handler.handle_accept_channel(&peer.their_node_id.unwrap(), peer.their_features.clone().unwrap(), &msg);
674-
},
613+
log_info!(self, "Received peer Init message: data_loss_protect: {}, initial_routing_sync: {}, upfront_shutdown_script: {}, unkown local flags: {}, unknown global flags: {}",
614+
if msg.features.supports_data_loss_protect() { "supported" } else { "not supported"},
615+
if msg.features.initial_routing_sync() { "requested" } else { "not requested" },
616+
if msg.features.supports_upfront_shutdown_script() { "supported" } else { "not supported"},
617+
if msg.features.supports_unknown_bits() { "present" } else { "none" },
618+
if msg.features.supports_unknown_bits() { "present" } else { "none" });
675619

676-
wire::Message::FundingCreated(msg) => {
677-
self.message_handler.chan_handler.handle_funding_created(&peer.their_node_id.unwrap(), &msg);
678-
},
679-
wire::Message::FundingSigned(msg) => {
680-
self.message_handler.chan_handler.handle_funding_signed(&peer.their_node_id.unwrap(), &msg);
681-
},
682-
wire::Message::FundingLocked(msg) => {
683-
self.message_handler.chan_handler.handle_funding_locked(&peer.their_node_id.unwrap(), &msg);
684-
},
620+
if msg.features.initial_routing_sync() {
621+
peer.sync_status = InitSyncTracker::ChannelsSyncing(0);
622+
peers.peers_needing_send.insert(peer_descriptor.clone());
623+
}
685624

686-
wire::Message::Shutdown(msg) => {
687-
self.message_handler.chan_handler.handle_shutdown(&peer.their_node_id.unwrap(), &msg);
688-
},
689-
wire::Message::ClosingSigned(msg) => {
690-
self.message_handler.chan_handler.handle_closing_signed(&peer.their_node_id.unwrap(), &msg);
691-
},
625+
if !peer.outbound {
626+
let mut features = InitFeatures::supported();
627+
if self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) {
628+
features.set_initial_routing_sync();
629+
}
692630

693-
// Commitment messages:
694-
wire::Message::UpdateAddHTLC(msg) => {
695-
self.message_handler.chan_handler.handle_update_add_htlc(&peer.their_node_id.unwrap(), &msg);
696-
},
697-
wire::Message::UpdateFulfillHTLC(msg) => {
698-
self.message_handler.chan_handler.handle_update_fulfill_htlc(&peer.their_node_id.unwrap(), &msg);
699-
},
700-
wire::Message::UpdateFailHTLC(msg) => {
701-
self.message_handler.chan_handler.handle_update_fail_htlc(&peer.their_node_id.unwrap(), &msg);
702-
},
703-
wire::Message::UpdateFailMalformedHTLC(msg) => {
704-
self.message_handler.chan_handler.handle_update_fail_malformed_htlc(&peer.their_node_id.unwrap(), &msg);
705-
},
631+
let resp = msgs::Init { features };
632+
encode_and_send_msg!(resp);
633+
}
706634

707-
wire::Message::CommitmentSigned(msg) => {
708-
self.message_handler.chan_handler.handle_commitment_signed(&peer.their_node_id.unwrap(), &msg);
709-
},
710-
wire::Message::RevokeAndACK(msg) => {
711-
self.message_handler.chan_handler.handle_revoke_and_ack(&peer.their_node_id.unwrap(), &msg);
712-
},
713-
wire::Message::UpdateFee(msg) => {
714-
self.message_handler.chan_handler.handle_update_fee(&peer.their_node_id.unwrap(), &msg);
715-
},
716-
wire::Message::ChannelReestablish(msg) => {
717-
self.message_handler.chan_handler.handle_channel_reestablish(&peer.their_node_id.unwrap(), &msg);
718-
},
635+
self.message_handler.chan_handler.peer_connected(&peer.their_node_id.unwrap(), &msg);
636+
peer.their_features = Some(msg.features);
637+
},
638+
wire::Message::Error(msg) => {
639+
let mut data_is_printable = true;
640+
for b in msg.data.bytes() {
641+
if b < 32 || b > 126 {
642+
data_is_printable = false;
643+
break;
644+
}
645+
}
719646

720-
// Routing messages:
721-
wire::Message::AnnouncementSignatures(msg) => {
722-
self.message_handler.chan_handler.handle_announcement_signatures(&peer.their_node_id.unwrap(), &msg);
723-
},
724-
wire::Message::ChannelAnnouncement(msg) => {
725-
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_announcement(&msg));
647+
if data_is_printable {
648+
log_debug!(self, "Got Err message from {}: {}", log_pubkey!(peer.their_node_id.unwrap()), msg.data);
649+
} else {
650+
log_debug!(self, "Got Err message from {} with non-ASCII error message", log_pubkey!(peer.their_node_id.unwrap()));
651+
}
652+
self.message_handler.chan_handler.handle_error(&peer.their_node_id.unwrap(), &msg);
653+
if msg.channel_id == [0; 32] {
654+
return Err(PeerHandleError { no_connection_possible: true });
655+
}
656+
},
726657

727-
if should_forward {
728-
// TODO: forward msg along to all our other peers!
729-
}
730-
},
731-
wire::Message::NodeAnnouncement(msg) => {
732-
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_node_announcement(&msg));
658+
wire::Message::Ping(msg) => {
659+
if msg.ponglen < 65532 {
660+
let resp = msgs::Pong { byteslen: msg.ponglen };
661+
encode_and_send_msg!(resp);
662+
}
663+
},
664+
wire::Message::Pong(_msg) => {
665+
peer.awaiting_pong = false;
666+
},
667+
668+
// Channel messages:
669+
wire::Message::OpenChannel(msg) => {
670+
self.message_handler.chan_handler.handle_open_channel(&peer.their_node_id.unwrap(), peer.their_features.clone().unwrap(), &msg);
671+
},
672+
wire::Message::AcceptChannel(msg) => {
673+
self.message_handler.chan_handler.handle_accept_channel(&peer.their_node_id.unwrap(), peer.their_features.clone().unwrap(), &msg);
674+
},
675+
676+
wire::Message::FundingCreated(msg) => {
677+
self.message_handler.chan_handler.handle_funding_created(&peer.their_node_id.unwrap(), &msg);
678+
},
679+
wire::Message::FundingSigned(msg) => {
680+
self.message_handler.chan_handler.handle_funding_signed(&peer.their_node_id.unwrap(), &msg);
681+
},
682+
wire::Message::FundingLocked(msg) => {
683+
self.message_handler.chan_handler.handle_funding_locked(&peer.their_node_id.unwrap(), &msg);
684+
},
685+
686+
wire::Message::Shutdown(msg) => {
687+
self.message_handler.chan_handler.handle_shutdown(&peer.their_node_id.unwrap(), &msg);
688+
},
689+
wire::Message::ClosingSigned(msg) => {
690+
self.message_handler.chan_handler.handle_closing_signed(&peer.their_node_id.unwrap(), &msg);
691+
},
692+
693+
// Commitment messages:
694+
wire::Message::UpdateAddHTLC(msg) => {
695+
self.message_handler.chan_handler.handle_update_add_htlc(&peer.their_node_id.unwrap(), &msg);
696+
},
697+
wire::Message::UpdateFulfillHTLC(msg) => {
698+
self.message_handler.chan_handler.handle_update_fulfill_htlc(&peer.their_node_id.unwrap(), &msg);
699+
},
700+
wire::Message::UpdateFailHTLC(msg) => {
701+
self.message_handler.chan_handler.handle_update_fail_htlc(&peer.their_node_id.unwrap(), &msg);
702+
},
703+
wire::Message::UpdateFailMalformedHTLC(msg) => {
704+
self.message_handler.chan_handler.handle_update_fail_malformed_htlc(&peer.their_node_id.unwrap(), &msg);
705+
},
706+
707+
wire::Message::CommitmentSigned(msg) => {
708+
self.message_handler.chan_handler.handle_commitment_signed(&peer.their_node_id.unwrap(), &msg);
709+
},
710+
wire::Message::RevokeAndACK(msg) => {
711+
self.message_handler.chan_handler.handle_revoke_and_ack(&peer.their_node_id.unwrap(), &msg);
712+
},
713+
wire::Message::UpdateFee(msg) => {
714+
self.message_handler.chan_handler.handle_update_fee(&peer.their_node_id.unwrap(), &msg);
715+
},
716+
wire::Message::ChannelReestablish(msg) => {
717+
self.message_handler.chan_handler.handle_channel_reestablish(&peer.their_node_id.unwrap(), &msg);
718+
},
719+
720+
// Routing messages:
721+
wire::Message::AnnouncementSignatures(msg) => {
722+
self.message_handler.chan_handler.handle_announcement_signatures(&peer.their_node_id.unwrap(), &msg);
723+
},
724+
wire::Message::ChannelAnnouncement(msg) => {
725+
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_announcement(&msg));
726+
727+
if should_forward {
728+
// TODO: forward msg along to all our other peers!
729+
}
730+
},
731+
wire::Message::NodeAnnouncement(msg) => {
732+
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_node_announcement(&msg));
733733

734-
if should_forward {
735-
// TODO: forward msg along to all our other peers!
736-
}
737-
},
738-
wire::Message::ChannelUpdate(msg) => {
739-
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_update(&msg));
734+
if should_forward {
735+
// TODO: forward msg along to all our other peers!
736+
}
737+
},
738+
wire::Message::ChannelUpdate(msg) => {
739+
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_update(&msg));
740740

741-
if should_forward {
742-
// TODO: forward msg along to all our other peers!
743-
}
744-
},
741+
if should_forward {
742+
// TODO: forward msg along to all our other peers!
743+
}
744+
},
745+
746+
// Unknown messages:
747+
wire::Message::Unknown(msg_type) if msg_type.is_even() => {
748+
// Fail the channel if message is an even, unknown type as per BOLT #1.
749+
return Err(PeerHandleError { no_connection_possible: true });
750+
},
751+
wire::Message::Unknown(_) => {},
752+
}
745753

746-
// Unknown messages:
747-
wire::Message::Unknown(msg_type) if msg_type.is_even() => {
748-
// Fail the channel if message is an even, unknown type as per BOLT #1.
749-
return Err(PeerHandleError{ no_connection_possible: true });
750-
},
751-
wire::Message::Unknown(_) => {},
752-
}
754+
}}}
753755
}
754756

755757
}

0 commit comments

Comments
 (0)