예제 #1
0
async def connect_swarm(swarm_0: Swarm, swarm_1: Swarm) -> None:
    peer_id = swarm_1.get_peer_id()
    addrs = tuple(addr for transport in swarm_1.listeners.values()
                  for addr in transport.get_addrs())
    swarm_0.peerstore.add_addrs(peer_id, addrs, 10000)
    await swarm_0.dial_peer(peer_id)
    assert swarm_0.get_peer_id() in swarm_1.connections
    assert swarm_1.get_peer_id() in swarm_0.connections
예제 #2
0
    def __init__(
        self,
        key_pair: KeyPair,
        peer_id: PeerID,
        peer_updater: PeerUpdater,
        status_provider: StatusProvider,
        finalized_root_provider: Callable[[Epoch], Root],
        block_provider_by_slot: BlockProviderBySlot,
        block_provider_by_root: BlockProviderByRoot,
        metadata_provider: MetadataProvider,
        fork_digest_provider: ForkDigestProvider,
        eth2_config: Eth2Config,
    ) -> None:
        peer_store = PeerStore()
        peer_store.add_key_pair(peer_id, key_pair)

        muxer_transports_by_protocol = {MPLEX_PROTOCOL_ID: Mplex}
        noise_key = ed25519.create_new_key_pair()
        security_transports_by_protocol = {
            TProtocol(secio.ID):
            secio.Transport(key_pair),
            TProtocol(noise.PROTOCOL_ID):
            noise.Transport(key_pair, noise_key.private_key)
        }
        upgrader = TransportUpgrader(security_transports_by_protocol,
                                     muxer_transports_by_protocol)

        transport = TCP()

        swarm = Swarm(peer_id, peer_store, upgrader, transport)
        BasicHost.__init__(self, swarm)

        self._peer_updater = peer_updater
        self._status_provider = status_provider
        self._finalized_root_provider_by_epoch = finalized_root_provider
        self._block_provider_by_slot = block_provider_by_slot
        self._block_provider_by_root = block_provider_by_root
        self._metadata_provider = metadata_provider

        self._request_responder = RequestResponder(
            self._peer_updater,
            self._status_provider,
            self._block_provider_by_slot,
            self._block_provider_by_root,
            self._metadata_provider,
        )
        self._install_req_resp_protocols()

        self._gossiper = Gossiper(fork_digest_provider, self)

        self._peers: Set[PeerID] = set()
예제 #3
0
def initialize_default_swarm(
    key_pair: KeyPair,
    id_opt: ID = None,
    transport_opt: Sequence[str] = None,
    muxer_opt: TMuxerOptions = None,
    sec_opt: TSecurityOptions = None,
    peerstore_opt: IPeerStore = None,
) -> Swarm:
    """
    initialize swarm when no swarm is passed in.

    :param id_opt: optional id for host
    :param transport_opt: optional choice of transport upgrade
    :param muxer_opt: optional choice of stream muxer
    :param sec_opt: optional choice of security upgrade
    :param peerstore_opt: optional peerstore
    :return: return a default swarm instance
    """

    if not id_opt:
        id_opt = generate_peer_id_from(key_pair)

    # TODO: Parse `transport_opt` to determine transport
    transport = TCP()

    muxer_transports_by_protocol = muxer_opt or {MPLEX_PROTOCOL_ID: Mplex}
    security_transports_by_protocol = sec_opt or {
        TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair),
        TProtocol(secio.ID): secio.Transport(key_pair),
    }
    upgrader = TransportUpgrader(security_transports_by_protocol,
                                 muxer_transports_by_protocol)

    peerstore = peerstore_opt or PeerStore()
    # Store our key pair in peerstore
    peerstore.add_key_pair(id_opt, key_pair)

    # TODO: Initialize discovery if not presented
    return Swarm(id_opt, peerstore, upgrader, transport)
예제 #4
0
def new_swarm(
    key_pair: KeyPair = None,
    muxer_opt: TMuxerOptions = None,
    sec_opt: TSecurityOptions = None,
    peerstore_opt: IPeerStore = None,
) -> INetworkService:
    """
    Create a swarm instance based on the parameters.

    :param key_pair: optional choice of the ``KeyPair``
    :param muxer_opt: optional choice of stream muxer
    :param sec_opt: optional choice of security upgrade
    :param peerstore_opt: optional peerstore
    :return: return a default swarm instance
    """

    if key_pair is None:
        key_pair = generate_new_rsa_identity()

    id_opt = generate_peer_id_from(key_pair)

    # TODO: Parse `listen_addrs` to determine transport
    transport = TCP()

    muxer_transports_by_protocol = muxer_opt or {MPLEX_PROTOCOL_ID: Mplex}
    security_transports_by_protocol = sec_opt or {
        TProtocol(PLAINTEXT_PROTOCOL_ID): InsecureTransport(key_pair),
        TProtocol(secio.ID): secio.Transport(key_pair),
    }
    upgrader = TransportUpgrader(security_transports_by_protocol,
                                 muxer_transports_by_protocol)

    peerstore = peerstore_opt or PeerStore()
    # Store our key pair in peerstore
    peerstore.add_key_pair(id_opt, key_pair)

    return Swarm(id_opt, peerstore, upgrader, transport)