Repository: Chia-Network/chia-blockchain
File: chia/server/ws_connection.py (Python)

Code before:
                ),
            )
            await self._send_message(outbound_handshake)

        try:
            message = await self._read_one_message()
        except Exception:
            raise ProtocolError(Err.INVALID_HANDSHAKE)

        if message is None:
            raise ProtocolError(Err.INVALID_HANDSHAKE)

        # Handle case of invalid ProtocolMessageType
        try:
            inbound_handshake = Handshake.from_bytes(message.data)
            message_type = ProtocolMessageTypes(message.type)
        except Exception:
            raise ProtocolError(Err.INVALID_HANDSHAKE)

        if message_type != ProtocolMessageTypes.handshake:
            raise ProtocolError(Err.INVALID_HANDSHAKE)

        if inbound_handshake.network_id != network_id:
            raise ProtocolError(Err.INCOMPATIBLE_NETWORK_ID)

        if self.is_outbound:
            if (
                local_type in {NodeType.FARMER, NodeType.HARVESTER}
                and inbound_handshake.protocol_version != protocol_version[local_type]
            ):
                self.log.warning(
                    f"protocol version mismatch: "
                    f"local_type={local_type} "
                    f"incoming={inbound_handshake.protocol_version} "
                    f"our={protocol_version[local_type]}"
                )

        self.version = inbound_handshake.software_version
        self.protocol_version = Version(inbound_handshake.protocol_version)
        self.peer_server_port = inbound_handshake.server_port
        remote_node_type = NodeType(inbound_handshake.node_type)
        self.connection_type = remote_node_type
        # "1" means capability is enabled
        self.peer_capabilities = known_active_capabilities(inbound_handshake.capabilities)

        if len(self.version.encode("utf-8")) > MAX_VERSION_STRING_BYTES:
            self.log.debug("version string too long")
            raise ProtocolError(Err.INVALID_HANDSHAKE)

        if not self.is_outbound:
            if (

Review comment (suggestion):
We can now combine these conditions:

```suggestion
        if (
            self.is_outbound
            and local_type in {NodeType.FARMER, NodeType.HARVESTER}
            and inbound_handshake.protocol_version != protocol_version[local_type]
        ):
```

Code after:
                ),
            )
            await self._send_message(outbound_handshake)

        try:
            message = await self._read_one_message()
        except Exception:
            raise ProtocolError(Err.INVALID_HANDSHAKE)

        if message is None:
            raise ProtocolError(Err.INVALID_HANDSHAKE)

        # Handle case of invalid ProtocolMessageType
        try:
            inbound_handshake = Handshake.from_bytes(message.data)
            message_type = ProtocolMessageTypes(message.type)
        except Exception:
            raise ProtocolError(Err.INVALID_HANDSHAKE)

        if message_type != ProtocolMessageTypes.handshake:
            raise ProtocolError(Err.INVALID_HANDSHAKE)

        if inbound_handshake.network_id != network_id:
            raise ProtocolError(Err.INCOMPATIBLE_NETWORK_ID)

        if (
            self.is_outbound
            and local_type in {NodeType.FARMER, NodeType.HARVESTER}
            and inbound_handshake.protocol_version != protocol_version[local_type]
        ):
            self.log.warning(
                f"protocol version mismatch: "
                f"local_type={local_type} "
                f"incoming={inbound_handshake.protocol_version} "
                f"our={protocol_version[local_type]}"
            )

        remote_node_type = NodeType(inbound_handshake.node_type)

        if len(inbound_handshake.software_version.encode("utf-8")) > MAX_VERSION_STRING_BYTES:
            self.log.debug("version string too long")
            raise ProtocolError(Err.INVALID_HANDSHAKE)

        if not self.is_outbound:
            if (
                remote_node_type in {NodeType.FARMER, NodeType.HARVESTER}
                and inbound_handshake.protocol_version != protocol_version[remote_node_type]
            ):
                self.log.warning(
                    f"protocol version mismatch: "
                    f"remote_type={remote_node_type} "