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

chore: added subscription limits check #370

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 15 additions & 4 deletions rpc/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@
import (
"context"
"errors"

"fmt"
"sort"
"time"

sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"

rconfig "github.com/dymensionxyz/dymint/config"
abciconv "github.com/dymensionxyz/dymint/conv/abci"
"github.com/dymensionxyz/dymint/mempool"
Expand Down Expand Up @@ -97,10 +100,8 @@
// This code is a local client, so we can assume that subscriber is ""
subscriber := "" //ctx.RemoteAddr()

if c.EventBus.NumClients() >= c.config.MaxSubscriptionClients {
return nil, fmt.Errorf("max_subscription_clients %d reached", c.config.MaxSubscriptionClients)
} else if c.EventBus.NumClientSubscriptions(subscriber) >= c.config.MaxSubscriptionsPerClient {
return nil, fmt.Errorf("max_subscriptions_per_client %d reached", c.config.MaxSubscriptionsPerClient)
if err := c.IsSubscriptionAllowed(subscriber); err != nil {
return nil, sdkerrors.Wrap(err, "subscription not allowed")

Check warning on line 104 in rpc/client/client.go

View check run for this annotation

Codecov / codecov/patch

rpc/client/client.go#L104

Added line #L104 was not covered by tests
}

// Subscribe to tx being committed in block.
Expand Down Expand Up @@ -878,6 +879,16 @@
return heightValue
}

func (c *Client) IsSubscriptionAllowed(subscriber string) error {
if c.EventBus.NumClients() >= c.config.MaxSubscriptionClients {
return fmt.Errorf("max_subscription_clients %d reached", c.config.MaxSubscriptionClients)

Check warning on line 884 in rpc/client/client.go

View check run for this annotation

Codecov / codecov/patch

rpc/client/client.go#L884

Added line #L884 was not covered by tests
} else if c.EventBus.NumClientSubscriptions(subscriber) >= c.config.MaxSubscriptionsPerClient {
return fmt.Errorf("max_subscriptions_per_client %d reached", c.config.MaxSubscriptionsPerClient)
}

Check warning on line 887 in rpc/client/client.go

View check run for this annotation

Codecov / codecov/patch

rpc/client/client.go#L886-L887

Added lines #L886 - L887 were not covered by tests

return nil
}

func validatePerPage(perPagePtr *int) int {
if perPagePtr == nil { // no per_page parameter
return defaultPerPage
Expand Down
5 changes: 4 additions & 1 deletion rpc/json/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"strconv"
"time"

"cosmossdk.io/errors"
"github.com/gorilla/rpc/v2/json2"
"github.com/tendermint/tendermint/libs/pubsub"
tmquery "github.com/tendermint/tendermint/libs/pubsub/query"
Expand Down Expand Up @@ -91,7 +92,9 @@
func (s *service) Subscribe(req *http.Request, args *subscribeArgs, wsConn *wsConn, subscriptionID []byte) (*ctypes.ResultSubscribe, error) {
addr := req.RemoteAddr

// TODO(tzdybal): pass config and check subscriptions limits
if err := s.client.IsSubscriptionAllowed(addr); err != nil {
return nil, errors.Wrap(err, "subscription not allowed")
}

Check warning on line 97 in rpc/json/service.go

View check run for this annotation

Codecov / codecov/patch

rpc/json/service.go#L96-L97

Added lines #L96 - L97 were not covered by tests

q, err := tmquery.New(args.Query)
if err != nil {
Expand Down
Loading