Skip to content

Commit

Permalink
Change port number type in PeerInfo to uint16
Browse files Browse the repository at this point in the history
Port numbers in TCP are 16-bit unsigned integers.
  • Loading branch information
rostislav authored and mariano54 committed Nov 15, 2019
1 parent 20e6be5 commit 99c612d
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/server/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(self, local_type: NodeType, connection_type: Optional[NodeType], sr
self.local_port = server_port
self.peer_host = self.writer.get_extra_info("peername")[0]
self.peer_port = self.writer.get_extra_info("peername")[1]
self.peer_server_port = None
self.peer_server_port: Optional[int] = None
self.node_id = None

# Connection metrics
Expand Down
5 changes: 3 additions & 2 deletions src/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from src.util import partial_func
from src.util.errors import InvalidHandshake, IncompatibleProtocolVersion, InvalidAck, InvalidProtocolMessage
from src.util.network import create_node_id
from src.util.ints import uint16

exited = False
# Each message is prepended with LENGTH_BYTES bytes specifying the length
Expand Down Expand Up @@ -226,7 +227,7 @@ async def perform_handshake(self, connection: Connection) -> AsyncGenerator[Conn
and nothing is yielded.
"""
# Send handshake message
outbound_handshake = Message("handshake", Handshake(protocol_version, self._node_id, self._port, self._local_type))
outbound_handshake = Message("handshake", Handshake(protocol_version, self._node_id, uint16(self._port), self._local_type))

try:
await connection.send(outbound_handshake)
Expand All @@ -239,7 +240,7 @@ async def perform_handshake(self, connection: Connection) -> AsyncGenerator[Conn

# Makes sure that we only start one connection with each peer
connection.node_id = inbound_handshake.node_id
connection.peer_server_port = inbound_handshake.server_port
connection.peer_server_port = int(inbound_handshake.server_port)
connection.connection_type = inbound_handshake.node_type
if self.global_connections.have_connection(connection):
log.warning(f"Duplicate connection to {connection}")
Expand Down
4 changes: 2 additions & 2 deletions src/types/peer_info.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from src.util.streamable import streamable, Streamable
from src.util.ints import uint32
from src.util.ints import uint16
from dataclasses import dataclass


@dataclass(frozen=True)
@streamable
class PeerInfo(Streamable):
host: str
port: uint32
port: uint16
4 changes: 2 additions & 2 deletions src/ui/prompt_ui.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from src.types.full_block import FullBlock
from src.types.sized_bytes import bytes32
from src.types.peer_info import PeerInfo
from src.util.ints import uint32
from src.util.ints import uint16
from src.server.server import ChiaServer
from src.server.connection import PeerConnections, NodeType

Expand Down Expand Up @@ -209,7 +209,7 @@ async def add_connection(self, text: str):
except ValueError: # Not yet in layout
self.error_msg.text = "Enter a valid IP and port in the following format: 10.5.4.3:8000"
return
target_node: PeerInfo = PeerInfo(ip, uint32(int(port)))
target_node: PeerInfo = PeerInfo(ip, uint16(int(port)))
log.error(f"Want to connect to {ip}, {port}")
if not (await self.node_server.start_client(target_node, None)):
self.error_msg.text = f"Failed to connect to {ip}:{port}"
Expand Down

0 comments on commit 99c612d

Please sign in to comment.