Skip to content

Commit

Permalink
docs: ica tx encoding documentation added (#4169) (#4192)
Browse files Browse the repository at this point in the history
* docs: added 'tx-encoding.md' file to docs

* feat: added tx encoding to frontend

* docs: improved tx encoding docs

* style(docs): moved tx encoding to bottom

* docs: improved tx encoding

* docs: improved ica tx encoding docs

* docs: in ica tx encoding, added small code blocks around some interface names

* docs: improved ica client docs + reordered tx encoding in sidebar

* docs(ica/cmd): updated docs of 'generate-packet-data'

* docs: improved ica tx encoding docs

(cherry picked from commit 585e56b)

Co-authored-by: srdtrk <59252793+srdtrk@users.noreply.github.com>
  • Loading branch information
mergify[bot] and srdtrk authored Jul 27, 2023
1 parent ec30a3a commit fd6e6a7
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 8 deletions.
9 changes: 7 additions & 2 deletions docs/.vuepress/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,11 @@ module.exports = {
directory: false,
path: "/apps/interchain-accounts/parameters.html",
},
{
title: "Tx Encoding",
directory: false,
path: "/apps/interchain-accounts/tx-encoding.html",
},
{
title: "Client",
directory: false,
Expand Down Expand Up @@ -286,7 +291,7 @@ module.exports = {
directory: false,
path: "/apps/interchain-accounts/legacy/keeper-api.html",
},
]
],
},
],
},
Expand Down Expand Up @@ -397,7 +402,7 @@ module.exports = {
directory: false,
path: "/ibc/light-clients/setup.html",
},
]
],
},
{
title: "Localhost",
Expand Down
2 changes: 1 addition & 1 deletion docs/apps/interchain-accounts/active-channels.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
order: 8
order: 9
-->

# Understanding Active Channels
Expand Down
6 changes: 3 additions & 3 deletions docs/apps/interchain-accounts/client.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!--
order: 7
order: 8
-->

# Client
Expand Down Expand Up @@ -58,7 +58,7 @@ See below for example contents of `packet-data.json`. The CLI handler will unmar
}
```

Note the `data` field is a base64 encoded byte string as per the [proto3 JSON encoding specification](https://developers.google.com/protocol-buffers/docs/proto3#json).
Note the `data` field is a base64 encoded byte string as per the tx encoding agreed upon during the channel handshake.

A helper CLI is provided in the host submodule which can be used to generate the packet data JSON using the counterparty chain's binary. See the [`generate-packet-data` command](#generate-packet-data) for an example.

Expand All @@ -84,7 +84,7 @@ simd tx interchain-accounts host --help

##### `generate-packet-data`

The `generate-packet-data` command allows users to generate interchain accounts packet data for input message(s). The packet data can then be used with the controller submodule's [`send-tx` command](#send-tx).
The `generate-packet-data` command allows users to generate protobuf encoded interchain accounts packet data for input message(s). The packet data can then be used with the controller submodule's [`send-tx` command](#send-tx).

```shell
simd tx interchain-accounts host generate-packet-data [message]
Expand Down
55 changes: 55 additions & 0 deletions docs/apps/interchain-accounts/tx-encoding.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!--
order: 7
-->

# Transaction Encoding

When orchestrating an interchain account transaction, which comprises multiple `sdk.Msg` objects represented as `Any` types, the transactions must be encoded as bytes within [`InterchainAccountPacketData`](https://github.com/cosmos/ibc-go/blob/v7.2.0/proto/ibc/applications/interchain_accounts/v1/packet.proto#L21-L26).

```protobuf
// InterchainAccountPacketData is comprised of a raw transaction, type of transaction and optional memo field.
message InterchainAccountPacketData {
Type type = 1;
bytes data = 2;
string memo = 3;
}
```

The `data` field must be encoded as a [`CosmosTx`](https://github.com/cosmos/ibc-go/blob/v7.2.0/proto/ibc/applications/interchain_accounts/v1/packet.proto#L28-L31).

```protobuf
// CosmosTx contains a list of sdk.Msg's. It should be used when sending transactions to an SDK host chain.
message CosmosTx {
repeated google.protobuf.Any messages = 1;
}
```

The encoding method for `CosmosTx` is determined during the channel handshake process. If the channel version [metadata's `encoding` field](https://github.com/cosmos/ibc-go/blob/v7.2.0/proto/ibc/applications/interchain_accounts/v1/metadata.proto#L22) is marked as `proto3`, then `CosmosTx` undergoes protobuf encoding. Conversely, if the field is set to `proto3json`, then [proto3 json](https://protobuf.dev/programming-guides/proto3/#json) encoding takes place, which generates a JSON representation of the protobuf message.

## Protobuf Encoding

Protobuf encoding serves as the standard encoding process for `CosmosTx`. This occurs if the channel handshake initiates with an empty channel version metadata or if the `encoding` field explicitly denotes `proto3`. In Golang, the protobuf encoding procedure utilizes the `proto.Marshal` function. Every protobuf autogenerated Golang type comes equipped with a `Marshal` method that can be employed to encode the message.

## (Protobuf) JSON Encoding

The proto3 JSON encoding presents an alternative encoding technique for `CosmosTx`. It is selected if the channel handshake begins with the channel version metadata `encoding` field labeled as `proto3json`. In Golang, the Proto3 canonical encoding in JSON is implemented by the `"github.com/cosmos/gogoproto/jsonpb"` package. Within Cosmos SDK, the `ProtoCodec` structure implements the `JSONCodec` interface, leveraging the `jsonpb` package. This method generates a JSON format as follows:

```json
{
"messages": [
{
"@type": "/cosmos.bank.v1beta1.MsgSend",
"from_address": "cosmos1...",
"to_address": "cosmos1...",
"amount": [
{
"denom": "uatom",
"amount": "1000000"
}
]
}
]
}
```

Here, the `"messages"` array is populated with transactions. Each transaction is represented as a JSON object with the `@type` field denoting the transaction type and the remaining fields representing the transaction's attributes.
4 changes: 2 additions & 2 deletions modules/apps/27-interchain-accounts/host/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const (
func generatePacketDataCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "generate-packet-data [message]",
Short: "Generates ICA packet data.",
Long: `generate-packet-data accepts a message string and serializes it
Short: "Generates protobuf encoded ICA packet data.",
Long: `generate-packet-data accepts a message string and serializes it using protobuf
into packet data which is outputted to stdout. It can be used in conjunction with send-tx"
which submits pre-built packet data containing messages to be executed on the host chain.
`,
Expand Down

0 comments on commit fd6e6a7

Please sign in to comment.