了解如何在链上上传 Wasm 轻客户端字节码,以及如何迁移现有的 Wasm 轻客户端合约。

设置授权方

Wasm 轻客户端字节码的存储以及现有 Wasm 轻客户端合约的迁移都属于受权限控制的操作(即仅允许治理等授权方执行)。指定的授权方会在实例化 08-wasm 的 keeper 时设置:NewKeeperWithVM 和 NewKeeperWithConfig 两个构造函数都接收一个 authority 参数,该参数必须是被授权执行者的地址。例如,在 app.go 中实例化 keeper 时,可以传入治理模块的地址:
// app.go
import (
    
  ...
  "github.com/cosmos/cosmos-sdk/runtime"
  authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
  govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

  ibcwasmkeeper "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/v11/keeper"
  ibcwasmtypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/v11/types"
  ...
)

// app.go
app.WasmClientKeeper = ibcwasmkeeper.NewKeeperWithVM(
  appCodec,
  runtime.NewKVStoreService(keys[ibcwasmtypes.StoreKey]),
  app.IBCKeeper.ClientKeeper,
 	authtypes.NewModuleAddress(govtypes.ModuleName).String(), // authority
  wasmVM,
  app.GRPCQueryRouter(),
)

存储新的 Wasm 轻客户端字节码

如果治理是允许的授权方,那么为了上传新的轻客户端合约而提交的治理 v1 提案中,应包含消息 MsgStoreCode,并附带 Wasm 合约字节码的 base64 编码。可参考以下 CLI 命令和 JSON 示例:
simd tx gov submit-proposal <path/to/proposal.json> --from <key_or_address>
其中,proposal.json 内容如下:
{
  "title": "上传 IBC Wasm 轻客户端",
  "summary": "上传 wasm 客户端",
  "messages": [
    {
  "@type": "/ibc.lightclients.wasm.v1.MsgStoreCode",
  "signer": "cosmos1...", / 授权方地址(例如治理模块账户地址)
      "wasm_byte_code": "YWJ...PUB+" / Wasm 合约字节码的标准 base64 编码
    }
  ],
  "metadata": "AQ==",
  "deposit": "100stake"
}
如需进一步了解 submit-proposal CLI 命令,请参阅 Cosmos SDK 文档中的相关章节。 另外,如果使用 CLI 命令 store-code,提交流程可能会更简单。该 CLI 命令接收 Wasm 轻客户端合约文件作为参数,并会负责构造带有 MsgStoreCode 的提案消息并进行广播。

迁移现有的 Wasm 轻客户端合约

如果治理是允许的授权方,那么为了迁移现有 Wasm 轻客户端合约而提交的治理 v1 提案中,应包含消息 MsgMigrateContract,并附带要迁移到的新 Wasm 字节码的校验和。可参考以下 CLI 命令和 JSON 示例:
simd tx gov submit-proposal <path/to/proposal.json> --from <key_or_address>
其中,proposal.json 内容如下:
{
  "title": "迁移 IBC Wasm 轻客户端",
  "summary": "迁移 wasm 客户端",
  "messages": [
    {
  "@type": "/ibc.lightclients.wasm.v1.MsgMigrateContract",
  "signer": "cosmos1...", / 授权方地址(例如治理模块账户地址)
      "client_id": "08-wasm-1", / 将被迁移的 Wasm 轻客户端合约的客户端标识符
      "checksum": "a8ad...4dc0", / 要迁移到的 Wasm 字节码的 SHA-256 哈希,此字节码此前已通过 MsgStoreCode 存储
      "msg": "{}" / 迁移时传递给合约的 JSON 编码消息
    }
  ],
  "metadata": "AQ==",
  "deposit": "100stake"
}
如需进一步了解 submit-proposal CLI 命令,请参阅 Cosmos SDK 文档中的相关章节。

删除现有校验和

如果治理是允许的授权方,那么为了从允许的校验和列表中移除某个特定校验和而提交的治理 v1 提案中,应包含消息 MsgRemoveChecksum,并附带该校验和(对应某个 Wasm 字节码)。可参考以下 CLI 命令和 JSON 示例:
simd tx gov submit-proposal <path/to/proposal.json> --from <key_or_address>
其中,proposal.json 内容如下:
{
  "title": "移除 Wasm 轻客户端字节码的校验和",
  "summary": "移除校验和",
  "messages": [
    {
  "@type": "/ibc.lightclients.wasm.v1.MsgRemoveChecksum",
  "signer": "cosmos1...", / 授权方地址(例如治理模块账户地址)
      "checksum": "a8ad...4dc0", / 应从允许的校验和列表中移除的 Wasm 字节码的 SHA-256 哈希
    }
  ],
  "metadata": "AQ==",
  "deposit": "100stake"
}
如需进一步了解 submit-proposal CLI 命令,请参阅 Cosmos SDK 文档中的相关章节。
Learn how to upload Wasm light client byte code on a chain, and how to migrate an existing Wasm light client contract.

Setting an authority

Both the storage of Wasm light client byte code as well as the migration of an existing Wasm light client contract are permissioned (i.e. only allowed to an authority such as governance). The designated authority is specified when instantiating 08-wasm’s keeper: both NewKeeperWithVM and NewKeeperWithConfig constructor functions accept an authority argument that must be the address of the authorized actor. For example, in app.go, when instantiating the keeper, you can pass the address of the governance module:
// app.go
import (
    
  ...
  "github.com/cosmos/cosmos-sdk/runtime"
  authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
  govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"

  ibcwasmkeeper "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/v11/keeper"
  ibcwasmtypes "github.com/cosmos/ibc-go/modules/light-clients/08-wasm/v11/types"
  ...
)

// app.go
app.WasmClientKeeper = ibcwasmkeeper.NewKeeperWithVM(
  appCodec,
  runtime.NewKVStoreService(keys[ibcwasmtypes.StoreKey]),
  app.IBCKeeper.ClientKeeper,
 	authtypes.NewModuleAddress(govtypes.ModuleName).String(), // authority
  wasmVM,
  app.GRPCQueryRouter(),
)

Storing new Wasm light client byte code

If governance is the allowed authority, the governance v1 proposal that needs to be submitted to upload a new light client contract should contain the message MsgStoreCode with the base64-encoded byte code of the Wasm contract. Use the following CLI command and JSON as an example:
simd tx gov submit-proposal <path/to/proposal.json> --from <key_or_address>
where proposal.json contains:
{
  "title": "Upload IBC Wasm light client",
  "summary": "Upload wasm client",
  "messages": [
    {
  "@type": "/ibc.lightclients.wasm.v1.MsgStoreCode",
  "signer": "cosmos1...", / the authority address (e.g. the gov module account address)
      "wasm_byte_code": "YWJ...PUB+" / standard base64 encoding of the Wasm contract byte code
    }
  ],
  "metadata": "AQ==",
  "deposit": "100stake"
}
To learn more about the submit-proposal CLI command, please check out the relevant section in Cosmos SDK documentation. Alternatively, the process of submitting the proposal may be simpler if you use the CLI command store-code. This CLI command accepts as argument the file of the Wasm light client contract and takes care of constructing the proposal message with MsgStoreCode and broadcasting it.

Migrating an existing Wasm light client contract

If governance is the allowed authority, the governance v1 proposal that needs to be submitted to migrate an existing new Wasm light client contract should contain the message MsgMigrateContract with the checksum of the Wasm byte code to migrate to. Use the following CLI command and JSON as an example:
simd tx gov submit-proposal <path/to/proposal.json> --from <key_or_address>
where proposal.json contains:
{
  "title": "Migrate IBC Wasm light client",
  "summary": "Migrate wasm client",
  "messages": [
    {
  "@type": "/ibc.lightclients.wasm.v1.MsgMigrateContract",
  "signer": "cosmos1...", / the authority address (e.g. the gov module account address)
      "client_id": "08-wasm-1", / client identifier of the Wasm light client contract that will be migrated
      "checksum": "a8ad...4dc0", / SHA-256 hash of the Wasm byte code to migrate to, previously stored with MsgStoreCode
      "msg": "{}" / JSON-encoded message to be passed to the contract on migration
    }
  ],
  "metadata": "AQ==",
  "deposit": "100stake"
}
To learn more about the submit-proposal CLI command, please check out the relevant section in Cosmos SDK documentation.

Removing an existing checksum

If governance is the allowed authority, the governance v1 proposal that needs to be submitted to remove a specific checksum from the list of allowed checksums should contain the message MsgRemoveChecksum with the checksum (of a corresponding Wasm byte code). Use the following CLI command and JSON as an example:
simd tx gov submit-proposal <path/to/proposal.json> --from <key_or_address>
where proposal.json contains:
{
  "title": "Remove checksum of Wasm light client byte code",
  "summary": "Remove checksum",
  "messages": [
    {
  "@type": "/ibc.lightclients.wasm.v1.MsgRemoveChecksum",
  "signer": "cosmos1...", / the authority address (e.g. the gov module account address)
      "checksum": "a8ad...4dc0", / SHA-256 hash of the Wasm byte code that should be removed from the list of allowed checksums
    }
  ],
  "metadata": "AQ==",
  "deposit": "100stake"
}
To learn more about the submit-proposal CLI command, please check out the relevant section in Cosmos SDK documentation.