Skip to content

Commit

Permalink
chore: add cmd (#497)
Browse files Browse the repository at this point in the history
* add cmd

* fix default endpoint of sp issue in cmd
  • Loading branch information
alexgao001 authored Oct 18, 2023
1 parent acc5aa3 commit a1d5ac0
Show file tree
Hide file tree
Showing 5 changed files with 155 additions and 25 deletions.
4 changes: 4 additions & 0 deletions sdk/client/gnfd_tm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions x/sp/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,7 @@ func CmdMaintenanceRecordsBySPOperatorAddress() *cobra.Command {
return clientCtx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)

return cmd
}

Expand Down
101 changes: 100 additions & 1 deletion x/storage/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -31,7 +36,10 @@ func GetQueryCmd() *cobra.Command {
CmdVerifyPermission(),
CmdHeadGroup(),
CmdListGroups(),
CmdHeadGroupMember())
CmdHeadGroupMember(),
CmdQueryAccountPolicy(),
CmdQueryGroupPolicy(),
)

// this line is used by starport scaffolding # 1

Expand Down Expand Up @@ -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
}
60 changes: 45 additions & 15 deletions x/storage/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -37,6 +39,8 @@ func GetTxCmd() *cobra.Command {
CmdUpdateBucketInfo(),
CmdMirrorBucket(),
CmdDiscontinueBucket(),
CmdMigrateBucket(),
CmdCancelMigrateBucket(),
)

cmd.AddCommand(
Expand All @@ -63,8 +67,6 @@ func GetTxCmd() *cobra.Command {
CmdPutPolicy(),
CmdDeletePolicy(),
)

cmd.AddCommand(CmdCancelMigrateBucket())
// this line is used by starport scaffolding # 1

return cmd
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down
13 changes: 6 additions & 7 deletions x/storage/client/cli/tx_cancel_migrate_bucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a1d5ac0

Please sign in to comment.