def _test() -> None: import argparse import signal from eth.chains.ropsten import RopstenChain, ROPSTEN_VM_CONFIGURATION from p2p import ecies from p2p.kademlia import Node from p2p.peer import DEFAULT_PREFERRED_NODES from tests.p2p.integration_test_helpers import ( FakeAsyncChainDB, FakeAsyncLevelDB, connect_to_peers_loop) 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") parser.add_argument('-enode', type=str, required=False, help="The enode we should connect to") args = parser.parse_args() log_level = logging.INFO if args.debug: log_level = logging.DEBUG logging.getLogger('p2p.state.StateDownloader').setLevel(log_level) db = FakeAsyncLevelDB(args.db) chaindb = FakeAsyncChainDB(db) network_id = RopstenChain.network_id if args.enode: nodes = tuple([Node.from_uri(args.enode)]) else: nodes = DEFAULT_PREFERRED_NODES[network_id] peer_pool = PeerPool( ETHPeer, chaindb, network_id, ecies.generate_privkey(), ROPSTEN_VM_CONFIGURATION) asyncio.ensure_future(peer_pool.run()) asyncio.ensure_future(connect_to_peers_loop(peer_pool, nodes)) head = chaindb.get_canonical_head() downloader = StateDownloader(chaindb, db, head.state_root, peer_pool) loop = asyncio.get_event_loop() sigint_received = asyncio.Event() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, sigint_received.set) async def exit_on_sigint() -> None: await sigint_received.wait() await peer_pool.cancel() await downloader.cancel() loop.stop() async def run() -> None: await downloader.run() downloader.logger.info("run() finished, exiting") sigint_received.set() # loop.set_debug(True) asyncio.ensure_future(exit_on_sigint()) asyncio.ensure_future(run()) loop.run_forever() loop.close()
def _test() -> None: import argparse from concurrent.futures import ProcessPoolExecutor import signal from p2p import ecies from evm.chains.ropsten import RopstenChain, ROPSTEN_GENESIS_HEADER from evm.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import FakeAsyncChainDB, LocalGethPeerPool parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-local-geth', action="store_true") parser.add_argument('-debug', action="store_true") args = parser.parse_args() logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%H:%M:%S') log_level = logging.INFO if args.debug: log_level = logging.DEBUG logging.getLogger('p2p.chain.ChainSyncer').setLevel(log_level) loop = asyncio.get_event_loop() # Use a ProcessPoolExecutor as the default because the tasks we want to offload from the main # thread are cpu intensive. loop.set_default_executor(ProcessPoolExecutor()) chaindb = FakeAsyncChainDB(LevelDB(args.db)) chaindb.persist_header(ROPSTEN_GENESIS_HEADER) privkey = ecies.generate_privkey() if args.local_geth: peer_pool = LocalGethPeerPool(ETHPeer, chaindb, RopstenChain.network_id, privkey) else: from p2p.peer import HardCodedNodesPeerPool min_peers = 5 peer_pool = HardCodedNodesPeerPool(ETHPeer, chaindb, RopstenChain.network_id, privkey, min_peers) asyncio.ensure_future(peer_pool.run()) downloader = ChainSyncer(chaindb, peer_pool) 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() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, downloader.cancel_token.trigger) loop.run_until_complete(run()) loop.close()
def _test() -> None: import argparse import signal from p2p import ecies from p2p import kademlia from p2p.constants import ROPSTEN_BOOTNODES from p2p.discovery import DiscoveryProtocol from evm.chains.ropsten import RopstenChain, ROPSTEN_GENESIS_HEADER from evm.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import FakeAsyncChainDB, LocalGethPeerPool logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') logging.getLogger('p2p.chain.ChainSyncer').setLevel(logging.DEBUG) parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-local-geth', action="store_true") args = parser.parse_args() loop = asyncio.get_event_loop() chaindb = FakeAsyncChainDB(LevelDB(args.db)) chaindb.persist_header(ROPSTEN_GENESIS_HEADER) privkey = ecies.generate_privkey() if args.local_geth: peer_pool = LocalGethPeerPool(ETHPeer, chaindb, RopstenChain.network_id, privkey) discovery = None else: listen_host = '0.0.0.0' listen_port = 30303 addr = kademlia.Address(listen_host, listen_port, listen_port) discovery = DiscoveryProtocol(privkey, addr, ROPSTEN_BOOTNODES) loop.run_until_complete(discovery.create_endpoint()) print("Bootstrapping discovery service...") loop.run_until_complete(discovery.bootstrap()) peer_pool = PeerPool(ETHPeer, chaindb, RopstenChain.network_id, privkey, discovery) asyncio.ensure_future(peer_pool.run()) downloader = ChainSyncer(chaindb, peer_pool) # On ROPSTEN the discovery table is usually full of bad peers so we can't require too many # peers in order to sync. downloader.min_peers_to_sync = 1 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() if discovery is not None: discovery.stop() # Give any pending discovery tasks some time to finish. await asyncio.sleep(2) for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, downloader.cancel_token.trigger) loop.run_until_complete(run()) 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) 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() -> None: import argparse import asyncio from concurrent.futures import ProcessPoolExecutor import signal from p2p import ecies from p2p.kademlia import Node from p2p.peer import ETHPeer, DEFAULT_PREFERRED_NODES from evm.chains.ropsten import RopstenChain, ROPSTEN_VM_CONFIGURATION from evm.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import (FakeAsyncChainDB, FakeAsyncRopstenChain, connect_to_peers_loop) 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('-enode', type=str, required=False, help="The enode we should connect to") args = parser.parse_args() chaindb = FakeAsyncChainDB(LevelDB(args.db)) chain = FakeAsyncRopstenChain(chaindb) network_id = RopstenChain.network_id privkey = ecies.generate_privkey() peer_pool = PeerPool(ETHPeer, chaindb, network_id, privkey, ROPSTEN_VM_CONFIGURATION) if args.enode: nodes = tuple([Node.from_uri(args.enode)]) else: nodes = DEFAULT_PREFERRED_NODES[network_id] asyncio.ensure_future(peer_pool.run()) asyncio.ensure_future(connect_to_peers_loop(peer_pool, nodes)) 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() -> None: 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 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, LocalGethPeerPool) 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('-local-geth', action="store_true") args = parser.parse_args() chaindb = FakeAsyncChainDB(LevelDB(args.db)) chain = FakeAsyncRopstenChain(chaindb) privkey = ecies.generate_privkey() if args.local_geth: peer_pool = LocalGethPeerPool(ETHPeer, chaindb, RopstenChain.network_id, privkey) else: discovery = None peer_pool = HardCodedNodesPeerPool( ETHPeer, chaindb, RopstenChain.network_id, privkey, discovery, ) 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() -> None: import argparse import signal from p2p import ecies from evm.chains.ropsten import RopstenChain, ROPSTEN_GENESIS_HEADER from evm.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import FakeAsyncChainDB, LocalGethPeerPool logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s') logging.getLogger('p2p.chain.ChainSyncer').setLevel(logging.DEBUG) parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-local-geth', action="store_true") args = parser.parse_args() chaindb = FakeAsyncChainDB(LevelDB(args.db)) chaindb.persist_header(ROPSTEN_GENESIS_HEADER) if args.local_geth: peer_pool = LocalGethPeerPool(ETHPeer, chaindb, RopstenChain.network_id, ecies.generate_privkey()) else: peer_pool = PeerPool(ETHPeer, chaindb, RopstenChain.network_id, ecies.generate_privkey()) asyncio.ensure_future(peer_pool.run()) downloader = ChainSyncer(chaindb, peer_pool) 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 = asyncio.get_event_loop() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, downloader.cancel_token.trigger) loop.run_until_complete(run()) loop.close()
def _test(): import argparse import signal from p2p import ecies 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_to_db(ROPSTEN_GENESIS_HEADER) peer_pool = PeerPool(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()
def _test() -> None: import argparse import signal from p2p import ecies from evm.chains.ropsten import RopstenChain, ROPSTEN_GENESIS_HEADER from evm.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import ( FakeAsyncChainDB, FakeAsyncRopstenChain, LocalGethPeerPool, FakeAsyncHeaderDB) parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-fast', action="store_true") parser.add_argument('-local-geth', action="store_true") parser.add_argument('-debug', action="store_true") args = parser.parse_args() logging.basicConfig( level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%H:%M:%S') log_level = logging.INFO if args.debug: log_level = logging.DEBUG logging.getLogger('p2p.chain.ChainSyncer').setLevel(log_level) loop = asyncio.get_event_loop() base_db = LevelDB(args.db) chaindb = FakeAsyncChainDB(base_db) chaindb.persist_header(ROPSTEN_GENESIS_HEADER) headerdb = FakeAsyncHeaderDB(base_db) privkey = ecies.generate_privkey() if args.local_geth: peer_pool = LocalGethPeerPool(ETHPeer, headerdb, RopstenChain.network_id, privkey) else: from p2p.peer import HardCodedNodesPeerPool discovery = None peer_pool = HardCodedNodesPeerPool( peer_class=ETHPeer, headerdb=headerdb, network_id=RopstenChain.network_id, privkey=privkey, discovery=discovery, ) asyncio.ensure_future(peer_pool.run()) if args.fast: syncer = FastChainSyncer(chaindb, peer_pool) else: chain = FakeAsyncRopstenChain(base_db) syncer = RegularChainSyncer(chain, chaindb, peer_pool) syncer.min_peers_to_sync = 1 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 syncer.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(): """ Create a Peer instance connected to a local geth instance and log messages exchanged with it. Use the following command line to run geth: ./build/bin/geth -vmodule p2p=4,p2p/discv5=0,eth/*=0 \ -nodekeyhex 45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8 \ -testnet -lightserv 90 """ import argparse import signal from evm.chains.ropsten import RopstenChain, ROPSTEN_GENESIS_HEADER from evm.db.backends.memory import MemoryDB from tests.p2p.integration_test_helpers import FakeAsyncChainDB logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s: %(message)s') # The default remoteid can be used if you pass nodekeyhex as above to geth. nodekey = keys.PrivateKey(decode_hex( "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8")) remoteid = nodekey.public_key.to_hex() parser = argparse.ArgumentParser() parser.add_argument('-remoteid', type=str, default=remoteid) parser.add_argument('-light', action='store_true', help="Connect as a light node") args = parser.parse_args() peer_class = ETHPeer # type: ignore if args.light: peer_class = LESPeer # type: ignore remote = Node( keys.PublicKey(decode_hex(args.remoteid)), Address('127.0.0.1', 30303, 30303)) chaindb = FakeAsyncChainDB(MemoryDB()) chaindb.persist_header(ROPSTEN_GENESIS_HEADER) network_id = RopstenChain.network_id loop = asyncio.get_event_loop() peer = loop.run_until_complete( asyncio.wait_for( handshake(remote, ecies.generate_privkey(), peer_class, chaindb, network_id), HANDSHAKE_TIMEOUT)) async def request_stuff(): # Request some stuff from ropsten's block 2440319 # (https://ropsten.etherscan.io/block/2440319), just as a basic test. nonlocal peer block_hash = decode_hex( '0x59af08ab31822c992bb3dad92ddb68d820aa4c69e9560f07081fa53f1009b152') if peer_class == ETHPeer: peer = cast(ETHPeer, peer) peer.sub_proto.send_get_block_headers(block_hash, 1) peer.sub_proto.send_get_block_bodies([block_hash]) peer.sub_proto.send_get_receipts([block_hash]) else: peer = cast(LESPeer, peer) request_id = 1 peer.sub_proto.send_get_block_headers(block_hash, 1, request_id) peer.sub_proto.send_get_block_bodies([block_hash], request_id + 1) peer.sub_proto.send_get_receipts(block_hash, request_id + 2) for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, peer.cancel_token.trigger) asyncio.ensure_future(request_stuff()) loop.run_until_complete(peer.run()) loop.close()
def _test() -> None: import argparse from eth_keys import keys from eth_utils import ( decode_hex, ) from evm.db.backends.memory import MemoryDB from p2p import ecies from p2p.exceptions import ( OperationCancelled, ) from tests.p2p.dumb_peer import DumbPeer from tests.p2p.integration_test_helpers import FakeAsyncChainDB parser = argparse.ArgumentParser() parser.add_argument('-debug', action="store_true") parser.add_argument('-server-address', type=str, required=True) parser.add_argument('-bootstrap', '--list', type=str) parser.add_argument('-privkey', type=str) args = parser.parse_args() logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%H:%M:%S') log_level = logging.INFO if args.debug: log_level = logging.DEBUG logging.getLogger('p2p.server.Server').setLevel(log_level) loop = asyncio.get_event_loop() chaindb = FakeAsyncChainDB(MemoryDB()) if args.privkey: privkey = keys.PrivateKey(decode_hex(args.privkey)) else: privkey = ecies.generate_privkey() ip, port = args.server_address.split(':') server_address = Address(ip, port) if args.list: bootstrap_nodes = args.list.split(',') else: bootstrap_nodes = [] server = Server(privkey, server_address, chaindb, bootstrap_nodes, 1, peer_class=DumbPeer) asyncio.ensure_future(server.run()) asyncio.ensure_future(server.peer_pool.run()) async def run(): try: while True: if len(server.peer_pool.connected_nodes) >= 1: server.logger.debug('peers: %d', len(server.peer_pool.connected_nodes)) else: server.logger.debug('no peer connected') await asyncio.sleep(1) except (OperationCancelled, KeyboardInterrupt): pass await server.peer_pool.stop() await server.stop() loop.run_until_complete(run()) loop.close()
def main() -> None: args = parser.parse_args() if args.ropsten: chain_identifier = ROPSTEN else: # TODO: mainnet chain_identifier = ROPSTEN if args.light: sync_mode = SYNC_LIGHT else: # TODO: actually use args.sync_mode (--sync-mode) sync_mode = SYNC_LIGHT chain_config = ChainConfig.from_parser_args( chain_identifier, args, ) if not is_data_dir_initialized(chain_config): # TODO: this will only work as is for chains with known genesis # parameters. Need to flesh out how genesis parameters for custom # chains are defined and passed around. initialize_data_dir(chain_config) pool_class = PeerPool if args.local_geth: pool_class = LocalGethPeerPool # if console command, run the trinity CLI if args.subcommand == 'console': use_ipython = not args.vanilla_shell debug = args.log_level.upper() == 'DEBUG' # TODO: this should use the base `Chain` class rather than the protocol # class since it's just a repl with access to the chain. chain_class = get_chain_protocol_class(chain_config, sync_mode) chaindb = FakeAsyncChainDB(LevelDB(chain_config.database_dir)) if not is_database_initialized(chaindb): initialize_database(chain_config, chaindb) peer_pool = pool_class(LESPeer, chaindb, chain_config.network_id, chain_config.nodekey) chain = chain_class(chaindb, peer_pool) console(chain, use_ipython=use_ipython, debug=debug) sys.exit(0) logger, log_queue, listener = setup_trinity_logging(args.log_level.upper()) # start the listener thread to handle logs produced by other processes in # the local logger. listener.start() # First initialize the database process. database_server_process = ctx.Process(target=run_database_process, args=( chain_config, LevelDB, ), kwargs={'log_queue': log_queue}) # For now we just run the light sync against ropsten by default. networking_process = ctx.Process(target=run_networking_process, args=(chain_config, sync_mode, pool_class), kwargs={'log_queue': log_queue}) # start the processes database_server_process.start() wait_for_ipc(chain_config.database_ipc_path) networking_process.start() try: networking_process.join() except KeyboardInterrupt: logger.info('Keyboard Interrupt: Stopping') kill_process_gracefully(networking_process) logger.info('KILLED networking_process') kill_process_gracefully(database_server_process) logger.info('KILLED database_server_process')
def _test(): """ Create a Peer instance connected to a local geth instance and log messages exchanged with it. Use the following command line to run geth: ./build/bin/geth -vmodule p2p=4,p2p/discv5=0,eth/*=0 \ -nodekeyhex 45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8 \ -testnet -lightserv 90 """ import argparse import signal from evm.chains.ropsten import RopstenChain, ROPSTEN_GENESIS_HEADER from evm.db.backends.memory import MemoryDB from tests.p2p.integration_test_helpers import FakeAsyncChainDB logging.basicConfig(level=logging.DEBUG, format='%(levelname)s: %(message)s') # The default remoteid can be used if you pass nodekeyhex as above to geth. nodekey = keys.PrivateKey(decode_hex( "45a915e4d060149eb4365960e6a7a45f334393093061116b197e3240065ff2d8")) remoteid = nodekey.public_key.to_hex() parser = argparse.ArgumentParser() parser.add_argument('-remoteid', type=str, default=remoteid) parser.add_argument('-light', action='store_true', help="Connect as a light node") args = parser.parse_args() peer_class = ETHPeer # type: ignore if args.light: peer_class = LESPeer # type: ignore remote = Node( keys.PublicKey(decode_hex(args.remoteid)), Address('127.0.0.1', 30303, 30303)) chaindb = FakeAsyncChainDB(MemoryDB()) chaindb.persist_header_to_db(ROPSTEN_GENESIS_HEADER) network_id = RopstenChain.network_id loop = asyncio.get_event_loop() peer = loop.run_until_complete( asyncio.wait_for( handshake(remote, ecies.generate_privkey(), peer_class, chaindb, network_id), HANDSHAKE_TIMEOUT)) async def request_stuff(): # Request some stuff from ropsten's block 2440319 # (https://ropsten.etherscan.io/block/2440319), just as a basic test. nonlocal peer block_hash = decode_hex( '0x59af08ab31822c992bb3dad92ddb68d820aa4c69e9560f07081fa53f1009b152') if peer_class == ETHPeer: peer = cast(ETHPeer, peer) peer.sub_proto.send_get_block_headers(block_hash, 1) peer.sub_proto.send_get_block_bodies([block_hash]) peer.sub_proto.send_get_receipts([block_hash]) else: peer = cast(LESPeer, peer) request_id = 1 peer.sub_proto.send_get_block_headers(block_hash, 1, request_id) peer.sub_proto.send_get_block_bodies([block_hash], request_id + 1) peer.sub_proto.send_get_receipts(block_hash, request_id + 2) for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, peer.cancel_token.trigger) asyncio.ensure_future(request_stuff()) loop.run_until_complete(peer.run()) loop.close()
def _test() -> None: import argparse from pathlib import Path import signal from evm.db.backends.level import LevelDB from evm.chains.ropsten import RopstenChain, ROPSTEN_GENESIS_HEADER from p2p import ecies from p2p.constants import ROPSTEN_BOOTNODES from p2p.peer import ETHPeer from trinity.utils.chains import load_nodekey from tests.p2p.integration_test_helpers import FakeAsyncHeaderDB from tests.p2p.integration_test_helpers import FakeAsyncChainDB, FakeAsyncRopstenChain parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-debug', action="store_true") parser.add_argument('-bootnodes', type=str, default=[]) parser.add_argument('-nodekey', type=str) args = parser.parse_args() logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%H:%M:%S') log_level = logging.INFO if args.debug: log_level = logging.DEBUG logging.getLogger('p2p.server.Server').setLevel(log_level) loop = asyncio.get_event_loop() db = LevelDB(args.db) headerdb = FakeAsyncHeaderDB(db) chaindb = FakeAsyncChainDB(db) chaindb.persist_header(ROPSTEN_GENESIS_HEADER) chain = FakeAsyncRopstenChain(db) # NOTE: Since we may create a different priv/pub key pair every time we run this, remote nodes # may try to establish a connection using the pubkey from one of our previous runs, which will # result in lots of DecryptionErrors in receive_handshake(). if args.nodekey: privkey = load_nodekey(Path(args.nodekey)) else: privkey = ecies.generate_privkey() port = 30303 if args.bootnodes: bootstrap_nodes = args.bootnodes.split(',') else: bootstrap_nodes = ROPSTEN_BOOTNODES bootstrap_nodes = [Node.from_uri(enode) for enode in bootstrap_nodes] server = Server( privkey, port, chain, chaindb, headerdb, db, RopstenChain.network_id, peer_class=ETHPeer, bootstrap_nodes=bootstrap_nodes, ) 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 server.cancel() loop.stop() loop.set_debug(True) asyncio.ensure_future(exit_on_sigint()) asyncio.ensure_future(server.run()) loop.run_forever() loop.close()
def _test() -> None: import argparse from concurrent.futures import ProcessPoolExecutor import signal from p2p import ecies from evm.chains.ropsten import RopstenChain, ROPSTEN_GENESIS_HEADER from evm.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import (FakeAsyncChainDB, FakeAsyncRopstenChain, LocalGethPeerPool) parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-fast', action="store_true") parser.add_argument('-local-geth', action="store_true") parser.add_argument('-debug', action="store_true") args = parser.parse_args() logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%H:%M:%S') log_level = logging.INFO if args.debug: log_level = logging.DEBUG logging.getLogger('p2p.chain.ChainSyncer').setLevel(log_level) loop = asyncio.get_event_loop() # Use a ProcessPoolExecutor as the default because the tasks we want to offload from the main # thread are cpu intensive. loop.set_default_executor(ProcessPoolExecutor()) chaindb = FakeAsyncChainDB(LevelDB(args.db)) chaindb.persist_header(ROPSTEN_GENESIS_HEADER) privkey = ecies.generate_privkey() if args.local_geth: peer_pool = LocalGethPeerPool(ETHPeer, chaindb, RopstenChain.network_id, privkey) else: from p2p.peer import HardCodedNodesPeerPool min_peers = 5 peer_pool = HardCodedNodesPeerPool(ETHPeer, chaindb, RopstenChain.network_id, privkey, min_peers) asyncio.ensure_future(peer_pool.run()) if args.fast: syncer = FastChainSyncer(chaindb, peer_pool) else: chain = FakeAsyncRopstenChain(chaindb) syncer = RegularChainSyncer(chain, chaindb, peer_pool) syncer.min_peers_to_sync = 1 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() -> None: import argparse import signal from p2p import ecies from p2p.kademlia import Node from p2p.peer import DEFAULT_PREFERRED_NODES from eth.chains.ropsten import RopstenChain, ROPSTEN_GENESIS_HEADER, ROPSTEN_VM_CONFIGURATION from eth.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import ( FakeAsyncChainDB, FakeAsyncRopstenChain, FakeAsyncHeaderDB, connect_to_peers_loop) parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-fast', action="store_true") parser.add_argument('-light', action="store_true") parser.add_argument('-enode', type=str, required=False, help="The enode we should connect to") parser.add_argument('-debug', action="store_true") args = parser.parse_args() logging.basicConfig( level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%H:%M:%S') log_level = logging.INFO if args.debug: log_level = logging.DEBUG loop = asyncio.get_event_loop() base_db = LevelDB(args.db) chaindb = FakeAsyncChainDB(base_db) chaindb.persist_header(ROPSTEN_GENESIS_HEADER) headerdb = FakeAsyncHeaderDB(base_db) peer_class: Type[HeaderRequestingPeer] = ETHPeer if args.light: peer_class = LESPeer network_id = RopstenChain.network_id privkey = ecies.generate_privkey() peer_pool = PeerPool(peer_class, headerdb, network_id, privkey, ROPSTEN_VM_CONFIGURATION) if args.enode: nodes = tuple([Node.from_uri(args.enode)]) else: nodes = DEFAULT_PREFERRED_NODES[network_id] asyncio.ensure_future(peer_pool.run()) asyncio.ensure_future(connect_to_peers_loop(peer_pool, nodes)) chain = FakeAsyncRopstenChain(base_db) syncer: BaseHeaderChainSyncer = None if args.fast: syncer = FastChainSyncer(chain, chaindb, peer_pool) elif args.light: syncer = LightChainSyncer(chain, headerdb, peer_pool) else: syncer = RegularChainSyncer(chain, chaindb, peer_pool) syncer.logger.setLevel(log_level) syncer.min_peers_to_sync = 1 sigint_received = asyncio.Event() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, sigint_received.set) async def exit_on_sigint() -> None: await sigint_received.wait() await peer_pool.cancel() await syncer.cancel() loop.stop() async def run() -> None: await syncer.run() syncer.logger.info("run() finished, exiting") sigint_received.set() # loop.set_debug(True) asyncio.ensure_future(exit_on_sigint()) asyncio.ensure_future(run()) loop.run_forever() loop.close()
def _test() -> None: import argparse from pathlib import Path import signal from p2p import ecies from p2p.kademlia import Node from p2p.peer import DEFAULT_PREFERRED_NODES from eth.chains.ropsten import RopstenChain, ROPSTEN_GENESIS_HEADER, ROPSTEN_VM_CONFIGURATION from eth.chains.mainnet import MainnetChain, MAINNET_GENESIS_HEADER, MAINNET_VM_CONFIGURATION from eth.db.backends.level import LevelDB from tests.p2p.integration_test_helpers import (FakeAsyncChainDB, FakeAsyncMainnetChain, FakeAsyncRopstenChain, FakeAsyncHeaderDB, connect_to_peers_loop) from trinity.protocol.eth.peer import ETHPeer # noqa: F811 from trinity.protocol.les.peer import LESPeer # noqa: F811 from trinity.utils.chains import load_nodekey parser = argparse.ArgumentParser() parser.add_argument('-db', type=str, required=True) parser.add_argument('-fast', action="store_true") parser.add_argument('-light', action="store_true") parser.add_argument('-nodekey', type=str) parser.add_argument('-enode', type=str, required=False, help="The enode we should connect to") parser.add_argument('-debug', action="store_true") args = parser.parse_args() logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s: %(message)s', datefmt='%H:%M:%S') log_level = logging.INFO if args.debug: log_level = logging.DEBUG loop = asyncio.get_event_loop() base_db = LevelDB(args.db) headerdb = FakeAsyncHeaderDB(base_db) chaindb = FakeAsyncChainDB(base_db) try: genesis = chaindb.get_canonical_block_header_by_number(0) except HeaderNotFound: genesis = ROPSTEN_GENESIS_HEADER chaindb.persist_header(genesis) peer_class: Type[Union[ETHPeer, LESPeer]] = ETHPeer if args.light: peer_class = LESPeer if genesis.hash == ROPSTEN_GENESIS_HEADER.hash: network_id = RopstenChain.network_id vm_config = ROPSTEN_VM_CONFIGURATION # type: ignore chain_class = FakeAsyncRopstenChain elif genesis.hash == MAINNET_GENESIS_HEADER.hash: network_id = MainnetChain.network_id vm_config = MAINNET_VM_CONFIGURATION # type: ignore chain_class = FakeAsyncMainnetChain else: raise RuntimeError("Unknown genesis: %s", genesis) if args.nodekey: privkey = load_nodekey(Path(args.nodekey)) else: privkey = ecies.generate_privkey() peer_pool = PeerPool(peer_class, headerdb, network_id, privkey, vm_config) if args.enode: nodes = tuple([Node.from_uri(args.enode)]) else: nodes = DEFAULT_PREFERRED_NODES[network_id] asyncio.ensure_future(peer_pool.run()) asyncio.ensure_future(connect_to_peers_loop(peer_pool, nodes)) chain = chain_class(base_db) syncer: BaseHeaderChainSyncer = None if args.fast: syncer = FastChainSyncer(chain, chaindb, peer_pool) elif args.light: syncer = LightChainSyncer(chain, headerdb, peer_pool) else: syncer = RegularChainSyncer(chain, chaindb, peer_pool) syncer.logger.setLevel(log_level) syncer.min_peers_to_sync = 1 sigint_received = asyncio.Event() for sig in [signal.SIGINT, signal.SIGTERM]: loop.add_signal_handler(sig, sigint_received.set) async def exit_on_sigint() -> None: await sigint_received.wait() await peer_pool.cancel() await syncer.cancel() loop.stop() async def run() -> None: await syncer.run() syncer.logger.info("run() finished, exiting") sigint_received.set() # loop.set_debug(True) asyncio.ensure_future(exit_on_sigint()) asyncio.ensure_future(run()) loop.run_forever() loop.close()