Skip to content

Commit

Permalink
add cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgao001 committed Sep 28, 2023
1 parent 3c6142f commit 9b1ef1a
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 23 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
57 changes: 57 additions & 0 deletions x/sp/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func GetQueryCmd() *cobra.Command {
CmdStorageProvider(),
CmdStorageProviderByOperatorAddress(),
CmdMaintenanceRecordsBySPOperatorAddress(),
CmdStorageProviderPrice(),
CmdStorageProviderGlobalPrice(),
)

// this line is used by starport scaffolding # 1
Expand Down Expand Up @@ -169,8 +171,63 @@ func CmdMaintenanceRecordsBySPOperatorAddress() *cobra.Command {
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func CmdStorageProviderPrice() *cobra.Command {
cmd := &cobra.Command{
Use: "price [sp-address]",
Short: "Query storage provider prices, including read, store price and free quota",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
spAddr, err := sdk.AccAddressFromHexUnsafe(args[0])
if err != nil {
return err
}

clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
res, err := types.NewQueryClient(clientCtx).
QuerySpStoragePrice(cmd.Context(), &types.QuerySpStoragePriceRequest{
SpAddr: spAddr.String(),
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}

func CmdStorageProviderGlobalPrice() *cobra.Command {
cmd := &cobra.Command{
Use: "global-price [timestamp]",
Short: "Query the global price at a specific time(in unix), return latest price price if timestamp is 0",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
ts, err := strconv.ParseInt(args[0], 10, 64)
if err != nil {
return err
}
res, err := types.NewQueryClient(clientCtx).
QueryGlobalSpStorePriceByTime(cmd.Context(), &types.QueryGlobalSpStorePriceByTimeRequest{
Timestamp: ts,
})
if err != nil {
return err
}
return clientCtx.PrintProto(res)
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
51 changes: 51 additions & 0 deletions x/sp/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cli

import (
"fmt"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -37,6 +38,7 @@ func GetTxCmd() *cobra.Command {
CmdEditStorageProvider(),
CmdGrantDepositAuthorization(),
CmdUpdateStorageProviderStatus(),
CmdUpdateStorageProviderStoragePrice(),
)

// this line is used by starport scaffolding # 1
Expand Down Expand Up @@ -715,3 +717,52 @@ Examples:
cmd.Flags().Int64(FlagDuration, 0, "maintenance duration requested by a SP")
return cmd
}

func CmdUpdateStorageProviderStoragePrice() *cobra.Command {
cmd := &cobra.Command{
Use: "update-price [sp-address] [read-price] [store-price] [free-read-quota]",
Short: "Update prices and free read quota of a storage provider, all prices in BNB wei",
Long: strings.TrimSpace(
fmt.Sprintf(`update the storage provider read, store price and free read quota, if there is no change to a specific price, the current value should also be provided.
Examples:
$ %s tx %s update-price 0x... 10000000 10000000 10000000
`, version.AppName, types.ModuleName),
),
Args: cobra.ExactArgs(4),
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
spAddress, err := sdk.AccAddressFromHexUnsafe(args[0])
if err != nil {
return err
}
readPrice, err := sdk.NewDecFromStr(args[1])
if err != nil {
return err
}
storePrice, err := sdk.NewDecFromStr(args[2])
if err != nil {
return err
}
quota, err := strconv.ParseUint(args[3], 10, 64)
if err != nil {
return err
}
msg := types.MsgUpdateSpStoragePrice{
SpAddress: spAddress.String(),
ReadPrice: readPrice,
StorePrice: storePrice,
FreeReadQuota: quota,
}
if err = msg.ValidateBasic(); err != nil {
return err
}
return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}
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 @@ -960,6 +949,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 9b1ef1a

Please sign in to comment.