Better error handling in handshake

This commit is contained in:
Mariano Sorgente 2019-11-04 10:23:54 -08:00
parent 1038194e3b
commit e14933eeca
4 changed files with 16 additions and 17 deletions

View File

@ -1,4 +1,3 @@
debug: True
host: "127.0.0.1"
port: 8001

View File

@ -3,7 +3,7 @@
# import sys
# from src.full_node import FullNode
# from src.server.server import ChiaServer
# from src.util.network import parse_host_port
# from src.util.network import parse_host_port, create_node_id
# from src.server.outbound_message import NodeType
# from src.types.peer_info import PeerInfo
# from src.store.full_node_store import FullNodeStore
@ -17,5 +17,4 @@
# await blockchain.initialize()
# full_node = FullNode(store, blockchain)
# server = ChiaServer(9000, full_node, NodeType.FULL_NODE)
# res = await server.start_client(PeerInfo("127.0.0.1", 8004, bytes.fromhex("b2c5ed761a9a1d776e6bfa75f751b2fb110a62d87bec9fe4c9c904ab9532c8e3"),
# NodeType.FULL_NODE, None)
# res = await server.start_client(PeerInfo(host, port, create_node_id(NodeType.FULL_NODE)), None)

View File

@ -64,7 +64,7 @@ class ChiaServer:
if self._server is not None:
return False
self._server, aiter = await start_server_aiter(self._port, host=host, reuse_address=True)
self._server, aiter = await start_server_aiter(self._port, host=None, reuse_address=True)
if on_connect is not None:
self._on_connect_generic_callback = on_connect
@ -76,7 +76,7 @@ class ChiaServer:
# Push all aiters that come from the server, into the pipeline
asyncio.create_task(self._add_to_srwt_aiter(srwt_aiter))
log.info(f"Server started at {host}:{self._port}")
log.info(f"Server started on port {self._port}")
return True
async def start_client(self, target_node: PeerInfo,
@ -222,11 +222,12 @@ class ChiaServer:
and nothing is yielded.
"""
# Send handshake message
node_id: bytes32 = create_node_id(connection)
node_id: bytes32 = create_node_id(connection.local_host, connection.local_port, connection.local_type)
outbound_handshake = Message("handshake", Handshake(protocol_version, node_id, self._local_type))
await connection.send(outbound_handshake)
try:
await connection.send(outbound_handshake)
# Read handshake message
full_message = await connection.read_one_message()
inbound_handshake = Handshake(**full_message.data)
@ -237,7 +238,8 @@ class ChiaServer:
connection.node_id = inbound_handshake.node_id
connection.connection_type = inbound_handshake.node_type
if self.global_connections.have_connection(connection):
raise DuplicateConnection(f"Duplicate connection to {connection}")
log.warning(f"Duplicate connection to {connection}")
return
self.global_connections.add(connection)
@ -258,10 +260,10 @@ class ChiaServer:
f" established"))
# Only yield a connection if the handshake is succesful and the connection is not a duplicate.
yield connection
except (IncompatibleProtocolVersion, InvalidAck, DuplicateConnection,
InvalidHandshake, asyncio.IncompleteReadError) as e:
log.warning(f"{e}")
except (IncompatibleProtocolVersion, InvalidAck,
InvalidHandshake, asyncio.IncompleteReadError, ConnectionResetError) as e:
log.warning(f"{e}, handshake not completed. Connection not created.")
connection.close()
async def connection_to_message(self, connection: Connection) -> AsyncGenerator[
Tuple[Connection, Message], None]:

View File

@ -2,7 +2,7 @@ import sys
from hashlib import sha256
from typing import Tuple
from src.types.sized_bytes import bytes32
from src.server.connection import Connection
from src.server.connection import NodeType
def parse_host_port(api) -> Tuple[str, int]:
@ -11,6 +11,5 @@ def parse_host_port(api) -> Tuple[str, int]:
return (host, port)
def create_node_id(connection: Connection) -> bytes32:
return bytes32(sha256((str(connection.local_type) + ":" + connection.local_host + ":" +
str(connection.local_port)).encode()).digest())
def create_node_id(host: str, port: int, connection_type: NodeType) -> bytes32:
return bytes32(sha256((f"{connection_type.name}:{host}:{str(port)}").encode()).digest())