コード例 #1
0
ファイル: node.py プロジェクト: pamir-s/trinity
    async def dial_peer_maddr_with_retries(self, maddr: Multiaddr) -> None:
        """
        Dial the peer with given multi-address repeatedly for `DIAL_RETRY_COUNT` times
        """
        try:
            p2p_id = maddr.value_for_protocol(protocols.P_P2P)
        except (BinaryParseError, ProtocolLookupError) as error:
            self.logger.debug("Invalid maddr: %s, error: %s", maddr, error)
            raise DialPeerError from error
        peer_id = ID.from_base58(p2p_id)

        for i in range(DIAL_RETRY_COUNT):
            try:
                # exponential backoff...
                await asyncio.sleep(2**i + random.random())
                await self.dial_peer_maddr(maddr, peer_id)
                return
            except DialPeerError:
                self.logger.debug(
                    "Could not dial peer: %s, maddr: %s retrying attempt %d of %d...",
                    peer_id,
                    maddr,
                    i,
                    DIAL_RETRY_COUNT,
                )
                continue
        raise DialPeerError
コード例 #2
0
 async def dial_peer_maddr(self, maddr: Multiaddr) -> None:
     """
     Parse `maddr`, get the ip:port and PeerID, and call `dial_peer` with the parameters.
     """
     ip = maddr.value_for_protocol(protocols.P_IP4)
     port = maddr.value_for_protocol(protocols.P_TCP)
     peer_id = ID.from_base58(maddr.value_for_protocol(protocols.P_P2P))
     await self.dial_peer(ip=ip, port=port, peer_id=peer_id)
コード例 #3
0
ファイル: test_peerid.py プロジェクト: zaibon/py-libp2p
def test_id_from_base58():
    random_id_string = ""
    for _ in range(10):
        random_id_string += random.choice(ALPHABETS)
    expected = ID(base58.b58decode(random_id_string))
    actual = ID.from_base58(random_id_string.encode())

    assert actual == expected
コード例 #4
0
ファイル: node.py プロジェクト: wschwab/trinity
 async def dial_peer_maddr(self, maddr: Multiaddr) -> None:
     """
     Parse `maddr`, get the ip:port and PeerID, and call `dial_peer` with the parameters.
     """
     try:
         ip = maddr.value_for_protocol(protocols.P_IP4)
         port = maddr.value_for_protocol(protocols.P_TCP)
         peer_id = ID.from_base58(maddr.value_for_protocol(protocols.P_P2P))
         await self.dial_peer_with_retries(ip=ip, port=port, peer_id=peer_id)
     except Exception:
         traceback.print_exc()
         raise
コード例 #5
0
 async def add_peer_from_maddr(self, maddr: Multiaddr) -> None:
     """
     Connect to the eth2 peer at ``maddr`` and incorporate them into
     our view of the network.
     """
     peer_id_encoded = maddr.value_for_protocol("p2p")
     peer_id = PeerID.from_base58(peer_id_encoded)
     try:
         await self.connect(PeerInfo(peer_id=peer_id, addrs=[maddr]))
         local_status = self._status_provider()
         remote_status = await self.exchange_status(peer_id, local_status)
         if self._peer_is_compatible(local_status, remote_status):
             self._peers.add(peer_id)
             await self._peer_updater(peer_id, remote_status)
         else:
             await self.disconnect(peer_id)
     except Exception as e:
         self.logger.exception(e)
コード例 #6
0
def peer_info_from_str(string: str) -> PeerInfo:
    peer_id, raw_addrs = json.loads(string)
    return PeerInfo(ID.from_base58(peer_id),
                    list(map(lambda a: multiaddr.Multiaddr(a), raw_addrs)))
コード例 #7
0
def test_peer_id_from_pubkey():
    pubkey = datatypes.PublicKey(
        b"n\x85UD\xe9^\xbfo\x05\xd1z\xbd\xe5k\x87Y\xe9\xfa\xb3z:\xf8z\xc5\xd7K\xa6\x00\xbbc\xda4M\x10\x1cO\x88\tl\x82\x7f\xd7\xec6\xd8\xdc\xe2\x9c\xdcG\xa5\xea|\x9e\xc57\xf8G\xbe}\xfa\x10\xe9\x12"  # noqa: E501
    )
    peer_id_expected = ID.from_base58("QmQiv6sR3qHqhUVgC5qUBVWi8YzM6HknYbu4oQKVAqPCGF")
    assert peer_id_from_pubkey(pubkey) == peer_id_expected