From 5dbd84361cc24bcebfa47d5078ce2692590283a5 Mon Sep 17 00:00:00 2001 From: mmsqe Date: Mon, 24 Apr 2023 13:19:07 +0800 Subject: [PATCH] Problem: golang relayer is not supported (#86) * support golang relayer * add change doc --- CHANGELOG.md | 4 ++ pystarport/cluster.py | 112 +++++++++++++++++++++++++++++++++++------- 2 files changed, 98 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7771e2a..ccbda7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +- [#86](https://github.com/crypto-com/pystarport/pull/86) support golang relayer + *Feb 7, 2023* ## v0.2.5 diff --git a/pystarport/cluster.py b/pystarport/cluster.py index a219c73..59cc685 100644 --- a/pystarport/cluster.py +++ b/pystarport/cluster.py @@ -915,19 +915,22 @@ def create_account(cli, account, use_ledger=False): ) -def relayer_chain_config(data_dir, chain, relayer_chains_config): - cfg = json.load((data_dir / chain["chain_id"] / "config.json").open()) - rpc_port = ports.rpc_port(cfg["validators"][0]["base_port"]) - grpc_port = ports.grpc_port(cfg["validators"][0]["base_port"]) - - chain_config = next( - (i for i in relayer_chains_config if i["id"] == chain["chain_id"]), {} +def get_relayer_chain_config(relayer_chains_config, chain_id): + return next( + (i for i in relayer_chains_config if i["id"] == chain_id), {} ) + +def relayer_chain_config_hermes(data_dir, chain, relayer_chains_config): + chain_id = chain["chain_id"] + cfg = json.load((data_dir / chain_id / "config.json").open()) + base_port = cfg["validators"][0]["base_port"] + rpc_port = ports.rpc_port(base_port) + grpc_port = ports.grpc_port(base_port) return jsonmerge.merge( { "key_name": "relayer", - "id": chain["chain_id"], + "id": chain_id, "rpc_addr": f"http://localhost:{rpc_port}", "grpc_addr": f"http://localhost:{grpc_port}", "websocket_addr": f"ws://localhost:{rpc_port}/websocket", @@ -938,10 +941,48 @@ def relayer_chain_config(data_dir, chain, relayer_chains_config): "gas_price": {"price": 0, "denom": "basecro"}, "trusting_period": "336h", }, - chain_config, + get_relayer_chain_config(relayer_chains_config, chain_id), ) +def relayer_chain_config_rly(data_dir, chain, relayer_chains_config): + chain_id = chain["chain_id"] + folder = data_dir / chain_id + cfg = json.load((folder / "config.json").open()) + base_port = cfg["validators"][0]["base_port"] + rpc_port = ports.rpc_port(base_port) + chain_config = get_relayer_chain_config(relayer_chains_config, chain_id) + address_type = chain_config.get("address_type", {}) + derivation = address_type.get("derivation") + gas_price = chain_config.get("gas_price", {}) + price = gas_price["price"] + denom = gas_price["denom"] + return { + "type": "cosmos", + "value": { + "key-directory": f"{folder}/node0", + "key": "relayer", + "chain-id": chain_id, + "rpc-addr": f"http://localhost:{rpc_port}", + "account-prefix": chain.get("account-prefix", "cro"), + "keyring-backend": "test", + "gas-adjustment": 1.2, + "gas-prices": f"{price}{denom}", + "min-gas-amount": 0, + "debug": False, + "timeout": "20s", + "block-timeout": "", + "output-format": "json", + "sign-mode": "direct", + "extra-codecs": [derivation] if derivation else [], + "coin-type": chain.get("coin-type", 118), + "signing-algorithm": "", + "broadcast-mode": "batch", + "min-loop-duration": "0s" + } + } + + def init_cluster( data_dir, config_path, @@ -974,10 +1015,10 @@ def init_cluster( supervisord_ini_group(config.keys()), ) if len(chains) > 1: - relayer_chains_config = relayer_config.pop("chains", {}) - relayer_config_file = data_dir / "relayer.toml" - # write relayer config - relayer_config_file.write_text( + 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( { @@ -985,26 +1026,47 @@ def init_cluster( "log_level": "info", }, "chains": [ - relayer_chain_config(data_dir, chain, relayer_chains_config) - for chain in chains + relayer_chain_config_hermes(data_dir, c, cfg) + for c in chains ], }, relayer_config, ) ) ) - - # restore the relayer account in relayer + # 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"] mnemonic_path = Path(data_dir) / "relayer.env" mnemonic_path.write_text(mnemonic) + # restore the relayer account for hermes subprocess.run( [ "hermes", "--config", - relayer_config_file, + relayer_config_hermes, "keys", "add", "--chain", @@ -1017,6 +1079,20 @@ def init_cluster( ], 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, + ) def find_account(data_dir, chain_id, name):