Skip to content

Commit

Permalink
Refactored node module CLI and REST
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Aug 12, 2021
1 parent 6d19d29 commit a4bcd80
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 110 deletions.
43 changes: 43 additions & 0 deletions x/node/client/cli/flags.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,52 @@
package cli

import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/pflag"

hubtypes "github.com/sentinel-official/hub/types"
)

const (
flagProvider = "provider"
flagPlan = "plan"
flagPrice = "price"
flagRemoteURL = "remote-url"
flagStatus = "status"
)

func GetProvider(flags *pflag.FlagSet) (hubtypes.ProvAddress, error) {
s, err := flags.GetString(flagProvider)
if err != nil {
return nil, err
}
if s == "" {
return nil, nil
}

return hubtypes.ProvAddressFromBech32(s)
}

func GetPrice(flags *pflag.FlagSet) (sdk.Coins, error) {
s, err := flags.GetString(flagPrice)
if err != nil {
return nil, err
}
if s == "" {
return nil, nil
}

return sdk.ParseCoinsNormalized(s)
}

func GetStatus(flags *pflag.FlagSet) (hubtypes.Status, error) {
s, err := flags.GetString(flagStatus)
if err != nil {
return hubtypes.Unspecified, err
}
if s == "" {
return hubtypes.Unspecified, nil
}

return hubtypes.StatusFromString(s), nil
}
65 changes: 38 additions & 27 deletions x/node/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (

func queryNode() *cobra.Command {
cmd := &cobra.Command{
Use: "node",
Use: "node [address]",
Short: "Query a node",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -31,8 +31,12 @@ func queryNode() *cobra.Command {
qc = types.NewQueryServiceClient(ctx)
)

res, err := qc.QueryNode(context.Background(),
types.NewQueryNodeRequest(address))
res, err := qc.QueryNode(
context.Background(),
types.NewQueryNodeRequest(
address,
),
)
if err != nil {
return err
}
Expand All @@ -56,7 +60,7 @@ func queryNodes() *cobra.Command {
return err
}

provider, err := cmd.Flags().GetString(flagProvider)
provider, err := GetProvider(cmd.Flags())
if err != nil {
return err
}
Expand All @@ -66,7 +70,7 @@ func queryNodes() *cobra.Command {
return err
}

s, err := cmd.Flags().GetString(flagStatus)
status, err := GetStatus(cmd.Flags())
if err != nil {
return err
}
Expand All @@ -77,42 +81,47 @@ func queryNodes() *cobra.Command {
}

var (
status = hubtypes.StatusFromString(s)
qc = types.NewQueryServiceClient(ctx)
qc = types.NewQueryServiceClient(ctx)
)

if len(provider) > 0 {
address, err := hubtypes.ProvAddressFromBech32(provider)
if err != nil {
return err
}

res, err := qc.QueryNodesForProvider(context.Background(),
types.NewQueryNodesForProviderRequest(address, status, pagination))
if provider != nil {
res, err := qc.QueryNodesForProvider(
context.Background(),
types.NewQueryNodesForProviderRequest(
provider,
status,
pagination,
),
)
if err != nil {
return err
}

return ctx.PrintProto(res)
} else if plan > 0 {
return nil
} else {
res, err := qc.QueryNodes(context.Background(),
types.NewQueryNodesRequest(status, pagination))
if err != nil {
return err
}
}

return ctx.PrintProto(res)
res, err := qc.QueryNodes(
context.Background(),
types.NewQueryNodesRequest(
status,
pagination,
),
)
if err != nil {
return err
}

return ctx.PrintProto(res)
},
}

flags.AddQueryFlagsToCmd(cmd)
flags.AddPaginationFlagsToCmd(cmd, "nodes")
cmd.Flags().String(flagProvider, "", "provider address")
cmd.Flags().Uint64(flagPlan, 0, "subscription plan ID")
cmd.Flags().String(flagStatus, "", "status")
cmd.Flags().String(flagProvider, "", "filter by provider address")
cmd.Flags().Uint64(flagPlan, 0, "filter by subscription plan identity")
cmd.Flags().String(flagStatus, "", "filter by status")

return cmd
}
Expand All @@ -131,8 +140,10 @@ func queryParams() *cobra.Command {
qc = types.NewQueryServiceClient(ctx)
)

res, err := qc.QueryParams(context.Background(),
types.NewQueryParamsRequest())
res, err := qc.QueryParams(
context.Background(),
types.NewQueryParamsRequest(),
)
if err != nil {
return err
}
Expand Down
85 changes: 29 additions & 56 deletions x/node/client/cli/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/spf13/cobra"

hubtypes "github.com/sentinel-official/hub/types"
Expand All @@ -13,46 +12,31 @@ import (

func txRegister() *cobra.Command {
cmd := &cobra.Command{
Use: "register",
Use: "register [remote-url]",
Short: "Register a node",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

s, err := cmd.Flags().GetString(flagProvider)
provider, err := GetProvider(cmd.Flags())
if err != nil {
return err
}

var provider hubtypes.ProvAddress
if len(s) > 0 {
provider, err = hubtypes.ProvAddressFromBech32(s)
if err != nil {
return err
}
}

s, err = cmd.Flags().GetString(flagPrice)
price, err := GetPrice(cmd.Flags())
if err != nil {
return err
}

var price sdk.Coins
if len(s) > 0 {
price, err = sdk.ParseCoinsNormalized(s)
if err != nil {
return err
}
}

remoteURL, err := cmd.Flags().GetString(flagRemoteURL)
if err != nil {
return err
}

msg := types.NewMsgRegisterRequest(ctx.FromAddress, provider, price, remoteURL)
msg := types.NewMsgRegisterRequest(
ctx.FromAddress,
provider,
price,
args[0],
)
if err := msg.ValidateBasic(); err != nil {
return err
}
Expand All @@ -62,11 +46,8 @@ func txRegister() *cobra.Command {
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String(flagProvider, "", "node provider address")
cmd.Flags().String(flagPrice, "", "node price per Gigabyte")
cmd.Flags().String(flagRemoteURL, "", "node remote URL")

_ = cmd.MarkFlagRequired(flagRemoteURL)
cmd.Flags().String(flagProvider, "", "provider address of the node")
cmd.Flags().String(flagPrice, "", "price per Gigabyte of the node")

return cmd
}
Expand All @@ -81,38 +62,27 @@ func txUpdate() *cobra.Command {
return err
}

s, err := cmd.Flags().GetString(flagProvider)
provider, err := GetProvider(cmd.Flags())
if err != nil {
return err
}

var provider hubtypes.ProvAddress
if len(s) > 0 {
provider, err = hubtypes.ProvAddressFromBech32(s)
if err != nil {
return err
}
}

s, err = cmd.Flags().GetString(flagPrice)
price, err := GetPrice(cmd.Flags())
if err != nil {
return err
}

var price sdk.Coins
if len(s) > 0 {
price, err = sdk.ParseCoinsNormalized(s)
if err != nil {
return err
}
}

remoteURL, err := cmd.Flags().GetString(flagRemoteURL)
if err != nil {
return err
}

msg := types.NewMsgUpdateRequest(ctx.FromAddress.Bytes(), provider, price, remoteURL)
msg := types.NewMsgUpdateRequest(
ctx.FromAddress.Bytes(),
provider,
price,
remoteURL,
)
if err := msg.ValidateBasic(); err != nil {
return err
}
Expand All @@ -122,25 +92,28 @@ func txUpdate() *cobra.Command {
}

flags.AddTxFlagsToCmd(cmd)
cmd.Flags().String(flagProvider, "", "node provider address")
cmd.Flags().String(flagPrice, "", "node price per Gigabyte")
cmd.Flags().String(flagRemoteURL, "", "node remote URL")
cmd.Flags().String(flagProvider, "", "provider address of the node")
cmd.Flags().String(flagPrice, "", "price per Gigabyte of the node")
cmd.Flags().String(flagRemoteURL, "", "remote URL of the node")

return cmd
}

func txSetStatus() *cobra.Command {
cmd := &cobra.Command{
Use: "status-set [Active | Inactive]",
Short: "Set a node status",
Use: "status-set [status]",
Short: "Set status for a node",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}

msg := types.NewMsgSetStatusRequest(ctx.FromAddress.Bytes(), hubtypes.StatusFromString(args[0]))
msg := types.NewMsgSetStatusRequest(
ctx.FromAddress.Bytes(),
hubtypes.StatusFromString(args[0]),
)
if err := msg.ValidateBasic(); err != nil {
return err
}
Expand Down
Loading

0 comments on commit a4bcd80

Please sign in to comment.