diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f09faf..38bd49a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - [#91](https://github.com/crypto-com/pystarport/pull/91) replace block with sync broadcast mode - [#93](https://github.com/crypto-com/pystarport/pull/93) add default gas prices for golang relayer - [#94](https://github.com/crypto-com/pystarport/pull/94) allow custom broadcast mode when create node +- [#95](https://github.com/crypto-com/pystarport/pull/95) make golang relayer optional *Feb 7, 2023* diff --git a/pystarport/cluster.py b/pystarport/cluster.py index dd65f17..2809166 100644 --- a/pystarport/cluster.py +++ b/pystarport/cluster.py @@ -9,6 +9,7 @@ import sys import threading import time +from enum import IntEnum from pathlib import Path from typing import List @@ -987,6 +988,10 @@ def relayer_chain_config_rly(data_dir, chain, relayer_chains_config): } +class Relayer(IntEnum): + (HERMES, RLY) = range(2) + + def init_cluster( data_dir, config_path, @@ -995,7 +1000,10 @@ def init_cluster( image=IMAGE, cmd=None, gen_compose_file=False, + relayer=Relayer.HERMES, ): + is_hermes = relayer == Relayer.HERMES + is_rly = relayer == Relayer.RLY extension = Path(config_path).suffix if extension == ".jsonnet": config = expand_jsonnet(config_path, dotenv) @@ -1020,83 +1028,85 @@ def init_cluster( ) if len(chains) > 1: cfg = relayer_config.pop("chains", {}) - # write relayer config for hermes - relayer_config_hermes = (data_dir / "relayer.toml") - relayer_config_hermes.write_text( - tomlkit.dumps( - jsonmerge.merge( + if is_hermes: + # write relayer config for hermes + relayer_config_hermes = (data_dir / "relayer.toml") + relayer_config_hermes.write_text( + tomlkit.dumps( + jsonmerge.merge( + { + "global": { + "log_level": "info", + }, + "chains": [ + relayer_chain_config_hermes(data_dir, c, cfg) + for c in chains + ], + }, + relayer_config, + ) + ) + ) + elif is_rly: + # write relayer config folder for rly + relayer_config_dir = (data_dir / "relayer/config") + relayer_config_dir.mkdir(parents=True, exist_ok=True) + relayer_config_rly = (relayer_config_dir / "config.yaml") + relayer_config_rly.write_text( + yaml.dump( { "global": { - "log_level": "info", + "api-listen-addr": ":5183", + "timeout": "10s", + "memo": "", + "light-cache-size": 20 }, - "chains": [ - relayer_chain_config_hermes(data_dir, c, cfg) + "chains": { + c["chain_id"]: relayer_chain_config_rly(data_dir, c, cfg) for c in chains - ], - }, - relayer_config, + }, + } ) ) - ) - # write relayer config folder for rly - relayer_config_rly = data_dir - for folder in ["relayer", "config"]: - relayer_config_rly = relayer_config_rly / folder - relayer_config_rly.mkdir() - relayer_config_rly = relayer_config_rly / "config.yaml" - relayer_config_rly.write_text( - yaml.dump( - { - "global": { - "api-listen-addr": ":5183", - "timeout": "10s", - "memo": "", - "light-cache-size": 20 - }, - "chains": { - c["chain_id"]: relayer_chain_config_rly(data_dir, c, cfg) - for c in chains - }, - } - ) - ) for chain in chains: - relayer = chain.get("key_name", "relayer") - mnemonic = find_account(data_dir, chain["chain_id"], relayer)["mnemonic"] + key_name = chain.get("key_name", "relayer") + mnemonic = find_account(data_dir, chain["chain_id"], key_name)["mnemonic"] mnemonic_path = Path(data_dir) / "relayer.env" mnemonic_path.write_text(mnemonic) - # restore the relayer account for hermes - subprocess.run( - [ - "hermes", - "--config", - relayer_config_hermes, - "keys", - "add", - "--chain", - chain["chain_id"], - "--mnemonic-file", - str(mnemonic_path), - "--overwrite", - "--hd-path", - "m/44'/" + str(chain.get("coin-type", 394)) + "'/0'/0/0", - ], - check=True, - ) - # restore the relayer account for rly - subprocess.run( - [ - "rly", - "keys", - "restore", - chain["chain_id"], - "relayer", - mnemonic, - "--home", - str(data_dir / "relayer"), - ], - check=True, - ) + if is_hermes: + # restore the relayer account for hermes + subprocess.run( + [ + "hermes", + "--config", + relayer_config_hermes, + "keys", + "add", + "--chain", + chain["chain_id"], + "--mnemonic-file", + str(mnemonic_path), + "--overwrite", + "--hd-path", + "m/44'/" + str(chain.get("coin-type", 394)) + "'/0'/0/0", + ], + check=True, + ) + elif is_rly: + # restore the relayer account for rly + subprocess.run( + [ + "rly", + "keys", + "restore", + chain["chain_id"], + "relayer", + mnemonic, + "--home", + str(data_dir / "relayer"), + ], + check=True, + ) def find_account(data_dir, chain_id, name):