cosmos 跨链

admin
admin 2023年08月13日
  • 在其它设备中阅读本文章

创建并运行测试链,构造跨链通道。cosmos 链有许多,这里采用 gaia 来进行测试,跨链采用 cosmos 的 IBC 协议,除了链需要实现这个协议外,还需要一个中继器(rly)来转发跨链数据包

一、环境

  1. 操作系统:Linux
  2. go 版本:1.21.0
  3. gaia 版本:10.0.2
  4. rly 版本:2.3.0

二、软件安装

需要先安装 go 编译器(1.20 以上版本),参考 官方教程,也可以使用系统包管理器直接安装(sudo pacman -S go

再安装 gaia 程序

git clone https://github.com/cosmos/gaia.git
cd gaia && && git checkout v10.0.2
$ make install

最后安装 rly 程序

$ git clone https://github.com/cosmos/relayer.git
$ cd relayer && git checkout v2.3.0
$ make install

三、配置启动 gaia

打开新的终端窗口:配置启动链 id 为 ibc- 0 的测试节点

gaiad init testnode --home node0 --chain-id ibc-0
gaiad keys add key0 --home node0 --keyring-backend os
gaiad add-genesis-account key0 10000000000000000000000000000stake --keyring-backend os --home node0
gaiad gentx key0 100000000stake --home node0 --chain-id ibc-0
gaiad collect-gentxs --home node0
gaiad start --home node0 --rpc.laddr tcp://127.0.0.1:26657 --p2p.laddr tcp://0.0.0.0:26656 --grpc.address 0.0.0.0:9090 --grpc-web.address 0.0.0.0:9091 --rpc.pprof_laddr 0.0.0.0:6060

打开新的终端窗口:配置启动链 id 为 ibc- 1 的测试节点

gaiad init testnode --home node1 --chain-id ibc-1
gaiad keys add key1 --home node1 --keyring-backend os
gaiad add-genesis-account key1 10000000000000000000000000000stake --keyring-backend os --home node1
gaiad gentx key1 100000000stake --home node1 --chain-id ibc-1
gaiad collect-gentxs --home node1
gaiad start --home node1 --rpc.laddr tcp://127.0.0.1:26667 --p2p.laddr tcp://0.0.0.0:26666 --grpc.address 0.0.0.0:9000 --grpc-web.address 0.0.0.0:9001 --rpc.pprof_laddr 0.0.0.0:6070

四、配置启动 rly

1、删除 rly 的工作目录数据, 如果之前没有运行过 rly 可以跳过此步骤。注意:会删除账号密钥文件

rm -rf ~/.relayer

2、初始化工作目录

rly config init

3、配置链信息
链的信息可以通过 git 仓库或者文件导入,测试只能使用文件导入方式,创建 ibc-0.json 和 ibc-1.json 两个文件,内容如下

{
  "type": "cosmos",
  "value": {
    "key": "testkey",
    "chain-id": "ibc-0",
    "rpc-addr": "http://localhost:26657",
    "account-prefix": "cosmos",
    "keyring-backend": "os",
    "gas-adjustment": 1.2,
    "gas-prices": "0.01stake",
    "debug": true,
    "timeout": "10s",
    "output-format": "json",
    "sign-mode": "direct"
  }
}
{
  "type": "cosmos",
  "value": {
    "key": "testkey",
    "chain-id": "ibc-1",
    "rpc-addr": "http://localhost:26667",
    "account-prefix": "cosmos",
    "keyring-backend": "os",
    "gas-adjustment": 1.2,
    "gas-prices": "0.01stake",
    "debug": true,
    "timeout": "10s",
    "output-format": "json",
    "sign-mode": "direct"
  }
}

导入两个测试链节点信息

rly chains add -f ibc-0.json
rly chains add -f ibc-1.json

4、配置账户
可以创建(rly keys add)或导入(rly keys restore)账户,跨链需要账户来发交易的

rly keys add ibc-0 testkey
rly keys add ibc-1 testkey

中继不使用链节点上的账户,需要单独配置

5、给中继账户分配资产
中继账户发交易需要支付手续费的,转账 100000000000

gaiad tx bank send key0 $(rly keys show ibc-0) 100000000000stake --home node0 --node tcp://localhost:26657 --chain-id ibc-0
gaiad tx bank send key1 $(rly keys show ibc-1) 100000000000stake --home node0 --node tcp://localhost:26667 --chain-id ibc-1

6、创建跨链通道
通道有一个唯一名称,可以指定链 id 创建跨链通道 demo,然后初始化跨链通道

rly paths new ibc-0 ibc-1 demo
rly tx link demo

通道初始化时会相互注册链的信息(链 id 和验证者列表)

7、启动跨链中继器

rly start

也可以只启动一条通道 demo

rly start demo

五、跨链转账

gaia 客户端发起转账交易

gaiad tx ibc-transfer transfer transfer channel-0 $(rly keys show ibc-1) 1000stake --from key0 --chain-id ibc-0 --node tcp://localhost:26657

也可以使用 rly 客户端发送转账交易

rly tx transfer ibc-1 ibc-0 1000tansfer/channel-0/stake $(gaiad keys show key0 --home node0) channel-0 

跨链后的资产有了特殊的名称 tansfer/channel-0/stake(//

可以使用一下命令查询账户资产,验证跨链转账

gaiad query bank balances $(gaiad keys show key0 -a --home node0) --node tcp://localhost:26657
gaiad query bank balances $(rly keys show ibc-1) --node tcp://localhost:26667