Exemple #1
0
def create_db_server_manager(trinity_config: TrinityConfig,
                             base_db: BaseAtomicDB) -> BaseManager:
    app_config = trinity_config.get_app_config(BeaconAppConfig)
    chain_config = app_config.get_chain_config()
    chaindb = BeaconChainDB(base_db)

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

    class DBManager(BaseManager):
        pass

    DBManager.register('get_db',
                       callable=lambda: TracebackRecorder(base_db),
                       proxytype=AsyncDBProxy)

    DBManager.register(
        'get_chaindb',
        callable=lambda: TracebackRecorder(chaindb),
        proxytype=AsyncBeaconChainDBProxy,
    )

    manager = DBManager(address=str(
        trinity_config.database_ipc_path))  # type: ignore
    return manager
Exemple #2
0
def create_db_server_manager(trinity_config: TrinityConfig,
                             base_db: BaseAtomicDB) -> BaseManager:
    app_config = trinity_config.get_app_config(BeaconAppConfig)
    chain_config = app_config.get_chain_config()
    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)

    # This enables connection when clients launch from another process on the shell
    multiprocessing.current_process().authkey = AUTH_KEY

    class DBManager(BaseManager):
        pass

    DBManager.register('get_db',
                       callable=lambda: TracebackRecorder(base_db),
                       proxytype=AsyncDBProxy)

    DBManager.register(
        'get_chaindb',
        callable=lambda: TracebackRecorder(chaindb),
        proxytype=AsyncBeaconChainDBProxy,
    )

    manager = DBManager(address=str(
        trinity_config.database_ipc_path))  # type: ignore
    return manager
Exemple #3
0
def get_base_db(boot_info: BootInfo) -> LevelDB:
    app_config = boot_info.trinity_config.get_app_config(BeaconAppConfig)
    chain_config = app_config.get_chain_config()
    base_db = LevelDB(db_path=app_config.database_dir)
    chaindb = BeaconChainDB(base_db, chain_config.genesis_config)
    if not is_beacon_database_initialized(chaindb):
        initialize_beacon_database(chain_config, chaindb, base_db)
    return base_db
Exemple #4
0
def _initialize_beacon_filesystem_and_db(boot_info: BootInfo) -> None:
    app_config = boot_info.trinity_config.get_app_config(BeaconAppConfig)
    ensure_beacon_dirs(app_config)

    base_db = LevelDB(db_path=app_config.database_dir)
    chain_config = app_config.get_chain_config()
    chaindb = BeaconChainDB(base_db)

    if not is_beacon_database_initialized(chaindb):
        initialize_beacon_database(chain_config, chaindb, base_db)
Exemple #5
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
Exemple #6
0
    def __init__(
        self,
        local_node_key: PrivateKey,
        eth2_config: Eth2Config,
        chain_config: BeaconChainConfig,
        database_dir: Path,
        chain_class: Type[BaseBeaconChain],
        time_provider: TimeProvider = get_unix_time,
    ) -> None:
        self._local_key_pair = create_new_key_pair(local_node_key.to_bytes())
        self._eth2_config = eth2_config

        self._clock = _mk_clock(eth2_config, chain_config.genesis_time,
                                time_provider)

        self._base_db = LevelDB(db_path=database_dir)
        self._chain_db = BeaconChainDB(self._base_db, eth2_config)

        if not is_beacon_database_initialized(self._chain_db):
            initialize_beacon_database(chain_config, self._chain_db,
                                       self._base_db)

        self._chain = chain_class(self._base_db, eth2_config)
Exemple #7
0
    def __init__(
        self,
        local_node_key: PrivateKey,
        eth2_config: Eth2Config,
        chain_config: BeaconChainConfig,
        database_dir: Path,
        chain_class: Type[BaseBeaconChain],
        clock: Clock,
        validator_api_port: int,
        client_identifier: str,
        p2p_maddr: Multiaddr,
        orchestration_profile: str,
        preferred_nodes: Collection[Multiaddr],
    ) -> None:
        local_node_key = _derive_local_node_key(local_node_key,
                                                orchestration_profile)
        self._local_key_pair = create_new_key_pair(local_node_key.to_bytes())
        self._eth2_config = eth2_config

        self._clock = clock

        self._block_pool: Set[SignedBeaconBlock] = set()
        self._slashable_block_pool: Set[SignedBeaconBlock] = set()

        self._base_db = LevelDB(db_path=database_dir)
        self._chain_db = BeaconChainDB(self._base_db)
        if not is_beacon_database_initialized(self._chain_db):
            initialize_beacon_database(chain_config, self._chain_db,
                                       self._base_db)
        self._chain = chain_class(self._chain_db)

        # FIXME: can we provide `p2p_maddr` as a default listening interface for `_mk_host`?
        p2p_maddr = _derive_port(p2p_maddr, orchestration_profile)
        peer_id = PeerID.from_pubkey(self._local_key_pair.public_key)
        if "p2p" in p2p_maddr:
            existing_peer_id = p2p_maddr.value_for_protocol("p2p")
            existing_p2p_maddr = Multiaddr(f"/p2p/{existing_peer_id}")
            self.logger.warning(
                "peer identity derived from local key pair %s overriding given identity %s",
                peer_id,
                existing_peer_id,
            )
            p2p_maddr = p2p_maddr.decapsulate(existing_p2p_maddr)
        self._p2p_maddr = p2p_maddr.encapsulate(Multiaddr(f"/p2p/{peer_id}"))

        # TODO: persist metadata and handle updates...
        self._metadata_provider = lambda: MetaData.create()
        self._peer_updater, self._peer_updates = trio.open_memory_channel[
            Tuple[PeerID, Any]](0)
        self._host = Host(
            self._local_key_pair,
            peer_id,
            self._accept_peer_updates,
            self._get_status,
            self._get_finalized_root_by_epoch,
            self._get_block_by_slot,
            self._get_block_by_root,
            self._metadata_provider,
            self._get_fork_digest,
            self._eth2_config,
        )
        self._preferred_nodes = preferred_nodes

        self._sync_notifier, self._sync_requests = trio.open_memory_channel[
            SyncRequest](0)
        self._syncer = _mk_syncer()

        api_context = Context(
            client_identifier,
            chain_config.genesis_time,
            eth2_config,
            self._syncer,
            self._chain,
            self._clock,
            _mk_block_broadcaster(self),
        )
        self._api_context = api_context
        self.validator_api_port = _derive_api_port(validator_api_port,
                                                   orchestration_profile)
        self._validator_api_server = _mk_validator_api_server(
            self.validator_api_port, api_context)