MsgRegisterInterchainAccount

可以使用 MsgRegisterInterchainAccount 发起 Interchain Accounts 通道握手:
type MsgRegisterInterchainAccount struct {
    Owner        string
  ConnectionID string
  Version      string
  Ordering     channeltypes.Order
}
在以下情况下,此消息预期会失败:
  • Owner 为空字符串,或其长度超过 2048 字节。
  • ConnectionID 无效(参见24-host 命名要求)。
该消息会在链上构造一个新的 MsgChannelOpenInit,并将其路由到 IBC 核心消息服务器,以发起通道握手的开启步骤。 控制器子模块会生成一个新的端口标识符。调用方需要提供合适的应用版本字符串。例如,这可以是 ICS-27 JSON 编码的 Metadata 类型,或带有嵌套应用版本的 ICS-29 JSON 编码 Metadata 类型。 如果省略 Version 字符串,控制器子模块会在 OnChanOpenInit 握手回调中构造默认版本字符串。
type MsgRegisterInterchainAccountResponse struct {
    ChannelID string
  PortId string
}
消息响应中会返回 ChannelID 和 PortID。

MsgSendTx

可以从对应的控制器链发送 MsgSendTx,以便在远程宿主链上执行一笔 Interchain Accounts 交易:
type MsgSendTx struct {
    Owner           string
  ConnectionID    string
  PacketData      InterchainAccountPacketData 
  RelativeTimeout uint64
}
在以下情况下,此消息预期会失败:
  • Owner 为空字符串,或其长度超过 2048 字节。
  • ConnectionID 无效(参见24-host 命名要求)。
  • PacketData 包含 UNSPECIFIED 类型枚举、Data 字节长度为零,或 Memo 字段长度超过 256 个字符。
  • RelativeTimeout 为零。
该消息会使用提供的 PacketData 创建一个新的 IBC 数据包,并通过与 Owner 和 ConnectionID 关联的通道发送出去。 PacketData 预期包含以 CosmosTx 形式序列化的 []sdk.Msg 列表。请注意,每个 sdk.Msg 的 signer 字段都必须是 interchain account 地址。 当该数据包被中继到宿主链后,PacketData 会被反序列化,并对其中的消息进行鉴权和执行。
type MsgSendTxResponse struct {
    Sequence uint64
}
消息响应中会返回数据包的 Sequence。

查询

可以使用 MsgModuleQuerySafe 在宿主链上执行一组查询。该消息可以包含在 InterchainPacketData 的已编码 sdk.Msg 列表中。宿主链会在 acknowledgment 中返回所有查询的响应。请注意,只能执行模块安全查询(即可从状态机内部安全调用的确定性查询)。 Cosmos SDK 提供的可用查询如下:
/cosmos.auth.v1beta1.Query/Accounts
/cosmos.auth.v1beta1.Query/Account
/cosmos.auth.v1beta1.Query/AccountAddressByID
/cosmos.auth.v1beta1.Query/Params
/cosmos.auth.v1beta1.Query/ModuleAccounts
/cosmos.auth.v1beta1.Query/ModuleAccountByName
/cosmos.auth.v1beta1.Query/AccountInfo
/cosmos.bank.v1beta1.Query/Balance
/cosmos.bank.v1beta1.Query/AllBalances
/cosmos.bank.v1beta1.Query/SpendableBalances
/cosmos.bank.v1beta1.Query/SpendableBalanceByDenom
/cosmos.bank.v1beta1.Query/TotalSupply
/cosmos.bank.v1beta1.Query/SupplyOf
/cosmos.bank.v1beta1.Query/Params
/cosmos.bank.v1beta1.Query/DenomMetadata
/cosmos.bank.v1beta1.Query/DenomMetadataByQueryString
/cosmos.bank.v1beta1.Query/DenomsMetadata
/cosmos.bank.v1beta1.Query/DenomOwners
/cosmos.bank.v1beta1.Query/SendEnabled
/cosmos.circuit.v1.Query/Account
/cosmos.circuit.v1.Query/Accounts
/cosmos.circuit.v1.Query/DisabledList
/cosmos.staking.v1beta1.Query/Validators
/cosmos.staking.v1beta1.Query/Validator
/cosmos.staking.v1beta1.Query/ValidatorDelegations
/cosmos.staking.v1beta1.Query/ValidatorUnbondingDelegations
/cosmos.staking.v1beta1.Query/Delegation
/cosmos.staking.v1beta1.Query/UnbondingDelegation
/cosmos.staking.v1beta1.Query/DelegatorDelegations
/cosmos.staking.v1beta1.Query/DelegatorUnbondingDelegations
/cosmos.staking.v1beta1.Query/Redelegations
/cosmos.staking.v1beta1.Query/DelegatorValidators
/cosmos.staking.v1beta1.Query/DelegatorValidator
/cosmos.staking.v1beta1.Query/HistoricalInfo
/cosmos.staking.v1beta1.Query/Pool
/cosmos.staking.v1beta1.Query/Params
ibc-go 提供的可用查询如下:
/ibc.core.client.v1.Query/VerifyMembership
下面的代码块展示了如何使用 MsgModuleQuerySafe 查询宿主链上某个账户的余额。生成的 packet data 变量会用于设置 MsgSendTx 的 PacketData。
balanceQuery := banktypes.NewQueryBalanceRequest("cosmos1...", "uatom")

queryBz, err := balanceQuery.Marshal()

/ signer of message must be the interchain account on the host
    queryMsg := icahosttypes.NewMsgModuleQuerySafe("cosmos2...", []icahosttypes.QueryRequest{
  {
    Path: "/cosmos.bank.v1beta1.Query/Balance",
    Data: queryBz,
},
})

bz, err := icatypes.SerializeCosmosTx(cdc, []proto.Message{
    queryMsg
}, icatypes.EncodingProtobuf)
    packetData := icatypes.InterchainAccountPacketData{
    Type: icatypes.EXECUTE_TX,
    Data: bz,
    Memo: "",
}

原子性

由于 Interchain Accounts 模块支持通过 Cosmos SDK Msg 接口执行多笔交易,因此它依托 Context 类型提供的 CacheMultiStore 架构,具备与基于 Cosmos SDK 的应用相同的原子性保证。 这意味着在使用 Interchain Accounts 执行交易时,只有当所有 Msg 都成功时,状态变更才会被提交。

MsgRegisterInterchainAccount

An Interchain Accounts channel handshake can be initiated using MsgRegisterInterchainAccount:
type MsgRegisterInterchainAccount struct {
    Owner        string
  ConnectionID string
  Version      string
  Ordering     channeltypes.Order
}
This message is expected to fail if: This message will construct a new MsgChannelOpenInit on chain and route it to the core IBC message server to initiate the opening step of the channel handshake. The controller submodule will generate a new port identifier. The caller is expected to provide an appropriate application version string. For example, this may be an ICS-27 JSON encoded Metadata type or an ICS-29 JSON encoded Metadata type with a nested application version. If the Version string is omitted, the controller submodule will construct a default version string in the OnChanOpenInit handshake callback.
type MsgRegisterInterchainAccountResponse struct {
    ChannelID string
  PortId string
}
The ChannelID and PortID are returned in the message response.

MsgSendTx

An Interchain Accounts transaction can be executed on a remote host chain by sending a MsgSendTx from the corresponding controller chain:
type MsgSendTx struct {
    Owner           string
  ConnectionID    string
  PacketData      InterchainAccountPacketData 
  RelativeTimeout uint64
}
This message is expected to fail if:
  • Owner is an empty string or contains more than 2048 bytes.
  • ConnectionID is invalid (see 24-host naming requirements).
  • PacketData contains an UNSPECIFIED type enum, the length of Data bytes is zero or the Memo field exceeds 256 characters in length.
  • RelativeTimeout is zero.
This message will create a new IBC packet with the provided PacketData and send it via the channel associated with the Owner and ConnectionID. The PacketData is expected to contain a list of serialized []sdk.Msg in the form of CosmosTx. Please note the signer field of each sdk.Msg must be the interchain account address. When the packet is relayed to the host chain, the PacketData is unmarshalled and the messages are authenticated and executed.
type MsgSendTxResponse struct {
    Sequence uint64
}
The packet Sequence is returned in the message response.

Queries

It is possible to use MsgModuleQuerySafe to execute a list of queries on the host chain. This message can be included in the list of encoded sdk.Msgs of InterchainPacketData. The host chain will return on the acknowledgment the responses for all the queries. Please note that only module safe queries can be executed (deterministic queries that are safe to be called from within the state machine). The queries available from Cosmos SDK are:
/cosmos.auth.v1beta1.Query/Accounts
/cosmos.auth.v1beta1.Query/Account
/cosmos.auth.v1beta1.Query/AccountAddressByID
/cosmos.auth.v1beta1.Query/Params
/cosmos.auth.v1beta1.Query/ModuleAccounts
/cosmos.auth.v1beta1.Query/ModuleAccountByName
/cosmos.auth.v1beta1.Query/AccountInfo
/cosmos.bank.v1beta1.Query/Balance
/cosmos.bank.v1beta1.Query/AllBalances
/cosmos.bank.v1beta1.Query/SpendableBalances
/cosmos.bank.v1beta1.Query/SpendableBalanceByDenom
/cosmos.bank.v1beta1.Query/TotalSupply
/cosmos.bank.v1beta1.Query/SupplyOf
/cosmos.bank.v1beta1.Query/Params
/cosmos.bank.v1beta1.Query/DenomMetadata
/cosmos.bank.v1beta1.Query/DenomMetadataByQueryString
/cosmos.bank.v1beta1.Query/DenomsMetadata
/cosmos.bank.v1beta1.Query/DenomOwners
/cosmos.bank.v1beta1.Query/SendEnabled
/cosmos.circuit.v1.Query/Account
/cosmos.circuit.v1.Query/Accounts
/cosmos.circuit.v1.Query/DisabledList
/cosmos.staking.v1beta1.Query/Validators
/cosmos.staking.v1beta1.Query/Validator
/cosmos.staking.v1beta1.Query/ValidatorDelegations
/cosmos.staking.v1beta1.Query/ValidatorUnbondingDelegations
/cosmos.staking.v1beta1.Query/Delegation
/cosmos.staking.v1beta1.Query/UnbondingDelegation
/cosmos.staking.v1beta1.Query/DelegatorDelegations
/cosmos.staking.v1beta1.Query/DelegatorUnbondingDelegations
/cosmos.staking.v1beta1.Query/Redelegations
/cosmos.staking.v1beta1.Query/DelegatorValidators
/cosmos.staking.v1beta1.Query/DelegatorValidator
/cosmos.staking.v1beta1.Query/HistoricalInfo
/cosmos.staking.v1beta1.Query/Pool
/cosmos.staking.v1beta1.Query/Params
And the query available from ibc-go is:
/ibc.core.client.v1.Query/VerifyMembership
The following code block shows an example of how MsgModuleQuerySafe can be used to query the account balance of an account on the host chain. The resulting packet data variable is used to set the PacketData of MsgSendTx.
balanceQuery := banktypes.NewQueryBalanceRequest("cosmos1...", "uatom")

queryBz, err := balanceQuery.Marshal()

/ signer of message must be the interchain account on the host
    queryMsg := icahosttypes.NewMsgModuleQuerySafe("cosmos2...", []icahosttypes.QueryRequest{
  {
    Path: "/cosmos.bank.v1beta1.Query/Balance",
    Data: queryBz,
},
})

bz, err := icatypes.SerializeCosmosTx(cdc, []proto.Message{
    queryMsg
}, icatypes.EncodingProtobuf)
    packetData := icatypes.InterchainAccountPacketData{
    Type: icatypes.EXECUTE_TX,
    Data: bz,
    Memo: "",
}

Atomicity

As the Interchain Accounts module supports the execution of multiple transactions using the Cosmos SDK Msg interface, it provides the same atomicity guarantees as Cosmos SDK-based applications, leveraging the CacheMultiStore architecture provided by the Context type. This provides atomic execution of transactions when using Interchain Accounts, where state changes are only committed if all Msgs succeed.