Пример #1
0
    def _get_peers_to_send(self, topic_ids: Iterable[str], msg_forwarder: ID,
                           origin: ID) -> Iterable[ID]:
        """
        Get the eligible peers to send the data to.
        :param msg_forwarder: the peer id of the peer who forwards the message to me.
        :param origin: peer id of the peer the message originate from.
        :return: a generator of the peer ids who we send data to.
        """
        send_to: MutableSet[ID] = set()
        for topic in topic_ids:
            if topic not in self.pubsub.peer_topics:
                continue

            # floodsub peers
            for peer_id_str in self.pubsub.peer_topics[topic]:
                # FIXME: `gossipsub.peers_floodsub` can be changed to `gossipsub.peers` in go.
                #   This will improve the efficiency when searching for a peer's protocol id.
                if peer_id_str in self.peers_floodsub:
                    peer_id = id_b58_decode(peer_id_str)
                    send_to.add(peer_id)

            # gossipsub peers
            # FIXME: Change `str` to `ID`
            in_topic_gossipsub_peers: List[str] = None
            # TODO: Do we need to check `topic in self.pubsub.my_topics`?
            if topic in self.mesh:
                in_topic_gossipsub_peers = self.mesh[topic]
            else:
                # TODO(robzajac): Is topic DEFINITELY supposed to be in fanout if we are not
                #   subscribed?
                # I assume there could be short periods between heartbeats where topic may not
                # be but we should check that this path gets hit appropriately

                # pylint: disable=len-as-condition
                if (topic not in self.fanout) or (len(self.fanout[topic])
                                                  == 0):
                    # If no peers in fanout, choose some peers from gossipsub peers in topic.
                    self.fanout[
                        topic] = self._get_in_topic_gossipsub_peers_from_minus(
                            topic,
                            self.degree,
                            [],
                        )
                in_topic_gossipsub_peers = self.fanout[topic]
            for peer_id_str in in_topic_gossipsub_peers:
                send_to.add(id_b58_decode(peer_id_str))
        # Excludes `msg_forwarder` and `origin`
        yield from send_to.difference([msg_forwarder, origin])
Пример #2
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_b58_decode(
        "QmQiv6sR3qHqhUVgC5qUBVWi8YzM6HknYbu4oQKVAqPCGF")
    assert peer_id_from_pubkey(pubkey) == peer_id_expected
Пример #3
0
            async def conn_handler(reader, writer):
                # Read in first message (should be peer_id of initiator) and ack
                peer_id = id_b58_decode((await reader.read(1024)).decode())

                writer.write("received peer id".encode())
                await writer.drain()

                # Upgrade reader/write to a net_stream and pass \
                # to appropriate stream handler (using multiaddr)
                raw_conn = RawConnection(multiaddr.value_for_protocol('ip4'),
                                         multiaddr.value_for_protocol('tcp'),
                                         reader, writer, False)

                # Per, https://discuss.libp2p.io/t/multistream-security/130, we first secure
                # the conn and then mux the conn
                secured_conn = await self.upgrader.upgrade_security(
                    raw_conn, peer_id, False)
                muxed_conn = self.upgrader.upgrade_connection(secured_conn, \
                    self.generic_protocol_handler, peer_id)

                # Store muxed_conn with peer id
                self.connections[peer_id] = muxed_conn

                # Call notifiers since event occurred
                for notifee in self.notifees:
                    await notifee.connected(self, muxed_conn)
Пример #4
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_b58_decode(maddr.value_for_protocol(protocols.P_P2P))
     await self.dial_peer(ip=ip, port=port, peer_id=peer_id)
Пример #5
0
def test_id_b58_decode():
    random_id_string = ''
    for _ in range(10):
        random_id_string += random.SystemRandom().choice(ALPHABETS)
    expected = ID(base58.b58decode(random_id_string))
    actual = id_b58_decode(random_id_string)

    assert actual == expected
Пример #6
0
            async def conn_handler(reader, writer):
                # Read in first message (should be peer_id of initiator) and ack
                peer_id = id_b58_decode((await reader.read(1024)).decode())

                writer.write("received peer id".encode())
                await writer.drain()

                # Upgrade reader/write to a net_stream and pass \
                # to appropriate stream handler (using multiaddr)
                raw_conn = RawConnection(multiaddr.value_for_protocol('ip4'),
                                         multiaddr.value_for_protocol('tcp'),
                                         reader, writer, False)
                muxed_conn = self.upgrader.upgrade_connection(raw_conn, \
                    self.generic_protocol_handler)

                # Store muxed_conn with peer id
                self.connections[peer_id] = muxed_conn
Пример #7
0
 def _get_peers_to_send(self, topic_ids: Iterable[str], msg_forwarder: ID,
                        origin: ID) -> Iterable[ID]:
     """
     Get the eligible peers to send the data to.
     :param msg_forwarder: peer ID of the peer who forwards the message to us.
     :param origin: peer id of the peer the message originate from.
     :return: a generator of the peer ids who we send data to.
     """
     for topic in topic_ids:
         if topic not in self.pubsub.peer_topics:
             continue
         for peer_id_str in self.pubsub.peer_topics[topic]:
             peer_id = id_b58_decode(peer_id_str)
             if peer_id in (msg_forwarder, origin):
                 continue
             # FIXME: Should change `self.pubsub.peers` to Dict[PeerID, ...]
             if str(peer_id) not in self.pubsub.peers:
                 continue
             yield peer_id