Skip to content

Commit

Permalink
fix: explicitly throw an error for filter methods until subscriptions…
Browse files Browse the repository at this point in the history
… is properly implemented
  • Loading branch information
aramalipoor authored and OBlackmon3 committed Sep 13, 2024
1 parent ad34c22 commit a072254
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
15 changes: 15 additions & 0 deletions common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,21 @@ func (e *ErrUnknownNetworkArchitecture) ErrorStatusCode() int {
return http.StatusBadRequest
}

type ErrNotImplemented struct{ BaseError }

var NewErrNotImplemented = func(msg string) error {
return &ErrNotImplemented{
BaseError{
Code: "ErrNotImplemented",
Message: msg,
},
}
}

func (e *ErrNotImplemented) ErrorStatusCode() int {
return http.StatusNotImplemented
}

type ErrInvalidEvmChainId struct{ BaseError }

var NewErrInvalidEvmChainId = func(chainId any) error {
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/operation/directives.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ When sending requests to eRPC you can instruct to use only one specific upstream
This will skip over any upstream that does not match the value you've provided.

<Callout type="info">
You can use "*" as wildcard character to match a group of upstreams. e.g. "priv-*" will match any upstream IDs starting with "priv-"
You can use `*` as wildcard character to match a group of upstreams. e.g. "priv-*" will match any upstream IDs starting with "priv-"
</Callout>

For example if you want to make sure that request is sent to a specific upstream:
Expand Down
37 changes: 29 additions & 8 deletions erpc/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ func (n *Network) Forward(ctx context.Context, req *common.NormalizedRequest) (*
}
}

upsList, err := n.upstreamsRegistry.GetSortedUpstreams(n.NetworkId, method)
if err != nil {
if inf != nil {
inf.Close(nil, err)
}
return nil, err
}

// 3) Check if we should handle this method on this network
if err := n.shouldHandleMethod(method, upsList); err != nil {
if inf != nil {
inf.Close(nil, err)
}
return nil, err
}

// 3) Apply rate limits
if err := n.acquireRateLimitPermit(req); err != nil {
if inf != nil {
Expand Down Expand Up @@ -184,14 +200,7 @@ func (n *Network) Forward(ctx context.Context, req *common.NormalizedRequest) (*
return resp, err
}

upsList, err := n.upstreamsRegistry.GetSortedUpstreams(n.NetworkId, method)
if err != nil {
if inf != nil {
inf.Close(nil, err)
}
return nil, err
}

// 5) Actual forwarding logic
var execution failsafe.Execution[*common.NormalizedResponse]
var errorsByUpstream = map[string]error{}

Expand Down Expand Up @@ -369,6 +378,18 @@ func (n *Network) EvmChainId() (int64, error) {
return n.cfg.Evm.ChainId, nil
}

func (n *Network) shouldHandleMethod(method string, upsList []*upstream.Upstream) error {
if method == "eth_newFilter" ||
method == "eth_newBlockFilter" ||
method == "eth_newPendingTransactionFilter" {
if len(upsList) > 1 {
return common.NewErrNotImplemented("eth_newFilter, eth_newBlockFilter and eth_newPendingTransactionFilter are not supported yet when there are more than 1 upstream defined")
}
}

return nil
}

func (n *Network) enrichStatePoller(method string, req *common.NormalizedRequest, resp *common.NormalizedResponse) {
switch n.Architecture() {
case common.ArchitectureEvm:
Expand Down

0 comments on commit a072254

Please sign in to comment.