Ejemplo n.º 1
0
 async def get_peer_candidates(
         self, max_candidates: int,
         should_skip_fn: Callable[[NodeAPI], bool]) -> Tuple[NodeAPI, ...]:
     await self.event_bus.wait_until_any_endpoint_subscribed_to(
         RandomBootnodeRequest)
     response = await self.event_bus.request(RandomBootnodeRequest(),
                                             TO_DISCOVERY_BROADCAST_CONFIG)
     return response.candidates
Ejemplo n.º 2
0
    async def get_peer_candidates(self, num_requested: int,
                                  num_connected_peers: int) -> Iterable[Node]:
        if num_connected_peers == 0:
            response = await self.event_bus.request(
                RandomBootnodeRequest(), TO_DISCOVERY_BROADCAST_CONFIG)

            return response.candidates
        else:
            return ()
Ejemplo n.º 3
0
    async def get_peer_candidates(
            self, num_requested: int,
            connected_remotes: Set[NodeAPI]) -> Tuple[NodeAPI, ...]:
        if len(connected_remotes) == 0:
            await self.event_bus.wait_until_any_endpoint_subscribed_to(
                RandomBootnodeRequest)
            response = await self.event_bus.request(
                RandomBootnodeRequest(), TO_DISCOVERY_BROADCAST_CONFIG)

            return tuple(candidate for candidate in response.candidates
                         if candidate not in connected_remotes)
        else:
            return ()
Ejemplo n.º 4
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))