Esempio n. 1
0
    async def connect(self, remote: kademlia.Node) -> LESPeer:
        """
        Connect to the given remote and return a Peer instance when successful.

        Returns None if the remote is unreachable, times out or is useless.
        """
        if remote in self.connected_nodes:
            self.logger.debug("Skipping %s; already connected to it", remote)
            return None
        try:
            peer = await asyncio.wait_for(
                handshake(remote, self.privkey, self.peer_class, self.chaindb,
                          self.network_id), HANDSHAKE_TIMEOUT)
            return cast(LESPeer, peer)
        except (UnreachablePeer, asyncio.TimeoutError,
                PeerConnectionLost) as e:
            self.logger.debug("Could not complete handshake with %s: %s",
                              remote, e)
        except UselessPeer:
            self.logger.debug("No matching capabilities with %s", remote)
        except PeerDisconnected as e:
            self.logger.debug(
                "%s disconnected before completing handshake; reason: %s",
                remote, e)
        except Exception:
            self.logger.warn(
                "Unexpected error during auth/p2p handhsake with %s: %s",
                remote, traceback.format_exc())
        return None
Esempio n. 2
0
 async def connect(self, remote: kademlia.Node) -> LESPeer:
     """
     Connect to the given remote and return a Peer instance when successful.
     Returns None if the remote is unreachable, times out or is useless.
     """
     if remote in self.connected_nodes:
         self.logger.debug("Skipping %s; already connected to it", remote)
         return None
     expected_exceptions = (UnreachablePeer, asyncio.TimeoutError,
                            PeerConnectionLost, UselessPeer,
                            PeerDisconnected)
     try:
         self.logger.info("Connecting to %s...", remote)
         peer = await asyncio.wait_for(
             handshake(remote, self.privkey, self.peer_class, self.chaindb,
                       self.network_id, self.msg_handler),
             HANDSHAKE_TIMEOUT)
         return cast(LESPeer, peer)
     except expected_exceptions as e:
         self.logger.info("Could not complete handshake with %s: %s",
                          remote, repr(e))
     except Exception:
         self.logger.warn(
             "Unexpected error during auth/p2p handhsake with %s: %s",
             remote, traceback.format_exc())
     return None