Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate protocol versions #17791

Merged
merged 3 commits into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions chia/_tests/connection_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from cryptography.hazmat.primitives import hashes, serialization

from chia._tests.util.time_out_assert import time_out_assert
from chia.protocols.shared_protocol import capabilities, protocol_version
from chia.protocols.shared_protocol import capabilities
from chia.server.outbound_message import NodeType
from chia.server.server import ChiaServer, ssl_context_for_client
from chia.server.ssl_context import chia_ssl_ca_paths, private_ssl_ca_paths
Expand Down Expand Up @@ -88,7 +88,7 @@ async def add_dummy_connection_wsc(
30,
local_capabilities_for_handshake=capabilities,
)
await wsc.perform_handshake(server._network_id, protocol_version, dummy_port, type)
await wsc.perform_handshake(server._network_id, dummy_port, type)
if wsc.incoming_message_task is not None:
wsc.incoming_message_task.cancel()
return wsc, peer_id
Expand Down
4 changes: 2 additions & 2 deletions chia/_tests/core/server/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from chia.protocols.protocol_message_types import ProtocolMessageTypes
from chia.protocols.shared_protocol import Error, protocol_version
from chia.protocols.wallet_protocol import RejectHeaderRequest
from chia.server.outbound_message import make_msg
from chia.server.outbound_message import NodeType, make_msg
from chia.server.server import ChiaServer
from chia.server.start_full_node import create_full_node_service
from chia.server.start_wallet import create_wallet_service
Expand Down Expand Up @@ -80,7 +80,7 @@ async def test_connection_versions(
outgoing_connection = wallet_node.server.all_connections[full_node.server.node_id]
incoming_connection = full_node.server.all_connections[wallet_node.server.node_id]
for connection in [outgoing_connection, incoming_connection]:
assert connection.protocol_version == Version(protocol_version)
assert connection.protocol_version == Version(protocol_version[NodeType.FULL_NODE])
assert connection.version == __version__
assert connection.get_version() == connection.version

Expand Down
4 changes: 2 additions & 2 deletions chia/_tests/core/ssl/test_ssl.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import aiohttp
import pytest

from chia.protocols.shared_protocol import capabilities, protocol_version
from chia.protocols.shared_protocol import capabilities
from chia.server.outbound_message import NodeType
from chia.server.server import ChiaServer, ssl_context_for_client
from chia.server.ssl_context import chia_ssl_ca_paths, private_ssl_ca_paths
Expand Down Expand Up @@ -33,7 +33,7 @@ async def establish_connection(server: ChiaServer, self_hostname: str, ssl_conte
30,
local_capabilities_for_handshake=capabilities,
)
await wsc.perform_handshake(server._network_id, protocol_version, dummy_port, NodeType.FULL_NODE)
await wsc.perform_handshake(server._network_id, dummy_port, NodeType.FULL_NODE)
await wsc.close()


Expand Down
11 changes: 10 additions & 1 deletion chia/protocols/shared_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,19 @@
from enum import IntEnum
from typing import List, Optional, Tuple

from chia.server.outbound_message import NodeType
from chia.util.ints import int16, uint8, uint16
from chia.util.streamable import Streamable, streamable

protocol_version = "0.0.36"
protocol_version = {
NodeType.FULL_NODE: "0.0.36",
NodeType.HARVESTER: "0.0.36",
NodeType.FARMER: "0.0.36",
NodeType.TIMELORD: "0.0.36",
NodeType.INTRODUCER: "0.0.36",
NodeType.WALLET: "0.0.36",
NodeType.DATA_LAYER: "0.0.36",
}


"""
Expand Down
5 changes: 2 additions & 3 deletions chia/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
from chia.protocols.protocol_message_types import ProtocolMessageTypes
from chia.protocols.protocol_state_machine import message_requires_reply
from chia.protocols.protocol_timing import INVALID_PROTOCOL_BAN_SECONDS
from chia.protocols.shared_protocol import protocol_version
from chia.server.api_protocol import ApiProtocol
from chia.server.introducer_peers import IntroducerPeers
from chia.server.outbound_message import Message, NodeType
Expand Down Expand Up @@ -334,7 +333,7 @@ async def incoming_connection(self, request: web.Request) -> web.StreamResponse:
outbound_rate_limit_percent=self._outbound_rate_limit_percent,
local_capabilities_for_handshake=self._local_capabilities_for_handshake,
)
await connection.perform_handshake(self._network_id, protocol_version, self.get_port(), self._local_type)
await connection.perform_handshake(self._network_id, self.get_port(), self._local_type)
assert connection.connection_type is not None, "handshake failed to set connection type, still None"

# Limit inbound connections to config's specifications.
Expand Down Expand Up @@ -485,7 +484,7 @@ async def start_client(
local_capabilities_for_handshake=self._local_capabilities_for_handshake,
session=session,
)
await connection.perform_handshake(self._network_id, protocol_version, server_port, self._local_type)
await connection.perform_handshake(self._network_id, server_port, self._local_type)
await self.connection_added(connection, on_connect)
# the session has been adopted by the connection, don't close it at
# the end of the function
Expand Down
53 changes: 39 additions & 14 deletions chia/server/ws_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
CONSENSUS_ERROR_BAN_SECONDS,
INTERNAL_PROTOCOL_ERROR_BAN_SECONDS,
)
from chia.protocols.shared_protocol import Capability, Error, Handshake
from chia.protocols.shared_protocol import Capability, Error, Handshake, protocol_version
from chia.server.api_protocol import ApiProtocol
from chia.server.capabilities import known_active_capabilities
from chia.server.outbound_message import Message, NodeType, make_msg
Expand Down Expand Up @@ -188,22 +188,21 @@ def _get_extra_info(self, name: str) -> Optional[Any]:
async def perform_handshake(
self,
network_id: str,
protocol_version: str,
server_port: int,
local_type: NodeType,
) -> None:
outbound_handshake = make_msg(
ProtocolMessageTypes.handshake,
Handshake(
network_id,
protocol_version,
__version__,
uint16(server_port),
uint8(local_type.value),
self.local_capabilities_for_handshake,
),
)
if self.is_outbound:
outbound_handshake = make_msg(
ProtocolMessageTypes.handshake,
Handshake(
network_id,
protocol_version[local_type],
__version__,
uint16(server_port),
uint8(local_type.value),
self.local_capabilities_for_handshake,
),
)
await self._send_message(outbound_handshake)
inbound_handshake_msg = await self._read_one_message()
if inbound_handshake_msg is None:
Expand All @@ -222,6 +221,13 @@ async def perform_handshake(
if inbound_handshake.network_id != network_id:
raise ProtocolError(Err.INCOMPATIBLE_NETWORK_ID)

if inbound_handshake.protocol_version != protocol_version[local_type]:
self.log.warning(
f"protocol version mismatch: "
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
Expand Down Expand Up @@ -249,11 +255,30 @@ async def perform_handshake(
inbound_handshake = Handshake.from_bytes(message.data)
if inbound_handshake.network_id != network_id:
raise ProtocolError(Err.INCOMPATIBLE_NETWORK_ID)

remote_node_type = NodeType(inbound_handshake.node_type)
emlowe marked this conversation as resolved.
Show resolved Hide resolved

if inbound_handshake.protocol_version != protocol_version[remote_node_type]:
self.log.warning(
f"protocol version mismatch: incoming={inbound_handshake.protocol_version} our={protocol_version}"
)

outbound_handshake = make_msg(
ProtocolMessageTypes.handshake,
Handshake(
network_id,
protocol_version[remote_node_type],
__version__,
uint16(server_port),
uint8(local_type.value),
self.local_capabilities_for_handshake,
),
)
await self._send_message(outbound_handshake)
self.version = inbound_handshake.software_version
self.protocol_version = Version(inbound_handshake.protocol_version)
self.peer_server_port = inbound_handshake.server_port
self.connection_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)

Expand Down
Loading