mirror of
https://github.com/varun-r-mallya/py-libp2p.git
synced 2025-12-31 20:36:24 +00:00
Use factories and fixtures in pubsub tests
Done - Add factories using factory-boy - Modify fixtures and tests to use factories - Modify tests to use fixtures and factories - Clean up
This commit is contained in:
@ -1,19 +1,11 @@
|
||||
import asyncio
|
||||
import struct
|
||||
from typing import Sequence
|
||||
|
||||
import multiaddr
|
||||
|
||||
from libp2p import new_node
|
||||
from libp2p.peer.id import ID
|
||||
from libp2p.pubsub.gossipsub import GossipSub
|
||||
from libp2p.pubsub.pb import rpc_pb2
|
||||
from libp2p.pubsub.pubsub import Pubsub
|
||||
|
||||
from tests.utils import connect
|
||||
|
||||
from .configs import LISTEN_MADDR
|
||||
|
||||
|
||||
def message_id_generator(start_val):
|
||||
"""
|
||||
@ -44,80 +36,6 @@ def make_pubsub_msg(
|
||||
)
|
||||
|
||||
|
||||
def generate_RPC_packet(origin_id, topics, msg_content, msg_id):
|
||||
"""
|
||||
Generate RPC packet to send over wire
|
||||
:param origin_id: peer id of the message origin
|
||||
:param topics: list of topics
|
||||
:param msg_content: string of content in data
|
||||
:param msg_id: seqno for the message
|
||||
"""
|
||||
packet = rpc_pb2.RPC()
|
||||
message = rpc_pb2.Message(
|
||||
from_id=origin_id.encode("utf-8"),
|
||||
seqno=msg_id,
|
||||
data=msg_content.encode("utf-8"),
|
||||
)
|
||||
|
||||
for topic in topics:
|
||||
message.topicIDs.extend([topic.encode("utf-8")])
|
||||
|
||||
packet.publish.extend([message])
|
||||
return packet
|
||||
|
||||
|
||||
async def create_libp2p_hosts(num_hosts):
|
||||
"""
|
||||
Create libp2p hosts
|
||||
:param num_hosts: number of hosts to create
|
||||
"""
|
||||
hosts = []
|
||||
tasks_create = []
|
||||
for i in range(0, num_hosts):
|
||||
# Create node
|
||||
tasks_create.append(new_node(transport_opt=[str(LISTEN_MADDR)]))
|
||||
hosts = await asyncio.gather(*tasks_create)
|
||||
|
||||
tasks_listen = []
|
||||
for node in hosts:
|
||||
# Start listener
|
||||
tasks_listen.append(node.get_network().listen(LISTEN_MADDR))
|
||||
await asyncio.gather(*tasks_listen)
|
||||
|
||||
return hosts
|
||||
|
||||
|
||||
def create_pubsub_and_gossipsub_instances(
|
||||
libp2p_hosts,
|
||||
supported_protocols,
|
||||
degree,
|
||||
degree_low,
|
||||
degree_high,
|
||||
time_to_live,
|
||||
gossip_window,
|
||||
gossip_history,
|
||||
heartbeat_interval,
|
||||
):
|
||||
pubsubs = []
|
||||
gossipsubs = []
|
||||
for node in libp2p_hosts:
|
||||
gossipsub = GossipSub(
|
||||
supported_protocols,
|
||||
degree,
|
||||
degree_low,
|
||||
degree_high,
|
||||
time_to_live,
|
||||
gossip_window,
|
||||
gossip_history,
|
||||
heartbeat_interval,
|
||||
)
|
||||
pubsub = Pubsub(node, gossipsub, node.get_id())
|
||||
pubsubs.append(pubsub)
|
||||
gossipsubs.append(gossipsub)
|
||||
|
||||
return pubsubs, gossipsubs
|
||||
|
||||
|
||||
# FIXME: There is no difference between `sparse_connect` and `dense_connect`,
|
||||
# before `connect_some` is fixed.
|
||||
|
||||
@ -130,12 +48,17 @@ async def dense_connect(hosts):
|
||||
await connect_some(hosts, 10)
|
||||
|
||||
|
||||
async def connect_all(hosts):
|
||||
for i, host in enumerate(hosts):
|
||||
for host2 in hosts[i + 1 :]:
|
||||
await connect(host, host2)
|
||||
|
||||
|
||||
# FIXME: `degree` is not used at all
|
||||
async def connect_some(hosts, degree):
|
||||
for i, host in enumerate(hosts):
|
||||
for j, host2 in enumerate(hosts):
|
||||
if i != j and i < j:
|
||||
await connect(host, host2)
|
||||
for host2 in hosts[i + 1 :]:
|
||||
await connect(host, host2)
|
||||
|
||||
# TODO: USE THE CODE BELOW
|
||||
# for i, host in enumerate(hosts):
|
||||
|
||||
Reference in New Issue
Block a user