diff --git a/Dockerfile b/Dockerfile index 7afb6c3..277a4f4 100644 --- a/Dockerfile +++ b/Dockerfile @@ -3,11 +3,13 @@ ARG BASE_IMAGE="binhex/arch-base" FROM --platform=${BUILDPLATFORM} ${BASE_IMAGE} -ARG HERMES_VERSION="v1.7.0" +ARG HERMES_VERSION="v1.7.3" COPY ./etc /etc/ +COPY ./bin /usr/local/bin/ RUN set -eux && \ + pacman -Syyu --noconfirm python-toml && \ curl -sSL https://github.com/informalsystems/hermes/releases/download/${HERMES_VERSION}/hermes-${HERMES_VERSION}-x86_64-unknown-linux-gnu.tar.gz | \ tar -xz -C /usr/local/bin && \ chmod +x /usr/local/bin/* && \ diff --git a/bin/update_clients.py b/bin/update_clients.py new file mode 100755 index 0000000..8a26dc9 --- /dev/null +++ b/bin/update_clients.py @@ -0,0 +1,83 @@ +#! /usr/bin/env python3 + +import toml, json, subprocess + +HERMES = "hermes" +HERMES_CONFIG = "/hermes/config.toml" + + +def hermes(command): + process = subprocess.run( + [HERMES, "--config", HERMES_CONFIG, "--json"] + command.split(" "), + stdout=subprocess.PIPE, + text=True, + ) + output_lines = process.stdout.splitlines() + + for line in output_lines: + line_json = json.loads(line) + if "result" in line_json: + return line_json + else: + return None + + +def get_connection(chain_id, port, channel): + output = hermes( + f"query channel end --chain {chain_id} --port {port} --channel {channel}" + ) + + if output["status"] == "success": + return output["result"]["connection_hops"][0] + else: + return None + + +def get_client_id(chain_id, connection): + output = hermes( + f"query connection end --chain {chain_id} --connection {connection}" + ) + + if output["status"] == "success": + return output["result"]["client_id"] + else: + return None + + +def get_clients_from_config(): + config = toml.load(HERMES_CONFIG) + clients = [] + + for chain in config["chains"]: + chain_id = chain["id"] + whitelist = chain["packet_filter"]["list"] + + for position in whitelist: + port = position[0] + channel = position[1] + + connection = get_connection(chain_id, port, channel) + if connection: + client = get_client_id(chain_id, connection) + if client: + clients.append((chain_id, client)) + + return clients + + +def update_clients(): + clients = get_clients_from_config() + for client in clients: + chain_id = client[0] + client_id = client[1] + + output = hermes(f"update client --host-chain {chain_id} --client {client_id}") + result = output["result"] + + if output["status"] == "success": + print(f"Success: {client_id} ({chain_id})") + else: + print(f"Failed: {result}") + + +update_clients()