From a1d5ac05ecefb9bfa13e5e0387f2d80d8f76dba1 Mon Sep 17 00:00:00 2001 From: Alexxxxxx <118710506+alexgao001@users.noreply.github.com> Date: Wed, 18 Oct 2023 11:34:36 +0800 Subject: [PATCH] chore: add cmd (#497) * add cmd * fix default endpoint of sp issue in cmd --- sdk/client/gnfd_tm.go | 4 + x/sp/client/cli/query.go | 2 - x/storage/client/cli/query.go | 101 +++++++++++++++++- x/storage/client/cli/tx.go | 60 ++++++++--- .../client/cli/tx_cancel_migrate_bucket.go | 13 ++- 5 files changed, 155 insertions(+), 25 deletions(-) diff --git a/sdk/client/gnfd_tm.go b/sdk/client/gnfd_tm.go index c8a82bab7..fb3fb5d22 100644 --- a/sdk/client/gnfd_tm.go +++ b/sdk/client/gnfd_tm.go @@ -8,6 +8,10 @@ import ( "github.com/cometbft/cometbft/votepool" ) +func (c *GreenfieldClient) ABCIInfo(ctx context.Context) (*ctypes.ResultABCIInfo, error) { + return c.tendermintClient.ABCIInfo(ctx) +} + // GetBlock by height, gets the latest block if height is nil func (c *GreenfieldClient) GetBlock(ctx context.Context, height *int64) (*ctypes.ResultBlock, error) { return c.tendermintClient.Block(ctx, height) diff --git a/x/sp/client/cli/query.go b/x/sp/client/cli/query.go index c51b6b507..b8dcdc820 100644 --- a/x/sp/client/cli/query.go +++ b/x/sp/client/cli/query.go @@ -171,9 +171,7 @@ func CmdMaintenanceRecordsBySPOperatorAddress() *cobra.Command { return clientCtx.PrintProto(res) }, } - flags.AddQueryFlagsToCmd(cmd) - return cmd } diff --git a/x/storage/client/cli/query.go b/x/storage/client/cli/query.go index 1da91bc8e..65d784838 100644 --- a/x/storage/client/cli/query.go +++ b/x/storage/client/cli/query.go @@ -3,6 +3,11 @@ package cli import ( "context" "fmt" + "strings" + + gnfd "github.com/bnb-chain/greenfield/types" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/cosmos/cosmos-sdk/version" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" @@ -31,7 +36,10 @@ func GetQueryCmd() *cobra.Command { CmdVerifyPermission(), CmdHeadGroup(), CmdListGroups(), - CmdHeadGroupMember()) + CmdHeadGroupMember(), + CmdQueryAccountPolicy(), + CmdQueryGroupPolicy(), + ) // this line is used by starport scaffolding # 1 @@ -318,3 +326,94 @@ func CmdHeadGroupMember() *cobra.Command { return cmd } + +func CmdQueryAccountPolicy() *cobra.Command { + cmd := &cobra.Command{ + Use: "account-policy [grn] [principle-address]", + Short: "Query the policy for a account that enforced on the resource", + Long: strings.TrimSpace( + fmt.Sprintf(`Query the polciy that a account has on the resource. + +Examples: + $ %s query %s account-policy grn:o::bucketName/objectName 0x.... + `, version.AppName, types.ModuleName), + ), + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) (err error) { + grnStr := args[0] + var grn gnfd.GRN + err = grn.ParseFromString(grnStr, false) + if err != nil { + return err + } + principalAcc, err := sdk.AccAddressFromHexUnsafe(args[1]) + if err != nil { + return err + } + + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + queryClient := types.NewQueryClient(clientCtx) + params := &types.QueryPolicyForAccountRequest{ + Resource: grn.String(), + PrincipalAddress: principalAcc.String(), + } + res, err := queryClient.QueryPolicyForAccount(cmd.Context(), params) + if err != nil { + return err + } + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} + +func CmdQueryGroupPolicy() *cobra.Command { + cmd := &cobra.Command{ + Use: "group-policy [grn] [principle-group-id]", + Short: "Query the policy for a group that enforced on the resource", + Long: strings.TrimSpace( + fmt.Sprintf(`Query the policy for a group that enforced on the resource + +Examples: + $ %s query %s group-policy grn:o::bucketName/objectName 1 + `, version.AppName, types.ModuleName), + ), + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) (err error) { + grnStr := args[0] + var grn gnfd.GRN + err = grn.ParseFromString(grnStr, false) + if err != nil { + return err + } + groupID, ok := sdk.NewIntFromString(args[1]) + if !ok { + return fmt.Errorf("failed to convert group id") + } + clientCtx, err := client.GetClientQueryContext(cmd) + if err != nil { + return err + } + queryClient := types.NewQueryClient(clientCtx) + params := &types.QueryPolicyForGroupRequest{ + Resource: grn.String(), + PrincipalGroupId: groupID.String(), + } + res, err := queryClient.QueryPolicyForGroup(cmd.Context(), params) + if err != nil { + return err + } + return clientCtx.PrintProto(res) + }, + } + + flags.AddQueryFlagsToCmd(cmd) + + return cmd +} diff --git a/x/storage/client/cli/tx.go b/x/storage/client/cli/tx.go index 8ecb8ea24..469a8d91f 100644 --- a/x/storage/client/cli/tx.go +++ b/x/storage/client/cli/tx.go @@ -9,6 +9,8 @@ import ( "strings" "time" + "github.com/bnb-chain/greenfield/types/common" + cmath "cosmossdk.io/math" "github.com/cosmos/cosmos-sdk/client" "github.com/cosmos/cosmos-sdk/client/flags" @@ -37,6 +39,8 @@ func GetTxCmd() *cobra.Command { CmdUpdateBucketInfo(), CmdMirrorBucket(), CmdDiscontinueBucket(), + CmdMigrateBucket(), + CmdCancelMigrateBucket(), ) cmd.AddCommand( @@ -63,8 +67,6 @@ func GetTxCmd() *cobra.Command { CmdPutPolicy(), CmdDeletePolicy(), ) - - cmd.AddCommand(CmdCancelMigrateBucket()) // this line is used by starport scaffolding # 1 return cmd @@ -323,19 +325,6 @@ func CmdCreateObject() *cobra.Command { approveTimeoutHeight, approveSignatureBytes, ) - primarySP, err := cmd.Flags().GetString(FlagPrimarySP) - if err != nil { - return err - } - _, spKeyName, _, err := GetPrimarySPField(clientCtx.Keyring, primarySP) - if err != nil { - return err - } - sig, _, err := clientCtx.Keyring.Sign(spKeyName, msg.GetApprovalBytes()) - if err != nil { - return err - } - msg.PrimarySpApproval.Sig = sig if err := msg.ValidateBasic(); err != nil { return err @@ -961,6 +950,47 @@ func CmdDiscontinueBucket() *cobra.Command { return cmd } +func CmdMigrateBucket() *cobra.Command { + cmd := &cobra.Command{ + Use: "migrate-bucket [bucket-name] [dest-primary-sp-id]", + Short: "migrate a bucket to another primary storage provider by user", + Args: cobra.ExactArgs(2), + RunE: func(cmd *cobra.Command, args []string) (err error) { + bucketName := args[0] + destPrimarySpID, err := strconv.ParseUint(args[1], 10, 32) + if err != nil { + return err + } + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + approveSignature, _ := cmd.Flags().GetString(FlagApproveSignature) + approveTimeoutHeight, _ := cmd.Flags().GetUint64(FlagApproveTimeoutHeight) + approveSignatureBytes, err := hex.DecodeString(approveSignature) + if err != nil { + return err + } + spApproval := &common.Approval{ExpiredHeight: approveTimeoutHeight, Sig: approveSignatureBytes} + + msg := &types.MsgMigrateBucket{ + Operator: clientCtx.GetFromAddress().String(), + BucketName: bucketName, + DstPrimarySpId: uint32(destPrimarySpID), + DstPrimarySpApproval: spApproval, + } + if err = msg.ValidateBasic(); err != nil { + return err + } + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + cmd.Flags().AddFlagSet(FlagSetApproval()) + return cmd +} + func CmdMirrorObject() *cobra.Command { cmd := &cobra.Command{ Use: "mirror-object", diff --git a/x/storage/client/cli/tx_cancel_migrate_bucket.go b/x/storage/client/cli/tx_cancel_migrate_bucket.go index b3a32a621..32895d3e4 100644 --- a/x/storage/client/cli/tx_cancel_migrate_bucket.go +++ b/x/storage/client/cli/tx_cancel_migrate_bucket.go @@ -15,21 +15,20 @@ var _ = strconv.Itoa(0) func CmdCancelMigrateBucket() *cobra.Command { cmd := &cobra.Command{ - Use: "cancel-migrate-bucket", - Short: "Broadcast message CancelMigrateBucket", - Args: cobra.ExactArgs(0), + Use: "cancel-migrate-bucket [bucket-name]", + Short: "cancel a existing bucket migration", + Args: cobra.ExactArgs(1), RunE: func(cmd *cobra.Command, args []string) (err error) { - + argBucketName := args[0] clientCtx, err := client.GetClientTxContext(cmd) if err != nil { return err } - msg := types.NewMsgCancelMigrateBucket( clientCtx.GetFromAddress(), - "bucket-name", + argBucketName, ) - if err := msg.ValidateBasic(); err != nil { + if err = msg.ValidateBasic(); err != nil { return err } return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), msg)