From 75dcfd9a7a6973d170ee0bb37e58881abd1bd763 Mon Sep 17 00:00:00 2001 From: Felipe Andrade Date: Thu, 4 May 2023 15:46:08 -0700 Subject: [PATCH] proxyd/config: skip peer count check --- proxyd/proxyd/backend.go | 8 ++++++++ proxyd/proxyd/config.go | 23 ++++++++++++----------- proxyd/proxyd/consensus_poller.go | 17 ++++++++++------- proxyd/proxyd/example.config.toml | 3 +++ proxyd/proxyd/proxyd.go | 1 + 5 files changed, 34 insertions(+), 18 deletions(-) diff --git a/proxyd/proxyd/backend.go b/proxyd/proxyd/backend.go index 9aec5d5..9244315 100644 --- a/proxyd/proxyd/backend.go +++ b/proxyd/proxyd/backend.go @@ -132,6 +132,8 @@ type Backend struct { stripTrailingXFF bool proxydIP string + skipPeerCountCheck bool + maxDegradedLatencyThreshold time.Duration maxLatencyThreshold time.Duration maxErrorRateThreshold float64 @@ -207,6 +209,12 @@ func WithProxydIP(ip string) BackendOpt { } } +func WithSkipPeerCountCheck(skipPeerCountCheck bool) BackendOpt { + return func(b *Backend) { + b.skipPeerCountCheck = skipPeerCountCheck + } +} + func WithMaxDegradedLatencyThreshold(maxDegradedLatencyThreshold time.Duration) BackendOpt { return func(b *Backend) { b.maxDegradedLatencyThreshold = maxDegradedLatencyThreshold diff --git a/proxyd/proxyd/config.go b/proxyd/proxyd/config.go index 0222a83..218dbc4 100644 --- a/proxyd/proxyd/config.go +++ b/proxyd/proxyd/config.go @@ -81,17 +81,18 @@ type BackendOptions struct { } type BackendConfig struct { - Username string `toml:"username"` - Password string `toml:"password"` - RPCURL string `toml:"rpc_url"` - WSURL string `toml:"ws_url"` - WSPort int `toml:"ws_port"` - MaxRPS int `toml:"max_rps"` - MaxWSConns int `toml:"max_ws_conns"` - CAFile string `toml:"ca_file"` - ClientCertFile string `toml:"client_cert_file"` - ClientKeyFile string `toml:"client_key_file"` - StripTrailingXFF bool `toml:"strip_trailing_xff"` + Username string `toml:"username"` + Password string `toml:"password"` + RPCURL string `toml:"rpc_url"` + WSURL string `toml:"ws_url"` + WSPort int `toml:"ws_port"` + MaxRPS int `toml:"max_rps"` + MaxWSConns int `toml:"max_ws_conns"` + CAFile string `toml:"ca_file"` + ClientCertFile string `toml:"client_cert_file"` + ClientKeyFile string `toml:"client_key_file"` + StripTrailingXFF bool `toml:"strip_trailing_xff"` + SkipPeerCountCheck bool `toml:"consensus_skip_peer_count"` } type BackendsConfig map[string]*BackendConfig diff --git a/proxyd/proxyd/consensus_poller.go b/proxyd/proxyd/consensus_poller.go index 7d01c9b..308c516 100644 --- a/proxyd/proxyd/consensus_poller.go +++ b/proxyd/proxyd/consensus_poller.go @@ -227,10 +227,13 @@ func (cp *ConsensusPoller) UpdateBackend(ctx context.Context, be *Backend) { return } - peerCount, err := cp.getPeerCount(ctx, be) - if err != nil { - log.Warn("error updating backend", "name", be.Name, "err", err) - return + var peerCount uint64 + if !be.skipPeerCountCheck { + peerCount, err = cp.getPeerCount(ctx, be) + if err != nil { + log.Warn("error updating backend", "name", be.Name, "err", err) + return + } } latestBlockNumber, latestBlockHash, err := cp.fetchBlock(ctx, be, "latest") @@ -257,7 +260,7 @@ func (cp *ConsensusPoller) UpdateBackendGroupConsensus(ctx context.Context) { for _, be := range cp.backendGroup.Backends { peerCount, backendLatestBlockNumber, backendLatestBlockHash, lastUpdate := cp.getBackendState(be) - if peerCount < cp.minPeerCount { + if !be.skipPeerCountCheck && peerCount < cp.minPeerCount { continue } if lastUpdate.Add(cp.maxUpdateThreshold).Before(time.Now()) { @@ -306,7 +309,7 @@ func (cp *ConsensusPoller) UpdateBackendGroupConsensus(ctx context.Context) { bs := cp.backendState[be] notUpdated := bs.lastUpdate.Add(cp.maxUpdateThreshold).Before(time.Now()) isBanned := time.Now().Before(bs.bannedUntil) - notEnoughPeers := bs.peerCount < cp.minPeerCount + notEnoughPeers := !be.skipPeerCountCheck && bs.peerCount < cp.minPeerCount if !be.IsHealthy() || be.IsRateLimited() || !be.Online() || notUpdated || isBanned || notEnoughPeers { filteredBackendsNames = append(filteredBackendsNames, be.Name) continue @@ -384,7 +387,7 @@ func (cp *ConsensusPoller) fetchBlock(ctx context.Context, be *Backend, block st return } -// isSyncing Convenient wrapper to check if the backend is syncing from the network +// getPeerCount Convenient wrapper to retrieve the current peer count from the backend func (cp *ConsensusPoller) getPeerCount(ctx context.Context, be *Backend) (count uint64, err error) { var rpcRes RPCRes err = be.ForwardRPC(ctx, &rpcRes, "67", "net_peerCount") diff --git a/proxyd/proxyd/example.config.toml b/proxyd/proxyd/example.config.toml index 053b5fd..cb41614 100644 --- a/proxyd/proxyd/example.config.toml +++ b/proxyd/proxyd/example.config.toml @@ -72,6 +72,9 @@ ca_file = "" client_cert_file = "" # Path to a custom client key file. client_key_file = "" +# Allows backends to skip peer count checking, default false +# consensus_skip_peer_count = true + [backends.alchemy] rpc_url = "" diff --git a/proxyd/proxyd/proxyd.go b/proxyd/proxyd/proxyd.go index f682272..cd02074 100644 --- a/proxyd/proxyd/proxyd.go +++ b/proxyd/proxyd/proxyd.go @@ -157,6 +157,7 @@ func Start(config *Config) (*Server, func(), error) { opts = append(opts, WithStrippedTrailingXFF()) } opts = append(opts, WithProxydIP(os.Getenv("PROXYD_IP"))) + opts = append(opts, WithSkipPeerCountCheck(cfg.SkipPeerCountCheck)) back := NewBackend(name, rpcURL, wsURL, lim, rpcRequestSemaphore, opts...) backendNames = append(backendNames, name)