def setup_eth1_modules( self, trinity_config: TrinityConfig) -> Tuple[Eth1ChainRPCModule, ...]: db_manager = create_db_manager(trinity_config.database_ipc_path) db_manager.connect() eth1_app_config = 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}") return initialize_eth1_modules(chain, self.event_bus)
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)
def setup_eth1_modules( self, trinity_config: TrinityConfig) -> Tuple[Eth1ChainRPCModule, ...]: db_manager = create_db_consumer_manager( trinity_config.database_ipc_path) eth1_app_config = trinity_config.get_app_config(Eth1AppConfig) chain_config = trinity_config.get_chain_config() chain: BaseAsyncChain if eth1_app_config.database_mode is Eth1DbMode.LIGHT: 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.database_mode is Eth1DbMode.FULL: db = db_manager.get_db() # type: ignore chain = chain_config.full_chain_class(db) else: raise Exception( f"Unsupported Database Mode: {eth1_app_config.database_mode}") return initialize_eth1_modules(chain, self.event_bus)
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 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))
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 setup_rpc_server(event_bus, chain_fixture, fixture_path): chain = MainnetFullChain(None) rpc = RPCServer(initialize_eth1_modules(chain, event_bus), chain, event_bus) setup_result, setup_error = await call_rpc(rpc, 'evm_resetToGenesisFixture', [chain_fixture]) # We need to advance the event loop for modules to be able to pickup the new chain await asyncio.sleep(0) assert setup_error is None and setup_result is True, f"cannot load chain for {fixture_path}" return rpc
async def test_access_control(event_bus, api, param, disabled_modules, expected_result, expected_error): chain = MainnetFullChain(None) trinity_config = TrinityConfig(app_identifier="eth1", network_id=1) rpc = RPCServer(initialize_eth1_modules(chain, event_bus, trinity_config), chain, event_bus) request = build_request(api, param) response = await rpc.execute_with_access_control(disabled_modules, request) result, error = result_from_response(response) assert result == expected_result assert error == expected_error
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 test_rpc_against_fixtures(event_bus, chain_fixture, fixture_data): chain = MainnetFullChain(None) rpc = RPCServer(initialize_eth1_modules(chain, event_bus), chain, event_bus) setup_result, setup_error = await call_rpc(rpc, 'evm_resetToGenesisFixture', [chain_fixture]) # We need to advance the event loop for modules to be able to pickup the new chain await asyncio.sleep(0) assert setup_error is None and setup_result is True, "cannot load chain for {0}".format( fixture_data) # noqa: E501 await validate_accounts(rpc, chain_fixture['pre']) for block_fixture in chain_fixture['blocks']: should_be_good_block = 'blockHeader' in block_fixture if 'rlp_error' in block_fixture: assert not should_be_good_block continue block_result, block_error = await call_rpc(rpc, 'evm_applyBlockFixture', [block_fixture]) if should_be_good_block: assert block_error is None assert block_result == block_fixture['rlp'] await validate_block(rpc, block_fixture, block_fixture['blockHeader']['hash']) else: assert block_error is not None if chain_fixture.get('lastblockhash', None): for block_fixture in chain_fixture['blocks']: if get_in(['blockHeader', 'hash'], block_fixture) == chain_fixture['lastblockhash']: await validate_last_block(rpc, block_fixture) # Fixtures do not include the `postState` field when its size would be past a certain limit. # https://github.com/ethereum/tests/issues/637#issuecomment-534072897 if chain_fixture.get('postState', None): await validate_accounts(rpc, chain_fixture['postState']) await validate_accounts(rpc, chain_fixture['pre'], 'earliest') await validate_accounts(rpc, chain_fixture['pre'], 0)
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)) 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()
async def test_rpc_against_fixtures(chain, event_bus, ipc_server, chain_fixture, fixture_data): rpc = RPCServer(initialize_eth1_modules(MainnetFullChain(None), event_bus)) setup_result, setup_error = await call_rpc(rpc, 'evm_resetToGenesisFixture', [chain_fixture]) # We need to advance the event loop for modules to be able to pickup the new chain await asyncio.sleep(0) assert setup_error is None and setup_result is True, "cannot load chain for {0}".format( fixture_data) # noqa: E501 await validate_accounts(rpc, chain_fixture['pre']) for block_fixture in chain_fixture['blocks']: should_be_good_block = 'blockHeader' in block_fixture if 'rlp_error' in block_fixture: assert not should_be_good_block continue block_result, block_error = await call_rpc(rpc, 'evm_applyBlockFixture', [block_fixture]) if should_be_good_block: assert block_error is None assert block_result == block_fixture['rlp'] await validate_block(rpc, block_fixture, block_fixture['blockHeader']['hash']) else: assert block_error is not None if chain_fixture.get('lastblockhash', None): for block_fixture in chain_fixture['blocks']: if get_in(['blockHeader', 'hash'], block_fixture) == chain_fixture['lastblockhash']: await validate_last_block(rpc, block_fixture) await validate_accounts(rpc, chain_fixture['postState']) await validate_accounts(rpc, chain_fixture['pre'], 'earliest') await validate_accounts(rpc, chain_fixture['pre'], 0)
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())
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)