Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v0.7.0 #160

Merged
merged 27 commits into from
Aug 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
fcf57ac
Remove network identifier and broadcast address from ipv4 pool
bsrinivas8687 Mar 22, 2023
10fce78
Bump google.golang.org/grpc from 1.53.0 to 1.54.0
dependabot[bot] Mar 23, 2023
6451035
Merge pull request #134
bsrinivas8687 Mar 24, 2023
17350c0
Updated dependencies
bsrinivas8687 Apr 6, 2023
a7eed30
Bump golang.org/x/crypto from 0.7.0 to 0.8.0
dependabot[bot] Apr 7, 2023
3dc37e7
Bump gorm.io/gorm from 1.24.6 to 1.25.0
dependabot[bot] Apr 11, 2023
5ea4497
Merge pull request #139
bsrinivas8687 Apr 12, 2023
1353a98
Merge pull request #140
bsrinivas8687 Apr 12, 2023
ebf733a
Bump gorm.io/driver/sqlite from 1.4.4 to 1.5.0
dependabot[bot] Apr 12, 2023
5afc119
Bump github.com/rs/zerolog from 1.29.0 to 1.29.1
dependabot[bot] Apr 13, 2023
c36a9cf
Merge pull request #141
bsrinivas8687 Apr 15, 2023
ef54f32
Merge pull request #142
bsrinivas8687 Apr 15, 2023
826d6ab
Updated dependencies
bsrinivas8687 Jul 14, 2023
7472ce9
Fixed encoding types
bsrinivas8687 Jul 14, 2023
0f2b753
Added gigabyte_prices and hourly_prices in the config
bsrinivas8687 Jul 15, 2023
f78dc05
Fixed issues in the query methods
bsrinivas8687 Jul 15, 2023
c932c8d
Fixed context and jobs
bsrinivas8687 Jul 15, 2023
5159ff6
Fixed jobs
bsrinivas8687 Jul 15, 2023
1c4c306
Fixed API handlers
bsrinivas8687 Jul 15, 2023
cc6d5cb
Updated Sentinel Hub to v0.11.0-rc1
bsrinivas8687 Jul 27, 2023
9fca66e
Fixed add session handler allocation issue
bsrinivas8687 Jul 27, 2023
6c90b9f
Updated Sentinel Hub dependency to v0.11.0-rc2
bsrinivas8687 Jul 27, 2023
041a758
Updated Sentinel Hub dependency version to v0.11.0-rc3
bsrinivas8687 Aug 4, 2023
f650ef1
Updated Sentinel Hub version to v0.11.0
bsrinivas8687 Aug 12, 2023
ce6dfbc
Updated dependencies
bsrinivas8687 Aug 15, 2023
21a45d5
Fixed validation of remaining bandwidth
bsrinivas8687 Aug 18, 2023
a477756
Fixed prices validation
bsrinivas8687 Aug 18, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading