Skip to content

Commit e670927

Browse files
committed
adjust scope indentation
1 parent 8c15b50 commit e670927

File tree

1 file changed

+175
-173
lines changed

1 file changed

+175
-173
lines changed

lightning/src/ln/peer_handler.rs

Lines changed: 175 additions & 173 deletions
Original file line numberDiff line numberDiff line change
@@ -556,202 +556,204 @@ impl<Descriptor: SocketDescriptor, CM: Deref> PeerManager<Descriptor, CM> where
556556
// something got read, so we definitely have a message
557557
let msg_data = next_message_result.0.unwrap();
558558

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;
559+
{
560+
{
561+
{
562+
let mut reader = ::std::io::Cursor::new(&msg_data[..]);
563+
let message_result = wire::read(&mut reader);
564+
let message = match message_result {
565+
Ok(x) => x,
566+
Err(e) => {
567+
match e {
568+
msgs::DecodeError::UnknownVersion => return Err(PeerHandleError { no_connection_possible: false }),
569+
msgs::DecodeError::UnknownRequiredFeature => {
570+
log_debug!(self, "Got a channel/node announcement with an known required feature flag, you may want to update!");
571+
continue;
572+
}
573+
msgs::DecodeError::InvalidValue => {
574+
log_debug!(self, "Got an invalid value while deserializing message");
575+
return Err(PeerHandleError { no_connection_possible: false });
576+
}
577+
msgs::DecodeError::ShortRead => {
578+
log_debug!(self, "Deserialization failed due to shortness of message");
579+
return Err(PeerHandleError { no_connection_possible: false });
580+
}
581+
msgs::DecodeError::ExtraAddressesPerType => {
582+
log_debug!(self, "Error decoding message, ignoring due to lnd spec incompatibility. See https://github.com/lightningnetwork/lnd/issues/1407");
583+
continue;
584+
}
585+
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError { no_connection_possible: false }),
586+
msgs::DecodeError::Io(_) => return Err(PeerHandleError { no_connection_possible: false }),
587+
}
583588
}
584-
msgs::DecodeError::BadLengthDescriptor => return Err(PeerHandleError { no_connection_possible: false }),
585-
msgs::DecodeError::Io(_) => return Err(PeerHandleError { no_connection_possible: false }),
586-
}
587-
}
588-
};
589+
};
589590

590-
log_trace!(self, "Received message of type {} from {}", message.type_id(), log_pubkey!(peer.their_node_id.unwrap()));
591+
log_trace!(self, "Received message of type {} from {}", message.type_id(), log_pubkey!(peer.their_node_id.unwrap()));
591592

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-
}
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() {
593+
// Need an Init as first message
594+
if let wire::Message::Init(_) = message {} else if peer.their_features.is_none() {
595+
log_trace!(self, "Peer {} sent non-Init first message", log_pubkey!(peer.their_node_id.unwrap()));
610596
return Err(PeerHandleError { no_connection_possible: false });
611597
}
612598

613-
log_info!(self, "Received peer Init message: data_loss_protect: {}, initial_routing_sync: {}, upfront_shutdown_script: {}, unkown local flags: {}, unknown global flags: {}",
599+
match message {
600+
// Setup and Control messages:
601+
wire::Message::Init(msg) => {
602+
if msg.features.requires_unknown_bits() {
603+
log_info!(self, "Peer global features required unknown version bits");
604+
return Err(PeerHandleError { no_connection_possible: true });
605+
}
606+
if msg.features.requires_unknown_bits() {
607+
log_info!(self, "Peer local features required unknown version bits");
608+
return Err(PeerHandleError { no_connection_possible: true });
609+
}
610+
if peer.their_features.is_some() {
611+
return Err(PeerHandleError { no_connection_possible: false });
612+
}
613+
614+
log_info!(self, "Received peer Init message: data_loss_protect: {}, initial_routing_sync: {}, upfront_shutdown_script: {}, unkown local flags: {}, unknown global flags: {}",
614615
if msg.features.supports_data_loss_protect() { "supported" } else { "not supported"},
615616
if msg.features.initial_routing_sync() { "requested" } else { "not requested" },
616617
if msg.features.supports_upfront_shutdown_script() { "supported" } else { "not supported"},
617618
if msg.features.supports_unknown_bits() { "present" } else { "none" },
618619
if msg.features.supports_unknown_bits() { "present" } else { "none" });
619620

620-
if msg.features.initial_routing_sync() {
621-
peer.sync_status = InitSyncTracker::ChannelsSyncing(0);
622-
peers.peers_needing_send.insert(peer_descriptor.clone());
623-
}
621+
if msg.features.initial_routing_sync() {
622+
peer.sync_status = InitSyncTracker::ChannelsSyncing(0);
623+
peers.peers_needing_send.insert(peer_descriptor.clone());
624+
}
625+
626+
if !peer.outbound {
627+
let mut features = InitFeatures::supported();
628+
if self.message_handler.route_handler.should_request_full_sync(&peer.their_node_id.unwrap()) {
629+
features.set_initial_routing_sync();
630+
}
631+
632+
let resp = msgs::Init { features };
633+
encode_and_send_msg!(resp);
634+
}
624635

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();
636+
self.message_handler.chan_handler.peer_connected(&peer.their_node_id.unwrap(), &msg);
637+
peer.their_features = Some(msg.features);
638+
}
639+
wire::Message::Error(msg) => {
640+
let mut data_is_printable = true;
641+
for b in msg.data.bytes() {
642+
if b < 32 || b > 126 {
643+
data_is_printable = false;
644+
break;
645+
}
646+
}
647+
648+
if data_is_printable {
649+
log_debug!(self, "Got Err message from {}: {}", log_pubkey!(peer.their_node_id.unwrap()), msg.data);
650+
} else {
651+
log_debug!(self, "Got Err message from {} with non-ASCII error message", log_pubkey!(peer.their_node_id.unwrap()));
652+
}
653+
self.message_handler.chan_handler.handle_error(&peer.their_node_id.unwrap(), &msg);
654+
if msg.channel_id == [0; 32] {
655+
return Err(PeerHandleError { no_connection_possible: true });
656+
}
629657
}
630658

631-
let resp = msgs::Init { features };
632-
encode_and_send_msg!(resp);
633-
}
659+
wire::Message::Ping(msg) => {
660+
if msg.ponglen < 65532 {
661+
let resp = msgs::Pong { byteslen: msg.ponglen };
662+
encode_and_send_msg!(resp);
663+
}
664+
}
665+
wire::Message::Pong(_msg) => {
666+
peer.awaiting_pong = false;
667+
}
634668

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;
669+
// Channel messages:
670+
wire::Message::OpenChannel(msg) => {
671+
self.message_handler.chan_handler.handle_open_channel(&peer.their_node_id.unwrap(), peer.their_features.clone().unwrap(), &msg);
672+
}
673+
wire::Message::AcceptChannel(msg) => {
674+
self.message_handler.chan_handler.handle_accept_channel(&peer.their_node_id.unwrap(), peer.their_features.clone().unwrap(), &msg);
644675
}
645-
}
646676

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-
},
677+
wire::Message::FundingCreated(msg) => {
678+
self.message_handler.chan_handler.handle_funding_created(&peer.their_node_id.unwrap(), &msg);
679+
}
680+
wire::Message::FundingSigned(msg) => {
681+
self.message_handler.chan_handler.handle_funding_signed(&peer.their_node_id.unwrap(), &msg);
682+
}
683+
wire::Message::FundingLocked(msg) => {
684+
self.message_handler.chan_handler.handle_funding_locked(&peer.their_node_id.unwrap(), &msg);
685+
}
657686

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));
687+
wire::Message::Shutdown(msg) => {
688+
self.message_handler.chan_handler.handle_shutdown(&peer.their_node_id.unwrap(), &msg);
689+
}
690+
wire::Message::ClosingSigned(msg) => {
691+
self.message_handler.chan_handler.handle_closing_signed(&peer.their_node_id.unwrap(), &msg);
692+
}
733693

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));
694+
// Commitment messages:
695+
wire::Message::UpdateAddHTLC(msg) => {
696+
self.message_handler.chan_handler.handle_update_add_htlc(&peer.their_node_id.unwrap(), &msg);
697+
}
698+
wire::Message::UpdateFulfillHTLC(msg) => {
699+
self.message_handler.chan_handler.handle_update_fulfill_htlc(&peer.their_node_id.unwrap(), &msg);
700+
}
701+
wire::Message::UpdateFailHTLC(msg) => {
702+
self.message_handler.chan_handler.handle_update_fail_htlc(&peer.their_node_id.unwrap(), &msg);
703+
}
704+
wire::Message::UpdateFailMalformedHTLC(msg) => {
705+
self.message_handler.chan_handler.handle_update_fail_malformed_htlc(&peer.their_node_id.unwrap(), &msg);
706+
}
707+
708+
wire::Message::CommitmentSigned(msg) => {
709+
self.message_handler.chan_handler.handle_commitment_signed(&peer.their_node_id.unwrap(), &msg);
710+
}
711+
wire::Message::RevokeAndACK(msg) => {
712+
self.message_handler.chan_handler.handle_revoke_and_ack(&peer.their_node_id.unwrap(), &msg);
713+
}
714+
wire::Message::UpdateFee(msg) => {
715+
self.message_handler.chan_handler.handle_update_fee(&peer.their_node_id.unwrap(), &msg);
716+
}
717+
wire::Message::ChannelReestablish(msg) => {
718+
self.message_handler.chan_handler.handle_channel_reestablish(&peer.their_node_id.unwrap(), &msg);
719+
}
740720

741-
if should_forward {
742-
// TODO: forward msg along to all our other peers!
721+
// Routing messages:
722+
wire::Message::AnnouncementSignatures(msg) => {
723+
self.message_handler.chan_handler.handle_announcement_signatures(&peer.their_node_id.unwrap(), &msg);
724+
}
725+
wire::Message::ChannelAnnouncement(msg) => {
726+
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_announcement(&msg));
727+
728+
if should_forward {
729+
// TODO: forward msg along to all our other peers!
730+
}
731+
}
732+
wire::Message::NodeAnnouncement(msg) => {
733+
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_node_announcement(&msg));
734+
735+
if should_forward {
736+
// TODO: forward msg along to all our other peers!
737+
}
738+
}
739+
wire::Message::ChannelUpdate(msg) => {
740+
let should_forward = try_potential_handleerror!(self.message_handler.route_handler.handle_channel_update(&msg));
741+
742+
if should_forward {
743+
// TODO: forward msg along to all our other peers!
744+
}
745+
}
746+
747+
// Unknown messages:
748+
wire::Message::Unknown(msg_type) if msg_type.is_even() => {
749+
// Fail the channel if message is an even, unknown type as per BOLT #1.
750+
return Err(PeerHandleError { no_connection_possible: true });
751+
}
752+
wire::Message::Unknown(_) => {}
743753
}
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(_) => {},
754+
}
752755
}
753-
754-
}}}
756+
}
755757
}
756758

757759
}

0 commit comments

Comments
 (0)