Esempio n. 1
0
def test_eq_true():
    random_id_string = ""
    for _ in range(10):
        random_id_string += random.choice(ALPHABETS)
    peer_id = ID(random_id_string.encode())

    assert peer_id == base58.b58encode(random_id_string).decode()
    assert peer_id == random_id_string.encode()
    assert peer_id == ID(random_id_string.encode())
Esempio n. 2
0
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
Esempio n. 3
0
def test_id_to_base58():
    random_id_string = ""
    for _ in range(10):
        random_id_string += random.choice(ALPHABETS)
    expected = base58.b58encode(random_id_string).decode()
    actual = ID(random_id_string.encode()).to_base58()

    assert actual == expected
Esempio n. 4
0
def test_str_more_than_10():
    random_id_string = ""
    for _ in range(10):
        random_id_string += random.choice(ALPHABETS)
    peer_id = base58.b58encode(random_id_string).decode()
    expected = peer_id
    actual = ID(random_id_string.encode()).__str__()

    assert actual == expected
Esempio n. 5
0
def test_pretty():
    random_id_string = ""
    for _ in range(10):
        random_id_string += random.choice(ALPHABETS)
    peer_id = ID(random_id_string.encode())
    actual = peer_id.pretty()
    expected = base58.b58encode(random_id_string).decode()

    assert actual == expected
Esempio n. 6
0
def test_init_():
    random_addrs = [random.randint(0, 255) for r in range(4)]
    random_id_string = ""
    for _ in range(10):
        random_id_string += random.SystemRandom().choice(ALPHABETS)
    peer_id = ID(random_id_string.encode())
    peer_info = PeerInfo(peer_id, random_addrs)

    assert peer_info.peer_id == peer_id
    assert peer_info.addrs == random_addrs
Esempio n. 7
0
def test_id_from_public_key():
    key_pair = create_new_key_pair()
    public_key = key_pair.public_key

    key_bin = public_key.serialize()
    algo = multihash.Func.sha2_256
    mh_digest = multihash.digest(key_bin, algo)
    expected = ID(mh_digest.encode())

    actual = ID.from_pubkey(public_key)

    assert actual == expected
Esempio n. 8
0
 async def get_closest_peers(self, key: bytes) -> Tuple[ID, ...]:
     """GET_CLOSEST_PEERS
     """
     dht_req = p2pd_pb.DHTRequest(type=p2pd_pb.DHTRequest.GET_CLOSEST_PEERS,
                                  key=key)
     resps = await self._do_dht(dht_req)
     try:
         peer_ids = tuple(ID(dht_resp.value) for dht_resp in resps)
     except AttributeError as e:
         raise ControlFailure(
             f"dht_resp should contains `value`: resps={resps}, e={e}")
     return peer_ids
Esempio n. 9
0
    async def list_peers(self, topic: str) -> Tuple[ID, ...]:
        """PUBSUB LIST_PEERS
        """
        pubsub_req = p2pd_pb.PSRequest(type=p2pd_pb.PSRequest.LIST_PEERS,
                                       topic=topic)
        req = p2pd_pb.Request(type=p2pd_pb.Request.PUBSUB, pubsub=pubsub_req)
        stream = await self.daemon_connector.open_connection()
        await write_pbmsg(stream, req)
        resp = p2pd_pb.Response()
        await read_pbmsg_safe(stream, resp)
        await stream.close()
        raise_if_failed(resp)

        return tuple(
            ID(peer_id_bytes) for peer_id_bytes in resp.pubsub.peerIDs)
Esempio n. 10
0
    async def identify(self) -> Tuple[ID, Tuple[Multiaddr, ...]]:
        stream = await self.daemon_connector.open_connection()
        req = p2pd_pb.Request(type=p2pd_pb.Request.IDENTIFY)
        await write_pbmsg(stream, req)

        resp = p2pd_pb.Response()
        await read_pbmsg_safe(stream, resp)
        await stream.close()
        raise_if_failed(resp)
        peer_id_bytes = resp.identify.id
        maddrs_bytes = resp.identify.addrs

        maddrs = tuple(Multiaddr(maddr_bytes) for maddr_bytes in maddrs_bytes)
        peer_id = ID(peer_id_bytes)

        return peer_id, maddrs
 def from_pb(cls, peer_info_pb: p2pd_pb2.PeerInfo) -> PeerInfoLibP2P:
     peer_id = ID(peer_info_pb.id)
     addrs = [Multiaddr(addr) for addr in peer_info_pb.addrs]
     return PeerInfoLibP2P(peer_id, addrs)
 def from_pb(cls, pb_msg: p2pd_pb2.StreamInfo) -> "StreamInfo":
     stream_info = cls(peer_id=ID(pb_msg.peer),
                       addr=Multiaddr(pb_msg.addr),
                       proto=pb_msg.proto)
     return stream_info
async def test_client_pubsub_subscribe(p2pcs):
    peer_id_0, _ = await p2pcs[0].identify()
    peer_id_1, _ = await p2pcs[1].identify()
    await connect_safe(p2pcs[0], p2pcs[1])
    await connect_safe(p2pcs[1], p2pcs[2])
    topic = "topic123"
    data = b"data"
    stream_0 = await p2pcs[0].pubsub_subscribe(topic)
    stream_1 = await p2pcs[1].pubsub_subscribe(topic)
    # test case: `get_topics` after subscriptions
    assert topic in await p2pcs[0].pubsub_get_topics()
    assert topic in await p2pcs[1].pubsub_get_topics()
    # wait for mesh built
    await anyio.sleep(2)
    # test case: `list_topic_peers` after subscriptions
    assert peer_id_0 in await p2pcs[1].pubsub_list_peers(topic)
    assert peer_id_1 in await p2pcs[0].pubsub_list_peers(topic)
    # test case: publish, and both clients receive data
    await p2pcs[0].pubsub_publish(topic, data)
    pubsub_msg_0 = p2pd_pb.PSMessage()
    await read_pbmsg_safe(stream_0, pubsub_msg_0)
    assert pubsub_msg_0.data == data
    pubsub_msg_1 = p2pd_pb.PSMessage()
    await read_pbmsg_safe(stream_1, pubsub_msg_1)
    assert pubsub_msg_1.data == data
    # test case: publish more data
    another_data_0 = b"another_data_0"
    another_data_1 = b"another_data_1"
    await p2pcs[0].pubsub_publish(topic, another_data_0)
    await p2pcs[0].pubsub_publish(topic, another_data_1)
    pubsub_msg_1_0 = p2pd_pb.PSMessage()
    await read_pbmsg_safe(stream_1, pubsub_msg_1_0)
    pubsub_msg_1_1 = p2pd_pb.PSMessage()
    await read_pbmsg_safe(stream_1, pubsub_msg_1_1)
    assert set([pubsub_msg_1_0.data,
                pubsub_msg_1_1.data]) == set([another_data_0, another_data_1])
    # test case: subscribe to multiple topics
    another_topic = "topic456"
    await p2pcs[0].pubsub_subscribe(another_topic)
    stream_1_another = await p2pcs[1].pubsub_subscribe(another_topic)
    await p2pcs[0].pubsub_publish(another_topic, another_data_0)
    pubsub_msg_1_another = p2pd_pb.PSMessage()
    await read_pbmsg_safe(stream_1_another, pubsub_msg_1_another)
    assert pubsub_msg_1_another.data == another_data_0
    # test case: test `from`
    assert ID(getattr(pubsub_msg_1_1, "from")) == peer_id_0
    # test case: test `from`, when it is sent through 1 hop(p2pcs[1])
    stream_2 = await p2pcs[2].pubsub_subscribe(topic)
    another_data_2 = b"another_data_2"
    await p2pcs[0].pubsub_publish(topic, another_data_2)
    pubsub_msg_2_0 = p2pd_pb.PSMessage()
    await read_pbmsg_safe(stream_2, pubsub_msg_2_0)
    assert ID(getattr(pubsub_msg_2_0, "from")) == peer_id_0
    # test case: unsubscribe by closing the stream
    await stream_0.close()
    await anyio.sleep(0)
    assert topic not in await p2pcs[0].pubsub_get_topics()

    async def is_peer_removed_from_topic():
        return peer_id_0 not in await p2pcs[1].pubsub_list_peers(topic)

    await try_until_success(is_peer_removed_from_topic)
Esempio n. 14
0
def test_eq_false():
    peer_id = ID("efgh")
    other = ID("abcd")

    assert peer_id != other
Esempio n. 15
0
def test_eq_impl_for_bytes():
    random_id_string = ""
    for _ in range(10):
        random_id_string += random.choice(ALPHABETS)
    peer_id = ID(random_id_string.encode())
    assert peer_id == random_id_string.encode()
def peer_id(peer_id_bytes):
    return ID(peer_id_bytes)