def service_kwargs_for_full_node_simulator( root_path: Path, config: Dict, consensus_constants: ConsensusConstants, bt: BlockTools, ) -> Dict: mkdir(path_from_root(root_path, config["database_path"]).parent) genesis_challenge = bytes32( bytes.fromhex( config["network_genesis_challenges"][config["selected_network"]])) node = FullNode( config, root_path=root_path, consensus_constants=consensus_constants, name=SERVICE_NAME, ) peer_api = FullNodeSimulator(node, bt) kwargs = dict( root_path=root_path, node=node, peer_api=peer_api, node_type=NodeType.FULL_NODE, advertised_port=config["port"], service_name=SERVICE_NAME, server_listen_ports=[config["port"]], on_connect_callback=node.on_connect, rpc_info=(FullNodeRpcApi, config["rpc_port"]), network_id=genesis_challenge, ) return kwargs
def service_kwargs_for_full_node( root_path: pathlib.Path, config: Dict, consensus_constants: ConsensusConstants) -> Dict: full_node = FullNode( config, root_path=root_path, consensus_constants=consensus_constants, ) api = FullNodeAPI(full_node) upnp_list = [] if config["enable_upnp"]: upnp_list = [config["port"]] kwargs = dict( root_path=root_path, node=api.full_node, peer_api=api, node_type=NodeType.FULL_NODE, advertised_port=config["port"], service_name=SERVICE_NAME, upnp_ports=upnp_list, server_listen_ports=[config["port"]], on_connect_callback=full_node.on_connect, network_id=consensus_constants.GENESIS_CHALLENGE, ) if config["start_rpc_server"]: kwargs["rpc_info"] = (FullNodeRpcApi, config["rpc_port"]) return kwargs
def service_kwargs_for_full_node_simulator(root_path: Path, config: Dict, bt: BlockTools) -> Dict: mkdir(path_from_root(root_path, config["database_path"]).parent) constants = bt.constants node = FullNode( config, root_path=root_path, consensus_constants=constants, name=SERVICE_NAME, ) peer_api = FullNodeSimulator(node, bt) kwargs = dict( root_path=root_path, node=node, peer_api=peer_api, node_type=NodeType.FULL_NODE, advertised_port=config["port"], service_name=SERVICE_NAME, server_listen_ports=[config["port"]], on_connect_callback=node.on_connect, rpc_info=(FullNodeRpcApi, config["rpc_port"]), network_id=constants.GENESIS_CHALLENGE, ) return kwargs
def service_kwargs_for_full_node(root_path): service_name = "full_node" config = load_config_cli(root_path, "config.yaml", service_name) api = FullNode(config, root_path=root_path, consensus_constants=constants) async def start_callback(): if config["enable_upnp"]: upnp_remap_port(config["port"]) await api._start() def stop_callback(): api._close() async def await_closed_callback(): await api._await_closed() kwargs = dict( root_path=root_path, api=api, node_type=NodeType.FULL_NODE, advertised_port=config["port"], service_name=service_name, server_listen_ports=[config["port"]], on_connect_callback=api._on_connect, start_callback=start_callback, stop_callback=stop_callback, await_closed_callback=await_closed_callback, ) if config["start_rpc_server"]: kwargs["rpc_info"] = (FullNodeRpcApi, config["rpc_port"]) return kwargs
async def setup_full_node(db_name, port, introducer_port=None, dic={}): # SETUP test_constants_copy = test_constants.copy() for k in dic.keys(): test_constants_copy[k] = dic[k] db_path = Path(db_name) connection = await aiosqlite.connect(db_path) store_1 = await FullNodeStore.create(connection) await store_1._clear_database() unspent_store_1 = await CoinStore.create(connection) await unspent_store_1._clear_database() mempool_1 = MempoolManager(unspent_store_1, test_constants_copy) b_1: Blockchain = await Blockchain.create(unspent_store_1, store_1, test_constants_copy) await mempool_1.new_tips(await b_1.get_full_tips()) await store_1.add_block( FullBlock.from_bytes(test_constants_copy["GENESIS_BLOCK"])) net_config = load_config(root_path, "config.yaml") ping_interval = net_config.get("ping_interval") network_id = net_config.get("network_id") config = load_config(root_path, "config.yaml", "full_node") if introducer_port is not None: config["introducer_peer"]["host"] = "127.0.0.1" config["introducer_peer"]["port"] = introducer_port full_node_1 = FullNode( store_1, b_1, config, mempool_1, unspent_store_1, f"full_node_{port}", test_constants_copy, ) assert ping_interval is not None assert network_id is not None server_1 = ChiaServer( port, full_node_1, NodeType.FULL_NODE, ping_interval, network_id, root_path, config, ) _ = await server_1.start_server(full_node_1._on_connect) full_node_1._set_server(server_1) yield (full_node_1, server_1) # TEARDOWN full_node_1._shutdown() server_1.close_all() await connection.close() Path(db_name).unlink()
def service_kwargs_for_full_node( root_path: pathlib.Path, config: Dict, consensus_constants: ConsensusConstants ) -> Dict: api = FullNode(config, root_path=root_path, consensus_constants=consensus_constants) kwargs = dict( root_path=root_path, api=api, node_type=NodeType.FULL_NODE, advertised_port=config["port"], service_name=SERVICE_NAME, upnp_ports=[config["port"]], server_listen_ports=[config["port"]], on_connect_callback=api._on_connect, ) if config["start_rpc_server"]: kwargs["rpc_info"] = (FullNodeRpcApi, config["rpc_port"]) return kwargs
def service_kwargs_for_full_node(root_path): service_name = "full_node" config = load_config_cli(root_path, "config.yaml", service_name) api = FullNode(config, root_path=root_path) introducer = config["introducer_peer"] peer_info = PeerInfo(introducer["host"], introducer["port"]) async def start_callback(): await api.start() if config["enable_upnp"]: upnp_remap_port(config["port"]) def stop_callback(): api._close() async def await_closed_callback(): await api._await_closed() kwargs = dict( root_path=root_path, api=api, node_type=NodeType.FULL_NODE, advertised_port=config["port"], service_name=service_name, server_listen_ports=[config["port"]], on_connect_callback=api._on_connect, start_callback=start_callback, stop_callback=stop_callback, await_closed_callback=await_closed_callback, rpc_start_callback_port=(start_full_node_rpc_server, config["rpc_port"]), periodic_introducer_poll=( peer_info, config["introducer_connect_interval"], config["target_peer_count"], ), ) return kwargs
async def setup_full_node( consensus_constants: ConsensusConstants, db_name, port, introducer_port=None, simulator=False, send_uncompact_interval=30, ): db_path = bt.root_path / f"{db_name}" if db_path.exists(): db_path.unlink() config = load_config(bt.root_path, "config.yaml", "full_node") config["database_path"] = db_name config["send_uncompact_interval"] = send_uncompact_interval periodic_introducer_poll = None if introducer_port is not None: periodic_introducer_poll = ( PeerInfo(self_hostname, introducer_port), 30, config["target_peer_count"], ) if not simulator: api: FullNode = FullNode( config=config, root_path=bt.root_path, consensus_constants=consensus_constants, name=f"full_node_{port}", ) else: api = FullNodeSimulator( config=config, root_path=bt.root_path, consensus_constants=consensus_constants, name=f"full_node_sim_{port}", bt=bt, ) started = asyncio.Event() async def start_callback(): await api._start() nonlocal started started.set() def stop_callback(): api._close() async def await_closed_callback(): await api._await_closed() service = Service( root_path=bt.root_path, api=api, node_type=NodeType.FULL_NODE, advertised_port=port, service_name="full_node", server_listen_ports=[port], auth_connect_peers=False, on_connect_callback=api._on_connect, start_callback=start_callback, stop_callback=stop_callback, await_closed_callback=await_closed_callback, periodic_introducer_poll=periodic_introducer_poll, parse_cli_args=False, ) run_task = asyncio.create_task(service.run()) await started.wait() yield api, api.server service.stop() await run_task if db_path.exists(): db_path.unlink()
async def async_main(): root_path = DEFAULT_ROOT_PATH config = load_config_cli(root_path, "config.yaml", "full_node") net_config = load_config(root_path, "config.yaml") setproctitle("chia_full_node") initialize_logging("FullNode %(name)-23s", config["logging"], root_path) log = logging.getLogger(__name__) server_closed = False db_path = path_from_root(root_path, config["database_path"]) mkdir(db_path.parent) # Create the store (DB) and full node instance connection = await aiosqlite.connect(db_path) store = await FullNodeStore.create(connection) genesis: FullBlock = FullBlock.from_bytes(constants["GENESIS_BLOCK"]) await store.add_block(genesis) unspent_store = await CoinStore.create(connection) log.info("Initializing blockchain from disk") blockchain = await Blockchain.create(unspent_store, store) log.info("Blockchain initialized") mempool_manager = MempoolManager(unspent_store) await mempool_manager.new_tips(await blockchain.get_full_tips()) full_node = FullNode(store, blockchain, config, mempool_manager, unspent_store) if config["enable_upnp"]: log.info(f"Attempting to enable UPnP (open up port {config['port']})") try: upnp = miniupnpc.UPnP() upnp.discoverdelay = 5 upnp.discover() upnp.selectigd() upnp.addportmapping( config["port"], "TCP", upnp.lanaddr, config["port"], "chia", "" ) log.info(f"Port {config['port']} opened with UPnP.") except Exception: log.exception(f"UPnP failed") # Starts the full node server (which full nodes can connect to) ping_interval = net_config.get("ping_interval") network_id = net_config.get("network_id") assert ping_interval is not None assert network_id is not None server = ChiaServer( config["port"], full_node, NodeType.FULL_NODE, ping_interval, network_id, DEFAULT_ROOT_PATH, config, ) full_node._set_server(server) _ = await server.start_server(full_node._on_connect) rpc_cleanup = None def master_close_cb(): nonlocal server_closed if not server_closed: # Called by the UI, when node is closed, or when a signal is sent log.info("Closing all connections, and server...") full_node._shutdown() server.close_all() server_closed = True if config["start_rpc_server"]: # Starts the RPC server rpc_cleanup = await start_rpc_server( full_node, master_close_cb, config["rpc_port"] ) try: asyncio.get_running_loop().add_signal_handler(signal.SIGINT, master_close_cb) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, master_close_cb) except NotImplementedError: log.info("signal handlers unsupported") full_node._start_bg_tasks() # Awaits for server and all connections to close await server.await_closed() log.info("Closed all node servers.") # Waits for the rpc server to close if rpc_cleanup is not None: await rpc_cleanup() log.info("Closed RPC server.") await connection.close() log.info("Closed db connection.") await asyncio.get_running_loop().shutdown_asyncgens() log.info("Node fully closed.")