Skip to content

Commit

Permalink
Do not use node_id to identify outbound connections
Browse files Browse the repository at this point in the history
Previously the target node's node_id had to be known before connecting
to it. This is inconvenient and not scalable.

Remove node_id from PeerInfo, use PeerInfo as a whole instead of node_id
to identify outbound connections in server.py.
  • Loading branch information
rostislav authored and mariano54 committed Nov 8, 2019
1 parent 8297aae commit ea7a836
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 18 deletions.
9 changes: 5 additions & 4 deletions src/server/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ChiaServer:
_outbound_aiter: push_aiter

# These will get called after a handshake is performed
_on_connect_callbacks: Dict[bytes32, Callable] = {}
_on_connect_callbacks: Dict[PeerInfo, Callable] = {}
_on_connect_generic_callback: Optional[Callable] = None

def __init__(self, port: int, api: Any, local_type: NodeType):
Expand Down Expand Up @@ -101,7 +101,7 @@ async def start_client(self, target_node: PeerInfo,
if not succeeded:
return False
if on_connect is not None:
self._on_connect_callbacks[target_node.node_id] = on_connect
self._on_connect_callbacks[target_node] = on_connect
asyncio.create_task(self._add_to_srwt_aiter(iter_to_aiter([(reader, writer)])))
return True

Expand Down Expand Up @@ -207,8 +207,9 @@ async def connection_to_outbound(self, connection: Connection) -> AsyncGenerator
"""
Async generator which calls the on_connect async generator method, and yields any outbound messages.
"""
if connection.node_id in self._on_connect_callbacks:
on_connect = self._on_connect_callbacks[connection.node_id]
peer = PeerInfo(connection.peer_host, connection.peer_port)
if peer in self._on_connect_callbacks:
on_connect = self._on_connect_callbacks[peer]
async for outbound_message in on_connect():
yield connection, outbound_message
if self._on_connect_generic_callback:
Expand Down
6 changes: 2 additions & 4 deletions src/server/start_farmer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@
async def main():
farmer = Farmer()
plotter_peer = PeerInfo(farmer.config['plotter_peer']['host'],
farmer.config['plotter_peer']['port'],
bytes.fromhex(farmer.config['plotter_peer']['node_id']))
farmer.config['plotter_peer']['port'])
full_node_peer = PeerInfo(farmer.config['full_node_peer']['host'],
farmer.config['full_node_peer']['port'],
bytes.fromhex(farmer.config['full_node_peer']['node_id']))
farmer.config['full_node_peer']['port'])
host, port = parse_host_port(farmer)
server = ChiaServer(port, farmer, NodeType.FARMER)

Expand Down
8 changes: 3 additions & 5 deletions src/server/start_full_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def master_close_cb():
peer_tasks = []
for peer in full_node.config['initial_peers']:
if not (host == peer['host'] and port == peer['port']):
peer_tasks.append(server.start_client(PeerInfo(peer['host'], peer['port'], bytes.fromhex(peer['node_id'])),
peer_tasks.append(server.start_client(PeerInfo(peer['host'], peer['port']),
full_node.on_connect))
await asyncio.gather(*peer_tasks)

Expand All @@ -70,14 +70,12 @@ def master_close_cb():

if connect_to_farmer:
peer_info = PeerInfo(full_node.config['farmer_peer']['host'],
full_node.config['farmer_peer']['port'],
bytes.fromhex(full_node.config['farmer_peer']['node_id']))
full_node.config['farmer_peer']['port'])
_ = await server.start_client(peer_info, None)

if connect_to_timelord:
peer_info = PeerInfo(full_node.config['timelord_peer']['host'],
full_node.config['timelord_peer']['port'],
bytes.fromhex(full_node.config['timelord_peer']['node_id']))
full_node.config['timelord_peer']['port'])
_ = await server.start_client(peer_info, None)

await server.await_closed()
Expand Down
3 changes: 1 addition & 2 deletions src/server/start_plotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ async def main():
_ = await server.start_server(host, None)

peer_info = PeerInfo(plotter.config['farmer_peer']['host'],
plotter.config['farmer_peer']['port'],
bytes.fromhex(plotter.config['farmer_peer']['node_id']))
plotter.config['farmer_peer']['port'])

_ = await server.start_client(peer_info, None)

Expand Down
3 changes: 1 addition & 2 deletions src/server/start_timelord.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ async def main():
_ = await server.start_server(host, None)

full_node_peer = PeerInfo(timelord.config['full_node_peer']['host'],
timelord.config['full_node_peer']['port'],
bytes.fromhex(timelord.config['full_node_peer']['node_id']))
timelord.config['full_node_peer']['port'])

await server.start_client(full_node_peer, None)

Expand Down
1 change: 0 additions & 1 deletion src/types/peer_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,3 @@
class PeerInfo(Streamable):
host: str
port: uint32
node_id: bytes32

0 comments on commit ea7a836

Please sign in to comment.