Ejemplo n.º 1
0
async def packer(enr_db, private_key, enr, incoming_packet_channels,
                 incoming_message_channels, outgoing_message_channels,
                 outgoing_packet_channels):
    packer = Packer(
        local_private_key=private_key,
        local_node_id=enr.node_id,
        enr_db=enr_db,
        message_type_registry=default_message_type_registry,
        incoming_packet_receive_channel=incoming_packet_channels[1],
        incoming_message_send_channel=incoming_message_channels[0],
        outgoing_message_receive_channel=outgoing_message_channels[1],
        outgoing_packet_send_channel=outgoing_packet_channels[0],
    )
    async with background_service(packer):
        yield packer
Ejemplo n.º 2
0
async def remote_packer(enr_db, remote_private_key, remote_enr,
                        remote_incoming_packet_channels,
                        remote_incoming_message_channels,
                        remote_outgoing_message_channels,
                        remote_outgoing_packet_channels, bridged_channels):
    remote_packer = Packer(
        local_private_key=remote_private_key,
        local_node_id=remote_enr.node_id,
        enr_db=enr_db,
        message_type_registry=default_message_type_registry,
        incoming_packet_receive_channel=remote_incoming_packet_channels[1],
        incoming_message_send_channel=remote_incoming_message_channels[0],
        outgoing_message_receive_channel=remote_outgoing_message_channels[1],
        outgoing_packet_send_channel=remote_outgoing_packet_channels[0],
    )
    async with background_service(remote_packer):
        yield packer
Ejemplo n.º 3
0
    async def do_run(self, event_bus: EndpointAPI) -> None:
        boot_info = self._boot_info
        identity_scheme_registry = default_identity_scheme_registry
        message_type_registry = default_message_type_registry

        nodedb_dir = get_nodedb_dir(boot_info)
        nodedb_dir.mkdir(exist_ok=True)
        node_db = NodeDB(default_identity_scheme_registry, LevelDB(nodedb_dir))

        local_private_key = get_local_private_key(boot_info)
        local_enr = await get_local_enr(boot_info, node_db, local_private_key)
        local_node_id = local_enr.node_id

        routing_table = KademliaRoutingTable(local_node_id,
                                             NUM_ROUTING_TABLE_BUCKETS)

        node_db.set_enr(local_enr)
        for enr_repr in boot_info.args.discovery_boot_enrs or ():
            enr = ENR.from_repr(enr_repr)
            node_db.set_enr(enr)
            routing_table.update(enr.node_id)

        port = boot_info.args.discovery_port

        socket = trio.socket.socket(family=trio.socket.AF_INET,
                                    type=trio.socket.SOCK_DGRAM)
        outgoing_datagram_channels = trio.open_memory_channel[
            OutgoingDatagram](0)
        incoming_datagram_channels = trio.open_memory_channel[
            IncomingDatagram](0)
        outgoing_packet_channels = trio.open_memory_channel[OutgoingPacket](0)
        incoming_packet_channels = trio.open_memory_channel[IncomingPacket](0)
        outgoing_message_channels = trio.open_memory_channel[OutgoingMessage](
            0)
        incoming_message_channels = trio.open_memory_channel[IncomingMessage](
            0)
        endpoint_vote_channels = trio.open_memory_channel[EndpointVote](0)

        # types ignored due to https://github.com/ethereum/async-service/issues/5
        datagram_sender = DatagramSender(  # type: ignore
            outgoing_datagram_channels[1], socket)
        datagram_receiver = DatagramReceiver(  # type: ignore
            socket, incoming_datagram_channels[0])

        packet_encoder = PacketEncoder(  # type: ignore
            outgoing_packet_channels[1], outgoing_datagram_channels[0])
        packet_decoder = PacketDecoder(  # type: ignore
            incoming_datagram_channels[1], incoming_packet_channels[0])

        packer = Packer(
            local_private_key=local_private_key.to_bytes(),
            local_node_id=local_node_id,
            node_db=node_db,
            message_type_registry=message_type_registry,
            incoming_packet_receive_channel=incoming_packet_channels[1],
            incoming_message_send_channel=incoming_message_channels[0],
            outgoing_message_receive_channel=outgoing_message_channels[1],
            outgoing_packet_send_channel=outgoing_packet_channels[0],
        )

        message_dispatcher = MessageDispatcher(
            node_db=node_db,
            incoming_message_receive_channel=incoming_message_channels[1],
            outgoing_message_send_channel=outgoing_message_channels[0],
        )

        endpoint_tracker = EndpointTracker(
            local_private_key=local_private_key.to_bytes(),
            local_node_id=local_node_id,
            node_db=node_db,
            identity_scheme_registry=identity_scheme_registry,
            vote_receive_channel=endpoint_vote_channels[1],
        )

        routing_table_manager = RoutingTableManager(
            local_node_id=local_node_id,
            routing_table=routing_table,
            message_dispatcher=message_dispatcher,
            node_db=node_db,
            outgoing_message_send_channel=outgoing_message_channels[0],
            endpoint_vote_send_channel=endpoint_vote_channels[0],
        )

        logger.info(f"Starting discovery, listening on port {port}")
        logger.info(f"Local Node ID: {encode_hex(local_enr.node_id)}")
        logger.info(f"Local ENR: {local_enr}")

        services = (
            datagram_sender,
            datagram_receiver,
            packet_encoder,
            packet_decoder,
            packer,
            message_dispatcher,
            endpoint_tracker,
            routing_table_manager,
        )
        await socket.bind(("0.0.0.0", port))
        with socket:
            async with trio.open_nursery() as nursery:
                for service in services:
                    nursery.start_soon(async_service.TrioManager.run_service,
                                       service)