# IBC 中继器 (IBC Relayers)

本文档介绍了如何为 Namada 上的跨链通信（IBC）协议操作中继器。本文档涵盖了通过 IBC 创建连接以及为测试目的设置 Namada 本地实例的能力。

本文档涵盖了与 Namada 一起使用 IBC 的基本步骤：

* 设置 Hermes（Setup Hermes）
* 安装 Hermes（Install Hermes）
* 设置中继器（Setting up the relayer）
* 启动中继器（Start the relayer）
* 设置本地 Namada 实例（Set up local Namada instances）

以下内容适用于希望在两个 Namada 链之间中继 IBC 消息传输的人员。当然，也可以在任何两个 IBC 兼容链（如 Cosmos 链）之间执行此操作。在这种情况下，为了进行任何包传输，目标链和源链上都需要运行一个节点。下面，我们首先讨论如何通过 Hermes 在两个预先存在的链之间启用此连接，其次，为此目的设置两个 Namada 本地实例或加入两个预先存在的 Namada 实例。

***

## 设置 Hermes (Setup Hermes)

Hermes 是一个 IBC 中继器，用于在链（实例）之间中继 IBC 数据包。Namada 使用支持 [Namada 实例的 Hermes 分支](https://github.com/heliaxdev/hermes/tree/1.6.0-namada)。在数据包中继之前，用户需要执行以下步骤来配置并启动 Hermes。

1. 创建 Hermes 配置文件 (Make Hermes config file)
2. 在实例之间创建 IBC 客户端/连接/通道 (Create IBC client/connection/channel between instances)
3. 运行 Hermes (Run Hermes)

### 创建 Hermes 配置文件 (Make Hermes config file)

解决这个问题的一个重要部分是创建一个 config.toml 文件，描述将设置哪些连接，中继器将负责这些连接。

```
export HERMES_CONFIG="<choose path for hermes config>/config.toml"
touch $HERMES_CONFIG
```

如果您不指定文件路径，则默认读取 `~/.hermes/config.toml`。

<br>

您可以在下面找到配置文件的示例。基本上，您只需更改配置文件中的链 ID、RPC 地址和密钥名称即可。如果您没有节点，请手动设置节点或通过我们的[脚本](#shi-yong-hermes-jiao-ben-she-zhi-ben-di-namada-shi-li-set-up-local-namada-instances-using-the-hermes)设置。

例子：

```
[global]
log_level = 'info'
 
[mode]
 
[mode.clients]
enabled = true
refresh = true
misbehaviour = true
 
[mode.connections]
enabled = false
 
[mode.channels]
enabled = false
 
[mode.packets]
enabled = true
clear_interval = 10
clear_on_start = false
tx_confirmation = true
 
[telemetry]
enabled = false
host = '127.0.0.1'
port = 3001
 
[[chains]]
id = 'namada-test.0a4c6786dbda39f786'  # set your chain ID
type = 'namada'
rpc_addr = 'http://127.0.0.1:27657'  # set the IP and the port of the chain
grpc_addr = 'http://127.0.0.1:9090'  # not used for now
event_source = { mode = 'push', url = 'ws://127.0.0.1:27657/websocket', batch_delay = '500ms' }  # set the IP and the port of the chain
account_prefix = ''  # not used
key_name = 'relayer'  # The key is an account name you made
store_prefix = 'ibc'
gas_price = { price = 0.001, denom = 'nam' }  # not used for now
 
[[chains]]
id = 'namada-test.647287156defa8728c'
type = 'namada'
rpc_addr = 'http://127.0.0.1:28657'
grpc_addr = 'http://127.0.0.1:9090'
event_source = { mode = 'push', url = 'ws://127.0.0.1:28657/websocket', batch_delay = '500ms' }
account_prefix = ''
key_name = 'relayer'
store_prefix = 'ibc'
gas_price = { price = 0.001, denom = 'nam' }
```

将在变量 `$HERMES_CONFIG` 中保存的配置文件路径将在后面有用。

{% hint style="info" %}
解析 toml (Interpreting the toml)

每个链配置都在 `[[chains]]` 对象下指定。这些是您想要密切关注的难题部分：

* `chains.id` 是链的名称
* `chains.rpc_address` 指定通道通过的端口，并将成为与账本交互时 Namada 的 `ledger_address` 的参数（稍后将更清晰）
* 请确保将 IP 地址更改为运行此节点的本地机器的 IP 地址！
* `chains.key_name` 指定签名交易的签名者的密钥。应在启动中继器之前生成密钥。
* `event_source` 指定链的 websocket 的 URL。为了使 Hermes 正常工作，它必须与 `rpc_address` 相同。
  {% endhint %}

### 在实例之间创建 IBC 客户端/连接/通道 (Create IBC client/connection/channel between instances)

Hermes CLI 有创建它们的命令。在创建之前，应在指定的 rpc 地址上运行每个实例的节点。如果您没有节点，请手动设置节点或通过我们的[脚本](#shi-yong-hermes-jiao-ben-she-zhi-ben-di-namada-shi-li-set-up-local-namada-instances-using-the-hermes)设置。

### 导出环境变量 (Export environment variables)

中继用户将需要保存某些环境变量。它们是：

```
export CHAIN_A_ID="<replace-with-chain-a-id>"
export CHAIN_B_ID="<replace-with-chain-b-id>"
export HERMES_CONFIG="<replace-with-hermes-config-path>"
```

***

## 安装 Hermes (Install Hermes)

在进行任何 IBC 操作之前，必须下载 Heliax 的分支 Hermes 二进制文件或从源代码中构建。

<br>

### 从二进制文件安装 (From binaries)

可以通过选择适当的架构，从我们的[发布页面](https://github.com/heliaxdev/hermes/releases)下载最新的二进制发布版本。

例如：

```
export TAG="v1.6.0-namada-beta3"
export ARCH="x86_64-unknown-linux-gnu" # or "aarch64-apple-darwin"
curl -Lo /tmp/hermes.tar.gz https://github.com/heliaxdev/hermes/releases/download/${TAG}/hermes-${TAG}-${ARCH}.tar.gz
tar -xvzf /tmp/hermes.tar.gz -C /usr/local/bin
```

{% hint style="info" %}
对于某些系统，`/usr/local/bin` 是受保护的目录。在这种情况下，您可能需要使用 `sudo` 运行上述命令。即

```
sudo tar -xvzf /tmp/hermes.tar.gz -C /usr/local/bin
```

这对于下面的命令 `cp ./target/release/hermes /usr/local/bin/` 也是如此（请参见注释）。
{% endhint %}

### 从源代码安装 (From source)

```
export TAG="v1.6.0-namada-beta3"
 
git clone https://github.com/heliaxdev/hermes.git
git checkout $TAG
cd hermes
cargo build --release --bin hermes
export HERMES=$(pwd) # if needed
```

检查二进制文件：

```
./target/release/hermes --version #or sudo cp ./target/release/hermes /usr/local/bin/
```

{% hint style="info" %}
现在建议将 hermes 添加到 $PATH 中，以便在没有任何前缀的情况下调用它。对于 ubuntu 用户，可以通过执行以下操作来实现：

```
cp ./target/release/hermes /usr/local/bin/
```

{% endhint %}

***

## 设置中继器 (Setting up the relayer)

### 创建 namada\_wallet 目录和链目录，以保存每个中继器的 wallet.toml。&#x20;

为了使中继器正常工作，它需要有一个钱包目录来存储中继器的密钥。可以通过运行以下命令来实现：

```
# in the Hermes folder
mkdir namada_wallet
mkdir -p ~/.hermes/namada_wallet/$CHAIN_A_ID
mkdir -p ~/.hermes/namada_wallet/$CHAIN_B_ID
```

{% hint style="info" %}
此步骤仅适用于 namada 链。对于基于 cosmos 的链，建议将密钥直接添加到 hermes 中。

```
./hermes --config $HERMES_CONFIG keys add --chain "<name-of-chain>" --key-file "<path-to-key>" --overwrite
```

{% endhint %}

### 创建中继器账户 (Create the relayer account)

在每条链上，必须有一个`relayer`账户。在 namada 链上，可以通过运行以下命令来实现：

```
namadaw key gen --alias relayer
```

这将为中继器账户生成一个密钥。密钥将存储在节点的[基础目录](https://tantalum666.gitbook.io/namada-docs-chinese/yun-ying-zhi-nan-operators-guide/yun-xing-wan-zheng-jie-dian-runningafull-node/ji-ben-mu-lu-base-directory)中的 `chain-id` 文件夹内的 `wallet.toml` 中。例如，如果 `chain-id` 是 `namada-test.0a4c6786dbda39f786`，则 `wallet.toml` 将位于 `$HOME/.local/share/namada/namada-test.0a4c6786dbda39f786/wallet.toml`（在未正确设置 `base-dir` 的 ubuntu 机器上）。

现在重要的是将此钱包文件复制到上面创建的 `namada_wallet` 目录中，每个链都需要这样做。继续这个例子，可以通过运行以下命令复制第一个钱包：

```
cp $HOME/.local/share/namada/$CHAIN_A_ID/wallet.toml ~/.hermes/namada_wallet/$CHAIN_A_ID/wallet.toml
# Make sure this is done for both wallets on each chain!
```

现在可以设置客户端了。

<br>

### 创建 IBC 通道 (Create IBC channel)

下面的 "create channel" 命令不仅创建了 IBC 通道，还创建了必要的 IBC 客户端连接。

```
hermes --config $HERMES_CONFIG \
  create channel \
  --a-chain $CHAIN_A_ID \
  --b-chain $CHAIN_B_ID \
  --a-port transfer \
  --b-port transfer \
  --new-client-connection --yes
```

{% hint style="info" %}
请注意，上述 `CHAIN_IDs` 将取决于您自己的设置，因此请自行检查！
{% endhint %}

当创建完成时，您可以看到通道 ID。例如，以下文本显示在 Chain A `namada-test.0a4c6786dbda39f786` 上已创建了 ID 为 `7` 的通道，并且在 Chain B `namada-test.647287156defa8728c` 上已创建了 ID 为 `12` 的通道。您将需要通道 ID 来通过 IBC 进行转移。这意味着您必须为从 Chain A 到 Chain B 的转移指定 `channel-7` 作为通道 ID（前缀 `channel-` 总是必需的）。同样，您必须为从 Chain B 到 Chain A 的转移指定 `channel-12` 作为通道 ID。

```
SUCCESS Channel {
    ordering: Unordered,
    a_side: ChannelSide {
        chain: BaseChainHandle {
            chain_id: ChainId {
                id: "namada-test.0a4c6786dbda39f786",
                version: 0,
            },
            runtime_sender: Sender { .. },
        },
        client_id: ClientId(
            "07-tendermint-0",
        ),
        connection_id: ConnectionId(
            "connection-3",
        ),
        port_id: PortId(
            "transfer",
        ),
        channel_id: Some(
            ChannelId(
                "channel-7",
            ),
        ),
        version: None,
    },
    b_side: ChannelSide {
        chain: BaseChainHandle {
            chain_id: ChainId {
                id: "namada-test.647287156defa8728c",
                version: 0,
            },
            runtime_sender: Sender { .. },
        },
        client_id: ClientId(
            "07-tendermint-1",
        ),
        connection_id: ConnectionId(
            "connection-2",
        ),
        port_id: PortId(
            "transfer",
        ),
        channel_id: Some(
            ChannelId(
                "channel-12",
            ),
        ),
        version: None,
    },
    connection_delay: 0ns,
}
```

***

## 启动中继器 (Start the relayer)

一旦运行 Hermes，它将通过节点监视实例并根据监视到的事件中继数据包。

```
hermes --config $HERMES_CONFIG start
```

您可以在[官方文档](https://hermes.informal.systems/)中查看更多有关 Hermes 的详细信息。

在同步完成后，您可以按照[上述说明](#chuang-jian-ibc-tong-dao-create-ibc-channel)创建通道并启动 Hermes。

```
# create a channel
hermes --config $HERMES_CONFIG \
  create channel \
  --a-chain $CHAIN_A_ID \
  --b-chain $CHAIN_B_ID \
  --a-port transfer \
  --b-port transfer \
  --new-client-connection --yes
```

### 通过 IBC 转移资产 (Transferring assets over IBC)

现在可以在[两条链之间转移资产了](https://tantalum666.gitbook.io/namada-docs-chinese/yong-hu-zhi-nan-user-guide/ibc-zhuan-zhang-ibc-transfers)。

***

## 使用 hermes 脚本设置本地 Namada 实例 (Set up local Namada instances using the hermes script)

脚本 `setup-namada` 将设置两个实例，每个实例有一个验证节点，为 Hermes 复制必要的文件，并在每个账本上为 Hermes 创建一个账户。它还会在 `hermes` 目录中创建一个 Hermes 的配置文件 `config_for_namada.toml`。

首先，您需要导出一些环境变量：

```
export NAMADA_DIR="<path-to-namada-source-directory>"
export TAG="v1.6.0-namada-beta3"
```

```
git clone https://github.com/heliaxdev/hermes.git
git checkout $TAG # The branch is the same as our Hermes
cd hermes
./scripts/setup-namada $NAMADA_DIR $CHAIN_ID_A $CHAIN_ID_B
```

在这种情况下，用户不必等待同步。如果每个实例上的中继器账户有足够的余额，用户可以按照上述[说明](#chuang-jian-zhong-ji-qi-zhang-hu-create-the-relayer-account)立即创建通道并启动 Hermes。用户可以在配置文件 `config_for_namada.toml` 中找到这些实例的链 ID。可以运行 `grep "id" ${HERMES_CONFIG}`。

```
# create a channel
hermes --config $HERMES_CONFIG \
  create channel \
  --a-chain $CHAIN_A_ID \
  --b-chain $CHAIN_B_ID \
  --a-port transfer \
  --b-port transfer \
  --new-client-connection --yes
 
# Run Hermes
hermes --config $HERMES_CONFIG start
```

每个节点的数据和配置文件都位于 `hermes/data/namada-*/.namada`。

为了关闭由脚本设置的任何账本，可以运行：

```
killall namadan
```
