From 741f9a6a5609f45f70666b89ad17a30b011395ad Mon Sep 17 00:00:00 2001 From: riyasng12 Date: Thu, 11 Jan 2024 10:41:06 +0530 Subject: [PATCH] chore: clean up code and handle conditions for parachain test/main --- main.star | 48 ++++++++++++++++++++++++++++++++----- package_io/utils.star | 34 ++++++++++---------------- parachain/build-spec.star | 7 ------ parachain/node_setup.star | 45 ++++++++++++++++++++++++++++++---- parachain/parachain.star | 21 +++++++++------- relaychain/relay-chain.star | 33 +++++++++++++++++-------- 6 files changed, 131 insertions(+), 57 deletions(-) diff --git a/main.star b/main.star index 8eeb6f1..3c22ff4 100644 --- a/main.star +++ b/main.star @@ -5,14 +5,28 @@ promethues = import_module("./package_io/promethues.star") grafana = import_module("./package_io/grafana.star") explorer_js = import_module("./package_io/polkadot_js_app.star") utils = import_module("./package_io/utils.star") +constant = import_module("./package_io/constant.star") def run(plan, chain_type = "local", relaychain = None, parachains = None, explorer = False): """ Main function to run the Polkadot relay and parachain setup. - + Args: plan (object): The Kurtosis plan object for orchestrating the test. - args (dict): Dictionary containing arguments for configuring the setup. + chain_type (str): The type of chain (local, testnet or mainnet). Default is local. + relaychain (dict): A dict containing data for relay chain config. + - name (str): Name of relay chain. + - node (dict): A dict of node details. + - name (str): Name of node. + - node_type (str): Type of node. + - prometheus (bool): Boolean value to enable metrics for a given node. + parachains (list): A list containing data for para chain config. Each item in the list has the following: + - name (str): Name of para chain. + - node (dict): A dict of node details. + - name (str): Name of node. + - node_type (str): Type of node. + - prometheus (bool): Boolean value to enable metrics for a given node. + explorer (bool): A boolean value indicating whether to enable polkadot js explorer or not. Returns: dict: Service details containing information about relay chains, parachains, and Prometheus. @@ -27,7 +41,20 @@ def run_polkadot_setup(plan, chain_type, relaychain, parachains, explorer): Args: plan (object): The Kurtosis plan object for orchestrating the test. - args (dict): Dictionary containing arguments for configuring the setup. + chain_type (str): The type of chain (local, testnet or mainnet). Default is local. + relaychain (dict): A dict containing data for relay chain config. + - name (str): Name of relay chain. + - node (dict): A dict of node details. + - name (str): Name of node. + - node_type (str): Type of node. + - prometheus (bool): Boolean value to enable metrics for a given node. + parachains (list): A list containing data for para chain config. Each item in the list has the following: + - name (str): Name of para chain. + - node (dict): A dict of node details. + - name (str): Name of node. + - node_type (str): Type of node. + - prometheus (bool): Boolean value to enable metrics for a given node. + explorer (bool): A boolean value indicating whether to enable polkadot js explorer or not. Returns: dict: Service details containing information about relay chains, parachains, and Prometheus. @@ -40,7 +67,7 @@ def run_polkadot_setup(plan, chain_type, relaychain, parachains, explorer): service_details = {} if chain_type == "local": - relay_chain_details = relay_chain.start_relay_chains_local(plan, chain_type, relaychain["name"], relaychain["nodes"]) + relay_chain_details = relay_chain.start_relay_chains_local(plan, relaychain) polkadot_service_name = None for key in relay_chain_details: polkadot_service_name = key @@ -50,10 +77,19 @@ def run_polkadot_setup(plan, chain_type, relaychain, parachains, explorer): service_details.update(parachain_details) else: if len(relaychain) != 0: - relay_node_details = relay_chain.start_test_main_net_relay_nodes(plan, chain_type, relaychain["name"], relaychain["nodes"]) + relay_node_details = relay_chain.start_test_main_net_relay_nodes(plan, chain_type, relaychain) service_details.update(relay_node_details) for paras in parachains: - parachain_info = parachain.run_testnet_mainnet(plan, chain_type, relaychain["name"], paras) + if relaychain != {}: + relaychain_name = relaychain["name"] + elif chain_type == "testnet": + relaychain_name = "rococo" + elif paras["name"] in constant.POLKADOT_PARACHAINS: + relaychain_name = "polkadot" + elif paras["name"] in constant.KUSAMA_PARACHAINS: + relaychain_name = "kusama" + + parachain_info = parachain.run_testnet_mainnet(plan, chain_type, relaychain_name, paras) service_details.update(parachain_info) #run prometheus , if it returs some endpoint then grafana will up diff --git a/package_io/utils.star b/package_io/utils.star index 421e6e3..53a616d 100644 --- a/package_io/utils.star +++ b/package_io/utils.star @@ -14,13 +14,6 @@ def path_base(path): split_path = path.split("/") return split_path[-1] -def path_dir(path): - split_path = path.split("/") - if len(split_path) <= 1: - return "." - split_path = split_path[:-1] - return "/".join(split_path) or "/" - def new_port_spec( number, transport_protocol, @@ -40,22 +33,19 @@ def new_port_spec( wait = wait, ) -def read_file_from_service(plan, service_name, filename): - output = plan.exec( - service_name = service_name, - recipe = ExecRecipe( - command = ["/bin/sh", "-c", "cat {} | tr -d '\n'".format(filename)], - ), - ) - return output["output"] - - def get_service_url(protocol ,ip_address, ports): url = "{0}://{1}:{2}".format(protocol, ip_address, ports) return url def check_config_validity(plan, chain_type, relaychain, parachains): + + if chain_type != "local" and chain_type != "mainnet" and chain_type != "testnet": + return fail("Invalid chain type") + + if chain_type == "local" and relaychain == {}: + return fail("relay config must be present for localnet") + if relaychain != {}: chain_name = relaychain["name"] relay_nodes = relaychain["nodes"] @@ -108,10 +98,12 @@ def upload_files(plan): def convert_to_lowercase(chain_type, relaychain, parachains): chain_type = chain_type.lower() - relaychain["name"] = relaychain["name"].lower() - for node in relaychain["nodes"]: - node["name"] = node["name"].lower() - node["node_type"] = node["node_type"].lower() + if relaychain != {}: + relaychain["name"] = relaychain["name"].lower() + if len(relaychain["nodes"]) > 0: + for node in relaychain["nodes"]: + node["name"] = node["name"].lower() + node["node_type"] = node["node_type"].lower() for para in parachains: para["name"] = para["name"].lower() diff --git a/parachain/build-spec.star b/parachain/build-spec.star index 46f5473..5f5a679 100644 --- a/parachain/build-spec.star +++ b/parachain/build-spec.star @@ -45,13 +45,6 @@ def create_raw_build_spec_genisis_state_genisis_wasm(plan, binary, image, chain_ result = plan.exec(service_name = chain_name + "raw", recipe = command) plan.verify(result["code"], "==", 0) - # command = ExecRecipe(command = [ - # "/bin/sh", - # "-c", - # "cp /build/{0}.json /tmp/{0}.json".format(chain_name), - # ]) - # plan.exec(service_name = chain_name+"raw", recipe = command) - plan.store_service_files(service_name = chain_name + "raw", src = "/tmp/*", name = chain_name + "raw") plan.stop_service(chain_name + "raw") diff --git a/parachain/node_setup.star b/parachain/node_setup.star index b91511b..37797b2 100644 --- a/parachain/node_setup.star +++ b/parachain/node_setup.star @@ -1,5 +1,20 @@ def run_testnet_node_with_entrypoint(plan, prometheus, image, chain_name, execute_command, rpc_port = None, prometheus_port = None, lib2lib_port = None): - + """ + Spawn a parachain node with specified configuration with entrypoint. + + Args: + plan (object): The Kurtosis plan. + prometheus (bool): Boolean value to enable metrics for a given node. + image (str): Docker image for the parachain node. + chain_name (str): Name of the parachain. + execute_command (list): Command to execute inside service. + rpc_port (int, optional): The RPC port value. Defaults to None. + prometheus_port (int, optional): The Prometheus port value. Defaults to None. + lib2lib_port (int, optional): The lib2lib port value. Defaults to None. + + Returns: + dict: The service details of spawned parachain node. + """ ports = { "ws": PortSpec(9947, transport_protocol = "TCP"), "lib2lib": PortSpec(30333, transport_protocol = "TCP") @@ -33,7 +48,22 @@ def run_testnet_node_with_entrypoint(plan, prometheus, image, chain_name, execut return parachain def run_testnet_node_with_command(plan, prometheus, image, chain_name, execute_command, rpc_port = None, prometheus_port = None, lib2lib_port = None): - + """ + Spawn a parachain node with specified configuration with command. + + Args: + plan (object): The Kurtosis plan. + prometheus (bool): Boolean value to enable metrics for a given node. + image (str): Docker image for the parachain node. + chain_name (str): Name of the parachain. + execute_command (list): Command to execute inside service. + rpc_port (int, optional): The RPC port value. Defaults to None. + prometheus_port (int, optional): The Prometheus port value. Defaults to None. + lib2lib_port (int, optional): The lib2lib port value. Defaults to None. + + Returns: + dict: The service details of spawned parachain node. + """ ports = { "ws": PortSpec(9947, transport_protocol = "TCP"), "lib": PortSpec(30333) @@ -67,14 +97,19 @@ def run_testnet_node_with_command(plan, prometheus, image, chain_name, execute_c return parachain def spawn_parachain(plan, prometheus, image, chain_name, execute_command, build_file, rpc_port = None, prometheus_port = None, lib2lib_port = None): - """Spawn a parachain node with specified configuration. + """ + Spawn a parachain node with specified configuration. Args: plan (object): The Kurtosis plan. - chain_name (str): Name of the parachain. + prometheus (bool): Boolean value to enable metrics for a given node. image (str): Docker image for the parachain node. - command (list): Command to execute inside service. + chain_name (str): Name of the parachain. + execute_command (list): Command to execute inside service. build_file (str): Path to the build spec file. + rpc_port (int, optional): The RPC port value. Defaults to None. + prometheus_port (int, optional): The Prometheus port value. Defaults to None. + lib2lib_port (int, optional): The lib2lib port value. Defaults to None. Returns: dict: The service details of spawned parachain node. diff --git a/parachain/parachain.star b/parachain/parachain.star index 50c5702..6d56b10 100644 --- a/parachain/parachain.star +++ b/parachain/parachain.star @@ -6,12 +6,13 @@ node_setup = import_module("./node_setup.star") utils = import_module("../package_io/utils.star") def start_local_parachain_node(plan, chain_type, parachain, para_id): - """Start local parachain nodes based on configuration. + """ + Start local parachain nodes based on configuration. Args: plan (object): The Kurtosis plan. - args (dict): arguments for configuration. - parachain_config (dict): Configuration for the parachain. + chain_type (str): The type of chain (local, testnet or mainnet). + parachains (dict): A dict containing data for para chain config. para_id (int): Parachain ID. Returns: @@ -71,11 +72,13 @@ def start_local_parachain_node(plan, chain_type, parachain, para_id): return parachain_final def start_nodes(plan, chain_type, parachains, relay_chain_ip): - """Start multiple parachain nodes. + """ + Start multiple parachain nodes. Args: plan (object): The kurtosis plan. - args (dict): arguments for configuration. + chain_type (str): The type of chain (local, testnet or mainnet). + parachains (list): A list containing data for para chain config. relay_chain_ip (str): IP address of the relay chain. Returns: @@ -92,12 +95,14 @@ def start_nodes(plan, chain_type, parachains, relay_chain_ip): return final_parachain_details def run_testnet_mainnet(plan, chain_type, relaychain_name, parachain): - """Run a testnet or mainnet based on configuration. + """ + Run a testnet or mainnet based on configuration. Args: plan (object): The kurtosis plan. - parachain (dict): Configuration for the parachain. - args (dict): arguments for configuration. + chain_type (str): The type of chain (local, testnet or mainnet). + relaychain_name (str): The name of relay chain. + parachain (dict): A dict containing data for para chain config. Returns: list: List of dictionaries containing details of each parachain node. diff --git a/relaychain/relay-chain.star b/relaychain/relay-chain.star index 0d711ad..1a2d55b 100644 --- a/relaychain/relay-chain.star +++ b/relaychain/relay-chain.star @@ -1,17 +1,21 @@ utils = import_module("../package_io/utils.star") -def start_relay_chain(plan, chain_type, chain_name, relay_nodes): +def start_relay_chain(plan, chain_type, relaychain): """ Starts relay chain nodes based on the provided arguments. Args: plan (object): The Kurtosis plan object for orchestrating the test. - args (dict): Dictionary containing arguments for configuring the relay chain setup. - + chain_type (str): The type of chain (local, testnet or mainnet). + relaychain (dict): A dict containing data for relay chain config. + Returns: list: List of dictionaries containing service details of started relay chain nodes. """ + chain_name = relaychain["name"] + relay_nodes = relaychain["nodes"] + final_details = {} ports = { @@ -72,33 +76,37 @@ def start_relay_chain(plan, chain_type, chain_name, relay_nodes): return final_details -def start_test_main_net_relay_nodes(plan, chain_type, chain_name, relay_nodes): +def start_test_main_net_relay_nodes(plan, chain_type, relaychain): """ Starts testnet/mainnet relay nodes based on the provided arguments. Args: plan (object): The Kurtosis plan object for orchestrating the test. - args (dict): Dictionary containing arguments for configuring the relay node setup. + chain_type (str): The type of chain (local, testnet or mainnet). + relaychain (dict): A dict containing data for relay chain config. Returns: list: List of dictionaries containing service details of started relay nodes. """ - relay_node_details = start_relay_chain(plan, chain_type, chain_name, relay_nodes) + relay_node_details = start_relay_chain(plan, chain_type, relaychain) return relay_node_details -def start_relay_chains_local(plan, chain_type, chain_name, relay_nodes): +def start_relay_chains_local(plan, relaychain): """ Starts local relay chain nodes based on the provided arguments. Args: plan (object): The Kurtosis plan object for orchestrating the test. - args (dict): Dictionary containing arguments for configuring the relay chain setup. + relaychain (dict): A dict containing data for relay chain config. Returns: list: List of dictionaries containing sevice details of started relay chain nodes. """ + chain_name = relaychain["name"] + relay_nodes = relaychain["nodes"] + final_details = {} for node in relay_nodes: relay_detail = {} @@ -135,8 +143,13 @@ def start_relay_chain_local(plan, chain_name, node_name, prometheus, rpc_port = Starts a local relay chain node based on the provided arguments. Args: - plan (object): The Kurtosis plan - name (str): Name of the relay chain node. + plan (object): The Kurtosis plan object for orchestrating the test. + chain_name (str): Name of relay chain. + node_name (str): Name of node. + prometheus (bool): Boolean value to enable metrics for a given node. + rpc_port (int, optional): The RPC port value. Defaults to None. + prometheus_port (int, optional): The Prometheus port value. Defaults to None. + lib2lib_port (int, optional): The lib2lib port value. Defaults to None. Returns: object: Service details of the started relay chain node.