Skip to content

Commit 9d11179

Browse files
committed
fix: prices validation in messages
1 parent 0710082 commit 9d11179

File tree

7 files changed

+56
-42
lines changed

7 files changed

+56
-42
lines changed

x/node/client/cli/tx.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/cosmos/cosmos-sdk/client"
77
"github.com/cosmos/cosmos-sdk/client/flags"
88
"github.com/cosmos/cosmos-sdk/client/tx"
9-
sdk "github.com/cosmos/cosmos-sdk/types"
109
"github.com/spf13/cobra"
1110

1211
base "github.com/sentinel-official/hub/v12/types"
@@ -16,21 +15,21 @@ import (
1615

1716
func txRegisterNode() *cobra.Command {
1817
cmd := &cobra.Command{
19-
Use: "register-node [remote-url] [gigabyte-prices] [hourly-prices]",
18+
Use: "register-node [remote-url]",
2019
Short: "Register a new node with a remote URL and pricing details",
21-
Args: cobra.ExactArgs(3),
20+
Args: cobra.ExactArgs(1),
2221
RunE: func(cmd *cobra.Command, args []string) error {
2322
ctx, err := client.GetClientTxContext(cmd)
2423
if err != nil {
2524
return err
2625
}
2726

28-
gigabytePrices, err := sdk.ParseDecCoins(args[1])
27+
gigabytePrices, err := GetGigabytePrices(cmd.Flags())
2928
if err != nil {
3029
return err
3130
}
3231

33-
hourlyPrices, err := sdk.ParseDecCoins(args[2])
32+
hourlyPrices, err := GetHourlyPrices(cmd.Flags())
3433
if err != nil {
3534
return err
3635
}
@@ -52,7 +51,7 @@ func txRegisterNode() *cobra.Command {
5251
flags.AddTxFlagsToCmd(cmd)
5352
cmd.Flags().String(flagGigabytePrices, "", "prices for one gigabyte of bandwidth (e.g., 1000token")
5453
cmd.Flags().String(flagHourlyPrices, "", "prices for one hour of bandwidth (e.g., 500token")
55-
cmd.Flags().String(flagRemoteURL, "", "remote URL address for the node")
54+
5655
return cmd
5756
}
5857

x/node/keeper/msg_handler.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,11 @@ func (k *Keeper) HandleMsgRegisterNode(ctx sdk.Context, msg *v3.MsgRegisterNodeR
6161
}
6262

6363
func (k *Keeper) HandleMsgUpdateNodeDetails(ctx sdk.Context, msg *v3.MsgUpdateNodeDetailsRequest) (*v3.MsgUpdateNodeDetailsResponse, error) {
64-
if msg.GigabytePrices != nil {
65-
if !k.IsValidGigabytePrices(ctx, msg.GigabytePrices) {
66-
return nil, types.NewErrorInvalidPrices(msg.GigabytePrices)
67-
}
64+
if !k.IsValidGigabytePrices(ctx, msg.GigabytePrices) {
65+
return nil, types.NewErrorInvalidPrices(msg.GigabytePrices)
6866
}
69-
if msg.HourlyPrices != nil {
70-
if !k.IsValidHourlyPrices(ctx, msg.HourlyPrices) {
71-
return nil, types.NewErrorInvalidPrices(msg.HourlyPrices)
72-
}
67+
if !k.IsValidHourlyPrices(ctx, msg.HourlyPrices) {
68+
return nil, types.NewErrorInvalidPrices(msg.HourlyPrices)
7369
}
7470

7571
nodeAddr, err := base.NodeAddressFromBech32(msg.From)
@@ -82,12 +78,8 @@ func (k *Keeper) HandleMsgUpdateNodeDetails(ctx sdk.Context, msg *v3.MsgUpdateNo
8278
return nil, types.NewErrorNodeNotFound(nodeAddr)
8379
}
8480

85-
if msg.GigabytePrices != nil {
86-
node.GigabytePrices = msg.GigabytePrices
87-
}
88-
if msg.HourlyPrices != nil {
89-
node.HourlyPrices = msg.HourlyPrices
90-
}
81+
node.GigabytePrices = msg.GigabytePrices
82+
node.HourlyPrices = msg.HourlyPrices
9183
if msg.RemoteURL != "" {
9284
node.RemoteURL = msg.RemoteURL
9385
}

x/node/keeper/params.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ func (k *Keeper) StakingShare(ctx sdk.Context) sdkmath.LegacyDec {
7676

7777
// IsValidGigabytePrices checks if the provided gigabyte prices are valid based on the minimum prices defined in the module's parameters.
7878
func (k *Keeper) IsValidGigabytePrices(ctx sdk.Context, prices sdk.DecCoins) bool {
79+
if prices.Len() == 0 {
80+
return true
81+
}
82+
7983
minPrices := k.MinGigabytePrices(ctx)
8084
for _, coin := range minPrices {
8185
amount := prices.AmountOf(coin.Denom)
@@ -89,6 +93,10 @@ func (k *Keeper) IsValidGigabytePrices(ctx sdk.Context, prices sdk.DecCoins) boo
8993

9094
// IsValidHourlyPrices checks if the provided hourly prices are valid based on the minimum prices defined in the module's parameters.
9195
func (k *Keeper) IsValidHourlyPrices(ctx sdk.Context, prices sdk.DecCoins) bool {
96+
if prices.Len() == 0 {
97+
return true
98+
}
99+
92100
minPrices := k.MinHourlyPrices(ctx)
93101
for _, coin := range minPrices {
94102
amount := prices.AmountOf(coin.Denom)

x/node/types/v3/msg.go

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,9 @@ func (m *MsgRegisterNodeRequest) ValidateBasic() error {
3535
if _, err := sdk.AccAddressFromBech32(m.From); err != nil {
3636
return sdkerrors.Wrap(types.ErrInvalidMessage, err.Error())
3737
}
38-
if m.GigabytePrices == nil {
39-
return sdkerrors.Wrap(types.ErrInvalidMessage, "gigabyte_prices cannot be nil")
40-
}
4138
if !m.GigabytePrices.IsValid() {
4239
return sdkerrors.Wrap(types.ErrInvalidMessage, "gigabyte_prices must be valid")
4340
}
44-
if m.HourlyPrices == nil {
45-
return sdkerrors.Wrap(types.ErrInvalidMessage, "hourly_prices cannot be nil")
46-
}
4741
if !m.HourlyPrices.IsValid() {
4842
return sdkerrors.Wrap(types.ErrInvalidMessage, "hourly_prices must be valid")
4943
}
@@ -93,15 +87,11 @@ func (m *MsgUpdateNodeDetailsRequest) ValidateBasic() error {
9387
if _, err := base.NodeAddressFromBech32(m.From); err != nil {
9488
return sdkerrors.Wrap(types.ErrInvalidMessage, err.Error())
9589
}
96-
if m.GigabytePrices != nil {
97-
if !m.GigabytePrices.IsValid() {
98-
return sdkerrors.Wrap(types.ErrInvalidMessage, "gigabyte_prices must be valid")
99-
}
90+
if !m.GigabytePrices.IsValid() {
91+
return sdkerrors.Wrap(types.ErrInvalidMessage, "gigabyte_prices must be valid")
10092
}
101-
if m.HourlyPrices != nil {
102-
if !m.HourlyPrices.IsValid() {
103-
return sdkerrors.Wrap(types.ErrInvalidMessage, "hourly_prices must be valid")
104-
}
93+
if !m.HourlyPrices.IsValid() {
94+
return sdkerrors.Wrap(types.ErrInvalidMessage, "hourly_prices must be valid")
10595
}
10696
if m.RemoteURL != "" {
10797
if len(m.RemoteURL) > 64 {

x/plan/client/cli/flags.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cli
2+
3+
import (
4+
sdk "github.com/cosmos/cosmos-sdk/types"
5+
"github.com/spf13/pflag"
6+
)
7+
8+
const (
9+
flagPrices = "prices"
10+
flagPrivate = "private"
11+
)
12+
13+
func GetPrices(flags *pflag.FlagSet) (sdk.DecCoins, error) {
14+
s, err := flags.GetString(flagPrices)
15+
if err != nil {
16+
return nil, err
17+
}
18+
if s == "" {
19+
return nil, nil
20+
}
21+
22+
return sdk.ParseDecCoins(s)
23+
}
24+
25+
func GetPrivate(flags *pflag.FlagSet) (bool, error) {
26+
return flags.GetBool(flagPrivate)
27+
}

x/plan/client/cli/tx.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/cosmos/cosmos-sdk/client"
1010
"github.com/cosmos/cosmos-sdk/client/flags"
1111
"github.com/cosmos/cosmos-sdk/client/tx"
12-
sdk "github.com/cosmos/cosmos-sdk/types"
1312
"github.com/spf13/cobra"
1413

1514
base "github.com/sentinel-official/hub/v12/types"
@@ -19,9 +18,9 @@ import (
1918

2019
func txCreatePlan() *cobra.Command {
2120
cmd := &cobra.Command{
22-
Use: "create-plan [bytes] [duration] [prices] [private]",
21+
Use: "create-plan [bytes] [duration]",
2322
Short: "Create a new subscription plan with bytes, duration and pricing details",
24-
Args: cobra.ExactArgs(4),
23+
Args: cobra.ExactArgs(2),
2524
RunE: func(cmd *cobra.Command, args []string) error {
2625
ctx, err := client.GetClientTxContext(cmd)
2726
if err != nil {
@@ -38,12 +37,12 @@ func txCreatePlan() *cobra.Command {
3837
return err
3938
}
4039

41-
prices, err := sdk.ParseDecCoins(args[2])
40+
prices, err := GetPrices(cmd.Flags())
4241
if err != nil {
4342
return err
4443
}
4544

46-
private, err := strconv.ParseBool(args[3])
45+
private, err := GetPrivate(cmd.Flags())
4746
if err != nil {
4847
return err
4948
}
@@ -64,6 +63,8 @@ func txCreatePlan() *cobra.Command {
6463
}
6564

6665
flags.AddTxFlagsToCmd(cmd)
66+
cmd.Flags().Bool(flagPrivate, false, "specify if the plan is private")
67+
cmd.Flags().String(flagPrices, "", "specify the list of prices (e.g., 1000token)")
6768

6869
return cmd
6970
}

x/plan/types/v3/msg.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ func (m *MsgCreatePlanRequest) ValidateBasic() error {
5252
if m.Duration < 0 {
5353
return sdkerrors.Wrap(types.ErrInvalidMessage, "duration cannot be negative")
5454
}
55-
if m.Prices == nil {
56-
return sdkerrors.Wrap(types.ErrInvalidMessage, "prices cannot be nil")
57-
}
5855
if !m.Prices.IsValid() {
5956
return sdkerrors.Wrap(types.ErrInvalidMessage, "prices must be valid")
6057
}

0 commit comments

Comments
 (0)