Пример #1
0
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()
Пример #2
0
def test_db_manager_lifecycle(db, ipc_path):
    manager = DBManager(db)

    assert not manager.is_running
    assert not manager.is_stopped

    with manager.run(ipc_path):
        assert manager.is_running
        assert not manager.is_stopped

    assert not manager.is_running
    assert manager.is_stopped
Пример #3
0
def run_server(ipc_path):
    with tempfile.TemporaryDirectory() as db_path:
        db = LevelDB(db_path=db_path)
        manager = DBManager(db)

        with manager.run(ipc_path):
            try:
                manager.wait_stopped()
            except KeyboardInterrupt:
                pass

        ipc_path.unlink()
Пример #4
0
def run_database_process(trinity_config: TrinityConfig,
                         db_class: Type[AtomicDatabaseAPI]) -> None:
    with trinity_config.process_id_file('database'):
        app_config = trinity_config.get_app_config(BeaconAppConfig)

        base_db = db_class(db_path=app_config.database_dir)

        manager = DBManager(base_db)
        with manager.run(trinity_config.database_ipc_path):
            try:
                manager.wait_stopped()
            except KeyboardInterrupt:
                pass
Пример #5
0
async def run_db_manager(
        boot_info: BootInfo, get_base_db_fn: Callable[[BootInfo],
                                                      LevelDB]) -> None:
    with child_process_logging(boot_info):
        trinity_config = boot_info.trinity_config
        manager = DBManager(get_base_db_fn(boot_info))
        with trinity_config.process_id_file('database'):
            with manager.run(trinity_config.database_ipc_path):
                loop = asyncio.get_event_loop()
                try:
                    await loop.run_in_executor(None, manager.wait_stopped)
                finally:
                    # We always need to call stop() before returning as asyncio can't cancel the
                    # thread started by run_in_executor() and that would prevent
                    # open_in_process(run_db_manager, ...) from returning.
                    manager.stop()
Пример #6
0
def run_database_process(trinity_config: TrinityConfig, db_class: Type[LevelDB]) -> None:
    with trinity_config.process_id_file('database'):
        app_config = trinity_config.get_app_config(BeaconAppConfig)
        chain_config = app_config.get_chain_config()

        base_db = db_class(db_path=app_config.database_dir)
        chaindb = BeaconChainDB(base_db, chain_config.genesis_config)

        if not is_beacon_database_initialized(chaindb, BeaconBlock):
            initialize_beacon_database(chain_config, chaindb, base_db, BeaconBlock)

        manager = DBManager(base_db)
        with manager.run(trinity_config.database_ipc_path):
            try:
                manager.wait_stopped()
            except KeyboardInterrupt:
                pass
Пример #7
0
def run_database_process(trinity_config: TrinityConfig,
                         db_class: Type[AtomicDatabaseAPI]) -> None:
    with trinity_config.process_id_file('database'):
        app_config = trinity_config.get_app_config(Eth1AppConfig)

        base_db = db_class(db_path=app_config.database_dir)
        chaindb = ChainDB(base_db)

        if not is_database_initialized(chaindb):
            chain_config = app_config.get_chain_config()
            initialize_database(chain_config, chaindb, base_db)

        manager = DBManager(base_db)
        with manager.run(trinity_config.database_ipc_path):
            try:
                manager.wait_stopped()
            except KeyboardInterrupt:
                pass
Пример #8
0
def run_database_process(boot_info: BootInfo, db_class: Type[LevelDB]) -> None:
    setup_child_process_logging(boot_info)
    trinity_config = boot_info.trinity_config

    with trinity_config.process_id_file('database'):
        app_config = trinity_config.get_app_config(Eth1AppConfig)

        base_db = db_class(db_path=app_config.database_dir)
        chaindb = ChainDB(base_db)

        if not is_database_initialized(chaindb):
            chain_config = app_config.get_chain_config()
            initialize_database(chain_config, chaindb, base_db)

        manager = DBManager(base_db)
        with manager.run(trinity_config.database_ipc_path):
            try:
                manager.wait_stopped()
            except KeyboardInterrupt:
                pass
Пример #9
0
def test_db_manager_lifecycle_with_connections(db, ipc_path):
    manager = DBManager(db)

    assert not manager.is_running
    assert not manager.is_stopped

    with manager.run(ipc_path):
        assert manager.is_running
        assert not manager.is_stopped

        client_a = DBClient.connect(ipc_path)
        client_b = DBClient.connect(ipc_path)

        assert manager.is_running
        assert not manager.is_stopped

    client_a.close()
    client_b.close()

    assert not manager.is_running
    assert manager.is_stopped
Пример #10
0
def db_manager(base_db, ipc_path):
    with DBManager(base_db).run(ipc_path) as manager:
        yield manager