Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: clean up code and handle conditions for parachain test/main #176

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 42 additions & 6 deletions main.star
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down
34 changes: 13 additions & 21 deletions package_io/utils.star
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"]
Expand Down Expand Up @@ -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()
Expand Down
7 changes: 0 additions & 7 deletions parachain/build-spec.star
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
45 changes: 40 additions & 5 deletions parachain/node_setup.star
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand Down
21 changes: 13 additions & 8 deletions parachain/parachain.star
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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.
Expand Down
33 changes: 23 additions & 10 deletions relaychain/relay-chain.star
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -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 = {}
Expand Down Expand Up @@ -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.
Expand Down