Skip to content

Commit 2fdae48

Browse files
committed
feat: using bytes and duration instead gigabytes and hours in plan
1 parent 8e40dba commit 2fdae48

File tree

12 files changed

+352
-224
lines changed

12 files changed

+352
-224
lines changed

proto/sentinel/plan/v3/events.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ option (gogoproto.goproto_getters_all) = false;
1010
message EventCreate {
1111
uint64 plan_id = 1 [(gogoproto.customname) = "PlanID"];
1212
string prov_address = 2;
13-
int64 gigabytes = 3;
14-
int64 hours = 4;
13+
string bytes = 3;
14+
string duration = 4;
1515
string prices = 5;
1616
}
1717

proto/sentinel/plan/v3/msg.proto

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ syntax = "proto3";
22
package sentinel.plan.v3;
33

44
import "gogoproto/gogo.proto";
5+
import "google/protobuf/duration.proto";
56
import "sentinel/types/v1/price.proto";
67
import "sentinel/types/v1/renewal.proto";
78
import "sentinel/types/v1/status.proto";
@@ -12,8 +13,14 @@ option (gogoproto.goproto_getters_all) = false;
1213

1314
message MsgCreatePlanRequest {
1415
string from = 1;
15-
int64 gigabytes = 2;
16-
int64 hours = 3;
16+
string bytes = 2 [
17+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
18+
(gogoproto.nullable) = false
19+
];
20+
google.protobuf.Duration duration = 3 [
21+
(gogoproto.nullable) = false,
22+
(gogoproto.stdduration) = true
23+
];
1724
repeated sentinel.types.v1.Price prices = 4 [(gogoproto.nullable) = false];
1825
}
1926

proto/sentinel/plan/v3/plan.proto

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ syntax = "proto3";
22
package sentinel.plan.v3;
33

44
import "gogoproto/gogo.proto";
5+
import "google/protobuf/duration.proto";
56
import "google/protobuf/timestamp.proto";
67
import "sentinel/types/v1/price.proto";
78
import "sentinel/types/v1/status.proto";
@@ -13,8 +14,14 @@ option (gogoproto.goproto_getters_all) = false;
1314
message Plan {
1415
uint64 id = 1 [(gogoproto.customname) = "ID"];
1516
string prov_address = 2;
16-
int64 gigabytes = 3;
17-
int64 hours = 4;
17+
string bytes = 3 [
18+
(gogoproto.customtype) = "cosmossdk.io/math.Int",
19+
(gogoproto.nullable) = false
20+
];
21+
google.protobuf.Duration duration = 4 [
22+
(gogoproto.nullable) = false,
23+
(gogoproto.stdduration) = true
24+
];
1825
repeated sentinel.types.v1.Price prices = 5 [(gogoproto.nullable) = false];
1926
sentinel.types.v1.Status status = 6;
2027
google.protobuf.Timestamp status_at = 7 [

x/plan/client/cli/tx.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
package cli
22

33
import (
4+
"fmt"
45
"strconv"
6+
"time"
57

8+
sdkmath "cosmossdk.io/math"
69
"github.com/cosmos/cosmos-sdk/client"
710
"github.com/cosmos/cosmos-sdk/client/flags"
811
"github.com/cosmos/cosmos-sdk/client/tx"
@@ -15,21 +18,21 @@ import (
1518

1619
func txCreatePlan() *cobra.Command {
1720
cmd := &cobra.Command{
18-
Use: "create-plan [gigabytes] [hours]",
19-
Short: "Create a new subscription plan with gigabytes, hours and pricing details",
21+
Use: "create-plan [bytes] [duration]",
22+
Short: "Create a new subscription plan with bytes, duration and pricing details",
2023
Args: cobra.ExactArgs(2),
2124
RunE: func(cmd *cobra.Command, args []string) error {
2225
ctx, err := client.GetClientTxContext(cmd)
2326
if err != nil {
2427
return err
2528
}
2629

27-
gigabytes, err := strconv.ParseInt(args[0], 10, 64)
28-
if err != nil {
29-
return err
30+
bytes, ok := sdkmath.NewIntFromString(args[0])
31+
if !ok {
32+
return fmt.Errorf("invalid bytes %s", args[0])
3033
}
3134

32-
hours, err := strconv.ParseInt(args[1], 10, 64)
35+
duration, err := time.ParseDuration(args[1])
3336
if err != nil {
3437
return err
3538
}
@@ -41,8 +44,8 @@ func txCreatePlan() *cobra.Command {
4144

4245
msg := v3.NewMsgCreatePlanRequest(
4346
ctx.FromAddress.Bytes(),
44-
gigabytes,
45-
hours,
47+
bytes,
48+
duration,
4649
prices,
4750
)
4851
if err := msg.ValidateBasic(); err != nil {

x/plan/keeper/msg_handler.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ func (k *Keeper) HandleMsgCreatePlan(ctx sdk.Context, msg *v3.MsgCreatePlanReque
2929
plan := v3.Plan{
3030
ID: count + 1,
3131
ProvAddress: provAddr.String(),
32-
Gigabytes: msg.Gigabytes,
33-
Hours: msg.Hours,
32+
Bytes: msg.Bytes,
33+
Duration: msg.Duration,
3434
Prices: msg.Prices,
3535
Status: v1base.StatusInactive,
3636
StatusAt: ctx.BlockTime(),
@@ -46,8 +46,8 @@ func (k *Keeper) HandleMsgCreatePlan(ctx sdk.Context, msg *v3.MsgCreatePlanReque
4646
&v3.EventCreate{
4747
PlanID: plan.ID,
4848
ProvAddress: plan.ProvAddress,
49-
Gigabytes: plan.Gigabytes,
50-
Hours: plan.Hours,
49+
Bytes: plan.Bytes.String(),
50+
Duration: plan.Duration.String(),
5151
Prices: plan.GetPrices().String(),
5252
},
5353
)

x/plan/migrations/migrator.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package migrations
22

33
import (
44
"fmt"
5-
"time"
65

76
"github.com/cosmos/cosmos-sdk/codec"
87
"github.com/cosmos/cosmos-sdk/store/prefix"
@@ -75,8 +74,8 @@ func (k *Migrator) migratePlans(ctx sdk.Context) {
7574
plan := v3.Plan{
7675
ID: item.ID,
7776
ProvAddress: item.ProviderAddress,
78-
Gigabytes: item.Gigabytes,
79-
Hours: int64(item.Duration / time.Hour),
77+
Bytes: base.Gigabyte.MulRaw(item.Gigabytes),
78+
Duration: item.Duration,
8079
Prices: prices,
8180
Status: item.Status,
8281
StatusAt: item.StatusAt,

x/plan/types/v3/events.pb.go

Lines changed: 76 additions & 44 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

x/plan/types/v3/msg.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package v3
22

33
import (
44
"fmt"
5+
"time"
56

7+
sdkmath "cosmossdk.io/math"
68
sdk "github.com/cosmos/cosmos-sdk/types"
79

810
base "github.com/sentinel-official/sentinelhub/v12/types"
@@ -20,12 +22,12 @@ var (
2022
)
2123

2224
// NewMsgCreatePlanRequest creates a new MsgCreatePlanRequest instance.
23-
func NewMsgCreatePlanRequest(from base.ProvAddress, gigabytes, hours int64, prices v1base.Prices) *MsgCreatePlanRequest {
25+
func NewMsgCreatePlanRequest(from base.ProvAddress, bytes sdkmath.Int, duration time.Duration, prices v1base.Prices) *MsgCreatePlanRequest {
2426
return &MsgCreatePlanRequest{
25-
From: from.String(),
26-
Gigabytes: gigabytes,
27-
Hours: hours,
28-
Prices: prices,
27+
From: from.String(),
28+
Bytes: bytes,
29+
Duration: duration,
30+
Prices: prices,
2931
}
3032
}
3133

@@ -44,20 +46,24 @@ func (m *MsgCreatePlanRequest) ValidateBasic() error {
4446
return types.NewErrorInvalidMessage(fmt.Errorf("invalid from: %w", err))
4547
}
4648

47-
if m.Gigabytes == 0 {
48-
return types.NewErrorInvalidMessage("gigabytes cannot be zero")
49+
if m.Bytes.IsNil() {
50+
return types.NewErrorInvalidMessage("bytes cannot be nil")
4951
}
5052

51-
if m.Gigabytes < 0 {
52-
return types.NewErrorInvalidMessage("gigabytes cannot be negative")
53+
if m.Bytes.IsZero() {
54+
return types.NewErrorInvalidMessage("bytes cannot be zero")
5355
}
5456

55-
if m.Hours == 0 {
56-
return types.NewErrorInvalidMessage("hours cannot be zero")
57+
if m.Bytes.IsNegative() {
58+
return types.NewErrorInvalidMessage("bytes cannot be negative")
5759
}
5860

59-
if m.Hours < 0 {
60-
return types.NewErrorInvalidMessage("hours cannot be negative")
61+
if m.Duration == 0 {
62+
return types.NewErrorInvalidMessage("duration cannot be zero")
63+
}
64+
65+
if m.Duration < 0 {
66+
return types.NewErrorInvalidMessage("duration cannot be negative")
6167
}
6268

6369
if prices := m.GetPrices(); !prices.IsValid() {

0 commit comments

Comments
 (0)