2019-08-29 21:38:06 +08:00
|
|
|
from typing import Dict
|
|
|
|
|
2019-08-01 00:09:09 +08:00
|
|
|
import factory
|
2019-08-16 07:26:59 +08:00
|
|
|
|
2019-08-29 21:38:06 +08:00
|
|
|
from libp2p import generate_new_rsa_identity, initialize_default_swarm
|
|
|
|
from libp2p.crypto.keys import KeyPair
|
2019-08-01 00:09:09 +08:00
|
|
|
from libp2p.host.basic_host import BasicHost
|
|
|
|
from libp2p.pubsub.floodsub import FloodSub
|
|
|
|
from libp2p.pubsub.gossipsub import GossipSub
|
|
|
|
from libp2p.pubsub.pubsub import Pubsub
|
2019-08-29 21:38:06 +08:00
|
|
|
from libp2p.security.base_transport import BaseSecureTransport
|
|
|
|
from libp2p.security.insecure.transport import PLAINTEXT_PROTOCOL_ID, InsecureTransport
|
2019-08-31 22:38:46 +08:00
|
|
|
import libp2p.security.secio.transport as secio
|
2019-08-29 21:38:06 +08:00
|
|
|
from libp2p.typing import TProtocol
|
2019-08-16 07:40:31 +08:00
|
|
|
from tests.pubsub.configs import (
|
2019-08-03 09:36:58 +08:00
|
|
|
FLOODSUB_PROTOCOL_ID,
|
|
|
|
GOSSIPSUB_PARAMS,
|
|
|
|
GOSSIPSUB_PROTOCOL_ID,
|
|
|
|
)
|
2019-08-01 00:09:09 +08:00
|
|
|
|
|
|
|
|
2019-08-29 21:38:06 +08:00
|
|
|
def security_transport_factory(
|
|
|
|
is_secure: bool, key_pair: KeyPair
|
|
|
|
) -> Dict[TProtocol, BaseSecureTransport]:
|
|
|
|
if not is_secure:
|
2019-08-31 22:38:46 +08:00
|
|
|
return {PLAINTEXT_PROTOCOL_ID: InsecureTransport(key_pair)}
|
2019-08-29 21:38:06 +08:00
|
|
|
else:
|
2019-08-31 22:38:46 +08:00
|
|
|
return {secio.ID: secio.Transport(key_pair)}
|
2019-08-29 21:38:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
def swarm_factory(is_secure: bool):
|
|
|
|
key_pair = generate_new_rsa_identity()
|
|
|
|
sec_opt = security_transport_factory(is_secure, key_pair)
|
|
|
|
return initialize_default_swarm(key_pair, sec_opt=sec_opt)
|
2019-08-01 00:09:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class HostFactory(factory.Factory):
|
|
|
|
class Meta:
|
|
|
|
model = BasicHost
|
|
|
|
|
2019-08-29 21:38:06 +08:00
|
|
|
class Params:
|
|
|
|
is_secure = False
|
|
|
|
|
|
|
|
network = factory.LazyAttribute(lambda o: swarm_factory(o.is_secure))
|
2019-08-01 00:09:09 +08:00
|
|
|
|
|
|
|
|
|
|
|
class FloodsubFactory(factory.Factory):
|
|
|
|
class Meta:
|
|
|
|
model = FloodSub
|
|
|
|
|
|
|
|
protocols = (FLOODSUB_PROTOCOL_ID,)
|
|
|
|
|
|
|
|
|
|
|
|
class GossipsubFactory(factory.Factory):
|
|
|
|
class Meta:
|
|
|
|
model = GossipSub
|
|
|
|
|
|
|
|
protocols = (GOSSIPSUB_PROTOCOL_ID,)
|
|
|
|
degree = GOSSIPSUB_PARAMS.degree
|
|
|
|
degree_low = GOSSIPSUB_PARAMS.degree_low
|
|
|
|
degree_high = GOSSIPSUB_PARAMS.degree_high
|
|
|
|
time_to_live = GOSSIPSUB_PARAMS.time_to_live
|
|
|
|
gossip_window = GOSSIPSUB_PARAMS.gossip_window
|
2019-08-01 21:38:14 +08:00
|
|
|
gossip_history = GOSSIPSUB_PARAMS.gossip_history
|
2019-08-01 00:09:09 +08:00
|
|
|
heartbeat_interval = GOSSIPSUB_PARAMS.heartbeat_interval
|
|
|
|
|
|
|
|
|
|
|
|
class PubsubFactory(factory.Factory):
|
|
|
|
class Meta:
|
|
|
|
model = Pubsub
|
|
|
|
|
|
|
|
host = factory.SubFactory(HostFactory)
|
|
|
|
router = None
|
|
|
|
my_id = factory.LazyAttribute(lambda obj: obj.host.get_id())
|
|
|
|
cache_size = None
|