async def do_run(self, event_bus: EndpointAPI) -> None: boot_info = self._boot_info trinity_config = boot_info.trinity_config with chain_for_config(trinity_config, event_bus) as chain: if trinity_config.has_app_config(Eth1AppConfig): modules = initialize_eth1_modules(chain, event_bus, trinity_config) elif trinity_config.has_app_config(BeaconAppConfig): modules = initialize_beacon_modules(chain, event_bus) else: raise Exception("Unsupported Node Type") rpc = RPCServer(modules, chain, event_bus) # Run IPC Server ipc_server = IPCServer(rpc, boot_info.trinity_config.jsonrpc_ipc_path) services_to_exit: Tuple[Service, ...] = (ipc_server, ) # Run HTTP Server if boot_info.args.enable_http: http_server = HTTPServer( handler=RPCHandler.handle(rpc.execute), port=boot_info.args.rpcport, ) services_to_exit += (http_server, ) async with contextlib.AsyncExitStack() as stack: managers = tuple([ await stack.enter_async_context( background_asyncio_service(service)) for service in services_to_exit ]) await managers[0].wait_finished()
def start(self) -> None: self.logger.info('JSON-RPC Server started') self.context.event_bus.connect() db_manager = create_db_manager( self.context.chain_config.database_ipc_path) db_manager.connect() chain_class = self.context.chain_config.node_class.chain_class if self.context.chain_config.sync_mode == SYNC_LIGHT: header_db = db_manager.get_headerdb() # type: ignore event_bus_light_peer_chain = EventBusLightPeerChain( self.context.event_bus) chain = chain_class(header_db, peer_chain=event_bus_light_peer_chain) else: db = db_manager.get_db() # type: ignore chain = chain_class(db) rpc = RPCServer(chain, self.context.event_bus) ipc_server = IPCServer(rpc, self.context.chain_config.jsonrpc_ipc_path) loop = asyncio.get_event_loop() asyncio.ensure_future( exit_on_signal(ipc_server, self.context.event_bus)) asyncio.ensure_future(ipc_server.run()) loop.run_forever() loop.close()
def do_start(self) -> None: trinity_config = self.boot_info.trinity_config chain = self.chain_for_config(trinity_config) if trinity_config.has_app_config(Eth1AppConfig): modules = initialize_eth1_modules(chain, self.event_bus) elif trinity_config.has_app_config(BeaconAppConfig): modules = initialize_beacon_modules(chain, self.event_bus) else: raise Exception("Unsupported Node Type") rpc = RPCServer(modules, chain, self.event_bus) # Run IPC Server ipc_server = IPCServer(rpc, self.boot_info.trinity_config.jsonrpc_ipc_path) asyncio.ensure_future(ipc_server.run()) services_to_exit: Tuple[BaseService, ...] = ( ipc_server, self._event_bus_service, ) # Run HTTP Server if self.boot_info.args.enable_http: http_server = HTTPServer(rpc, port=self.boot_info.args.rpcport) asyncio.ensure_future(http_server.run()) services_to_exit += (http_server, ) asyncio.ensure_future(exit_with_services(*services_to_exit))
def do_start(self) -> None: db_manager = create_db_manager( self.context.trinity_config.database_ipc_path) db_manager.connect() trinity_config = self.context.trinity_config eth1_app_config = self.context.trinity_config.get_app_config( Eth1AppConfig) chain_config = trinity_config.get_chain_config() chain: BaseAsyncChain if eth1_app_config.is_light_mode: header_db = db_manager.get_headerdb() # type: ignore event_bus_light_peer_chain = EventBusLightPeerChain( self.context.event_bus) chain = chain_config.light_chain_class( header_db, peer_chain=event_bus_light_peer_chain) elif eth1_app_config.is_full_mode: db = db_manager.get_db() # type: ignore chain = chain_config.full_chain_class(db) else: raise NotImplementedError( f"Unsupported mode: {trinity_config.sync_mode}") rpc = RPCServer(chain, self.context.event_bus) ipc_server = IPCServer(rpc, self.context.trinity_config.jsonrpc_ipc_path) loop = asyncio.get_event_loop() asyncio.ensure_future( exit_with_service_and_endpoint(ipc_server, self.context.event_bus)) asyncio.ensure_future(ipc_server.run()) loop.run_forever() loop.close()
async def do_run(self, event_bus: EndpointAPI) -> None: boot_info = self._boot_info trinity_config = boot_info.trinity_config with chain_for_config(trinity_config, event_bus) as chain: if trinity_config.has_app_config(Eth1AppConfig): modules = initialize_eth1_modules(chain, event_bus, trinity_config) elif trinity_config.has_app_config(BeaconAppConfig): modules = initialize_beacon_modules(chain, event_bus) else: raise Exception("Unsupported Node Type") rpc = RPCServer(modules, chain, event_bus) # Run IPC Server ipc_server = IPCServer(rpc, boot_info.trinity_config.jsonrpc_ipc_path) services_to_exit: Tuple[Service, ...] = (ipc_server, ) # Run HTTP Server if boot_info.args.enable_http: http_server = HTTPServer( host=boot_info.args.http_listen_address, handler=RPCHandler.handle(rpc.execute), port=boot_info.args.http_port, ) services_to_exit += (http_server, ) await run_background_asyncio_services(services_to_exit)
async def do_run(cls, boot_info: BootInfo, event_bus: EndpointAPI) -> None: trinity_config = boot_info.trinity_config chain = chain_for_config(trinity_config, event_bus) if trinity_config.has_app_config(Eth1AppConfig): modules = initialize_eth1_modules(chain, event_bus) elif trinity_config.has_app_config(BeaconAppConfig): modules = initialize_beacon_modules(chain, event_bus) else: raise Exception("Unsupported Node Type") rpc = RPCServer(modules, chain, event_bus) # Run IPC Server ipc_server = IPCServer(rpc, boot_info.trinity_config.jsonrpc_ipc_path) services_to_exit: Tuple[BaseService, ...] = (ipc_server, ) # Run HTTP Server if boot_info.args.enable_http: http_server = HTTPServer(rpc, port=boot_info.args.rpcport) services_to_exit += (http_server, ) async with AsyncExitStack() as stack: for service in services_to_exit: await stack.enter_async_context(run_service(service)) await ipc_server.cancellation()
async def test_json_rpc_http_server(aiohttp_raw_server, aiohttp_client, event_bus, base_db, ipc_path): manager = DBManager(base_db) with manager.run(ipc_path): # Set chaindb override_lengths(SERENITY_CONFIG) db = DBClient.connect(ipc_path) genesis_config = SERENITY_CONFIG chaindb = AsyncBeaconChainDB(db, genesis_config) fork_choice_scoring = HigherSlotScoring() genesis_state, genesis_block = create_mock_genesis( pubkeys=(), config=SERENITY_CONFIG, keymap=dict(), genesis_block_class=BeaconBlock, genesis_time=0, ) chaindb.persist_state(genesis_state) chaindb.persist_block( SignedBeaconBlock.create(message=genesis_block), SignedBeaconBlock, fork_choice_scoring, ) try: rpc = RPCServer(initialize_beacon_modules(chaindb, event_bus), chaindb, event_bus) raw_server = await aiohttp_raw_server( RPCHandler.handle(rpc.execute)) client = await aiohttp_client(raw_server) request_id = 1 request_data = { "jsonrpc": "2.0", "method": "beacon_head", "params": [], "id": request_id, } response = await client.post("/", json=request_data) response_data = await response.json() assert response_data["id"] == request_id result = response_data["result"] assert result["slot"] == 0 assert decode_hex( result["block_root"]) == genesis_block.hash_tree_root assert decode_hex( result["state_root"]) == genesis_state.hash_tree_root except KeyboardInterrupt: pass finally: await raw_server.close() db.close()
def ipc_server(jsonrpc_ipc_pipe_path, event_loop, chain_with_block_validation): ''' This fixture runs a single RPC server over IPC over the course of all tests. It never needs to be actually used as a fixture, so it doesn't return (yield) a value. ''' rpc = RPCServer(chain_with_block_validation) ipc_server = IPCServer(rpc, jsonrpc_ipc_pipe_path) asyncio.ensure_future(ipc_server.run(loop=event_loop), loop=event_loop) try: yield finally: event_loop.run_until_complete(ipc_server.stop())
async def ipc_server(monkeypatch, event_bus, jsonrpc_ipc_pipe_path, event_loop, chain_with_block_validation): """ This fixture runs a single RPC server over IPC over the course of all tests. It yields the IPC server only for monkeypatching purposes """ rpc = RPCServer( initialize_eth1_modules(chain_with_block_validation, event_bus), chain_with_block_validation, event_bus, ) ipc_server = IPCServer(rpc, jsonrpc_ipc_pipe_path) async with background_asyncio_service(ipc_server): yield ipc_server
async def ipc_server(monkeypatch, event_bus, jsonrpc_ipc_pipe_path, event_loop, chain_with_block_validation): ''' This fixture runs a single RPC server over IPC over the course of all tests. It yields the IPC server only for monkeypatching purposes ''' rpc = RPCServer(chain_with_block_validation, event_bus) ipc_server = IPCServer(rpc, jsonrpc_ipc_pipe_path, loop=event_loop) asyncio.ensure_future(ipc_server.run(), loop=event_loop) try: yield ipc_server finally: await ipc_server.cancel()
def do_start(self) -> None: trinity_config = self.boot_info.trinity_config if trinity_config.has_app_config(Eth1AppConfig): modules = self.setup_eth1_modules(trinity_config) elif trinity_config.has_app_config(BeaconAppConfig): modules = self.setup_beacon_modules() else: raise Exception("Unsupported Node Type") rpc = RPCServer(modules, self.event_bus) ipc_server = IPCServer(rpc, self.boot_info.trinity_config.jsonrpc_ipc_path) asyncio.ensure_future(exit_with_endpoint_and_services(self.event_bus, ipc_server)) asyncio.ensure_future(ipc_server.run())
def start(path, chain=None, loop=None): ''' :returns: initialized server :rtype: asyncio.Server ''' if loop is None: loop = asyncio.get_event_loop() rpc = RPCServer(chain) server = loop.run_until_complete( asyncio.start_unix_server( connection_handler(rpc.execute), path, loop=loop, limit=MAXIMUM_REQUEST_BYTES, )) return server
def run_lightnode_process(chain_config: ChainConfig) -> None: logger = logging.getLogger('trinity') logger.info(TRINITY_HEADER) logger.info(construct_trinity_client_identifier()) logger.info( "enode://%s@%s:%s", chain_config.nodekey.to_hex()[2:], "[:]", chain_config.port, ) logger.info('network: %s', chain_config.network_id) manager = create_dbmanager(chain_config.database_ipc_path) headerdb = manager.get_headerdb() # type: ignore if chain_config.network_id == MAINNET_NETWORK_ID: chain_class = MainnetLightPeerChain # type: ignore elif chain_config.network_id == ROPSTEN_NETWORK_ID: chain_class = RopstenLightPeerChain # type: ignore else: raise NotImplementedError( "Only the mainnet and ropsten chains are currently supported" ) discovery = None peer_pool = HardCodedNodesPeerPool( LESPeer, headerdb, chain_config.network_id, chain_config.nodekey, discovery) chain = chain_class(headerdb, peer_pool) loop = asyncio.get_event_loop() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, chain.cancel_token.trigger) rpc = RPCServer(chain) ipc_server = IPCServer(rpc, chain_config.jsonrpc_ipc_path) async def run_chain(chain): try: asyncio.ensure_future(chain.peer_pool.run()) asyncio.ensure_future(ipc_server.run()) await chain.run() finally: await ipc_server.stop() await chain.peer_pool.cancel() await chain.stop() loop.run_until_complete(run_chain(chain)) loop.close()
async def ipc_server(monkeypatch, p2p_server, jsonrpc_ipc_pipe_path, event_loop, chain_with_block_validation): ''' This fixture runs a single RPC server over IPC over the course of all tests. It never needs to be actually used as a fixture, so it doesn't return (yield) a value. ''' rpc = RPCServer(chain_with_block_validation, p2p_server.peer_pool) ipc_server = IPCServer(rpc, jsonrpc_ipc_pipe_path, loop=event_loop) asyncio.ensure_future(ipc_server.run(), loop=event_loop) try: yield finally: await ipc_server.cancel()
def do_start(self) -> None: trinity_config = self.context.trinity_config if trinity_config.has_app_config(Eth1AppConfig): modules = self.setup_eth1_modules(trinity_config) elif trinity_config.has_app_config(BeaconAppConfig): modules = self.setup_beacon_modules() else: raise Exception("Unsupported Node Type") rpc = RPCServer(modules, self.context.event_bus) ipc_server = IPCServer(rpc, self.context.trinity_config.jsonrpc_ipc_path) loop = asyncio.get_event_loop() asyncio.ensure_future(exit_with_service_and_endpoint(ipc_server, self.context.event_bus)) asyncio.ensure_future(ipc_server.run()) loop.run_forever() loop.close()
def do_start(self) -> None: trinity_config = self.boot_info.trinity_config chain = self.chain_for_config(trinity_config) if trinity_config.has_app_config(Eth1AppConfig): modules = initialize_eth1_modules(chain, self.event_bus) elif trinity_config.has_app_config(BeaconAppConfig): modules = initialize_beacon_modules(chain, self.event_bus) else: raise Exception("Unsupported Node Type") rpc = RPCServer(modules, chain, self.event_bus) ipc_server = IPCServer(rpc, self.boot_info.trinity_config.jsonrpc_ipc_path) asyncio.ensure_future(exit_with_services( ipc_server, self._event_bus_service, )) asyncio.ensure_future(ipc_server.run())
def run_networking_process(chain_config: ChainConfig, sync_mode: str, pool_class: Type[HardCodedNodesPeerPool]) -> None: class DBManager(BaseManager): pass # Typeshed definitions for multiprocessing.managers is incomplete, so ignore them for now: # https://github.com/python/typeshed/blob/85a788dbcaa5e9e9a62e55f15d44530cd28ba830/stdlib/3/multiprocessing/managers.pyi#L3 DBManager.register('get_db', proxytype=DBProxy) # type: ignore DBManager.register('get_chaindb', proxytype=ChainDBProxy) # type: ignore manager = DBManager(address=chain_config.database_ipc_path) # type: ignore manager.connect() # type: ignore chaindb = manager.get_chaindb() # type: ignore if not is_database_initialized(chaindb): initialize_database(chain_config, chaindb) chain_class = get_chain_protocol_class(chain_config, sync_mode=sync_mode) peer_pool = pool_class(LESPeer, chaindb, chain_config.network_id, chain_config.nodekey) chain = chain_class(chaindb, peer_pool) loop = asyncio.get_event_loop() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, chain.cancel_token.trigger) rpc = RPCServer(chain) ipc_server = IPCServer(rpc, chain_config.jsonrpc_ipc_path) async def run_chain(chain): try: asyncio.ensure_future(chain.peer_pool.run()) asyncio.ensure_future(ipc_server.run()) await chain.run() finally: await ipc_server.stop() await chain.peer_pool.stop() await chain.stop() loop.run_until_complete(run_chain(chain)) loop.close()
async def do_run(self, event_bus: EndpointAPI) -> None: boot_info = self._boot_info trinity_config = boot_info.trinity_config with chain_for_config(trinity_config, event_bus) as chain: if trinity_config.has_app_config(Eth1AppConfig): modules = initialize_eth1_modules(chain, event_bus, trinity_config) else: raise Exception("Unsupported Node Type") rpc = RPCServer(modules, chain, event_bus) # Run IPC Server ipc_server = IPCServer(rpc, boot_info.trinity_config.jsonrpc_ipc_path) services_to_exit: Tuple[Service, ...] = (ipc_server, ) try: http_modules = get_http_enabled_modules( boot_info.args.enable_http_apis, modules) except ValidationError as error: self.logger.error(error) return # Run HTTP Server if there are http enabled APIs if len(http_modules) > 0: enabled_module_names = tuple(mod.get_name() for mod in http_modules) self.logger.info("JSON-RPC modules exposed via HTTP: %s", enabled_module_names) non_http_modules = set(type(mod) for mod in modules) - set(http_modules) exec = rpc.execute_with_access_control(non_http_modules) http_server = HTTPServer( host=boot_info.args.http_listen_address, handler=RPCHandler.handle(exec), port=boot_info.args.http_port, ) services_to_exit += (http_server, ) await run_background_asyncio_services(services_to_exit)
def run_lightnode_process(chain_config: ChainConfig, pool_class: Type[HardCodedNodesPeerPool]) -> None: manager = create_dbmanager(chain_config.database_ipc_path) chaindb = manager.get_chaindb() # type: ignore if chain_config.network_id == MAINNET_NETWORK_ID: chain_class = MainnetLightChain # type: ignore elif chain_config.network_id == ROPSTEN_NETWORK_ID: chain_class = RopstenLightChain # type: ignore else: raise NotImplementedError( "Only the mainnet and ropsten chains are currently supported") peer_pool = pool_class(LESPeer, chaindb, chain_config.network_id, chain_config.nodekey) chain = chain_class(chaindb, peer_pool) loop = asyncio.get_event_loop() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, chain.cancel_token.trigger) rpc = RPCServer(chain) ipc_server = IPCServer(rpc, chain_config.jsonrpc_ipc_path) async def run_chain(chain): try: asyncio.ensure_future(chain.peer_pool.run()) asyncio.ensure_future(ipc_server.run()) await chain.run() finally: await ipc_server.stop() await chain.peer_pool.cancel() await chain.stop() loop.run_until_complete(run_chain(chain)) loop.close()
def make_ipc_server(self, loop: asyncio.AbstractEventLoop) -> BaseService: if self.has_ipc_server: rpc = RPCServer(self.get_chain(), self.get_peer_pool()) return IPCServer(rpc, self._jsonrpc_ipc_path, loop=loop) else: return None
def make_ipc_server(self) -> Union[IPCServer, BaseService]: if self._jsonrpc_ipc_path: rpc = RPCServer(self.get_chain(), self.get_p2p_server()) return IPCServer(rpc, self._jsonrpc_ipc_path) else: return EmptyService()