async def setup_two_nodes(): """ Setup and teardown of two full nodes, with blockchains and separate DBs. """ # SETUP store_1 = await FullNodeStore.create("blockchain_test") store_2 = await FullNodeStore.create("blockchain_test_2") await store_1._clear_database() await store_2._clear_database() b_1: Blockchain = await Blockchain.create({}, test_constants) b_2: Blockchain = await Blockchain.create({}, test_constants) await store_1.add_block(FullBlock.from_bytes(test_constants["GENESIS_BLOCK"])) await store_2.add_block(FullBlock.from_bytes(test_constants["GENESIS_BLOCK"])) full_node_1 = FullNode(store_1, b_1) server_1 = ChiaServer(21234, full_node_1, NodeType.FULL_NODE) _ = await server_1.start_server("127.0.0.1", full_node_1._on_connect) full_node_1._set_server(server_1) full_node_2 = FullNode(store_2, b_2) server_2 = ChiaServer(21235, full_node_2, NodeType.FULL_NODE) full_node_2._set_server(server_2) yield (full_node_1, full_node_2, server_1, server_2) # TEARDOWN full_node_1._shutdown() full_node_2._shutdown() server_1.close_all() server_2.close_all() await server_1.await_closed() await server_2.await_closed() await store_1.close() await store_2.close()
async def test1(self): store = FullNodeStore("fndb_test") await store._clear_database() blocks = bt.get_consecutive_blocks(test_constants, 10, [], 10) b: Blockchain = Blockchain(test_constants) await store.add_block(blocks[0]) await b.initialize({}) for i in range(1, 9): assert (await b.receive_block(blocks[i] )) == ReceiveBlockResult.ADDED_TO_HEAD await store.add_block(blocks[i]) full_node_1 = FullNode(store, b) server_1 = ChiaServer(21234, full_node_1, NodeType.FULL_NODE) _ = await server_1.start_server("127.0.0.1", None) full_node_1._set_server(server_1) full_node_2 = FullNode(store, b) server_2 = ChiaServer(21235, full_node_2, NodeType.FULL_NODE) full_node_2._set_server(server_2) await server_2.start_client(PeerInfo("127.0.0.1", uint16(21234)), None) await asyncio.sleep(2) # Allow connections to get made num_unfinished_blocks = 1000 start_unf = time.time() for i in range(num_unfinished_blocks): msg = Message("unfinished_block", peer_protocol.UnfinishedBlock(blocks[9])) server_1.push_message( OutboundMessage(NodeType.FULL_NODE, msg, Delivery.BROADCAST)) # Send the whole block ast the end so we can detect when the node is done block_msg = Message("block", peer_protocol.Block(blocks[9])) server_1.push_message( OutboundMessage(NodeType.FULL_NODE, block_msg, Delivery.BROADCAST)) while time.time() - start_unf < 300: if max([h.height for h in b.get_current_tips()]) == 9: print( f"Time taken to process {num_unfinished_blocks} is {time.time() - start_unf}" ) server_1.close_all() server_2.close_all() await server_1.await_closed() await server_2.await_closed() return await asyncio.sleep(0.1) server_1.close_all() server_2.close_all() await server_1.await_closed() await server_2.await_closed() raise Exception("Took too long to process blocks")
async def main(): config = load_config_cli("config.yaml", "timelord") initialize_logging("Timelord %(name)-23s", config["logging"]) log = logging.getLogger(__name__) setproctitle("chia_timelord") timelord = Timelord(config) server = ChiaServer(config["port"], timelord, NodeType.TIMELORD) _ = await server.start_server(config["host"], None) def signal_received(): server.close_all() asyncio.create_task(timelord._shutdown()) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, signal_received) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, signal_received) full_node_peer = PeerInfo( timelord.config["full_node_peer"]["host"], timelord.config["full_node_peer"]["port"], ) await asyncio.sleep(1) # Prevents TCP simultaneous connect with full node await server.start_client(full_node_peer, None) async for msg in timelord._manage_discriminant_queue(): server.push_message(msg) await server.await_closed() log.info("Timelord fully closed.")
def __init__( self, root_path, api: Any, node_type: NodeType, advertised_port: int, service_name: str, server_listen_ports: List[int] = [], connect_peers: List[PeerInfo] = [], on_connect_callback: Optional[OutboundMessage] = None, rpc_start_callback_port: Optional[Tuple[Callable, int]] = None, start_callback: Optional[Callable] = None, stop_callback: Optional[Callable] = None, await_closed_callback: Optional[Callable] = None, periodic_introducer_poll: Optional[Tuple[PeerInfo, int, int]] = None, ): net_config = load_config(root_path, "config.yaml") 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 self._node_type = node_type proctitle_name = f"chia_{service_name}" setproctitle(proctitle_name) self._log = logging.getLogger(service_name) config = load_config_cli(root_path, "config.yaml", service_name) initialize_logging(f"{service_name:<30s}", config["logging"], root_path) self._rpc_start_callback_port = rpc_start_callback_port self._server = ChiaServer( config["port"], api, node_type, ping_interval, network_id, root_path, config, ) for _ in ["set_server", "_set_server"]: f = getattr(api, _, None) if f: f(self._server) self._connect_peers = connect_peers self._server_listen_ports = server_listen_ports self._api = api self._task = None self._is_stopping = False self._periodic_introducer_poll = periodic_introducer_poll self._on_connect_callback = on_connect_callback self._start_callback = start_callback self._stop_callback = stop_callback self._await_closed_callback = await_closed_callback
async def setup_introducer(port, dic={}): 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", "introducer") introducer = Introducer(config["max_peers_to_send"], config["recent_peer_threshold"]) assert ping_interval is not None assert network_id is not None server = ChiaServer( port, introducer, NodeType.INTRODUCER, ping_interval, network_id, bt.root_path, config, f"introducer_server_{port}", ) _ = await start_server(server) yield (introducer, server) _.close() server.close_all() await server.await_closed()
async def main(): timelord = Timelord() host, port = parse_host_port(timelord) server = ChiaServer(port, timelord, NodeType.TIMELORD) _ = await server.start_server(host, None) def signal_received(): server.close_all() asyncio.create_task(timelord._shutdown()) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, signal_received) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, signal_received) full_node_peer = PeerInfo( timelord.config["full_node_peer"]["host"], timelord.config["full_node_peer"]["port"], ) await asyncio.sleep(1) # Prevents TCP simultaneous connect with full node await server.start_client(full_node_peer, None) async for msg in timelord._manage_discriminant_queue(): server.push_message(msg) await server.await_closed()
async def main(): farmer = Farmer() harvester_peer = PeerInfo(farmer.config["harvester_peer"]["host"], farmer.config["harvester_peer"]["port"]) full_node_peer = PeerInfo(farmer.config["full_node_peer"]["host"], farmer.config["full_node_peer"]["port"]) host, port = parse_host_port(farmer) server = ChiaServer(port, farmer, NodeType.FARMER) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) async def on_connect(): # Sends a handshake to the harvester pool_sks: List[PrivateKey] = [ PrivateKey.from_bytes(bytes.fromhex(ce)) for ce in farmer.key_config["pool_sks"] ] msg = HarvesterHandshake([sk.get_public_key() for sk in pool_sks]) yield OutboundMessage(NodeType.HARVESTER, Message("harvester_handshake", msg), Delivery.BROADCAST) _ = await server.start_server(host, on_connect) await asyncio.sleep(1) # Prevents TCP simultaneous connect with harvester _ = await server.start_client(harvester_peer, None) _ = await server.start_client(full_node_peer, None) await server.await_closed()
async def setup_harvester(port, dic={}): config = load_config(root_path, "config.yaml", "harvester") harvester = await Harvester.create(config, bt.plot_config) net_config = load_config(root_path, "config.yaml") 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( port, harvester, NodeType.HARVESTER, ping_interval, network_id, root_path, config, f"harvester_server_{port}", ) yield (harvester, server) harvester._shutdown() server.close_all() await harvester._await_shutdown() await server.await_closed()
async def main(): root_path = DEFAULT_ROOT_PATH net_config = load_config(root_path, "config.yaml") config = load_config_cli(root_path, "config.yaml", "introducer") initialize_logging("Introducer %(name)-21s", config["logging"]) log = logging.getLogger(__name__) setproctitle("chia_introducer") introducer = Introducer(config) 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"], introducer, NodeType.INTRODUCER, ping_interval, network_id) introducer.set_server(server) _ = await server.start_server(None, config) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) await server.await_closed() log.info("Introducer fully closed.")
async def main(): config = load_config_cli("config.yaml", "harvester") try: key_config = load_config("keys.yaml") except FileNotFoundError: raise RuntimeError( "Keys not generated. Run python3 ./scripts/regenerate_keys.py.") try: plot_config = load_config("plots.yaml") except FileNotFoundError: raise RuntimeError( "Plots not generated. Run python3.7 ./scripts/create_plots.py.") initialize_logging("Harvester %(name)-22s", config["logging"]) log = logging.getLogger(__name__) setproctitle("chia_harvester") harvester = Harvester(config, key_config, plot_config) server = ChiaServer(config["port"], harvester, NodeType.HARVESTER) _ = await server.start_server(config["port"], None) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) peer_info = PeerInfo(harvester.config["farmer_peer"]["host"], harvester.config["farmer_peer"]["port"]) _ = await server.start_client(peer_info, None) await server.await_closed() harvester._shutdown() await harvester._await_shutdown() log.info("Harvester fully closed.")
async def setup_farmer(port, dic={}): config = load_config(root_path, "config.yaml", "farmer") pool_sk = bt.pool_sk pool_target = create_puzzlehash_for_pk( BLSPublicKey(bytes(pool_sk.get_public_key()))) wallet_sk = bt.wallet_sk wallet_target = create_puzzlehash_for_pk( BLSPublicKey(bytes(wallet_sk.get_public_key()))) key_config = { "wallet_sk": bytes(wallet_sk).hex(), "wallet_target": wallet_target.hex(), "pool_sks": [bytes(pool_sk).hex()], "pool_target": pool_target.hex(), } test_constants_copy = test_constants.copy() for k in dic.keys(): test_constants_copy[k] = dic[k] net_config = load_config(root_path, "config.yaml") ping_interval = net_config.get("ping_interval") network_id = net_config.get("network_id") farmer = Farmer(config, key_config, test_constants_copy) assert ping_interval is not None assert network_id is not None server = ChiaServer(port, farmer, NodeType.FARMER, ping_interval, network_id) _ = await server.start_server(farmer._on_connect) yield (farmer, server) server.close_all() await server.await_closed()
async def main(): root_path = DEFAULT_ROOT_PATH net_config = load_config(root_path, "config.yaml") config = load_config_cli(root_path, "config.yaml", "harvester") try: plot_config = load_config(root_path, "plots.yaml") except FileNotFoundError: raise RuntimeError("Plots not generated. Run chia-create-plots") initialize_logging("Harvester %(name)-22s", config["logging"]) log = logging.getLogger(__name__) setproctitle("chia_harvester") harvester = Harvester(config, plot_config) 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"], harvester, NodeType.HARVESTER, ping_interval, network_id) _ = await server.start_server(None, config) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) peer_info = PeerInfo(harvester.config["farmer_peer"]["host"], harvester.config["farmer_peer"]["port"]) _ = await server.start_client(peer_info, None, config) await server.await_closed() harvester._shutdown() await harvester._await_shutdown() log.info("Harvester fully closed.")
async def setup_introducer(port, dic={}): 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", "introducer") introducer = Introducer(config) assert ping_interval is not None assert network_id is not None server = ChiaServer( port, introducer, NodeType.INTRODUCER, ping_interval, network_id, bt.root_path, config, ) _ = await server.start_server(None) yield (introducer, server) server.close_all() await server.await_closed()
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 async_main(): root_path = DEFAULT_ROOT_PATH net_config = load_config(root_path, "config.yaml") config = load_config_cli(root_path, "config.yaml", "farmer") # TOD: Remove once we have pool server config_pool = load_config_cli(root_path, "config.yaml", "pool") initialize_logging("Farmer %(name)-25s", config["logging"], root_path) log = logging.getLogger(__name__) setproctitle("chia_farmer") keychain = Keychain() farmer = Farmer(config, config_pool, keychain) 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"], farmer, NodeType.FARMER, ping_interval, network_id, root_path, config, ) try: asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) except NotImplementedError: log.info("signal handlers unsupported") _ = await server.start_server(farmer._on_connect) farmer.set_server(server) rpc_cleanup = None if config["start_rpc_server"]: # Starts the RPC server rpc_cleanup = await start_farmer_rpc_server(farmer, server.close_all, config["rpc_port"]) await asyncio.sleep(10) # Allows full node to startup farmer._start_bg_tasks() await server.await_closed() farmer._shut_down = True # Waits for the rpc server to close if rpc_cleanup is not None: await rpc_cleanup() log.info("Closed RPC server.") log.info("Farmer fully closed.")
async def async_main(): root_path = DEFAULT_ROOT_PATH net_config = load_config(root_path, "config.yaml") config = load_config_cli(root_path, "config.yaml", "timelord") initialize_logging("Timelord %(name)-23s", config["logging"], root_path) log = logging.getLogger(__name__) setproctitle("chia_timelord") timelord = Timelord(config, constants) 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"], timelord, NodeType.TIMELORD, ping_interval, network_id, DEFAULT_ROOT_PATH, config, ) timelord.set_server(server) coro = asyncio.start_server( timelord._handle_client, config["vdf_server"]["host"], config["vdf_server"]["port"], loop=asyncio.get_running_loop(), ) def stop_all(): server.close_all() timelord._shutdown() try: asyncio.get_running_loop().add_signal_handler(signal.SIGINT, stop_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, stop_all) except NotImplementedError: log.info("signal handlers unsupported") await asyncio.sleep(10) # Allows full node to startup peer_info = PeerInfo(config["full_node_peer"]["host"], config["full_node_peer"]["port"]) bg_task = start_timelord_bg_task(server, peer_info, log) vdf_server = asyncio.ensure_future(coro) await timelord._manage_discriminant_queue() log.info("Closed discriminant queue.") log.info("Shutdown timelord.") await server.await_closed() vdf_server.cancel() bg_task.cancel() log.info("Timelord fully closed.")
async def test2(self): num_blocks = 100 store = FullNodeStore("fndb_test") await store._clear_database() blocks = bt.get_consecutive_blocks(test_constants, num_blocks, [], 10) b: Blockchain = Blockchain(test_constants) await store.add_block(blocks[0]) await b.initialize({}) full_node_1 = FullNode(store, b) server_1 = ChiaServer(21236, full_node_1, NodeType.FULL_NODE) _ = await server_1.start_server("127.0.0.1", None) full_node_1._set_server(server_1) full_node_2 = FullNode(store, b) server_2 = ChiaServer(21237, full_node_2, NodeType.FULL_NODE) full_node_2._set_server(server_2) await server_2.start_client(PeerInfo("127.0.0.1", uint16(21236)), None) await asyncio.sleep(2) # Allow connections to get made start_unf = time.time() for i in range(1, num_blocks): msg = Message("block", peer_protocol.Block(blocks[i])) server_1.push_message( OutboundMessage(NodeType.FULL_NODE, msg, Delivery.BROADCAST)) while time.time() - start_unf < 300: if max([h.height for h in b.get_current_tips()]) == num_blocks - 1: print( f"Time taken to process {num_blocks} is {time.time() - start_unf}" ) server_1.close_all() server_2.close_all() await server_1.await_closed() await server_2.await_closed() return await asyncio.sleep(0.1) server_1.close_all() server_2.close_all() await server_1.await_closed() await server_2.await_closed() raise Exception("Took too long to process blocks")
async def main(): root_path = DEFAULT_ROOT_PATH net_config = load_config(root_path, "config.yaml") config = load_config_cli(root_path, "config.yaml", "timelord") initialize_logging("Timelord %(name)-23s", config["logging"]) log = logging.getLogger(__name__) setproctitle("chia_timelord") timelord = Timelord(config, constants) 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"], timelord, NodeType.TIMELORD, ping_interval, network_id) _ = await server.start_server(None, config) timelord_shutdown_task: Optional[asyncio.Task] = None coro = asyncio.start_server( timelord._handle_client, config["vdf_server"]["host"], config["vdf_server"]["port"], loop=asyncio.get_running_loop(), ) def signal_received(): nonlocal timelord_shutdown_task server.close_all() timelord_shutdown_task = asyncio.create_task(timelord._shutdown()) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, signal_received) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, signal_received) full_node_peer = PeerInfo( timelord.config["full_node_peer"]["host"], timelord.config["full_node_peer"]["port"], ) await asyncio.sleep(1) # Prevents TCP simultaneous connect with full node await server.start_client(full_node_peer, None, config) vdf_server = asyncio.ensure_future(coro) async for msg in timelord._manage_discriminant_queue(): server.push_message(msg) log.info("Closed discriminant queue.") if timelord_shutdown_task is not None: await timelord_shutdown_task log.info("Shutdown timelord.") await server.await_closed() vdf_server.cancel() log.info("Timelord fully closed.")
async def async_main(): root_path = DEFAULT_ROOT_PATH net_config = load_config(root_path, "config.yaml") config = load_config_cli(root_path, "config.yaml", "harvester") try: plot_config = load_config(root_path, "plots.yaml") except FileNotFoundError: raise RuntimeError("Plots not generated. Run chia-create-plots") initialize_logging("Harvester %(name)-22s", config["logging"], root_path) log = logging.getLogger(__name__) setproctitle("chia_harvester") harvester = await Harvester.create(config, plot_config, root_path) 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"], harvester, NodeType.HARVESTER, ping_interval, network_id, DEFAULT_ROOT_PATH, config, ) try: asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) except NotImplementedError: log.info("signal handlers unsupported") rpc_cleanup = None if config["start_rpc_server"]: # Starts the RPC server rpc_cleanup = await start_harvester_rpc_server( harvester, server.close_all, config["rpc_port"] ) harvester.set_server(server) await asyncio.sleep(1) harvester._start_bg_tasks() await server.await_closed() harvester._shutdown() await harvester._await_shutdown() # Waits for the rpc server to close if rpc_cleanup is not None: await rpc_cleanup() log.info("Closed RPC server.") log.info("Harvester fully closed.")
async def setup_full_node_simulator(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 = root_path / f"{db_name}" if db_path.exists(): db_path.unlink() 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") config["database_path"] = str(db_path) if introducer_port is not None: config["introducer_peer"]["host"] = "127.0.0.1" config["introducer_peer"]["port"] = introducer_port full_node_1 = await FullNodeSimulator.create( config=config, name=f"full_node_{port}", root_path=root_path, override_constants=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, bt.root_path, config, "full-node-simulator-server", ) _ = await start_server(server_1, full_node_1._on_connect) full_node_1._set_server(server_1) yield (full_node_1, server_1) # TEARDOWN _.close() server_1.close_all() full_node_1._close() await server_1.await_closed() await full_node_1._await_closed() db_path.unlink()
async def main(): introducer = Introducer() host, port = parse_host_port(introducer) server = ChiaServer(port, introducer, NodeType.INTRODUCER) introducer.set_server(server) _ = await server.start_server(host, None) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) await server.await_closed()
async def setup_wallet_node(port, introducer_port=None, key_seed=b"setup_wallet_node", dic={}): config = load_config(root_path, "config.yaml", "wallet") if "starting_height" in dic: config["starting_height"] = dic["starting_height"] keychain = Keychain(key_seed.hex(), True) keychain.add_private_key_seed(key_seed) private_key = keychain.get_all_private_keys()[0][0] test_constants_copy = test_constants.copy() for k in dic.keys(): test_constants_copy[k] = dic[k] db_path = root_path / f"test-wallet-db-{port}.db" if db_path.exists(): db_path.unlink() config["database_path"] = str(db_path) net_config = load_config(root_path, "config.yaml") ping_interval = net_config.get("ping_interval") network_id = net_config.get("network_id") wallet = await WalletNode.create( config, private_key, root_path, override_constants=test_constants_copy, name="wallet1", ) assert ping_interval is not None assert network_id is not None server = ChiaServer( port, wallet, NodeType.WALLET, ping_interval, network_id, root_path, config, "wallet-server", ) wallet.set_server(server) yield (wallet, server) server.close_all() await wallet.wallet_state_manager.clear_all_stores() await wallet.wallet_state_manager.close_all_stores() wallet.wallet_state_manager.unlink_db() await server.await_closed()
async def setup_timelord(port, dic={}): config = load_config(root_path, "config.yaml", "timelord") test_constants_copy = test_constants.copy() for k in dic.keys(): test_constants_copy[k] = dic[k] timelord = Timelord(config, test_constants_copy) net_config = load_config(root_path, "config.yaml") 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( port, timelord, NodeType.TIMELORD, ping_interval, network_id, bt.root_path, config, f"timelord_server_{port}", ) coro = asyncio.start_server( timelord._handle_client, config["vdf_server"]["host"], config["vdf_server"]["port"], loop=asyncio.get_running_loop(), ) vdf_server = asyncio.ensure_future(coro) timelord.set_server(server) timelord._start_bg_tasks() async def run_timelord(): async for msg in timelord._manage_discriminant_queue(): server.push_message(msg) timelord_task = asyncio.create_task(run_timelord()) yield (timelord, server) vdf_server.cancel() server.close_all() await timelord._shutdown() await timelord_task await server.await_closed()
async def async_main(): root_path = DEFAULT_ROOT_PATH net_config = load_config(root_path, "config.yaml") config = load_config_cli(root_path, "config.yaml", "farmer") try: check_keys(root_path) key_config = load_config(root_path, "keys.yaml") except FileNotFoundError: raise RuntimeError("Keys not generated. Run `chia generate keys`") initialize_logging("Farmer %(name)-25s", config["logging"], root_path) log = logging.getLogger(__name__) setproctitle("chia_farmer") farmer = Farmer(config, key_config) 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"], farmer, NodeType.FARMER, ping_interval, network_id, root_path, config, ) try: asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) except NotImplementedError: log.info("signal handlers unsupported") _ = await server.start_server(farmer._on_connect) farmer.set_server(server) await asyncio.sleep(10) # Allows full node to startup farmer._start_bg_tasks() await server.await_closed() farmer._shut_down = True log.info("Farmer fully closed.")
async def main(): harvester = Harvester() host, port = parse_host_port(harvester) server = ChiaServer(port, harvester, NodeType.HARVESTER) _ = await server.start_server(host, None) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) peer_info = PeerInfo(harvester.config["farmer_peer"]["host"], harvester.config["farmer_peer"]["port"]) _ = await server.start_client(peer_info, None) await server.await_closed() harvester._shutdown() await harvester._await_shutdown()
async def main(): config = load_config_cli("config.yaml", "introducer") initialize_logging("Introducer %(name)-21s", config["logging"]) log = logging.getLogger(__name__) setproctitle("chia_introducer") introducer = Introducer(config) server = ChiaServer(config["port"], introducer, NodeType.INTRODUCER) introducer.set_server(server) _ = await server.start_server(config["host"], None) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) await server.await_closed() log.info("Introducer fully closed.")
async def main(): config = load_config_cli("config.yaml", "farmer") try: key_config = load_config("keys.yaml") except FileNotFoundError: raise RuntimeError( "Keys not generated. Run python3 ./scripts/regenerate_keys.py.") initialize_logging("Farmer %(name)-25s", config["logging"]) log = logging.getLogger(__name__) setproctitle("chia_farmer") farmer = Farmer(config, key_config) harvester_peer = PeerInfo(config["harvester_peer"]["host"], config["harvester_peer"]["port"]) full_node_peer = PeerInfo(config["full_node_peer"]["host"], config["full_node_peer"]["port"]) server = ChiaServer(config["port"], farmer, NodeType.FARMER) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) async def on_connect(): # Sends a handshake to the harvester pool_sks: List[PrivateKey] = [ PrivateKey.from_bytes(bytes.fromhex(ce)) for ce in key_config["pool_sks"] ] msg = HarvesterHandshake([sk.get_public_key() for sk in pool_sks]) yield OutboundMessage(NodeType.HARVESTER, Message("harvester_handshake", msg), Delivery.BROADCAST) _ = await server.start_server(config["host"], on_connect) await asyncio.sleep(1) # Prevents TCP simultaneous connect with harvester _ = await server.start_client(harvester_peer, None) _ = await server.start_client(full_node_peer, None) await server.await_closed() log.info("Farmer fully closed.")
async def main(): root_path = DEFAULT_ROOT_PATH net_config = load_config(root_path, "config.yaml") config = load_config_cli(root_path, "config.yaml", "farmer") try: key_config = load_config(root_path, "keys.yaml") except FileNotFoundError: raise RuntimeError("Keys not generated. Run chia-generate-keys") initialize_logging("Farmer %(name)-25s", config["logging"]) log = logging.getLogger(__name__) setproctitle("chia_farmer") farmer = Farmer(config, key_config) harvester_peer = PeerInfo(config["harvester_peer"]["host"], config["harvester_peer"]["port"]) full_node_peer = PeerInfo(config["full_node_peer"]["host"], config["full_node_peer"]["port"]) 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"], farmer, NodeType.FARMER, ping_interval, network_id) asyncio.get_running_loop().add_signal_handler(signal.SIGINT, server.close_all) asyncio.get_running_loop().add_signal_handler(signal.SIGTERM, server.close_all) _ = await server.start_server(farmer._on_connect, config) await asyncio.sleep(2) # Prevents TCP simultaneous connect with harvester _ = await server.start_client(harvester_peer, None, config) _ = await server.start_client(full_node_peer, None, config) farmer.set_server(server) farmer._start_bg_tasks() await server.await_closed() farmer._shut_down = True log.info("Farmer fully closed.")
async def setup_farmer(port, dic={}): print("root path", root_path) config = load_config(root_path, "config.yaml", "farmer") config_pool = load_config(root_path, "config.yaml", "pool") test_constants_copy = test_constants.copy() for k in dic.keys(): test_constants_copy[k] = dic[k] net_config = load_config(root_path, "config.yaml") ping_interval = net_config.get("ping_interval") network_id = net_config.get("network_id") config["xch_target_puzzle_hash"] = bt.fee_target.hex() config["pool_public_keys"] = [ bytes(epk.get_public_key()).hex() for epk in bt.keychain.get_all_public_keys() ] config_pool["xch_target_puzzle_hash"] = bt.fee_target.hex() farmer = Farmer(config, config_pool, bt.keychain, test_constants_copy) assert ping_interval is not None assert network_id is not None server = ChiaServer( port, farmer, NodeType.FARMER, ping_interval, network_id, root_path, config, f"farmer_server_{port}", ) farmer.set_server(server) _ = await start_server(server, farmer._on_connect) yield (farmer, server) _.close() server.close_all() await server.await_closed()
async def setup_wallet_node(port, introducer_port=None, key_seed=b"", dic={}): config = load_config(root_path, "config.yaml", "wallet") if "starting_height" in dic: config["starting_height"] = dic["starting_height"] key_config = { "wallet_sk": bytes(blspy.ExtendedPrivateKey.from_seed(key_seed)).hex(), } test_constants_copy = test_constants.copy() for k in dic.keys(): test_constants_copy[k] = dic[k] db_path = root_path / ("test-wallet-db%s.db" % token_bytes(32).hex()) if db_path.exists(): db_path.unlink() config["database_path"] = str(db_path) net_config = load_config(root_path, "config.yaml") ping_interval = net_config.get("ping_interval") network_id = net_config.get("network_id") wallet = await WalletNode.create( config, key_config, override_constants=test_constants_copy, name="wallet1", ) assert ping_interval is not None assert network_id is not None server = ChiaServer(port, wallet, NodeType.WALLET, ping_interval, network_id, "wallet-server") wallet.set_server(server) yield (wallet, server) server.close_all() await wallet.wallet_state_manager.clear_all_stores() await wallet.wallet_state_manager.close_all_stores() wallet.wallet_state_manager.unlink_db() await server.await_closed()