def is_database_initialized(chaindb: ChainDB) -> bool: try: chaindb.get_canonical_head() except CanonicalHeadNotFound: # empty chain database return False else: return True
def initialize_database(chain_config: ChainConfig, chaindb: ChainDB) -> None: try: chaindb.get_canonical_head() except CanonicalHeadNotFound: if chain_config.chain_identifier == ROPSTEN: # We're starting with a fresh DB. # TODO: log that we initialized the chain chaindb.persist_header(ROPSTEN_GENESIS_HEADER) else: # TODO: add genesis data to ChainConfig and if it's present, use it # here to initialize the chain. raise NotImplementedError("Not implemented for other chains yet")
def initialize_database(chain_config: ChainConfig, chaindb: ChainDB) -> 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")
print("Logging to", LOGFILE) if args.debug: LOGLEVEL = logging.DEBUG logging.basicConfig(level=LOGLEVEL, filename=LOGFILE) DemoLightChain = LightChain.configure( name='Demo LightChain', vm_configuration=MAINNET_VM_CONFIGURATION, network_id=ROPSTEN_NETWORK_ID, ) chaindb = ChainDB(LevelDB(args.db)) peer_pool = PeerPool(LESPeer, chaindb, ROPSTEN_NETWORK_ID, ecies.generate_privkey()) try: chaindb.get_canonical_head() except CanonicalHeadNotFound: # We're starting with a fresh DB. chain = DemoLightChain.from_genesis_header(chaindb, ROPSTEN_GENESIS_HEADER, peer_pool) else: # We're reusing an existing db. chain = DemoLightChain(chaindb, peer_pool) async def run(): asyncio.ensure_future(peer_pool.run()) # chain.run() will run in a loop until our atexit handler is called, at which point it returns # and we cleanly stop the pool and chain. await chain.run() await peer_pool.stop()
def _test(): import argparse import signal from evm.chains.mainnet import (MAINNET_GENESIS_HEADER, MAINNET_VM_CONFIGURATION, MAINNET_NETWORK_ID) from evm.chains.ropsten import ROPSTEN_GENESIS_HEADER, ROPSTEN_NETWORK_ID from evm.db.backends.level import LevelDB from evm.exceptions import CanonicalHeadNotFound from p2p import ecies from p2p.integration_test_helpers import LocalGethPeerPool logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') logging.getLogger("p2p.lightchain.LightChain").setLevel(logging.DEBUG) parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-mainnet', action="store_true") parser.add_argument('-local-geth', action="store_true") args = parser.parse_args() GENESIS_HEADER = ROPSTEN_GENESIS_HEADER NETWORK_ID = ROPSTEN_NETWORK_ID if args.mainnet: GENESIS_HEADER = MAINNET_GENESIS_HEADER NETWORK_ID = MAINNET_NETWORK_ID DemoLightChain = LightChain.configure( 'DemoLightChain', vm_configuration=MAINNET_VM_CONFIGURATION, network_id=NETWORK_ID, ) chaindb = ChainDB(LevelDB(args.db)) if args.local_geth: peer_pool = LocalGethPeerPool(LESPeer, chaindb, NETWORK_ID, ecies.generate_privkey()) else: peer_pool = PeerPool(LESPeer, chaindb, NETWORK_ID, ecies.generate_privkey()) try: chaindb.get_canonical_head() except CanonicalHeadNotFound: # We're starting with a fresh DB. chain = DemoLightChain.from_genesis_header(chaindb, GENESIS_HEADER, peer_pool) else: # We're reusing an existing db. chain = DemoLightChain(chaindb, peer_pool) asyncio.ensure_future(peer_pool.run()) loop = asyncio.get_event_loop() async def run(): # chain.run() will run in a loop until the SIGINT/SIGTERM handler triggers its cancel # token, at which point it returns and we stop the pool and chain. await chain.run() await peer_pool.stop() await chain.stop() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, chain.cancel_token.trigger) loop.run_until_complete(run()) loop.close()
parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-mainnet', action="store_true") args = parser.parse_args() GENESIS_HEADER = ROPSTEN_GENESIS_HEADER NETWORK_ID = ROPSTEN_NETWORK_ID if args.mainnet: GENESIS_HEADER = MAINNET_GENESIS_HEADER NETWORK_ID = MAINNET_NETWORK_ID DemoLightChain = LightChain.configure( 'RPCDemoLightChain', vm_configuration=MAINNET_VM_CONFIGURATION, network_id=NETWORK_ID, privkey=ecies.generate_privkey(), ) chaindb = ChainDB(LevelDB(args.db)) try: chaindb.get_canonical_head() except CanonicalHeadNotFound: # We're starting with a fresh DB. chain = DemoLightChain.from_genesis_header(chaindb, GENESIS_HEADER) else: # We're reusing an existing db. chain = DemoLightChain(chaindb) app = App(chain) web.run_app(app, port=8080)