-
Notifications
You must be signed in to change notification settings - Fork 639
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add host cli to generate ica packet data #2297
Merged
chatton
merged 21 commits into
main
from
cian/issue#2243-add-a-ics27-host-cli-to-generate-ica-packet-data
Sep 28, 2022
Merged
Changes from 3 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
22d9e1b
wip: adding command to display json value for interchain packet data
chatton 3ecdc1d
tests: adding tests for generatePacketData
chatton 1f12260
Merge branch 'main' into cian/issue#2243-add-a-ics27-host-cli-to-gene…
chatton fe4880a
chore: adding changelog entry
chatton 4f3ef30
chore: fix useless assignment to local var
chatton 4e6ed4e
chore: fix typo
chatton 47abc0b
Merge branch 'main' into cian/issue#2243-add-a-ics27-host-cli-to-gene…
chatton 96f1f24
Merge branch 'main' into cian/issue#2243-add-a-ics27-host-cli-to-gene…
chatton e067218
Merge branch 'main' into cian/issue#2243-add-a-ics27-host-cli-to-gene…
chatton 8645a8b
chore: addressing PR feedback
chatton d520833
Merge branch 'main' into cian/issue#2243-add-a-ics27-host-cli-to-gene…
chatton 2a43f5c
chore: merge main
chatton c5f7fdd
Merge branch 'main' into cian/issue#2243-add-a-ics27-host-cli-to-gene…
chatton cf2179f
chore: bumping imports
chatton d477702
Merge branch 'cian/issue#2243-add-a-ics27-host-cli-to-generate-ica-pa…
chatton 5ae13d3
feat: adding support for both single and multiple messages
chatton e306637
chore: addressing PR feedback
chatton 7411b53
Update modules/apps/27-interchain-accounts/host/client/cli/cli.go
chatton 5076fbd
Merge branch 'main' into cian/issue#2243-add-a-ics27-host-cli-to-gene…
chatton 30b7907
chore: use version AppName variable
chatton 8bfbf57
Merge branch 'main' into cian/issue#2243-add-a-ics27-host-cli-to-gene…
chatton File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package cli | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/cosmos/cosmos-sdk/client" | ||
"github.com/cosmos/cosmos-sdk/codec" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/spf13/cobra" | ||
|
||
icatypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types" | ||
colin-axner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
const ( | ||
memoFlag string = "memo" | ||
) | ||
|
||
func generatePacketDataCmd() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "generate-packet-data [message]", | ||
Short: "Generate ICA packet data.", | ||
Long: strings.TrimSpace(`Generate ICA packet data.`), // TODO write more here | ||
Args: cobra.ExactArgs(1), | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
clientCtx, err := client.GetClientTxContext(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
cdc := codec.NewProtoCodec(clientCtx.InterfaceRegistry) | ||
memo, err := cmd.Flags().GetString(memoFlag) | ||
if err != nil { | ||
return err | ||
} | ||
packetDataBytes, err := generatePacketData(cdc, []byte(args[0]), memo) | ||
|
||
cmd.Println(string(packetDataBytes)) | ||
return nil | ||
}, | ||
} | ||
|
||
cmd.Flags().String(memoFlag, "", "") | ||
return cmd | ||
} | ||
|
||
func generatePacketData(cdc *codec.ProtoCodec, msgBytes []byte, memo string) ([]byte, error) { | ||
var msg sdk.Msg | ||
if err := cdc.UnmarshalInterfaceJSON(msgBytes, &msg); err != nil { | ||
return nil, err | ||
} | ||
|
||
icaPacketDataBytes, err := icatypes.SerializeCosmosTx(cdc, []sdk.Msg{msg}) | ||
if err != nil { | ||
return nil, err | ||
colin-axner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
icaPacketData := icatypes.InterchainAccountPacketData{ | ||
Type: icatypes.EXECUTE_TX, | ||
Data: icaPacketDataBytes, | ||
Memo: memo, | ||
} | ||
|
||
if err := icaPacketData.ValidateBasic(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return cdc.MarshalJSON(&icaPacketData) | ||
} |
132 changes: 132 additions & 0 deletions
132
modules/apps/27-interchain-accounts/host/client/cli/tx_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package cli | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/cosmos/cosmos-sdk/codec" | ||
codectypes "github.com/cosmos/cosmos-sdk/codec/types" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
"github.com/stretchr/testify/require" | ||
|
||
icatypes "github.com/cosmos/ibc-go/v5/modules/apps/27-interchain-accounts/types" | ||
colin-axner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
) | ||
|
||
const msgDelegateMessage = `{ | ||
"@type": "/cosmos.staking.v1beta1.MsgDelegate", | ||
"delegator_address": "cosmos15ccshhmp0gsx29qpqq6g4zmltnnvgmyu9ueuadh9y2nc5zj0szls5gtddz", | ||
"validator_address": "cosmosvaloper1qnk2n4nlkpw9xfqntladh74w6ujtulwnmxnh3k", | ||
"amount": { | ||
"denom": "stake", | ||
"amount": "1000" | ||
} | ||
}` | ||
|
||
const bankSendMessage = `{ | ||
"@type":"/cosmos.bank.v1beta1.MsgSend", | ||
"from_address":"cosmos15ccshhmp0gsx29qpqq6g4zmltnnvgmyu9ueuadh9y2nc5zj0szls5gtddz", | ||
"to_address":"cosmos10h9stc5v6ntgeygf5xf945njqq5h32r53uquvw", | ||
"amount": [ | ||
{ | ||
"denom": "stake", | ||
"amount": "1000" | ||
} | ||
] | ||
}` | ||
|
||
func TestGeneratePacketData(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
memo string | ||
expectedPass bool | ||
message string | ||
registerInterfaceFn func(registry codectypes.InterfaceRegistry) | ||
assertionFn func(msg sdk.Msg) | ||
colin-axner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}{ | ||
{ | ||
name: "packet data generation succeeds (MsgDelegate)", | ||
memo: "non-empty-memo", | ||
expectedPass: true, | ||
message: msgDelegateMessage, | ||
registerInterfaceFn: stakingtypes.RegisterInterfaces, | ||
assertionFn: func(msg sdk.Msg) { | ||
msgDelegate := msg.(*stakingtypes.MsgDelegate) | ||
require.Equal(t, "cosmos15ccshhmp0gsx29qpqq6g4zmltnnvgmyu9ueuadh9y2nc5zj0szls5gtddz", msgDelegate.DelegatorAddress) | ||
require.Equal(t, "cosmosvaloper1qnk2n4nlkpw9xfqntladh74w6ujtulwnmxnh3k", msgDelegate.ValidatorAddress) | ||
require.Equal(t, "stake", msgDelegate.Amount.Denom) | ||
require.Equal(t, uint64(1000), msgDelegate.Amount.Amount.Uint64()) | ||
}, | ||
}, | ||
{ | ||
name: "packet data generation succeeds (MsgSend)", | ||
memo: "non-empty-memo", | ||
expectedPass: true, | ||
message: bankSendMessage, | ||
registerInterfaceFn: banktypes.RegisterInterfaces, | ||
assertionFn: func(msg sdk.Msg) { | ||
bankSendMsg := msg.(*banktypes.MsgSend) | ||
require.Equal(t, "cosmos15ccshhmp0gsx29qpqq6g4zmltnnvgmyu9ueuadh9y2nc5zj0szls5gtddz", bankSendMsg.FromAddress) | ||
require.Equal(t, "cosmos10h9stc5v6ntgeygf5xf945njqq5h32r53uquvw", bankSendMsg.ToAddress) | ||
require.Equal(t, "stake", bankSendMsg.Amount.GetDenomByIndex(0)) | ||
require.Equal(t, uint64(1000), bankSendMsg.Amount[0].Amount.Uint64()) | ||
}, | ||
}, | ||
{ | ||
name: "empty memo is valid", | ||
memo: "", | ||
expectedPass: true, | ||
message: msgDelegateMessage, | ||
registerInterfaceFn: stakingtypes.RegisterInterfaces, | ||
assertionFn: func(msg sdk.Msg) { | ||
msgDelegate := msg.(*stakingtypes.MsgDelegate) | ||
require.Equal(t, "cosmos15ccshhmp0gsx29qpqq6g4zmltnnvgmyu9ueuadh9y2nc5zj0szls5gtddz", msgDelegate.DelegatorAddress) | ||
require.Equal(t, "cosmosvaloper1qnk2n4nlkpw9xfqntladh74w6ujtulwnmxnh3k", msgDelegate.ValidatorAddress) | ||
require.Equal(t, "stake", msgDelegate.Amount.Denom) | ||
require.Equal(t, uint64(1000), msgDelegate.Amount.Amount.Uint64()) | ||
}, | ||
}, | ||
{ | ||
name: "invalid message string", | ||
memo: "", | ||
expectedPass: false, | ||
message: "{}", | ||
registerInterfaceFn: func(codectypes.InterfaceRegistry) {}, | ||
assertionFn: func(sdk.Msg) {}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
colin-axner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
tt := tt | ||
ir := codectypes.NewInterfaceRegistry() | ||
tt.registerInterfaceFn(ir) | ||
|
||
cdc := codec.NewProtoCodec(ir) | ||
t.Run(tt.name, func(t *testing.T) { | ||
bz, err := generatePacketData(cdc, []byte(tt.message), tt.memo) | ||
if !tt.expectedPass { | ||
require.Error(t, err) | ||
require.Nil(t, bz) | ||
return | ||
} | ||
require.NoError(t, err) | ||
require.NotNil(t, bz) | ||
colin-axner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
packetData := icatypes.InterchainAccountPacketData{} | ||
err = cdc.UnmarshalJSON(bz, &packetData) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, err) | ||
colin-axner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
require.Equal(t, icatypes.EXECUTE_TX, packetData.Type) | ||
require.Equal(t, tt.memo, packetData.Memo) | ||
|
||
data := packetData.Data | ||
messages, err := icatypes.DeserializeCosmosTx(cdc, data) | ||
|
||
require.NoError(t, err) | ||
require.NotNil(t, messages) | ||
|
||
tt.assertionFn(messages[0]) | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See this PR.