Skip to content

Commit a628afa

Browse files
Merge pull request #160
Release v0.7.0
2 parents 6530890 + a477756 commit a628afa

File tree

13 files changed

+767
-402
lines changed

13 files changed

+767
-402
lines changed

api/session/handlers.go

Lines changed: 80 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
sdk "github.com/cosmos/cosmos-sdk/types"
88
"github.com/gin-gonic/gin"
99
hubtypes "github.com/sentinel-official/hub/types"
10+
subscriptiontypes "github.com/sentinel-official/hub/x/subscription/types"
1011

1112
"github.com/sentinel-official/dvpn-node/context"
1213
"github.com/sentinel-official/dvpn-node/types"
@@ -51,7 +52,7 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
5152
).First(&item)
5253

5354
if item.ID != 0 {
54-
err = fmt.Errorf("peer %s for service already exist", req.Body.Key)
55+
err = fmt.Errorf("key %s for service already exist", req.Body.Key)
5556
c.JSON(http.StatusBadRequest, types.NewResponseError(3, err))
5657
return
5758
}
@@ -71,8 +72,14 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
7172
c.JSON(http.StatusNotFound, types.NewResponseError(4, err))
7273
return
7374
}
74-
if ok := account.GetPubKey().VerifySignature(sdk.Uint64ToBigEndian(req.URI.ID), req.Signature); !ok {
75-
err = fmt.Errorf("failed to verify the signature %s", req.Signature)
75+
76+
var (
77+
pubKey = account.GetPubKey()
78+
msg = sdk.Uint64ToBigEndian(req.URI.ID)
79+
)
80+
81+
if ok := pubKey.VerifySignature(msg, req.Signature); !ok {
82+
err = fmt.Errorf("invalid signature %s", req.Signature)
7683
c.JSON(http.StatusBadRequest, types.NewResponseError(4, err))
7784
return
7885
}
@@ -88,7 +95,7 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
8895
return
8996
}
9097
if !session.Status.Equal(hubtypes.StatusActive) {
91-
err = fmt.Errorf("invalid status for session %d; expected %s, got %s", session.Id, hubtypes.StatusActive, session.Status)
98+
err = fmt.Errorf("invalid status %s for session %d", session.Status, session.ID)
9299
c.JSON(http.StatusNotFound, types.NewResponseError(5, err))
93100
return
94101
}
@@ -98,58 +105,104 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
98105
return
99106
}
100107

101-
subscription, err := ctx.Client().QuerySubscription(session.Subscription)
108+
subscription, err := ctx.Client().QuerySubscription(session.SubscriptionID)
102109
if err != nil {
103110
c.JSON(http.StatusInternalServerError, types.NewResponseError(6, err))
104111
return
105112
}
106113
if subscription == nil {
107-
err = fmt.Errorf("subscription %d does not exist", session.Subscription)
114+
err = fmt.Errorf("subscription %d does not exist", session.SubscriptionID)
108115
c.JSON(http.StatusNotFound, types.NewResponseError(6, err))
109116
return
110117
}
111-
if !subscription.Status.Equal(hubtypes.StatusActive) {
112-
err = fmt.Errorf("invalid status for subscription %d; expected %s, got %s", subscription.Id, hubtypes.StatusActive, subscription.Status)
118+
if !subscription.GetStatus().Equal(hubtypes.StatusActive) {
119+
err = fmt.Errorf("invalid status %s for subscription %d", subscription.GetStatus(), subscription.GetID())
113120
c.JSON(http.StatusBadRequest, types.NewResponseError(6, err))
114121
return
115122
}
116123

117-
if subscription.Plan == 0 {
118-
if subscription.Node != ctx.Address().String() {
119-
err = fmt.Errorf("node address mismatch; expected %s, got %s", ctx.Address(), subscription.Node)
124+
switch s := subscription.(type) {
125+
case *subscriptiontypes.NodeSubscription:
126+
if s.NodeAddress != ctx.Address().String() {
127+
err = fmt.Errorf("node address mismatch; expected %s, got %s", ctx.Address(), s.NodeAddress)
120128
c.JSON(http.StatusBadRequest, types.NewResponseError(7, err))
121129
return
122130
}
123-
} else {
124-
ok, err := ctx.Client().HasNodeForPlan(subscription.Plan, ctx.Address())
131+
case *subscriptiontypes.PlanSubscription:
132+
exists, err := ctx.Client().HasNodeForPlan(s.PlanID, ctx.Address())
125133
if err != nil {
126134
c.JSON(http.StatusInternalServerError, types.NewResponseError(7, err))
127135
return
128136
}
129-
if !ok {
130-
err = fmt.Errorf("node %s does not exist for plan %d", ctx.Address(), subscription.Plan)
137+
if !exists {
138+
err = fmt.Errorf("node %s does not exist for plan %d", ctx.Address(), s.PlanID)
131139
c.JSON(http.StatusBadRequest, types.NewResponseError(7, err))
132140
return
133141
}
142+
default:
143+
err = fmt.Errorf("invalid type %T for subscription %d", s, subscription.GetID())
144+
c.JSON(http.StatusBadRequest, types.NewResponseError(7, err))
145+
return
134146
}
135147

136-
quota, err := ctx.Client().QueryQuota(subscription.Id, req.AccAddress)
137-
if err != nil {
138-
c.JSON(http.StatusInternalServerError, types.NewResponseError(8, err))
139-
return
148+
var (
149+
checkAllocation = true
150+
remainingBytes int64 = 0
151+
)
152+
153+
if s, ok := subscription.(*subscriptiontypes.NodeSubscription); ok {
154+
if req.URI.AccAddress != s.Address {
155+
err = fmt.Errorf("account address mismatch; expected %s, got %s", req.URI.AccAddress, s.Address)
156+
c.JSON(http.StatusBadRequest, types.NewResponseError(8, err))
157+
return
158+
}
159+
if s.Hours != 0 {
160+
checkAllocation = false
161+
}
140162
}
141-
if quota == nil {
142-
err = fmt.Errorf("quota for address %s does not exist", req.URI.AccAddress)
143-
c.JSON(http.StatusNotFound, types.NewResponseError(8, err))
144-
return
163+
164+
if checkAllocation {
165+
alloc, err := ctx.Client().QueryAllocation(subscription.GetID(), req.AccAddress)
166+
if err != nil {
167+
c.JSON(http.StatusInternalServerError, types.NewResponseError(8, err))
168+
return
169+
}
170+
if alloc == nil {
171+
err = fmt.Errorf("allocation %d/%s does not exist", subscription.GetID(), req.AccAddress)
172+
c.JSON(http.StatusNotFound, types.NewResponseError(8, err))
173+
return
174+
}
175+
176+
var items []types.Session
177+
ctx.Database().Model(
178+
&types.Session{},
179+
).Where(
180+
&types.Session{
181+
Subscription: subscription.GetID(),
182+
Address: req.URI.AccAddress,
183+
},
184+
).Find(&items)
185+
186+
for i := 0; i < len(items); i++ {
187+
utilisedBytes := sdk.NewInt(items[i].Download + items[i].Upload)
188+
alloc.UtilisedBytes = alloc.UtilisedBytes.Add(utilisedBytes)
189+
}
190+
191+
if alloc.UtilisedBytes.GTE(alloc.GrantedBytes) {
192+
err = fmt.Errorf("invalid allocation; granted bytes %s, utilised bytes %s", alloc.GrantedBytes, alloc.UtilisedBytes)
193+
c.JSON(http.StatusBadRequest, types.NewResponseError(8, err))
194+
return
195+
}
196+
197+
remainingBytes = alloc.GrantedBytes.Sub(alloc.UtilisedBytes).Int64()
145198
}
146199

147200
var items []types.Session
148201
ctx.Database().Model(
149202
&types.Session{},
150203
).Where(
151204
&types.Session{
152-
Subscription: subscription.Id,
205+
Subscription: subscription.GetID(),
153206
Address: req.URI.AccAddress,
154207
},
155208
).Find(&items)
@@ -159,30 +212,11 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
159212
c.JSON(http.StatusInternalServerError, types.NewResponseError(9, err))
160213
return
161214
}
162-
163-
consumed := sdk.NewInt(items[i].Download + items[i].Upload)
164-
if subscription.Plan == 0 {
165-
quota.Consumed = quota.Consumed.Add(
166-
hubtypes.NewBandwidth(
167-
consumed, sdk.ZeroInt(),
168-
).CeilTo(
169-
hubtypes.Gigabyte.Quo(subscription.Price.Amount),
170-
).Sum(),
171-
)
172-
} else {
173-
quota.Consumed = quota.Consumed.Add(consumed)
174-
}
175-
}
176-
177-
if quota.Consumed.GTE(quota.Allocated) {
178-
err = fmt.Errorf("quota exceeded; allocated %s, consumed %s", quota.Allocated, quota.Consumed)
179-
c.JSON(http.StatusBadRequest, types.NewResponseError(10, err))
180-
return
181215
}
182216

183217
result, err := ctx.Service().AddPeer(req.Key)
184218
if err != nil {
185-
c.JSON(http.StatusInternalServerError, types.NewResponseError(11, err))
219+
c.JSON(http.StatusInternalServerError, types.NewResponseError(10, err))
186220
return
187221
}
188222
ctx.Log().Info("Added a new peer", "key", req.Body.Key, "count", ctx.Service().PeerCount())
@@ -192,10 +226,10 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
192226
).Create(
193227
&types.Session{
194228
ID: req.URI.ID,
195-
Subscription: subscription.Id,
229+
Subscription: subscription.GetID(),
196230
Key: req.Body.Key,
197231
Address: req.URI.AccAddress,
198-
Available: quota.Allocated.Sub(quota.Consumed).Int64(),
232+
Available: remainingBytes,
199233
},
200234
)
201235

api/status/handlers.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func HandlerGetStatus(ctx *context.Context) gin.HandlerFunc {
3131
Latitude: ctx.Location().Latitude,
3232
Longitude: ctx.Location().Longitude,
3333
},
34-
Moniker: ctx.Moniker(),
35-
Operator: ctx.Operator().String(),
36-
Peers: ctx.Service().PeerCount(),
37-
Price: ctx.Price().String(),
38-
Provider: ctx.Provider().String(),
34+
Moniker: ctx.Moniker(),
35+
Operator: ctx.Operator().String(),
36+
Peers: ctx.Service().PeerCount(),
37+
GigabytePrices: ctx.GigabytePrices().String(),
38+
HourlyPrices: ctx.HourlyPrices().String(),
3939
QOS: &QOS{
4040
MaxPeers: ctx.Config().QOS.MaxPeers,
4141
},

api/status/responses.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ type (
3333
Moniker string `json:"moniker"`
3434
Operator string `json:"operator"`
3535
Peers int `json:"peers"`
36-
Price string `json:"price"`
37-
Provider string `json:"provider"`
36+
GigabytePrices string `json:"gigabyte_prices"`
37+
HourlyPrices string `json:"hourly_prices"`
3838
QOS *QOS `json:"qos"`
3939
Type uint64 `json:"type"`
4040
Version string `json:"version"`

context/context.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,25 +68,25 @@ func (c *Context) IPv4Address() net.IP {
6868
return net.ParseIP(addr).To4()
6969
}
7070

71-
func (c *Context) Provider() hubtypes.ProvAddress {
72-
if c.Config().Node.Provider == "" {
71+
func (c *Context) GigabytePrices() sdk.Coins {
72+
if c.Config().Node.GigabytePrices == "" {
7373
return nil
7474
}
7575

76-
address, err := hubtypes.ProvAddressFromBech32(c.Config().Node.Provider)
76+
coins, err := sdk.ParseCoinsNormalized(c.Config().Node.GigabytePrices)
7777
if err != nil {
7878
panic(err)
7979
}
8080

81-
return address
81+
return coins
8282
}
8383

84-
func (c *Context) Price() sdk.Coins {
85-
if c.Config().Node.Price == "" {
84+
func (c *Context) HourlyPrices() sdk.Coins {
85+
if c.Config().Node.HourlyPrices == "" {
8686
return nil
8787
}
8888

89-
coins, err := sdk.ParseCoinsNormalized(c.Config().Node.Price)
89+
coins, err := sdk.ParseCoinsNormalized(c.Config().Node.HourlyPrices)
9090
if err != nil {
9191
panic(err)
9292
}

context/tx.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ func (c *Context) RegisterNode() error {
1515
_, err := c.Client().Tx(
1616
nodetypes.NewMsgRegisterRequest(
1717
c.Operator(),
18-
c.Provider(),
19-
c.Price(),
18+
c.GigabytePrices(),
19+
c.HourlyPrices(),
2020
c.RemoteURL(),
2121
),
2222
)
@@ -32,10 +32,10 @@ func (c *Context) UpdateNodeInfo() error {
3232
c.Log().Info("Updating the node info...")
3333

3434
_, err := c.Client().Tx(
35-
nodetypes.NewMsgUpdateRequest(
35+
nodetypes.NewMsgUpdateDetailsRequest(
3636
c.Address(),
37-
c.Provider(),
38-
c.Price(),
37+
c.GigabytePrices(),
38+
c.HourlyPrices(),
3939
c.RemoteURL(),
4040
),
4141
)
@@ -51,7 +51,7 @@ func (c *Context) UpdateNodeStatus() error {
5151
c.Log().Info("Updating the node status...")
5252

5353
_, err := c.Client().Tx(
54-
nodetypes.NewMsgSetStatusRequest(
54+
nodetypes.NewMsgUpdateStatusRequest(
5555
c.Address(),
5656
hubtypes.StatusActive,
5757
),
@@ -70,10 +70,10 @@ func (c *Context) UpdateSessions(items ...types.Session) error {
7070
messages := make([]sdk.Msg, 0, len(items))
7171
for _, item := range items {
7272
messages = append(messages,
73-
sessiontypes.NewMsgUpdateRequest(
73+
sessiontypes.NewMsgUpdateDetailsRequest(
7474
c.Address(),
7575
sessiontypes.Proof{
76-
Id: item.ID,
76+
ID: item.ID,
7777
Duration: item.UpdatedAt.Sub(item.CreatedAt),
7878
Bandwidth: hubtypes.NewBandwidthFromInt64(item.Upload, item.Download),
7979
},

0 commit comments

Comments
 (0)