Skip to content

Commit

Permalink
Merge pull request #160
Browse files Browse the repository at this point in the history
Release v0.7.0
  • Loading branch information
bsrinivas8687 committed Aug 18, 2023
2 parents 6530890 + a477756 commit a628afa
Show file tree
Hide file tree
Showing 13 changed files with 767 additions and 402 deletions.
126 changes: 80 additions & 46 deletions api/session/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/gin-gonic/gin"
hubtypes "github.com/sentinel-official/hub/types"
subscriptiontypes "github.com/sentinel-official/hub/x/subscription/types"

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

if item.ID != 0 {
err = fmt.Errorf("peer %s for service already exist", req.Body.Key)
err = fmt.Errorf("key %s for service already exist", req.Body.Key)
c.JSON(http.StatusBadRequest, types.NewResponseError(3, err))
return
}
Expand All @@ -71,8 +72,14 @@ 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 {
err = fmt.Errorf("failed to verify the signature %s", req.Signature)

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 All @@ -88,7 +95,7 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
return
}
if !session.Status.Equal(hubtypes.StatusActive) {
err = fmt.Errorf("invalid status for session %d; expected %s, got %s", session.Id, hubtypes.StatusActive, session.Status)
err = fmt.Errorf("invalid status %s for session %d", session.Status, session.ID)
c.JSON(http.StatusNotFound, types.NewResponseError(5, err))
return
}
Expand All @@ -98,58 +105,104 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
return
}

subscription, err := ctx.Client().QuerySubscription(session.Subscription)
subscription, err := ctx.Client().QuerySubscription(session.SubscriptionID)
if err != nil {
c.JSON(http.StatusInternalServerError, types.NewResponseError(6, err))
return
}
if subscription == nil {
err = fmt.Errorf("subscription %d does not exist", session.Subscription)
err = fmt.Errorf("subscription %d does not exist", session.SubscriptionID)
c.JSON(http.StatusNotFound, types.NewResponseError(6, err))
return
}
if !subscription.Status.Equal(hubtypes.StatusActive) {
err = fmt.Errorf("invalid status for subscription %d; expected %s, got %s", subscription.Id, hubtypes.StatusActive, subscription.Status)
if !subscription.GetStatus().Equal(hubtypes.StatusActive) {
err = fmt.Errorf("invalid status %s for subscription %d", subscription.GetStatus(), subscription.GetID())
c.JSON(http.StatusBadRequest, types.NewResponseError(6, err))
return
}

if subscription.Plan == 0 {
if subscription.Node != ctx.Address().String() {
err = fmt.Errorf("node address mismatch; expected %s, got %s", ctx.Address(), subscription.Node)
switch s := subscription.(type) {
case *subscriptiontypes.NodeSubscription:
if s.NodeAddress != ctx.Address().String() {
err = fmt.Errorf("node address mismatch; expected %s, got %s", ctx.Address(), s.NodeAddress)
c.JSON(http.StatusBadRequest, types.NewResponseError(7, err))
return
}
} else {
ok, err := ctx.Client().HasNodeForPlan(subscription.Plan, ctx.Address())
case *subscriptiontypes.PlanSubscription:
exists, err := ctx.Client().HasNodeForPlan(s.PlanID, ctx.Address())
if err != nil {
c.JSON(http.StatusInternalServerError, types.NewResponseError(7, err))
return
}
if !ok {
err = fmt.Errorf("node %s does not exist for plan %d", ctx.Address(), subscription.Plan)
if !exists {
err = fmt.Errorf("node %s does not exist for plan %d", ctx.Address(), s.PlanID)
c.JSON(http.StatusBadRequest, types.NewResponseError(7, err))
return
}
default:
err = fmt.Errorf("invalid type %T for subscription %d", s, subscription.GetID())
c.JSON(http.StatusBadRequest, types.NewResponseError(7, err))
return
}

quota, err := ctx.Client().QueryQuota(subscription.Id, req.AccAddress)
if err != nil {
c.JSON(http.StatusInternalServerError, types.NewResponseError(8, err))
return
var (
checkAllocation = true
remainingBytes int64 = 0
)

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 quota == nil {
err = fmt.Errorf("quota for address %s does not exist", req.URI.AccAddress)
c.JSON(http.StatusNotFound, types.NewResponseError(8, err))
return

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
}

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
ctx.Database().Model(
&types.Session{},
).Where(
&types.Session{
Subscription: subscription.Id,
Subscription: subscription.GetID(),
Address: req.URI.AccAddress,
},
).Find(&items)
Expand All @@ -159,30 +212,11 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
c.JSON(http.StatusInternalServerError, types.NewResponseError(9, err))
return
}

consumed := sdk.NewInt(items[i].Download + items[i].Upload)
if subscription.Plan == 0 {
quota.Consumed = quota.Consumed.Add(
hubtypes.NewBandwidth(
consumed, sdk.ZeroInt(),
).CeilTo(
hubtypes.Gigabyte.Quo(subscription.Price.Amount),
).Sum(),
)
} else {
quota.Consumed = quota.Consumed.Add(consumed)
}
}

if quota.Consumed.GTE(quota.Allocated) {
err = fmt.Errorf("quota exceeded; allocated %s, consumed %s", quota.Allocated, quota.Consumed)
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 @@ -192,10 +226,10 @@ func HandlerAddSession(ctx *context.Context) gin.HandlerFunc {
).Create(
&types.Session{
ID: req.URI.ID,
Subscription: subscription.Id,
Subscription: subscription.GetID(),
Key: req.Body.Key,
Address: req.URI.AccAddress,
Available: quota.Allocated.Sub(quota.Consumed).Int64(),
Available: remainingBytes,
},
)

Expand Down
10 changes: 5 additions & 5 deletions api/status/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func HandlerGetStatus(ctx *context.Context) gin.HandlerFunc {
Latitude: ctx.Location().Latitude,
Longitude: ctx.Location().Longitude,
},
Moniker: ctx.Moniker(),
Operator: ctx.Operator().String(),
Peers: ctx.Service().PeerCount(),
Price: ctx.Price().String(),
Provider: ctx.Provider().String(),
Moniker: ctx.Moniker(),
Operator: ctx.Operator().String(),
Peers: ctx.Service().PeerCount(),
GigabytePrices: ctx.GigabytePrices().String(),
HourlyPrices: ctx.HourlyPrices().String(),
QOS: &QOS{
MaxPeers: ctx.Config().QOS.MaxPeers,
},
Expand Down
4 changes: 2 additions & 2 deletions api/status/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ type (
Moniker string `json:"moniker"`
Operator string `json:"operator"`
Peers int `json:"peers"`
Price string `json:"price"`
Provider string `json:"provider"`
GigabytePrices string `json:"gigabyte_prices"`
HourlyPrices string `json:"hourly_prices"`
QOS *QOS `json:"qos"`
Type uint64 `json:"type"`
Version string `json:"version"`
Expand Down
14 changes: 7 additions & 7 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,25 +68,25 @@ func (c *Context) IPv4Address() net.IP {
return net.ParseIP(addr).To4()
}

func (c *Context) Provider() hubtypes.ProvAddress {
if c.Config().Node.Provider == "" {
func (c *Context) GigabytePrices() sdk.Coins {
if c.Config().Node.GigabytePrices == "" {
return nil
}

address, err := hubtypes.ProvAddressFromBech32(c.Config().Node.Provider)
coins, err := sdk.ParseCoinsNormalized(c.Config().Node.GigabytePrices)
if err != nil {
panic(err)
}

return address
return coins
}

func (c *Context) Price() sdk.Coins {
if c.Config().Node.Price == "" {
func (c *Context) HourlyPrices() sdk.Coins {
if c.Config().Node.HourlyPrices == "" {
return nil
}

coins, err := sdk.ParseCoinsNormalized(c.Config().Node.Price)
coins, err := sdk.ParseCoinsNormalized(c.Config().Node.HourlyPrices)
if err != nil {
panic(err)
}
Expand Down
16 changes: 8 additions & 8 deletions context/tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func (c *Context) RegisterNode() error {
_, err := c.Client().Tx(
nodetypes.NewMsgRegisterRequest(
c.Operator(),
c.Provider(),
c.Price(),
c.GigabytePrices(),
c.HourlyPrices(),
c.RemoteURL(),
),
)
Expand All @@ -32,10 +32,10 @@ func (c *Context) UpdateNodeInfo() error {
c.Log().Info("Updating the node info...")

_, err := c.Client().Tx(
nodetypes.NewMsgUpdateRequest(
nodetypes.NewMsgUpdateDetailsRequest(
c.Address(),
c.Provider(),
c.Price(),
c.GigabytePrices(),
c.HourlyPrices(),
c.RemoteURL(),
),
)
Expand All @@ -51,7 +51,7 @@ func (c *Context) UpdateNodeStatus() error {
c.Log().Info("Updating the node status...")

_, err := c.Client().Tx(
nodetypes.NewMsgSetStatusRequest(
nodetypes.NewMsgUpdateStatusRequest(
c.Address(),
hubtypes.StatusActive,
),
Expand All @@ -70,10 +70,10 @@ func (c *Context) UpdateSessions(items ...types.Session) error {
messages := make([]sdk.Msg, 0, len(items))
for _, item := range items {
messages = append(messages,
sessiontypes.NewMsgUpdateRequest(
sessiontypes.NewMsgUpdateDetailsRequest(
c.Address(),
sessiontypes.Proof{
Id: item.ID,
ID: item.ID,
Duration: item.UpdatedAt.Sub(item.CreatedAt),
Bandwidth: hubtypes.NewBandwidthFromInt64(item.Upload, item.Download),
},
Expand Down

0 comments on commit a628afa

Please sign in to comment.