async def do_run(self, event_bus: EndpointAPI) -> None: boot_info = self._boot_info config = boot_info.trinity_config db = DBClient.connect(config.database_ipc_path) if boot_info.args.disable_discovery: discovery_service: async_service.Service = StaticDiscoveryService( event_bus, config.preferred_nodes, ) else: vm_config = config.get_app_config( Eth1AppConfig).get_chain_config().vm_configuration headerdb = TrioHeaderDB(db) eth_cap_provider = functools.partial(generate_eth_cap_enr_field, vm_config, headerdb) socket = trio.socket.socket(family=trio.socket.AF_INET, type=trio.socket.SOCK_DGRAM) await socket.bind(("0.0.0.0", config.port)) base_db = LevelDB(config.node_db_dir) node_db = NodeDB(default_identity_scheme_registry, base_db) discovery_service = PreferredNodeDiscoveryService( config.nodekey, config.port, config.port, config.bootstrap_nodes, config.preferred_nodes, event_bus, socket, node_db, (eth_cap_provider, ), ) with db: await async_service.run_trio_service(discovery_service)
def __init__(self, bootnodes): privkey = eth_keys.keys.PrivateKey(keccak(b"seed")) self.messages = [] node_db = NodeDB(default_identity_scheme_registry, MemoryDB()) socket = trio.socket.socket(family=trio.socket.AF_INET, type=trio.socket.SOCK_DGRAM) event_bus = None address = AddressFactory() super().__init__( privkey, address.udp_port, address.tcp_port, bootnodes, event_bus, socket, node_db)
async def _manually_driven_discovery(seed, socket, nursery): _, port = socket.getsockname() discovery = ManuallyDrivenDiscoveryService( keys.PrivateKey(keccak(seed)), port, port, bootstrap_nodes=[], event_bus=None, socket=socket, node_db=NodeDB(default_identity_scheme_registry, MemoryDB())) async with background_trio_service(discovery): # Wait until we're fully initialized (i.e. until the ENR stub created in the constructor # is replaced with the real one). while discovery.this_node.enr.sequence_number == 0: await trio.hazmat.checkpoint() yield discovery
async def do_run(cls, boot_info: BootInfo, event_bus: EndpointAPI) -> None: config = boot_info.trinity_config db = DBClient.connect(config.database_ipc_path) if boot_info.args.disable_discovery: discovery_service: async_service.Service = StaticDiscoveryService( event_bus, config.preferred_nodes, ) else: vm_config = config.get_app_config(Eth1AppConfig).get_chain_config().vm_configuration headerdb = TrioHeaderDB(db) eth_cap_provider = functools.partial(generate_eth_cap_enr_field, vm_config, headerdb) socket = trio.socket.socket(family=trio.socket.AF_INET, type=trio.socket.SOCK_DGRAM) await socket.bind(("0.0.0.0", config.port)) base_db = LevelDB(config.node_db_dir) node_db = NodeDB(default_identity_scheme_registry, base_db) # discovery_service = PreferredNodeDiscoveryService( # config.nodekey, # config.port, # config.port, # config.bootstrap_nodes, # config.preferred_nodes, # event_bus, # socket, # node_db, # (eth_cap_provider,), # ) discovery_service = DiscoveryService( config.nodekey, config.port, config.port, config.bootstrap_nodes, event_bus, socket, node_db, (eth_cap_provider,), ) try: with db: await async_service.run_trio_service(discovery_service) except Exception: event_bus.broadcast_nowait(ShutdownRequest("Discovery ended unexpectedly")) raise
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)
async def node_db(initial_enr): node_db = NodeDB(default_identity_scheme_registry, MemoryDB()) node_db.set_enr(initial_enr) return node_db
async def node_db(enr, remote_enr): db = NodeDB(default_identity_scheme_registry, MemoryDB()) db.set_enr(enr) db.set_enr(remote_enr) return db
async def node_db(local_enr, remote_enr): node_db = NodeDB(default_identity_scheme_registry, MemoryDB()) node_db.set_enr(local_enr) node_db.set_enr(remote_enr) return node_db
def test_checks_identity_scheme(): db = NodeDB(IdentitySchemeRegistry(), MemoryDB()) enr = ENRFactory() with pytest.raises(ValueError): db.set_enr(enr)
def node_db(): return NodeDB(default_identity_scheme_registry, MemoryDB())