def is_database_initialized(chaindb: AsyncChainDB) -> bool: try: chaindb.get_canonical_head() except CanonicalHeadNotFound: # empty chain database return False else: return True
def serve_chaindb(chain_config: ChainConfig, db: BaseDB) -> None: chaindb = AsyncChainDB(db) if not is_database_initialized(chaindb): initialize_database(chain_config, chaindb) if chain_config.network_id == MAINNET_NETWORK_ID: chain_class = MainnetChain # type: ignore elif chain_config.network_id == ROPSTEN_NETWORK_ID: chain_class = RopstenChain # type: ignore else: raise NotImplementedError( "Only the mainnet and ropsten chains are currently supported") chain = chain_class(chaindb) # type: ignore 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', callable=lambda: db, proxytype=DBProxy) # type: ignore DBManager.register( # type: ignore 'get_chaindb', callable=lambda: chaindb, proxytype=ChainDBProxy) DBManager.register('get_chain', callable=lambda: chain, proxytype=ChainProxy) # type: ignore manager = DBManager(address=chain_config.database_ipc_path) # type: ignore server = manager.get_server() # type: ignore server.serve_forever() # type: ignore
def initialize_database(chain_config: ChainConfig, chaindb: AsyncChainDB) -> None: try: chaindb.get_canonical_head() except CanonicalHeadNotFound: if chain_config.network_id == ROPSTEN_NETWORK_ID: # We're starting with a fresh DB. chaindb.persist_header(ROPSTEN_GENESIS_HEADER) elif chain_config.network_id == MAINNET_NETWORK_ID: chaindb.persist_header(MAINNET_GENESIS_HEADER) else: # TODO: add genesis data to ChainConfig and if it's present, use it # here to initialize the chain. raise NotImplementedError( "Only the mainnet and ropsten chains are currently supported")