From a072254d12774c6ae8cd0960952dfd824330a369 Mon Sep 17 00:00:00 2001 From: aramalipoor Date: Sun, 8 Sep 2024 15:10:19 +0200 Subject: [PATCH] fix: explicitly throw an error for filter methods until subscriptions is properly implemented --- common/errors.go | 15 ++++++++++++ docs/pages/operation/directives.mdx | 2 +- erpc/networks.go | 37 ++++++++++++++++++++++------- 3 files changed, 45 insertions(+), 9 deletions(-) diff --git a/common/errors.go b/common/errors.go index fca959de..3635017d 100644 --- a/common/errors.go +++ b/common/errors.go @@ -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 { diff --git a/docs/pages/operation/directives.mdx b/docs/pages/operation/directives.mdx index 578c8174..7b00248a 100644 --- a/docs/pages/operation/directives.mdx +++ b/docs/pages/operation/directives.mdx @@ -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. - 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-" For example if you want to make sure that request is sent to a specific upstream: diff --git a/erpc/networks.go b/erpc/networks.go index e99a01e9..0206ff1b 100644 --- a/erpc/networks.go +++ b/erpc/networks.go @@ -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 { @@ -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{} @@ -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: