async def _handle_status(self, stream: INetStream) -> None: # TODO: Find out when we should respond the `ResponseCode` # other than `ResponseCode.SUCCESS`. async with self.new_handshake_interaction(stream) as interaction: peer_id = interaction.peer_id peer_status = await interaction.read_request(Status) self.logger.info("Received Status from %s %s", str(peer_id), peer_status) await validate_peer_status(self.chain, peer_status) my_status = get_my_status(self.chain) await interaction.write_response(my_status) self._add_peer_from_status(peer_id, peer_status) if peer_is_ahead(self.chain, peer_status): logger.debug( "Peer's chain is ahead of us, start syncing with the peer(%s)", str(peer_id), ) await self._event_bus.broadcast(SyncRequest())
async def request_status(self, peer_id: ID) -> None: self.logger.info("Initiate handshake with %s", str(peer_id)) try: stream = await self.new_stream(peer_id, REQ_RESP_STATUS_SSZ) except StreamFailure as error: self.logger.debug("Fail to open stream to %s", str(peer_id)) raise HandshakeFailure from error async with self.new_handshake_interaction(stream) as interaction: my_status = get_my_status(self.chain) await interaction.write_request(my_status) peer_status = await interaction.read_response(Status) await validate_peer_status(self.chain, peer_status) self._add_peer_from_status(peer_id, peer_status) if peer_is_ahead(self.chain, peer_status): logger.debug( "Peer's chain is ahead of us, start syncing with the peer(%s)", str(peer_id), ) await self._event_bus.broadcast(SyncRequest())