Esempio n. 1
0
    async def maybe_connect_more_peers(self) -> None:
        while self.is_operational:
            await self.sleep(DISOVERY_INTERVAL)

            available_peer_slots = self.max_peers - len(self)
            if available_peer_slots > 0:
                try:
                    response = await self.wait(
                        # TODO: This should use a BroadcastConfig to send the request to discovery
                        # only as soon as we have cut a new Lahja release.
                        self.event_bus.request(
                            PeerCandidatesRequest(available_peer_slots)),
                        timeout=REQUEST_PEER_CANDIDATE_TIMEOUT)
                except TimeoutError:
                    self.logger.warning(
                        "Discovery did not answer PeerCandidateRequest in time"
                    )
                    continue

                # In some cases (e.g ROPSTEN or private testnets), the discovery table might be
                # full of bad peers so if we can't connect to any peers we try a random bootstrap
                # node as well.
                if not len(self):
                    try:
                        response = await self.wait(
                            # TODO: This should use a BroadcastConfig to send the request to
                            # discovery only as soon as we have cut a new Lahja release.
                            self.event_bus.request(RandomBootnodeRequest()),
                            timeout=REQUEST_PEER_CANDIDATE_TIMEOUT)
                    except TimeoutError:
                        self.logger.warning(
                            "Discovery did not answer RandomBootnodeRequest in time"
                        )
                        continue

                self.logger.debug2("Received candidates to connect to (%s)",
                                   response.candidates)
                await self.connect_to_nodes(from_uris(response.candidates))
Esempio n. 2
0
 async def accept_connect_commands(self) -> None:
     async for command in self.wait_iter(
             self.event_bus.stream(ConnectToNodeCommand)):
         self.logger.debug('Received request to connect to %s',
                           command.node)
         self.run_task(self.connect_to_nodes(from_uris([command.node])))