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()
async def tx_in_pool(mempool: MempoolManager, tx_id: bytes32): tx = mempool.get_spendbundle(tx_id) if tx is None: return False return True
async def main(): root_path = DEFAULT_ROOT_PATH net_config = load_config(root_path, "config.yaml") config = load_config_cli(root_path, "config.yaml", "full_node") 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(DEFAULT_ROOT_PATH, config["simulator_database_path"]) mkdir(db_path.parent) connection = await aiosqlite.connect(db_path) # Create the store (DB) and full node instance store = await FullNodeStore.create(connection) await store._clear_database() genesis: FullBlock = FullBlock.from_bytes(test_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, test_constants) mempool_manager = MempoolManager(unspent_store, test_constants) await mempool_manager.new_tips(await blockchain.get_full_tips()) full_node = FullNodeSimulator( store, blockchain, config, mempool_manager, unspent_store, override_constants=test_constants, ) ping_interval = net_config.get("ping_interval") network_id = net_config.get("network_id") # Starts the full node server (which full nodes can connect to) 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") log.info("Waiting to connect to some peers...") await asyncio.sleep(3) log.info( f"Connected to {len(server.global_connections.get_connections())} peers." ) # 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 store.close() log.info("Closed store.") await unspent_store.close() log.info("Closed unspent store.") await asyncio.get_running_loop().shutdown_asyncgens() log.info("Node fully closed.")
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.")