Skip to content

Commit

Permalink
feat: add missing ibc perm cmd (#275)
Browse files Browse the repository at this point in the history
* add missing ibc perm cmd

* fix test to be determinitic
  • Loading branch information
beer-1 authored Oct 4, 2024
1 parent 30d4e29 commit 1298e7f
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 6 deletions.
5 changes: 3 additions & 2 deletions x/gov/keeper/custom_grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ func Test_CustomGrpcQuerier_Proposal(t *testing.T) {
func Test_CustomGrpcQuerier_TallyResult(t *testing.T) {
ctx, input := createDefaultTestInput(t)

setupVesting(t, ctx, input)
now := time.Now().UTC()
setupVesting(t, ctx, input, now)

proposal, err := input.GovKeeper.SubmitProposal(ctx, nil, "", "test", "description", addrs[0], false)
require.NoError(t, err)
Expand Down Expand Up @@ -188,7 +189,7 @@ func Test_CustomGrpcQuerier_TallyResult(t *testing.T) {
require.NoError(t, err)

// 15 minutes passed
ctx = ctx.WithBlockTime(time.Now().UTC().Add(time.Minute * 15))
ctx = ctx.WithBlockTime(now.Add(time.Minute * 15))
cacheCtx, _ := ctx.CacheContext()

quorumReached, passed, burnDeposits, tallyResults, err := input.GovKeeper.Tally(cacheCtx, params, proposal)
Expand Down
8 changes: 4 additions & 4 deletions x/gov/keeper/tally_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func Test_isLowThresholdProposal(t *testing.T) {
require.False(t, keeper.IsLowThresholdProposal(params, proposal))
}

func setupVesting(t *testing.T, ctx sdk.Context, input TestKeepers) {
func setupVesting(t *testing.T, ctx sdk.Context, input TestKeepers, now time.Time) {
err := input.MoveKeeper.PublishModuleBundle(ctx, vmtypes.TestAddress, vmtypes.NewModuleBundle(vmtypes.NewModule(vestingModule)), movetypes.UpgradePolicy_COMPATIBLE)
require.NoError(t, err)

Expand All @@ -68,7 +68,6 @@ func setupVesting(t *testing.T, ctx sdk.Context, input TestKeepers) {
err = input.MoveKeeper.ExecuteEntryFunctionJSON(ctx, creatorAddr, moduleAddr, moduleName, "create_vesting_store", []vmtypes.TypeTag{}, []string{fmt.Sprintf("\"%s\"", metadata)})
require.NoError(t, err)

now := time.Now().UTC()
ctx = ctx.WithBlockTime(now)

// add vesting
Expand Down Expand Up @@ -104,7 +103,8 @@ func setupVesting(t *testing.T, ctx sdk.Context, input TestKeepers) {
func Test_Tally(t *testing.T) {
ctx, input := createDefaultTestInput(t)

setupVesting(t, ctx, input)
now := time.Now().UTC()
setupVesting(t, ctx, input, now)

proposal, err := input.GovKeeper.SubmitProposal(ctx, nil, "", "test", "description", addrs[0], false)
require.NoError(t, err)
Expand Down Expand Up @@ -168,7 +168,7 @@ func Test_Tally(t *testing.T) {
require.NoError(t, err)

// 15 minutes passed
ctx = ctx.WithBlockTime(time.Now().UTC().Add(time.Minute * 15))
ctx = ctx.WithBlockTime(now.Add(time.Minute * 15))

quorumReached, passed, burnDeposits, tallyResults, err := input.GovKeeper.Tally(ctx, params, proposal)
require.NoError(t, err)
Expand Down
26 changes: 26 additions & 0 deletions x/ibc/perm/client/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package cli

import (
"cosmossdk.io/core/address"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
)

// NewTxCmd returns the transaction commands for IBC non-fungible token transfer
func NewTxCmd(ac address.Codec) *cobra.Command {
txCmd := &cobra.Command{
Use: "ibc-perm",
Short: "IBC channel permission tx subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}

txCmd.AddCommand(
NewUpdateAdminCmd(ac),
NewUpdatePermissionedRelayersCmd(ac),
)

return txCmd
}
88 changes: 88 additions & 0 deletions x/ibc/perm/client/cli/tx.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package cli

import (
"fmt"
"strings"

"github.com/spf13/cobra"

"cosmossdk.io/core/address"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/version"

"github.com/initia-labs/initia/x/ibc/perm/types"
)

// NewUpdateAdmin returns the command to create a MsgUpdateAdmin transaction
func NewUpdateAdminCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "update-admin [port] [channel] [admin]",
Short: "Transfer a ownership of a channel to a new admin",
Example: fmt.Sprintf("%s tx ibc-perm update-admin [port] [channel] [admin]", version.AppName),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

sender, err := ac.BytesToString(clientCtx.GetFromAddress())
if err != nil {
return err
}

port := args[0]
channel := args[1]
admin := args[2]

if _, err := ac.StringToBytes(admin); err != nil {
return err
}

msg := types.NewMsgUpdateAdmin(sender, port, channel, admin)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)
return cmd
}

// NewUpdatePermissionedRelayersCmd returns the command to create a MsgUpdatePermissionedRelayers transaction
func NewUpdatePermissionedRelayersCmd(ac address.Codec) *cobra.Command {
cmd := &cobra.Command{
Use: "update-relayers [port] [channel] [relayer],...,[relayer]",
Short: "Give a list of relayers permission to relay packets on a channel",
Example: fmt.Sprintf("%s tx ibc-perm update-relayers [port] [channel] [relayer],...,[relayer]", version.AppName),
Args: cobra.ExactArgs(3),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

sender, err := ac.BytesToString(clientCtx.GetFromAddress())
if err != nil {
return err
}

port := args[0]
channel := args[1]
relayers := strings.Split(args[2], ",")

for _, relayer := range relayers {
if _, err := ac.StringToBytes(relayer); err != nil {
return err
}
}

msg := types.NewMsgUpdatePermissionedRelayers(sender, port, channel, relayers)
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)
},
}

flags.AddTxFlagsToCmd(cmd)
return cmd
}
7 changes: 7 additions & 0 deletions x/ibc/perm/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"cosmossdk.io/core/appmodule"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/grpc-ecosystem/grpc-gateway/runtime"
"github.com/spf13/cobra"

"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/codec"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"

"github.com/initia-labs/initia/x/ibc/perm/client/cli"
"github.com/initia-labs/initia/x/ibc/perm/keeper"
"github.com/initia-labs/initia/x/ibc/perm/types"
)
Expand Down Expand Up @@ -76,6 +78,11 @@ func (AppModuleBasic) RegisterGRPCGatewayRoutes(clientCtx client.Context, mux *r
}
}

// GetTxCmd implements AppModuleBasic interface
func (b AppModuleBasic) GetTxCmd() *cobra.Command {
return cli.NewTxCmd(b.cdc.InterfaceRegistry().SigningContext().AddressCodec())
}

// AppModule represents the AppModule for this module
type AppModule struct {
AppModuleBasic
Expand Down

0 comments on commit 1298e7f

Please sign in to comment.