def do_start(self) -> None: trinity_config = self.context.trinity_config beacon_config = trinity_config.get_app_config(BeaconAppConfig) db_manager = create_db_consumer_manager(trinity_config.database_ipc_path) base_db = db_manager.get_db() # type: ignore chain_db = db_manager.get_chaindb() # type: ignore chain_config = beacon_config.get_chain_config() chain = chain_config.beacon_chain_class(base_db) if self.context.args.beacon_nodekey: from eth_keys.datatypes import PrivateKey privkey = PrivateKey(bytes.fromhex(self.context.args.beacon_nodekey)) else: privkey = ecies.generate_privkey() server = BCCServer( privkey=privkey, port=self.context.args.port, chain=chain, chaindb=chain_db, headerdb=None, base_db=base_db, network_id=trinity_config.network_id, max_peers=DEFAULT_MAX_PEERS, bootstrap_nodes=None, preferred_nodes=None, event_bus=self.context.event_bus, token=None, ) syncer = BeaconChainSyncer( chain_db, server.peer_pool, server.cancel_token, ) loop = asyncio.get_event_loop() asyncio.ensure_future(exit_with_service_and_endpoint(server, self.context.event_bus)) asyncio.ensure_future(server.run()) asyncio.ensure_future(syncer.run()) loop.run_forever() loop.close()
async def get_sync_setup(request, event_loop, event_bus, alice_chain_db, bob_chain_db, genesis_config=SERENITY_GENESIS_CONFIG): alice_context = BeaconContextFactory(chain_db=alice_chain_db) bob_context = BeaconContextFactory(chain_db=bob_chain_db) peer_pair = BCCPeerPairFactory( alice_peer_context=alice_context, bob_peer_context=bob_context, event_bus=event_bus, ) async with peer_pair as (alice, bob): async with BCCPeerPoolFactory.run_for_peer( alice) as alice_peer_pool, BCCPeerPoolFactory.run_for_peer( bob) as bob_peer_pool: # noqa: E501 bob_request_server = BCCRequestServer( event_bus, TO_NETWORKING_BROADCAST_CONFIG, bob.context.chain_db) alice_syncer = BeaconChainSyncer( alice_chain_db, alice_peer_pool, SimpleWriterBlockImporter(alice_chain_db), genesis_config, ) async with run_peer_pool_event_server( event_bus, bob_peer_pool, handler_type=BCCPeerPoolEventServer): asyncio.ensure_future(bob_request_server.run()) asyncio.ensure_future(alice_syncer.run()) def finalizer(): event_loop.run_until_complete(alice_syncer.cancel()) event_loop.run_until_complete(bob_request_server.cancel()) request.addfinalizer(finalizer) await alice_syncer.events.finished.wait() yield alice_syncer
async def get_sync_setup(request, event_loop, alice_chain_db, bob_chain_db): alice, alice_peer_pool, bob, bob_peer_pool = await get_directly_linked_peers_in_peer_pools( request, event_loop, alice_chain_db=alice_chain_db, bob_chain_db=bob_chain_db, ) bob_request_server = BCCRequestServer(bob.context.chain_db, bob_peer_pool) alice_syncer = BeaconChainSyncer(alice_chain_db, alice_peer_pool) asyncio.ensure_future(bob_request_server.run()) asyncio.ensure_future(alice_syncer.run()) def finalizer(): event_loop.run_until_complete(alice_syncer.cancel()) event_loop.run_until_complete(bob_request_server.cancel()) request.addfinalizer(finalizer) return alice_syncer
async def get_sync_setup(request, event_loop, event_bus, alice_chain_db, bob_chain_db, genesis_config=SERENITY_GENESIS_CONFIG): alice, alice_peer_pool, bob, bob_peer_pool = await get_directly_linked_peers_in_peer_pools( request, event_loop, alice_chain_db=alice_chain_db, bob_chain_db=bob_chain_db, bob_peer_pool_event_bus=event_bus, ) bob_request_server = BCCRequestServer(event_bus, TO_NETWORKING_BROADCAST_CONFIG, bob.context.chain_db) alice_syncer = BeaconChainSyncer( alice_chain_db, alice_peer_pool, SimpleWriterBlockImporter(alice_chain_db), genesis_config, ) async with run_peer_pool_event_server(event_bus, bob_peer_pool, handler_type=BCCPeerPoolEventServer): asyncio.ensure_future(bob_request_server.run()) asyncio.ensure_future(alice_syncer.run()) def finalizer(): event_loop.run_until_complete(alice_syncer.cancel()) event_loop.run_until_complete(bob_request_server.cancel()) request.addfinalizer(finalizer) await alice_syncer.events.finished.wait() yield alice_syncer
def do_start(self) -> None: trinity_config = self.boot_info.trinity_config key_pair = self._load_or_create_node_key() beacon_app_config = trinity_config.get_app_config(BeaconAppConfig) base_db = DBClient.connect(trinity_config.database_ipc_path) chain_config = beacon_app_config.get_chain_config() attestation_pool = AttestationPool() chain = chain_config.beacon_chain_class( base_db, attestation_pool, chain_config.genesis_config ) # TODO: Handle `bootstrap_nodes`. libp2p_node = Node( key_pair=key_pair, listen_ip="127.0.0.1", # FIXME: Should be configurable listen_port=self.boot_info.args.port, preferred_nodes=trinity_config.preferred_nodes, chain=chain, ) receive_server = BCCReceiveServer( chain=chain, p2p_node=libp2p_node, topic_msg_queues=libp2p_node.pubsub.my_topics, cancel_token=libp2p_node.cancel_token, ) state = chain.get_state_by_slot(chain_config.genesis_config.GENESIS_SLOT) registry_pubkeys = [v_record.pubkey for v_record in state.validators] validator_privkeys = {} validator_keymap = chain_config.genesis_data.validator_keymap for pubkey in validator_keymap: try: validator_index = cast(ValidatorIndex, registry_pubkeys.index(pubkey)) except ValueError: self.logger.error(f'Could not find pubkey {pubkey.hex()} in genesis state') raise validator_privkeys[validator_index] = validator_keymap[pubkey] validator = Validator( chain=chain, p2p_node=libp2p_node, validator_privkeys=validator_privkeys, event_bus=self.event_bus, token=libp2p_node.cancel_token, get_ready_attestations_fn=receive_server.get_ready_attestations, ) slot_ticker = SlotTicker( genesis_slot=chain_config.genesis_config.GENESIS_SLOT, genesis_time=chain_config.genesis_data.genesis_time, seconds_per_slot=chain_config.genesis_config.SECONDS_PER_SLOT, event_bus=self.event_bus, token=libp2p_node.cancel_token, ) syncer = BeaconChainSyncer( chain_db=AsyncBeaconChainDB( base_db, chain_config.genesis_config, ), peer_pool=libp2p_node.handshaked_peers, block_importer=SyncBlockImporter(chain), genesis_config=chain_config.genesis_config, token=libp2p_node.cancel_token, ) asyncio.ensure_future(exit_with_services( self._event_bus_service, libp2p_node, receive_server, slot_ticker, validator, syncer, )) asyncio.ensure_future(libp2p_node.run()) asyncio.ensure_future(receive_server.run()) asyncio.ensure_future(slot_ticker.run()) asyncio.ensure_future(validator.run()) asyncio.ensure_future(syncer.run())
def do_start(self) -> None: trinity_config = self.boot_info.trinity_config beacon_app_config = trinity_config.get_app_config(BeaconAppConfig) db_manager = create_db_consumer_manager( trinity_config.database_ipc_path) base_db = db_manager.get_db() # type: ignore chain_db = db_manager.get_chaindb() # type: ignore chain_config = beacon_app_config.get_chain_config() chain = chain_config.beacon_chain_class(base_db, chain_config.eth2_config) if self.boot_info.args.beacon_nodekey: privkey = PrivateKey( bytes.fromhex(self.boot_info.args.beacon_nodekey)) else: privkey = ecies.generate_privkey() server = BCCServer( privkey=privkey, port=self.boot_info.args.port, chain=chain, chaindb=chain_db, headerdb=None, base_db=base_db, network_id=trinity_config.network_id, max_peers=DEFAULT_MAX_PEERS, bootstrap_nodes=None, preferred_nodes=None, event_bus=self.event_bus, token=None, ) syncer = BeaconChainSyncer( chain_db, server.peer_pool, SyncBlockImporter(chain), server.cancel_token, ) state_machine = chain.get_state_machine() state = state_machine.state registry_pubkeys = [ v_record.pubkey for v_record in state.validator_registry ] validator_privkeys = {} validator_keymap = chain_config.genesis_data.validator_keymap for pubkey in validator_keymap: validator_index = cast(ValidatorIndex, registry_pubkeys.index(pubkey)) validator_privkeys[validator_index] = validator_keymap[pubkey] validator = Validator( chain=chain, peer_pool=server.peer_pool, validator_privkeys=validator_privkeys, event_bus=self.event_bus, token=server.cancel_token, genesis_epoch=state_machine.config.GENESIS_EPOCH, slots_per_epoch=state_machine.config.SLOTS_PER_EPOCH, ) slot_ticker = SlotTicker( genesis_slot=state_machine.config.GENESIS_SLOT, genesis_time=chain_config.genesis_data.genesis_time, seconds_per_slot=state_machine.config.SECONDS_PER_SLOT, event_bus=self.event_bus, token=server.cancel_token, ) asyncio.ensure_future( exit_with_endpoint_and_services(self.event_bus, server)) asyncio.ensure_future(server.run()) asyncio.ensure_future(syncer.run()) asyncio.ensure_future(slot_ticker.run()) asyncio.ensure_future(validator.run())
async def do_run(cls, boot_info: BootInfo, event_bus: EndpointAPI) -> None: trinity_config = boot_info.trinity_config key_pair = cls._load_or_create_node_key(boot_info) beacon_app_config = trinity_config.get_app_config(BeaconAppConfig) base_db = DBClient.connect(trinity_config.database_ipc_path) if boot_info.args.debug_libp2p: logging.getLogger("libp2p").setLevel(logging.DEBUG) else: logging.getLogger("libp2p").setLevel(logging.INFO) with base_db: chain_config = beacon_app_config.get_chain_config() chain = chain_config.beacon_chain_class( base_db, chain_config.genesis_config) # TODO: To simplify, subsribe all subnets subnets: Set[SubnetId] = set( SubnetId(subnet_id) for subnet_id in range(ATTESTATION_SUBNET_COUNT)) # TODO: Handle `bootstrap_nodes`. libp2p_node = Node( key_pair=key_pair, listen_ip="0.0.0.0", listen_port=boot_info.args.port, preferred_nodes=trinity_config.preferred_nodes, chain=chain, subnets=subnets, event_bus=event_bus, ) receive_server = BCCReceiveServer( chain=chain, p2p_node=libp2p_node, topic_msg_queues=libp2p_node.pubsub.my_topics, subnets=subnets, cancel_token=libp2p_node.cancel_token, ) state = chain.get_state_by_slot( chain_config.genesis_config.GENESIS_SLOT) registry_pubkeys = [ v_record.pubkey for v_record in state.validators ] validator_privkeys = {} validator_keymap = chain_config.genesis_data.validator_keymap for pubkey in validator_keymap: try: validator_index = cast(ValidatorIndex, registry_pubkeys.index(pubkey)) except ValueError: cls.logger.error( f'Could not find pubkey {pubkey.hex()} in genesis state' ) raise validator_privkeys[validator_index] = validator_keymap[pubkey] validator = Validator( chain=chain, p2p_node=libp2p_node, validator_privkeys=validator_privkeys, event_bus=event_bus, token=libp2p_node.cancel_token, get_ready_attestations_fn=receive_server. get_ready_attestations, get_aggregatable_attestations_fn=receive_server. get_aggregatable_attestations, import_attestation_fn=receive_server.import_attestation, ) slot_ticker = SlotTicker( genesis_slot=chain_config.genesis_config.GENESIS_SLOT, genesis_time=chain_config.genesis_data.genesis_time, seconds_per_slot=chain_config.genesis_config.SECONDS_PER_SLOT, event_bus=event_bus, token=libp2p_node.cancel_token, ) syncer = BeaconChainSyncer( chain_db=AsyncBeaconChainDB( base_db, chain_config.genesis_config, ), peer_pool=libp2p_node.handshaked_peers, block_importer=SyncBlockImporter(chain), genesis_config=chain_config.genesis_config, event_bus=event_bus, token=libp2p_node.cancel_token, ) metrics_server = HTTPServer( handler=MetricsHandler.handle(chain)(event_bus), port=boot_info.args.metrics_port, ) api_server = HTTPServer( handler=APIHandler.handle(chain)(event_bus), port=boot_info.args.api_port, ) services: Tuple[BaseService, ...] = (libp2p_node, receive_server, slot_ticker, validator, syncer) if boot_info.args.enable_metrics: services += (metrics_server, ) if boot_info.args.enable_api: services += (api_server, ) async with AsyncExitStack() as stack: for service in services: await stack.enter_async_context(run_service(service)) await asyncio.gather(*(service.cancellation() for service in services))
async def do_run(cls, boot_info: BootInfo, event_bus: EndpointAPI) -> None: trinity_config = boot_info.trinity_config key_pair = _load_secp256k1_key_pair_from(trinity_config) beacon_app_config = trinity_config.get_app_config(BeaconAppConfig) base_db = DBClient.connect(trinity_config.database_ipc_path) if boot_info.args.debug_libp2p: logging.getLogger("libp2p").setLevel(logging.DEBUG) else: logging.getLogger("libp2p").setLevel(logging.INFO) with base_db: chain_config = beacon_app_config.get_chain_config() chain = chain_config.beacon_chain_class( base_db, chain_config.genesis_config) # TODO: To simplify, subsribe all subnets subnets: Set[SubnetId] = set( SubnetId(subnet_id) for subnet_id in range(ATTESTATION_SUBNET_COUNT)) # TODO: Handle `bootstrap_nodes`. libp2p_node = Node( key_pair=key_pair, listen_ip="0.0.0.0", listen_port=boot_info.args.port, preferred_nodes=trinity_config.preferred_nodes, chain=chain, subnets=subnets, event_bus=event_bus, ) receive_server = BCCReceiveServer( chain=chain, p2p_node=libp2p_node, topic_msg_queues=libp2p_node.pubsub.my_topics, subnets=subnets, cancel_token=libp2p_node.cancel_token, ) chain_maintainer = ChainMaintainer(chain=chain, event_bus=event_bus, token=libp2p_node.cancel_token) validator_handler = ValidatorHandler( chain=chain, p2p_node=libp2p_node, event_bus=event_bus, get_ready_attestations_fn=receive_server. get_ready_attestations, get_aggregatable_attestations_fn=receive_server. get_aggregatable_attestations, import_attestation_fn=receive_server.import_attestation, token=libp2p_node.cancel_token, ) slot_ticker = SlotTicker( genesis_slot=chain_config.genesis_config.GENESIS_SLOT, genesis_time=chain_config.genesis_time, seconds_per_slot=chain_config.genesis_config.SECONDS_PER_SLOT, event_bus=event_bus, token=libp2p_node.cancel_token, ) syncer = BeaconChainSyncer( chain_db=AsyncBeaconChainDB(base_db, chain_config.genesis_config), peer_pool=libp2p_node.handshaked_peers, block_importer=SyncBlockImporter(chain), genesis_config=chain_config.genesis_config, event_bus=event_bus, token=libp2p_node.cancel_token, ) metrics_server = HTTPServer( handler=MetricsHandler.handle(chain)(event_bus), port=boot_info.args.metrics_port, ) # NOTE: this API server provides an interface into the beacon node api_server = HTTPServer( handler=APIHandler.handle(chain)(event_bus), port=boot_info.args.api_port, ) # NOTE: this API server provides an interface between the beacon node and # any connected validator clients. validator_api_handler = ValidatorAPIHandler( chain, event_bus, chain_config.genesis_time) validator_api_server = HTTPAppServer( routes=validator_api_handler.make_routes(), port=30303) services: Tuple[BaseService, ...] = ( libp2p_node, receive_server, slot_ticker, syncer, validator_api_server, ) if boot_info.args.enable_metrics: services += (metrics_server, ) if boot_info.args.enable_api: services += (api_server, ) if boot_info.args.bn_only: services += (chain_maintainer, validator_handler) async with AsyncExitStack() as stack: for service in services: await stack.enter_async_context(run_service(service)) await asyncio.gather(*(service.cancellation() for service in services))
async def do_run(cls, boot_info: BootInfo, event_bus: EndpointAPI) -> None: trinity_config = boot_info.trinity_config key_pair = cls._load_or_create_node_key(boot_info) beacon_app_config = trinity_config.get_app_config(BeaconAppConfig) base_db = DBClient.connect(trinity_config.database_ipc_path) chain_config = beacon_app_config.get_chain_config() chain = chain_config.beacon_chain_class(base_db, chain_config.genesis_config) # TODO: Handle `bootstrap_nodes`. libp2p_node = Node( key_pair=key_pair, listen_ip="0.0.0.0", listen_port=boot_info.args.port, preferred_nodes=trinity_config.preferred_nodes, chain=chain, ) receive_server = BCCReceiveServer( chain=chain, p2p_node=libp2p_node, topic_msg_queues=libp2p_node.pubsub.my_topics, cancel_token=libp2p_node.cancel_token, ) state = chain.get_state_by_slot( chain_config.genesis_config.GENESIS_SLOT) registry_pubkeys = [v_record.pubkey for v_record in state.validators] validator_privkeys = {} validator_keymap = chain_config.genesis_data.validator_keymap for pubkey in validator_keymap: try: validator_index = cast(ValidatorIndex, registry_pubkeys.index(pubkey)) except ValueError: cls.logger.error( f'Could not find pubkey {pubkey.hex()} in genesis state') raise validator_privkeys[validator_index] = validator_keymap[pubkey] validator = Validator( chain=chain, p2p_node=libp2p_node, validator_privkeys=validator_privkeys, event_bus=event_bus, token=libp2p_node.cancel_token, get_ready_attestations_fn=receive_server.get_ready_attestations, ) slot_ticker = SlotTicker( genesis_slot=chain_config.genesis_config.GENESIS_SLOT, genesis_time=chain_config.genesis_data.genesis_time, seconds_per_slot=chain_config.genesis_config.SECONDS_PER_SLOT, event_bus=event_bus, token=libp2p_node.cancel_token, ) syncer = BeaconChainSyncer( chain_db=AsyncBeaconChainDB( base_db, chain_config.genesis_config, ), peer_pool=libp2p_node.handshaked_peers, block_importer=SyncBlockImporter(chain), genesis_config=chain_config.genesis_config, token=libp2p_node.cancel_token, ) services = (libp2p_node, receive_server, slot_ticker, validator, syncer) async with AsyncExitStack() as stack: for service in services: await stack.enter_async_context(run_service(service)) await asyncio.gather(*(service.cancellation() for service in services))