def _test(): import argparse from concurrent.futures import ProcessPoolExecutor import signal from p2p import ecies from p2p.peer import HardCodedNodesPeerPool from evm.chains.ropsten import RopstenChain from evm.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import FakeAsyncChainDB logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s') parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-debug', action="store_true") args = parser.parse_args() log_level = logging.INFO if args.debug: log_level = logging.DEBUG logging.getLogger('p2p.state.StateDownloader').setLevel(log_level) db = LevelDB(args.db) chaindb = FakeAsyncChainDB(db) discovery = None peer_pool = HardCodedNodesPeerPool(ETHPeer, chaindb, RopstenChain.network_id, ecies.generate_privkey(), discovery, min_peers=5) asyncio.ensure_future(peer_pool.run()) head = chaindb.get_canonical_head() downloader = StateDownloader(db, head.state_root, peer_pool) loop = asyncio.get_event_loop() loop.set_default_executor(ProcessPoolExecutor()) sigint_received = asyncio.Event() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, sigint_received.set) async def exit_on_sigint(): await sigint_received.wait() await peer_pool.cancel() await downloader.cancel() loop.stop() loop.set_debug(True) asyncio.ensure_future(exit_on_sigint()) asyncio.ensure_future(downloader.run()) loop.run_forever() loop.close()
def _test(): import argparse from concurrent.futures import ProcessPoolExecutor import signal from p2p import ecies from p2p.peer import HardCodedNodesPeerPool from evm.chains.ropsten import RopstenChain from evm.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import FakeAsyncChainDB logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s') parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-debug', action="store_true") args = parser.parse_args() log_level = logging.INFO if args.debug: log_level = logging.DEBUG logging.getLogger('p2p.state.StateDownloader').setLevel(log_level) db = LevelDB(args.db) chaindb = FakeAsyncChainDB(db) peer_pool = HardCodedNodesPeerPool(ETHPeer, chaindb, RopstenChain.network_id, ecies.generate_privkey(), min_peers=5) asyncio.ensure_future(peer_pool.run()) head = chaindb.get_canonical_head() downloader = StateDownloader(db, head.state_root, peer_pool) loop = asyncio.get_event_loop() loop.set_default_executor(ProcessPoolExecutor()) for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, downloader.cancel_token.trigger) async def run(): # downloader.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 downloader. try: await downloader.run() except OperationCancelled: pass await peer_pool.stop() await downloader.stop() loop.run_until_complete(run()) loop.close()
def _test(): import argparse import asyncio from concurrent.futures import ProcessPoolExecutor import signal from p2p import ecies from p2p.peer import ETHPeer, HardCodedNodesPeerPool from evm.chains.ropsten import RopstenChain from evm.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import FakeAsyncChainDB, FakeAsyncRopstenChain logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s') parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) args = parser.parse_args() chaindb = FakeAsyncChainDB(LevelDB(args.db)) chain = FakeAsyncRopstenChain(chaindb) discovery = None peer_pool = HardCodedNodesPeerPool(ETHPeer, chaindb, RopstenChain.network_id, ecies.generate_privkey(), discovery, min_peers=5) asyncio.ensure_future(peer_pool.run()) loop = asyncio.get_event_loop() loop.set_default_executor(ProcessPoolExecutor()) syncer = FullNodeSyncer(chain, chaindb, chaindb.db, peer_pool) sigint_received = asyncio.Event() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, sigint_received.set) async def exit_on_sigint(): await sigint_received.wait() await syncer.cancel() await peer_pool.cancel() loop.stop() loop.set_debug(True) asyncio.ensure_future(exit_on_sigint()) asyncio.ensure_future(syncer.run()) loop.run_forever() loop.close()
def _test(): import argparse import signal from p2p import ecies from p2p.peer import HardCodedNodesPeerPool from evm.chains.ropsten import RopstenChain, ROPSTEN_GENESIS_HEADER from evm.db.backends.level import LevelDB from evm.db.backends.memory import MemoryDB from tests.p2p.integration_test_helpers import FakeAsyncChainDB logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-root-hash', type=str, required=True, help='Hex encoded root hash') args = parser.parse_args() chaindb = FakeAsyncChainDB(MemoryDB()) chaindb.persist_header(ROPSTEN_GENESIS_HEADER) peer_pool = HardCodedNodesPeerPool(ETHPeer, chaindb, RopstenChain.network_id, ecies.generate_privkey()) asyncio.ensure_future(peer_pool.run()) state_db = LevelDB(args.db) root_hash = decode_hex(args.root_hash) downloader = StateDownloader(state_db, root_hash, peer_pool) loop = asyncio.get_event_loop() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, downloader.cancel_token.trigger) async def run(): # downloader.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 downloader. await downloader.run() await peer_pool.stop() await downloader.stop() loop.run_until_complete(run()) loop.close()
def _test(): import argparse import asyncio from concurrent.futures import ProcessPoolExecutor import signal from p2p import ecies from p2p.peer import ETHPeer, HardCodedNodesPeerPool from evm.chains.ropsten import RopstenChain from evm.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import FakeAsyncChainDB logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s') parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) args = parser.parse_args() chaindb = FakeAsyncChainDB(LevelDB(args.db)) peer_pool = HardCodedNodesPeerPool(ETHPeer, chaindb, RopstenChain.network_id, ecies.generate_privkey(), min_peers=5) asyncio.ensure_future(peer_pool.run()) loop = asyncio.get_event_loop() loop.set_default_executor(ProcessPoolExecutor()) syncer = FullNodeSyncer(chaindb, peer_pool) for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, syncer.cancel_token.trigger) async def run(): try: await syncer.run() except OperationCancelled: pass await peer_pool.stop() loop.run_until_complete(run()) loop.close()