2019-03-24 01:52:02 +08:00
|
|
|
import asyncio
|
|
|
|
import multiaddr
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from libp2p import new_node
|
2019-07-25 16:58:00 +08:00
|
|
|
from libp2p.peer.id import ID
|
2019-03-24 01:52:02 +08:00
|
|
|
from libp2p.pubsub.floodsub import FloodSub
|
2019-07-27 11:49:03 +08:00
|
|
|
from libp2p.pubsub.pubsub import Pubsub
|
2019-07-25 14:08:16 +08:00
|
|
|
|
2019-07-25 23:11:27 +08:00
|
|
|
from tests.utils import (
|
|
|
|
cleanup,
|
|
|
|
connect,
|
|
|
|
)
|
2019-07-27 11:49:03 +08:00
|
|
|
|
|
|
|
from .configs import (
|
|
|
|
FLOODSUB_PROTOCOL_ID,
|
|
|
|
LISTEN_MADDR,
|
|
|
|
)
|
|
|
|
from .floodsub_integration_test_settings import (
|
|
|
|
perform_test_from_obj,
|
|
|
|
floodsub_protocol_pytest_params,
|
2019-07-25 14:08:16 +08:00
|
|
|
)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-25 16:58:00 +08:00
|
|
|
|
2019-07-25 23:11:27 +08:00
|
|
|
SUPPORTED_PROTOCOLS = [FLOODSUB_PROTOCOL_ID]
|
2019-07-27 11:49:03 +08:00
|
|
|
# pylint: disable=too-many-locals
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-25 14:08:16 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
@pytest.mark.asyncio
|
2019-04-06 09:46:18 +08:00
|
|
|
async def test_simple_two_nodes():
|
2019-07-25 23:11:27 +08:00
|
|
|
node_a = await new_node(transport_opt=[str(LISTEN_MADDR)])
|
|
|
|
node_b = await new_node(transport_opt=[str(LISTEN_MADDR)])
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-25 23:11:27 +08:00
|
|
|
await node_a.get_network().listen(LISTEN_MADDR)
|
|
|
|
await node_b.get_network().listen(LISTEN_MADDR)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-25 23:11:27 +08:00
|
|
|
supported_protocols = [FLOODSUB_PROTOCOL_ID]
|
2019-07-25 14:08:16 +08:00
|
|
|
topic = "my_topic"
|
|
|
|
data = b"some data"
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
floodsub_a = FloodSub(supported_protocols)
|
2019-07-25 16:58:00 +08:00
|
|
|
pubsub_a = Pubsub(node_a, floodsub_a, ID(b"a" * 32))
|
2019-03-24 01:52:02 +08:00
|
|
|
floodsub_b = FloodSub(supported_protocols)
|
2019-07-25 16:58:00 +08:00
|
|
|
pubsub_b = Pubsub(node_b, floodsub_b, ID(b"b" * 32))
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
await connect(node_a, node_b)
|
|
|
|
await asyncio.sleep(0.25)
|
|
|
|
|
2019-07-25 16:58:00 +08:00
|
|
|
sub_b = await pubsub_b.subscribe(topic)
|
|
|
|
# Sleep to let a know of b's subscription
|
2019-03-24 01:52:02 +08:00
|
|
|
await asyncio.sleep(0.25)
|
|
|
|
|
2019-07-25 16:58:00 +08:00
|
|
|
await pubsub_a.publish(topic, data)
|
2019-03-24 01:52:02 +08:00
|
|
|
|
2019-07-25 14:08:16 +08:00
|
|
|
res_b = await sub_b.get()
|
2019-03-24 01:52:02 +08:00
|
|
|
|
|
|
|
# Check that the msg received by node_b is the same
|
|
|
|
# as the message sent by node_a
|
2019-07-25 16:58:00 +08:00
|
|
|
assert ID(res_b.from_id) == node_a.get_id()
|
|
|
|
assert res_b.data == data
|
|
|
|
assert res_b.topicIDs == [topic]
|
2019-04-02 04:23:20 +08:00
|
|
|
|
2019-03-24 01:52:02 +08:00
|
|
|
# Success, terminate pending tasks.
|
|
|
|
await cleanup()
|
|
|
|
|
2019-07-25 14:08:16 +08:00
|
|
|
|
2019-04-06 09:46:18 +08:00
|
|
|
@pytest.mark.asyncio
|
2019-07-25 16:58:00 +08:00
|
|
|
async def test_lru_cache_two_nodes(monkeypatch):
|
2019-04-06 09:46:18 +08:00
|
|
|
# two nodes with cache_size of 4
|
2019-07-25 16:58:00 +08:00
|
|
|
# `node_a` send the following messages to node_b
|
|
|
|
message_indices = [1, 1, 2, 1, 3, 1, 4, 1, 5, 1]
|
|
|
|
# `node_b` should only receive the following
|
|
|
|
expected_received_indices = [1, 2, 3, 4, 5, 1]
|
2019-04-06 09:46:18 +08:00
|
|
|
|
2019-07-25 23:11:27 +08:00
|
|
|
node_a = await new_node(transport_opt=[str(LISTEN_MADDR)])
|
|
|
|
node_b = await new_node(transport_opt=[str(LISTEN_MADDR)])
|
2019-07-25 16:58:00 +08:00
|
|
|
|
2019-07-25 23:11:27 +08:00
|
|
|
await node_a.get_network().listen(LISTEN_MADDR)
|
|
|
|
await node_b.get_network().listen(LISTEN_MADDR)
|
2019-04-06 09:46:18 +08:00
|
|
|
|
2019-07-25 23:11:27 +08:00
|
|
|
supported_protocols = SUPPORTED_PROTOCOLS
|
2019-07-25 16:58:00 +08:00
|
|
|
topic = "my_topic"
|
2019-04-06 09:46:18 +08:00
|
|
|
|
2019-07-25 16:58:00 +08:00
|
|
|
# Mock `get_msg_id` to make us easier to manipulate `msg_id` by `data`.
|
|
|
|
def get_msg_id(msg):
|
|
|
|
# Originally it is `(msg.seqno, msg.from_id)`
|
|
|
|
return (msg.data, msg.from_id)
|
|
|
|
import libp2p.pubsub.pubsub
|
|
|
|
monkeypatch.setattr(libp2p.pubsub.pubsub, "get_msg_id", get_msg_id)
|
|
|
|
|
|
|
|
# Initialize Pubsub with a cache_size of 4
|
|
|
|
cache_size = 4
|
2019-04-06 09:46:18 +08:00
|
|
|
floodsub_a = FloodSub(supported_protocols)
|
2019-07-25 16:58:00 +08:00
|
|
|
pubsub_a = Pubsub(node_a, floodsub_a, ID(b"a" * 32), cache_size)
|
|
|
|
|
2019-04-06 09:46:18 +08:00
|
|
|
floodsub_b = FloodSub(supported_protocols)
|
2019-07-25 16:58:00 +08:00
|
|
|
pubsub_b = Pubsub(node_b, floodsub_b, ID(b"b" * 32), cache_size)
|
2019-04-06 09:46:18 +08:00
|
|
|
|
|
|
|
await connect(node_a, node_b)
|
|
|
|
await asyncio.sleep(0.25)
|
|
|
|
|
2019-07-25 16:58:00 +08:00
|
|
|
sub_b = await pubsub_b.subscribe(topic)
|
2019-04-06 09:46:18 +08:00
|
|
|
await asyncio.sleep(0.25)
|
|
|
|
|
2019-07-25 16:58:00 +08:00
|
|
|
def _make_testing_data(i: int) -> bytes:
|
|
|
|
num_int_bytes = 4
|
|
|
|
if i >= 2**(num_int_bytes * 8):
|
2019-07-25 23:11:27 +08:00
|
|
|
raise ValueError("integer is too large to be serialized")
|
2019-07-25 16:58:00 +08:00
|
|
|
return b"data" + i.to_bytes(num_int_bytes, "big")
|
2019-04-06 09:46:18 +08:00
|
|
|
|
2019-07-25 16:58:00 +08:00
|
|
|
for index in message_indices:
|
|
|
|
await pubsub_a.publish(topic, _make_testing_data(index))
|
2019-04-06 09:46:18 +08:00
|
|
|
await asyncio.sleep(0.25)
|
|
|
|
|
2019-07-25 16:58:00 +08:00
|
|
|
for index in expected_received_indices:
|
|
|
|
res_b = await sub_b.get()
|
|
|
|
assert res_b.data == _make_testing_data(index)
|
|
|
|
assert sub_b.empty()
|
2019-07-25 14:08:16 +08:00
|
|
|
|
2019-04-06 09:46:18 +08:00
|
|
|
# Success, terminate pending tasks.
|
|
|
|
await cleanup()
|
|
|
|
|
|
|
|
|
2019-07-27 11:49:03 +08:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"test_case_obj",
|
|
|
|
floodsub_protocol_pytest_params,
|
|
|
|
)
|
2019-04-04 02:20:50 +08:00
|
|
|
@pytest.mark.asyncio
|
2019-07-27 11:49:03 +08:00
|
|
|
async def test_gossipsub_run_with_floodsub_tests(test_case_obj):
|
|
|
|
await perform_test_from_obj(
|
|
|
|
test_case_obj,
|
|
|
|
FloodSub,
|
|
|
|
)
|