Skip to content

Commit

Permalink
Fixed validation of remaining bandwidth
Browse files Browse the repository at this point in the history
  • Loading branch information
bsrinivas8687 committed Aug 18, 2023
1 parent ce6dfbc commit 21a45d5
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 42 deletions.
79 changes: 47 additions & 32 deletions api/session/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package session

import (
"fmt"
"math"
"net/http"

sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -73,7 +72,13 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
c.JSON(http.StatusNotFound, types.NewResponseError(4, err))
return
}
if ok := account.GetPubKey().VerifySignature(sdk.Uint64ToBigEndian(req.URI.ID), req.Signature); !ok {

var (
pubKey = account.GetPubKey()
msg = sdk.Uint64ToBigEndian(req.URI.ID)
)

if ok := pubKey.VerifySignature(msg, req.Signature); !ok {
err = fmt.Errorf("invalid signature %s", req.Signature)
c.JSON(http.StatusBadRequest, types.NewResponseError(4, err))
return
Expand Down Expand Up @@ -140,37 +145,56 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
return
}

checkAllocation := true
var (
checkAllocation = true
remainingBytes int64 = 0
)

switch s := subscription.(type) {
case *subscriptiontypes.NodeSubscription:
if s, ok := subscription.(*subscriptiontypes.NodeSubscription); ok {
if req.URI.AccAddress != s.Address {
err = fmt.Errorf("account address mismatch; expected %s, got %s", req.URI.AccAddress, s.Address)
c.JSON(http.StatusBadRequest, types.NewResponseError(8, err))
return
}
if s.Hours != 0 {
checkAllocation = false
if req.URI.AccAddress != s.Address {
err = fmt.Errorf("account address mismatch; expected %s, got %s", req.URI.AccAddress, s.Address)
c.JSON(http.StatusBadRequest, types.NewResponseError(8, err))
return
}
}
}

alloc, err := ctx.Client().QueryAllocation(subscription.GetID(), req.AccAddress)
if err != nil {
c.JSON(http.StatusInternalServerError, types.NewResponseError(8, err))
return
}

if alloc == nil {
if checkAllocation {
if checkAllocation {
alloc, err := ctx.Client().QueryAllocation(subscription.GetID(), req.AccAddress)
if err != nil {
c.JSON(http.StatusInternalServerError, types.NewResponseError(8, err))
return
}
if alloc == nil {
err = fmt.Errorf("allocation %d/%s does not exist", subscription.GetID(), req.AccAddress)
c.JSON(http.StatusNotFound, types.NewResponseError(8, err))
return
}

alloc = &subscriptiontypes.Allocation{
UtilisedBytes: sdk.ZeroInt(),
GrantedBytes: sdk.NewInt(math.MaxInt64),
var items []types.Session
ctx.Database().Model(
&types.Session{},
).Where(
&types.Session{
Subscription: subscription.GetID(),
Address: req.URI.AccAddress,
},
).Find(&items)

for i := 0; i < len(items); i++ {
utilisedBytes := sdk.NewInt(items[i].Download + items[i].Upload)
alloc.UtilisedBytes = alloc.UtilisedBytes.Add(utilisedBytes)
}

if alloc.UtilisedBytes.GTE(alloc.GrantedBytes) {
err = fmt.Errorf("invalid allocation; granted bytes %s, utilised bytes %s", alloc.GrantedBytes, alloc.UtilisedBytes)
c.JSON(http.StatusBadRequest, types.NewResponseError(8, err))
return
}

remainingBytes = alloc.GrantedBytes.Sub(alloc.UtilisedBytes).Int64()
}

var items []types.Session
Expand All @@ -188,20 +212,11 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
c.JSON(http.StatusInternalServerError, types.NewResponseError(9, err))
return
}

alloc.UtilisedBytes = alloc.UtilisedBytes.
Add(sdk.NewInt(items[i].Download + items[i].Upload))
}

if alloc.UtilisedBytes.GTE(alloc.GrantedBytes) {
err = fmt.Errorf("invalid allocation; granted bytes %s, utilised bytes %s", alloc.GrantedBytes, alloc.UtilisedBytes)
c.JSON(http.StatusBadRequest, types.NewResponseError(10, err))
return
}

result, err := ctx.Service().AddPeer(req.Key)
if err != nil {
c.JSON(http.StatusInternalServerError, types.NewResponseError(11, err))
c.JSON(http.StatusInternalServerError, types.NewResponseError(10, err))
return
}
ctx.Log().Info("Added a new peer", "key", req.Body.Key, "count", ctx.Service().PeerCount())
Expand All @@ -214,7 +229,7 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
Subscription: subscription.GetID(),
Key: req.Body.Key,
Address: req.URI.AccAddress,
Available: alloc.GrantedBytes.Sub(alloc.UtilisedBytes).Int64(),
Available: remainingBytes,
},
)

Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
golang.org/x/crypto v0.12.0
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
gorm.io/driver/sqlite v1.5.2
gorm.io/driver/sqlite v1.5.3
gorm.io/gorm v1.25.3
)

Expand Down Expand Up @@ -79,7 +79,7 @@ require (
github.com/go-logfmt/logfmt v0.5.1 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.15.0 // indirect
github.com/go-playground/validator/v10 v10.15.1 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/godbus/dbus v0.0.0-20190726142602-4481cbc300e2 // indirect
github.com/gogo/gateway v1.1.0 // indirect
Expand Down Expand Up @@ -167,7 +167,7 @@ require (
golang.org/x/text v0.12.0 // indirect
google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230814215434-ca7cfce7776a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230815205213-6bfd019c3878 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -414,8 +414,8 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.2.0/go.mod h1:uOYAAleCW8F/7oMFd6aG0GOhaH6EGOAJShg8Id5JGkI=
github.com/go-playground/validator/v10 v10.10.0/go.mod h1:74x4gJWsvQexRdW8Pn3dXSGrTK4nAUsbPlLADvpJkos=
github.com/go-playground/validator/v10 v10.15.0 h1:nDU5XeOKtB3GEa+uB7GNYwhVKsgjAR7VgKoNB6ryXfw=
github.com/go-playground/validator/v10 v10.15.0/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-playground/validator/v10 v10.15.1 h1:BSe8uhN+xQ4r5guV/ywQI4gO59C2raYcGffYWZEjZzM=
github.com/go-playground/validator/v10 v10.15.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-sourcemap/sourcemap v2.1.3+incompatible/go.mod h1:F8jJfvm2KbVjc5NqelyYJmf/v5J0dwNLS2mL4sNA1Jg=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.4.1/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
Expand Down Expand Up @@ -1557,8 +1557,8 @@ google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5 h1:L6iMMGrtzgHsWof
google.golang.org/genproto v0.0.0-20230803162519-f966b187b2e5/go.mod h1:oH/ZOT02u4kWEp7oYBGYFFkCdKS/uYR9Z7+0/xuuFp8=
google.golang.org/genproto/googleapis/api v0.0.0-20230803162519-f966b187b2e5 h1:nIgk/EEq3/YlnmVVXVnm14rC2oxgs1o0ong4sD/rd44=
google.golang.org/genproto/googleapis/api v0.0.0-20230803162519-f966b187b2e5/go.mod h1:5DZzOUPCLYL3mNkQ0ms0F3EuUNZ7py1Bqeq6sxzI7/Q=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230814215434-ca7cfce7776a h1:5rTPHLf5eLPfqGvw3fLpEmUpko2Ky91ft14LxGs5BZc=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230814215434-ca7cfce7776a/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230815205213-6bfd019c3878 h1:lv6/DhyiFFGsmzxbsUUTOkN29II+zeWHxvT8Lpdxsv0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20230815205213-6bfd019c3878/go.mod h1:+Bk1OCOj40wS2hwAMA+aCW9ypzm63QTBBHp6lQ3p+9M=
google.golang.org/grpc v1.12.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
Expand Down Expand Up @@ -1643,8 +1643,8 @@ gopkg.in/yaml.v3 v3.0.0-20200605160147-a5ece683394c/go.mod h1:K4uyk7z7BCEPqu6E+C
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/sqlite v1.5.2 h1:TpQ+/dqCY4uCigCFyrfnrJnrW9zjpelWVoEVNy5qJkc=
gorm.io/driver/sqlite v1.5.2/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
gorm.io/driver/sqlite v1.5.3 h1:7/0dUgX28KAcopdfbRWWl68Rflh6osa4rDh+m51KL2g=
gorm.io/driver/sqlite v1.5.3/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
gorm.io/gorm v1.25.3 h1:zi4rHZj1anhZS2EuEODMhDisGy+Daq9jtPrNGgbQYD8=
gorm.io/gorm v1.25.3/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
Expand Down
2 changes: 1 addition & 1 deletion node/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func (n *Node) jobSetSessions() error {
consumed = sdk.NewInt(peers[i].Upload + peers[i].Download)
)

if consumed.GT(available) {
if available.IsPositive() && consumed.GT(available) {
n.Log().Info("Peer allocation exceeded", "key", item.Key)
if err = n.RemovePeer(item.Key); err != nil {
return err
Expand Down

0 comments on commit 21a45d5

Please sign in to comment.