2019-07-27 18:41:16 +08:00
|
|
|
import asyncio
|
|
|
|
from typing import NamedTuple
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
from multiaddr import Multiaddr
|
|
|
|
|
|
|
|
from libp2p import new_node
|
|
|
|
|
|
|
|
from libp2p.pubsub.floodsub import FloodSub
|
|
|
|
from libp2p.pubsub.gossipsub import GossipSub
|
|
|
|
from libp2p.pubsub.pubsub import Pubsub
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
from .configs import FLOODSUB_PROTOCOL_ID, GOSSIPSUB_PROTOCOL_ID, LISTEN_MADDR
|
2019-07-27 18:41:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=redefined-outer-name
|
|
|
|
|
2019-08-01 06:00:12 +08:00
|
|
|
|
2019-07-27 18:41:16 +08:00
|
|
|
@pytest.fixture
|
|
|
|
def num_hosts():
|
|
|
|
return 3
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
async def hosts(num_hosts):
|
2019-08-01 06:00:12 +08:00
|
|
|
_hosts = await asyncio.gather(
|
|
|
|
*[new_node(transport_opt=[str(LISTEN_MADDR)]) for _ in range(num_hosts)]
|
|
|
|
)
|
|
|
|
await asyncio.gather(
|
|
|
|
*[_host.get_network().listen(LISTEN_MADDR) for _host in _hosts]
|
|
|
|
)
|
2019-07-27 18:41:16 +08:00
|
|
|
yield _hosts
|
|
|
|
# Clean up
|
2019-07-28 18:07:48 +08:00
|
|
|
listeners = []
|
2019-07-27 18:41:16 +08:00
|
|
|
for _host in _hosts:
|
|
|
|
for listener in _host.get_network().listeners.values():
|
|
|
|
listener.server.close()
|
2019-07-28 18:07:48 +08:00
|
|
|
listeners.append(listener)
|
2019-08-01 06:00:12 +08:00
|
|
|
await asyncio.gather(*[listener.server.wait_closed() for listener in listeners])
|
2019-07-27 18:41:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def floodsubs(num_hosts):
|
2019-08-01 06:00:12 +08:00
|
|
|
return tuple(FloodSub(protocols=[FLOODSUB_PROTOCOL_ID]) for _ in range(num_hosts))
|
2019-07-27 18:41:16 +08:00
|
|
|
|
|
|
|
|
|
|
|
class GossipsubParams(NamedTuple):
|
|
|
|
degree: int = 10
|
|
|
|
degree_low: int = 9
|
|
|
|
degree_high: int = 11
|
|
|
|
fanout_ttl: int = 30
|
|
|
|
gossip_window: int = 3
|
|
|
|
gossip_history: int = 5
|
|
|
|
heartbeat_interval: float = 0.5
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def gossipsub_params():
|
|
|
|
return GossipsubParams()
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def gossipsubs(num_hosts, gossipsub_params):
|
|
|
|
yield tuple(
|
2019-08-01 06:00:12 +08:00
|
|
|
GossipSub(protocols=[GOSSIPSUB_PROTOCOL_ID], **gossipsub_params._asdict())
|
2019-07-27 18:41:16 +08:00
|
|
|
for _ in range(num_hosts)
|
|
|
|
)
|
|
|
|
# TODO: Clean up
|
|
|
|
|
|
|
|
|
|
|
|
def _make_pubsubs(hosts, pubsub_routers):
|
|
|
|
if len(pubsub_routers) != len(hosts):
|
|
|
|
raise ValueError(
|
2019-07-31 16:23:07 +08:00
|
|
|
f"lenght of pubsub_routers={pubsub_routers} should be equaled to the "
|
|
|
|
f"length of hosts={len(hosts)}"
|
2019-07-27 18:41:16 +08:00
|
|
|
)
|
|
|
|
return tuple(
|
2019-08-01 06:00:12 +08:00
|
|
|
Pubsub(host=host, router=router, my_id=host.get_id())
|
2019-07-27 18:41:16 +08:00
|
|
|
for host, router in zip(hosts, pubsub_routers)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def pubsubs_fsub(hosts, floodsubs):
|
2019-07-31 16:07:04 +08:00
|
|
|
_pubsubs_fsub = _make_pubsubs(hosts, floodsubs)
|
2019-07-31 16:07:48 +08:00
|
|
|
yield _pubsubs_fsub
|
2019-07-27 18:41:16 +08:00
|
|
|
# TODO: Clean up
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
def pubsubs_gsub(hosts, gossipsubs):
|
|
|
|
_pubsubs_gsub = _make_pubsubs(hosts, gossipsubs)
|
|
|
|
yield _pubsubs_gsub
|
|
|
|
# TODO: Clean up
|