Skip to content

Commit

Permalink
cli: add argument to generate-packet-data cli to use selected encod…
Browse files Browse the repository at this point in the history
…ing format (backport #4537) (#4569)

* imp: add argument to `generate-packet-data` cli to use selected encoding format (#4537)

* add encoding parameter to cli

* updated docs

* testing encoding parameter of cli

* linter fixes

* review feedback

* Update client.md

* typos

* Update modules/apps/27-interchain-accounts/host/client/cli/tx.go

Co-authored-by: Damian Nolan <damiannolan@gmail.com>

* address review feedback

---------

Co-authored-by: srdtrk <59252793+srdtrk@users.noreply.github.com>
Co-authored-by: Damian Nolan <damiannolan@gmail.com>
(cherry picked from commit 1e3eee6)

# Conflicts:
#	modules/apps/27-interchain-accounts/host/client/cli/tx.go
#	modules/apps/27-interchain-accounts/host/client/cli/tx_test.go

* fix: fixed backport conflicts

* add changelog

---------

Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
Co-authored-by: srdtrk <srdtrk@hotmail.com>
  • Loading branch information
3 people authored Sep 7, 2023
1 parent 6c91777 commit de5104b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 42 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Ref: https://keepachangelog.com/en/1.0.0/

### Improvements

* (apps/27-interchain-accounts) [\#4537](https://github.com/cosmos/ibc-go/pull/4537) Add argument to `generate-packet-data` cli to choose the encoding format for the messages in the ICA packet data.

### Features

### Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion docs/apps/interchain-accounts/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ simd tx interchain-accounts host --help

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

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).
The `generate-packet-data` command allows users to generate protobuf or proto3 JSON 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). The `--encoding` flag can be uesd to specify the encoding format (value must be either `proto3` or `proto3json`); if not specified, the default will be `proto3`. The `--memo` flag can be used to include a memo string in the interchain accounts packet data.

```shell
simd tx interchain-accounts host generate-packet-data [message]
Expand Down
38 changes: 25 additions & 13 deletions modules/apps/27-interchain-accounts/host/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,19 @@ import (
)

const (
memoFlag string = "memo"
memoFlag string = "memo"
encodingFlag string = "encoding"
)

func generatePacketDataCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "generate-packet-data [message]",
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.
`,
Short: "Generates protobuf or proto3 JSON encoded ICA packet data.",
Long: `generate-packet-data accepts a message string and serializes it (depending on the
encoding parameter) using protobuf or proto3 JSON 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. The default encoding format is protobuf if none is specified;
otherwise the encoding flag can be used in combination with either "proto3" or "proto3json".`,
Example: fmt.Sprintf(`%s tx interchain-accounts host generate-packet-data '{
"@type":"/cosmos.bank.v1beta1.MsgSend",
"from_address":"cosmos15ccshhmp0gsx29qpqq6g4zmltnnvgmyu9ueuadh9y2nc5zj0szls5gtddz",
Expand All @@ -36,7 +38,7 @@ which submits pre-built packet data containing messages to be executed on the ho
"amount": "1000"
}
]
}' --memo memo
}' --memo memo --encoding proto3json
%s tx interchain-accounts host generate-packet-data '[{
Expand Down Expand Up @@ -73,7 +75,16 @@ which submits pre-built packet data containing messages to be executed on the ho
return err
}

packetDataBytes, err := generatePacketData(cdc, []byte(args[0]), memo)
encoding, err := cmd.Flags().GetString(encodingFlag)
if err != nil {
return err
}

if encoding != icatypes.EncodingProtobuf && encoding != icatypes.EncodingProto3JSON {
return fmt.Errorf("unsupported encoding type: %s", encoding)
}

packetDataBytes, err := generatePacketData(cdc, []byte(args[0]), memo, encoding)
if err != nil {
return err
}
Expand All @@ -84,19 +95,20 @@ which submits pre-built packet data containing messages to be executed on the ho
},
}

cmd.Flags().String(memoFlag, "", "an optional memo to be included in the interchain account packet data")
cmd.Flags().String(memoFlag, "", "optional memo to be included in the interchain accounts packet data")
cmd.Flags().String(encodingFlag, "", "optional encoding format of the messages in the interchain accounts packet data")
return cmd
}

// generatePacketData takes in message bytes and a memo and serializes the message into an
// instance of InterchainAccountPacketData which is returned as bytes.
func generatePacketData(cdc *codec.ProtoCodec, msgBytes []byte, memo string) ([]byte, error) {
func generatePacketData(cdc *codec.ProtoCodec, msgBytes []byte, memo string, encoding string) ([]byte, error) {
protoMessages, err := convertBytesIntoProtoMessages(cdc, msgBytes)
if err != nil {
return nil, err
}

return generateIcaPacketDataFromProtoMessages(cdc, protoMessages, memo)
return generateIcaPacketDataFromProtoMessages(cdc, protoMessages, memo, encoding)
}

// convertBytesIntoProtoMessages returns a list of proto messages from bytes. The bytes can be in the form of a single
Expand Down Expand Up @@ -128,8 +140,8 @@ func convertBytesIntoProtoMessages(cdc *codec.ProtoCodec, msgBytes []byte) ([]pr
}

// generateIcaPacketDataFromProtoMessages generates ica packet data as bytes from a given set of proto encoded sdk messages and a memo.
func generateIcaPacketDataFromProtoMessages(cdc *codec.ProtoCodec, sdkMessages []proto.Message, memo string) ([]byte, error) {
icaPacketDataBytes, err := icatypes.SerializeCosmosTx(cdc, sdkMessages)
func generateIcaPacketDataFromProtoMessages(cdc *codec.ProtoCodec, sdkMessages []proto.Message, memo string, encoding string) ([]byte, error) {
icaPacketDataBytes, err := icatypes.SerializeCosmosTxWithEncoding(cdc, sdkMessages, encoding)
if err != nil {
return nil, err
}
Expand Down
59 changes: 31 additions & 28 deletions modules/apps/27-interchain-accounts/host/client/cli/tx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,43 +96,46 @@ func TestGeneratePacketData(t *testing.T) {
},
}

for _, tc := range tests {
tc := tc
ir := codectypes.NewInterfaceRegistry()
if tc.registerInterfaceFn != nil {
tc.registerInterfaceFn(ir)
}
encodings := []string{icatypes.EncodingProtobuf, icatypes.EncodingProto3JSON}
for _, encoding := range encodings {
for _, tc := range tests {
tc := tc
ir := codectypes.NewInterfaceRegistry()
if tc.registerInterfaceFn != nil {
tc.registerInterfaceFn(ir)
}

cdc := codec.NewProtoCodec(ir)
cdc := codec.NewProtoCodec(ir)

t.Run(tc.name, func(t *testing.T) {
bz, err := generatePacketData(cdc, []byte(tc.message), tc.memo)
t.Run(fmt.Sprintf("%s with %s encoding", tc.name, encoding), func(t *testing.T) {
bz, err := generatePacketData(cdc, []byte(tc.message), tc.memo, encoding)

if tc.expectedPass {
require.NoError(t, err)
require.NotNil(t, bz)
if tc.expectedPass {
require.NoError(t, err)
require.NotNil(t, bz)

packetData := icatypes.InterchainAccountPacketData{}
err = cdc.UnmarshalJSON(bz, &packetData)
require.NoError(t, err)
packetData := icatypes.InterchainAccountPacketData{}
err = cdc.UnmarshalJSON(bz, &packetData)
require.NoError(t, err)

require.Equal(t, icatypes.EXECUTE_TX, packetData.Type)
require.Equal(t, tc.memo, packetData.Memo)
require.Equal(t, icatypes.EXECUTE_TX, packetData.Type)
require.Equal(t, tc.memo, packetData.Memo)

data := packetData.Data
messages, err := icatypes.DeserializeCosmosTx(cdc, data)
data := packetData.Data
messages, err := icatypes.DeserializeCosmosTxWithEncoding(cdc, data, encoding)

require.NoError(t, err)
require.NotNil(t, messages)
require.NoError(t, err)
require.NotNil(t, messages)

if tc.assertionFn != nil {
tc.assertionFn(t, messages)
if tc.assertionFn != nil {
tc.assertionFn(t, messages)
}
} else {
require.Error(t, err)
require.Nil(t, bz)
}
} else {
require.Error(t, err)
require.Nil(t, bz)
}
})
})
}
}
}

Expand Down

0 comments on commit de5104b

Please sign in to comment.