2022-05-05 00:51:24 +03:00
|
|
|
package proxyd
|
|
|
|
|
|
|
|
import (
|
2023-09-14 22:36:24 +03:00
|
|
|
"context"
|
2022-05-05 00:51:24 +03:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/math"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2023-09-14 22:36:24 +03:00
|
|
|
"github.com/redis/go-redis/v9"
|
2024-03-18 21:26:59 +03:00
|
|
|
"golang.org/x/exp/slog"
|
2022-05-05 00:51:24 +03:00
|
|
|
"golang.org/x/sync/semaphore"
|
|
|
|
)
|
|
|
|
|
2024-03-18 21:26:59 +03:00
|
|
|
func SetLogLevel(logLevel slog.Leveler) {
|
|
|
|
log.SetDefault(log.NewLogger(slog.NewJSONHandler(
|
|
|
|
os.Stdout, &slog.HandlerOptions{Level: logLevel})))
|
|
|
|
}
|
|
|
|
|
2023-04-18 21:57:55 +03:00
|
|
|
func Start(config *Config) (*Server, func(), error) {
|
2022-05-05 00:51:24 +03:00
|
|
|
if len(config.Backends) == 0 {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, errors.New("must define at least one backend")
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
if len(config.BackendGroups) == 0 {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, errors.New("must define at least one backend group")
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
if len(config.RPCMethodMappings) == 0 {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, errors.New("must define at least one RPC method mapping")
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for authKey := range config.Authentication {
|
|
|
|
if authKey == "none" {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, errors.New("cannot use none as an auth key")
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 23:23:09 +03:00
|
|
|
// redis primary client
|
2022-10-09 23:26:27 +03:00
|
|
|
var redisClient *redis.Client
|
2022-05-05 00:51:24 +03:00
|
|
|
if config.Redis.URL != "" {
|
|
|
|
rURL, err := ReadFromEnvOrConfig(config.Redis.URL)
|
|
|
|
if err != nil {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, err
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
2022-10-09 23:26:27 +03:00
|
|
|
redisClient, err = NewRedisClient(rURL)
|
|
|
|
if err != nil {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, err
|
2022-10-09 23:26:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-19 23:23:09 +03:00
|
|
|
// redis read replica client
|
|
|
|
// if read endpoint is not set, use primary endpoint
|
|
|
|
var redisReadClient = redisClient
|
|
|
|
if config.Redis.ReadURL != "" {
|
|
|
|
if redisClient == nil {
|
|
|
|
return nil, nil, errors.New("must specify a Redis primary URL. only read endpoint is set")
|
|
|
|
}
|
|
|
|
rURL, err := ReadFromEnvOrConfig(config.Redis.ReadURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
redisReadClient, err = NewRedisClient(rURL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-09 23:26:27 +03:00
|
|
|
if redisClient == nil && config.RateLimit.UseRedis {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, errors.New("must specify a Redis URL if UseRedis is true in rate limit config")
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 23:21:12 +03:00
|
|
|
// While modifying shared globals is a bad practice, the alternative
|
|
|
|
// is to clone these errors on every invocation. This is inefficient.
|
|
|
|
// We'd also have to make sure that errors.Is and errors.As continue
|
|
|
|
// to function properly on the cloned errors.
|
|
|
|
if config.RateLimit.ErrorMessage != "" {
|
|
|
|
ErrOverRateLimit.Message = config.RateLimit.ErrorMessage
|
|
|
|
}
|
|
|
|
if config.WhitelistErrorMessage != "" {
|
|
|
|
ErrMethodNotWhitelisted.Message = config.WhitelistErrorMessage
|
|
|
|
}
|
2022-09-24 00:06:02 +03:00
|
|
|
if config.BatchConfig.ErrorMessage != "" {
|
|
|
|
ErrTooManyBatchRequests.Message = config.BatchConfig.ErrorMessage
|
|
|
|
}
|
2022-09-23 23:21:12 +03:00
|
|
|
|
2023-01-23 02:40:26 +03:00
|
|
|
if config.SenderRateLimit.Enabled {
|
|
|
|
if config.SenderRateLimit.Limit <= 0 {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, errors.New("limit in sender_rate_limit must be > 0")
|
2023-01-23 02:40:26 +03:00
|
|
|
}
|
|
|
|
if time.Duration(config.SenderRateLimit.Interval) < time.Second {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, errors.New("interval in sender_rate_limit must be >= 1s")
|
2023-01-23 02:40:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 00:51:24 +03:00
|
|
|
maxConcurrentRPCs := config.Server.MaxConcurrentRPCs
|
|
|
|
if maxConcurrentRPCs == 0 {
|
|
|
|
maxConcurrentRPCs = math.MaxInt64
|
|
|
|
}
|
|
|
|
rpcRequestSemaphore := semaphore.NewWeighted(maxConcurrentRPCs)
|
|
|
|
|
|
|
|
backendNames := make([]string, 0)
|
|
|
|
backendsByName := make(map[string]*Backend)
|
|
|
|
for name, cfg := range config.Backends {
|
|
|
|
opts := make([]BackendOpt, 0)
|
|
|
|
|
|
|
|
rpcURL, err := ReadFromEnvOrConfig(cfg.RPCURL)
|
|
|
|
if err != nil {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, err
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
wsURL, err := ReadFromEnvOrConfig(cfg.WSURL)
|
|
|
|
if err != nil {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, err
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
if rpcURL == "" {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, fmt.Errorf("must define an RPC URL for backend %s", name)
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if config.BackendOptions.ResponseTimeoutSeconds != 0 {
|
|
|
|
timeout := secondsToDuration(config.BackendOptions.ResponseTimeoutSeconds)
|
|
|
|
opts = append(opts, WithTimeout(timeout))
|
|
|
|
}
|
|
|
|
if config.BackendOptions.MaxRetries != 0 {
|
|
|
|
opts = append(opts, WithMaxRetries(config.BackendOptions.MaxRetries))
|
|
|
|
}
|
|
|
|
if config.BackendOptions.MaxResponseSizeBytes != 0 {
|
|
|
|
opts = append(opts, WithMaxResponseSize(config.BackendOptions.MaxResponseSizeBytes))
|
|
|
|
}
|
|
|
|
if config.BackendOptions.OutOfServiceSeconds != 0 {
|
|
|
|
opts = append(opts, WithOutOfServiceDuration(secondsToDuration(config.BackendOptions.OutOfServiceSeconds)))
|
|
|
|
}
|
2023-05-04 02:23:43 +03:00
|
|
|
if config.BackendOptions.MaxDegradedLatencyThreshold > 0 {
|
|
|
|
opts = append(opts, WithMaxDegradedLatencyThreshold(time.Duration(config.BackendOptions.MaxDegradedLatencyThreshold)))
|
|
|
|
}
|
|
|
|
if config.BackendOptions.MaxLatencyThreshold > 0 {
|
|
|
|
opts = append(opts, WithMaxLatencyThreshold(time.Duration(config.BackendOptions.MaxLatencyThreshold)))
|
|
|
|
}
|
|
|
|
if config.BackendOptions.MaxErrorRateThreshold > 0 {
|
|
|
|
opts = append(opts, WithMaxErrorRateThreshold(config.BackendOptions.MaxErrorRateThreshold))
|
|
|
|
}
|
2022-05-05 00:51:24 +03:00
|
|
|
if cfg.MaxRPS != 0 {
|
|
|
|
opts = append(opts, WithMaxRPS(cfg.MaxRPS))
|
|
|
|
}
|
|
|
|
if cfg.MaxWSConns != 0 {
|
|
|
|
opts = append(opts, WithMaxWSConns(cfg.MaxWSConns))
|
|
|
|
}
|
|
|
|
if cfg.Password != "" {
|
|
|
|
passwordVal, err := ReadFromEnvOrConfig(cfg.Password)
|
|
|
|
if err != nil {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, err
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
opts = append(opts, WithBasicAuth(cfg.Username, passwordVal))
|
|
|
|
}
|
2023-11-11 12:52:09 +03:00
|
|
|
|
|
|
|
headers := map[string]string{}
|
|
|
|
for headerName, headerValue := range cfg.Headers {
|
|
|
|
headerValue, err := ReadFromEnvOrConfig(headerValue)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
headers[headerName] = headerValue
|
|
|
|
}
|
|
|
|
opts = append(opts, WithHeaders(headers))
|
|
|
|
|
2022-05-05 00:51:24 +03:00
|
|
|
tlsConfig, err := configureBackendTLS(cfg)
|
|
|
|
if err != nil {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, err
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
if tlsConfig != nil {
|
|
|
|
log.Info("using custom TLS config for backend", "name", name)
|
|
|
|
opts = append(opts, WithTLSConfig(tlsConfig))
|
|
|
|
}
|
|
|
|
if cfg.StripTrailingXFF {
|
|
|
|
opts = append(opts, WithStrippedTrailingXFF())
|
|
|
|
}
|
|
|
|
opts = append(opts, WithProxydIP(os.Getenv("PROXYD_IP")))
|
2023-06-01 23:16:40 +03:00
|
|
|
opts = append(opts, WithConsensusSkipPeerCountCheck(cfg.ConsensusSkipPeerCountCheck))
|
2023-08-19 02:38:03 +03:00
|
|
|
opts = append(opts, WithConsensusForcedCandidate(cfg.ConsensusForcedCandidate))
|
2023-11-01 04:41:55 +03:00
|
|
|
opts = append(opts, WithWeight(cfg.Weight))
|
2023-06-01 23:16:40 +03:00
|
|
|
|
|
|
|
receiptsTarget, err := ReadFromEnvOrConfig(cfg.ConsensusReceiptsTarget)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
receiptsTarget, err = validateReceiptsTarget(receiptsTarget)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
opts = append(opts, WithConsensusReceiptTarget(receiptsTarget))
|
2023-05-04 02:23:43 +03:00
|
|
|
|
2023-05-14 06:54:27 +03:00
|
|
|
back := NewBackend(name, rpcURL, wsURL, rpcRequestSemaphore, opts...)
|
2022-05-05 00:51:24 +03:00
|
|
|
backendNames = append(backendNames, name)
|
|
|
|
backendsByName[name] = back
|
2023-05-14 06:54:27 +03:00
|
|
|
log.Info("configured backend",
|
|
|
|
"name", name,
|
|
|
|
"backend_names", backendNames,
|
|
|
|
"rpc_url", rpcURL,
|
|
|
|
"ws_url", wsURL)
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
backendGroups := make(map[string]*BackendGroup)
|
|
|
|
for bgName, bg := range config.BackendGroups {
|
|
|
|
backends := make([]*Backend, 0)
|
2024-06-05 02:19:54 +03:00
|
|
|
fallbackBackends := make(map[string]bool)
|
|
|
|
fallbackCount := 0
|
2022-05-05 00:51:24 +03:00
|
|
|
for _, bName := range bg.Backends {
|
|
|
|
if backendsByName[bName] == nil {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, fmt.Errorf("backend %s is not defined", bName)
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
backends = append(backends, backendsByName[bName])
|
2024-06-05 02:19:54 +03:00
|
|
|
|
|
|
|
for _, fb := range bg.Fallbacks {
|
|
|
|
if bName == fb {
|
|
|
|
fallbackBackends[bName] = true
|
|
|
|
log.Info("configured backend as fallback",
|
|
|
|
"backend_name", bName,
|
|
|
|
"backend_group", bgName,
|
|
|
|
)
|
|
|
|
fallbackCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := fallbackBackends[bName]; !ok {
|
|
|
|
fallbackBackends[bName] = false
|
|
|
|
log.Info("configured backend as primary",
|
|
|
|
"backend_name", bName,
|
|
|
|
"backend_group", bgName,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if fallbackCount != len(bg.Fallbacks) {
|
|
|
|
return nil, nil,
|
|
|
|
fmt.Errorf(
|
|
|
|
"error: number of fallbacks instantiated (%d) did not match configured (%d) for backend group %s",
|
|
|
|
fallbackCount, len(bg.Fallbacks), bgName,
|
|
|
|
)
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
2023-11-06 05:07:39 +03:00
|
|
|
|
2023-11-09 04:59:36 +03:00
|
|
|
backendGroups[bgName] = &BackendGroup{
|
2024-06-05 02:19:54 +03:00
|
|
|
Name: bgName,
|
|
|
|
Backends: backends,
|
|
|
|
WeightedRouting: bg.WeightedRouting,
|
|
|
|
FallbackBackends: fallbackBackends,
|
2024-07-23 00:44:27 +03:00
|
|
|
routingStrategy: bg.RoutingStrategy,
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var wsBackendGroup *BackendGroup
|
|
|
|
if config.WSBackendGroup != "" {
|
|
|
|
wsBackendGroup = backendGroups[config.WSBackendGroup]
|
|
|
|
if wsBackendGroup == nil {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, fmt.Errorf("ws backend group %s does not exist", config.WSBackendGroup)
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if wsBackendGroup == nil && config.Server.WSPort != 0 {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, fmt.Errorf("a ws port was defined, but no ws group was defined")
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, bg := range config.RPCMethodMappings {
|
|
|
|
if backendGroups[bg] == nil {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, fmt.Errorf("undefined backend group %s", bg)
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var resolvedAuth map[string]string
|
|
|
|
|
|
|
|
if config.Authentication != nil {
|
|
|
|
resolvedAuth = make(map[string]string)
|
|
|
|
for secret, alias := range config.Authentication {
|
|
|
|
resolvedSecret, err := ReadFromEnvOrConfig(secret)
|
|
|
|
if err != nil {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, err
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
resolvedAuth[resolvedSecret] = alias
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2023-05-16 05:20:08 +03:00
|
|
|
cache Cache
|
|
|
|
rpcCache RPCCache
|
2022-05-05 00:51:24 +03:00
|
|
|
)
|
|
|
|
if config.Cache.Enabled {
|
2022-10-09 23:26:27 +03:00
|
|
|
if redisClient == nil {
|
2022-05-05 00:51:24 +03:00
|
|
|
log.Warn("redis is not configured, using in-memory cache")
|
|
|
|
cache = newMemoryCache()
|
2022-10-09 23:26:27 +03:00
|
|
|
} else {
|
2024-02-24 00:58:00 +03:00
|
|
|
ttl := defaultCacheTtl
|
|
|
|
if config.Cache.TTL != 0 {
|
|
|
|
ttl = time.Duration(config.Cache.TTL)
|
|
|
|
}
|
2024-08-19 23:23:09 +03:00
|
|
|
cache = newRedisCache(redisClient, redisReadClient, config.Redis.Namespace, ttl)
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
2023-05-16 05:20:08 +03:00
|
|
|
rpcCache = newRPCCache(newCacheWithCompression(cache))
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
2022-08-04 20:34:43 +03:00
|
|
|
srv, err := NewServer(
|
2022-05-05 00:51:24 +03:00
|
|
|
backendGroups,
|
|
|
|
wsBackendGroup,
|
|
|
|
NewStringSetFromStrings(config.WSMethodWhitelist),
|
|
|
|
config.RPCMethodMappings,
|
|
|
|
config.Server.MaxBodySizeBytes,
|
|
|
|
resolvedAuth,
|
|
|
|
secondsToDuration(config.Server.TimeoutSeconds),
|
|
|
|
config.Server.MaxUpstreamBatchSize,
|
2023-10-19 22:48:03 +03:00
|
|
|
config.Server.EnableXServedByHeader,
|
2022-05-05 00:51:24 +03:00
|
|
|
rpcCache,
|
2022-08-04 20:34:43 +03:00
|
|
|
config.RateLimit,
|
2023-01-23 02:40:26 +03:00
|
|
|
config.SenderRateLimit,
|
2022-07-27 20:12:47 +03:00
|
|
|
config.Server.EnableRequestLog,
|
|
|
|
config.Server.MaxRequestBodyLogLen,
|
2022-09-24 00:06:02 +03:00
|
|
|
config.BatchConfig.MaxSize,
|
2022-10-09 23:26:27 +03:00
|
|
|
redisClient,
|
2022-05-05 00:51:24 +03:00
|
|
|
)
|
2022-08-04 20:34:43 +03:00
|
|
|
if err != nil {
|
2023-04-18 21:57:55 +03:00
|
|
|
return nil, nil, fmt.Errorf("error creating server: %w", err)
|
2022-08-04 20:34:43 +03:00
|
|
|
}
|
2022-05-05 00:51:24 +03:00
|
|
|
|
2024-04-12 01:07:16 +03:00
|
|
|
// Enable to support browser websocket connections.
|
|
|
|
// See https://pkg.go.dev/github.com/gorilla/websocket#hdr-Origin_Considerations
|
|
|
|
if config.Server.AllowAllOrigins {
|
|
|
|
srv.upgrader.CheckOrigin = func(r *http.Request) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 00:51:24 +03:00
|
|
|
if config.Metrics.Enabled {
|
|
|
|
addr := fmt.Sprintf("%s:%d", config.Metrics.Host, config.Metrics.Port)
|
|
|
|
log.Info("starting metrics server", "addr", addr)
|
|
|
|
go func() {
|
|
|
|
if err := http.ListenAndServe(addr, promhttp.Handler()); err != nil {
|
|
|
|
log.Error("error starting metrics server", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// To allow integration tests to cleanly come up, wait
|
|
|
|
// 10ms to give the below goroutines enough time to
|
|
|
|
// encounter an error creating their servers
|
|
|
|
errTimer := time.NewTimer(10 * time.Millisecond)
|
|
|
|
|
|
|
|
if config.Server.RPCPort != 0 {
|
|
|
|
go func() {
|
|
|
|
if err := srv.RPCListenAndServe(config.Server.RPCHost, config.Server.RPCPort); err != nil {
|
|
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
|
|
log.Info("RPC server shut down")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Crit("error starting RPC server", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Server.WSPort != 0 {
|
|
|
|
go func() {
|
|
|
|
if err := srv.WSListenAndServe(config.Server.WSHost, config.Server.WSPort); err != nil {
|
|
|
|
if errors.Is(err, http.ErrServerClosed) {
|
|
|
|
log.Info("WS server shut down")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Crit("error starting WS server", "err", err)
|
|
|
|
}
|
|
|
|
}()
|
2023-04-18 21:57:55 +03:00
|
|
|
} else {
|
|
|
|
log.Info("WS server not enabled (ws_port is set to 0)")
|
|
|
|
}
|
|
|
|
|
|
|
|
for bgName, bg := range backendGroups {
|
2023-05-04 02:23:43 +03:00
|
|
|
bgcfg := config.BackendGroups[bgName]
|
2024-07-23 00:44:27 +03:00
|
|
|
|
|
|
|
if !bgcfg.ValidateRoutingStrategy(bgName) {
|
|
|
|
log.Crit("Invalid routing strategy provided. Valid options: fallback, multicall, consensus_aware, \"\"", "name", bgName)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("configuring routing strategy for backend_group", "name", bgName, "routing_strategy", bgcfg.RoutingStrategy)
|
|
|
|
|
|
|
|
if bgcfg.RoutingStrategy == ConsensusAwareRoutingStrategy {
|
2023-04-18 21:57:55 +03:00
|
|
|
log.Info("creating poller for consensus aware backend_group", "name", bgName)
|
|
|
|
|
|
|
|
copts := make([]ConsensusOpt, 0)
|
|
|
|
|
2023-05-04 02:23:43 +03:00
|
|
|
if bgcfg.ConsensusAsyncHandler == "noop" {
|
2023-04-18 21:57:55 +03:00
|
|
|
copts = append(copts, WithAsyncHandler(NewNoopAsyncHandler()))
|
|
|
|
}
|
2023-05-04 02:23:43 +03:00
|
|
|
if bgcfg.ConsensusBanPeriod > 0 {
|
|
|
|
copts = append(copts, WithBanPeriod(time.Duration(bgcfg.ConsensusBanPeriod)))
|
|
|
|
}
|
|
|
|
if bgcfg.ConsensusMaxUpdateThreshold > 0 {
|
|
|
|
copts = append(copts, WithMaxUpdateThreshold(time.Duration(bgcfg.ConsensusMaxUpdateThreshold)))
|
|
|
|
}
|
2023-05-09 02:29:05 +03:00
|
|
|
if bgcfg.ConsensusMaxBlockLag > 0 {
|
|
|
|
copts = append(copts, WithMaxBlockLag(bgcfg.ConsensusMaxBlockLag))
|
|
|
|
}
|
2023-05-04 02:23:43 +03:00
|
|
|
if bgcfg.ConsensusMinPeerCount > 0 {
|
|
|
|
copts = append(copts, WithMinPeerCount(uint64(bgcfg.ConsensusMinPeerCount)))
|
|
|
|
}
|
2023-08-19 02:38:03 +03:00
|
|
|
if bgcfg.ConsensusMaxBlockRange > 0 {
|
|
|
|
copts = append(copts, WithMaxBlockRange(bgcfg.ConsensusMaxBlockRange))
|
|
|
|
}
|
2024-04-12 01:07:16 +03:00
|
|
|
if bgcfg.ConsensusPollerInterval > 0 {
|
|
|
|
copts = append(copts, WithPollerInterval(time.Duration(bgcfg.ConsensusPollerInterval)))
|
|
|
|
}
|
2023-05-04 02:23:43 +03:00
|
|
|
|
2024-06-05 02:19:54 +03:00
|
|
|
for _, be := range bgcfg.Backends {
|
|
|
|
if fallback, ok := bg.FallbackBackends[be]; !ok {
|
|
|
|
log.Crit("error backend not found in backend fallback configurations", "backend_name", be)
|
|
|
|
} else {
|
|
|
|
log.Debug("configuring new backend for group", "backend_group", bgName, "backend_name", be, "fallback", fallback)
|
|
|
|
RecordBackendGroupFallbacks(bg, be, fallback)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-14 22:36:24 +03:00
|
|
|
var tracker ConsensusTracker
|
|
|
|
if bgcfg.ConsensusHA {
|
2024-03-18 21:26:39 +03:00
|
|
|
if bgcfg.ConsensusHARedis.URL == "" {
|
|
|
|
log.Crit("must specify a consensus_ha_redis config when consensus_ha is true")
|
2023-09-14 22:36:24 +03:00
|
|
|
}
|
|
|
|
topts := make([]RedisConsensusTrackerOpt, 0)
|
|
|
|
if bgcfg.ConsensusHALockPeriod > 0 {
|
|
|
|
topts = append(topts, WithLockPeriod(time.Duration(bgcfg.ConsensusHALockPeriod)))
|
|
|
|
}
|
|
|
|
if bgcfg.ConsensusHAHeartbeatInterval > 0 {
|
2024-04-12 01:07:16 +03:00
|
|
|
topts = append(topts, WithHeartbeatInterval(time.Duration(bgcfg.ConsensusHAHeartbeatInterval)))
|
2023-09-14 22:36:24 +03:00
|
|
|
}
|
2024-03-18 21:26:39 +03:00
|
|
|
consensusHARedisClient, err := NewRedisClient(bgcfg.ConsensusHARedis.URL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2024-03-25 21:10:11 +03:00
|
|
|
ns := fmt.Sprintf("%s:%s", bgcfg.ConsensusHARedis.Namespace, bg.Name)
|
|
|
|
tracker = NewRedisConsensusTracker(context.Background(), consensusHARedisClient, bg, ns, topts...)
|
2023-09-14 22:36:24 +03:00
|
|
|
copts = append(copts, WithTracker(tracker))
|
|
|
|
}
|
|
|
|
|
2023-04-18 21:57:55 +03:00
|
|
|
cp := NewConsensusPoller(bg, copts...)
|
|
|
|
bg.Consensus = cp
|
2023-09-14 22:36:24 +03:00
|
|
|
|
|
|
|
if bgcfg.ConsensusHA {
|
|
|
|
tracker.(*RedisConsensusTracker).Init()
|
|
|
|
}
|
2023-04-18 21:57:55 +03:00
|
|
|
}
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
<-errTimer.C
|
|
|
|
log.Info("started proxyd")
|
|
|
|
|
2023-04-18 21:57:55 +03:00
|
|
|
shutdownFunc := func() {
|
2022-05-05 00:51:24 +03:00
|
|
|
log.Info("shutting down proxyd")
|
|
|
|
srv.Shutdown()
|
|
|
|
log.Info("goodbye")
|
2023-04-18 21:57:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return srv, shutdownFunc, nil
|
2022-05-05 00:51:24 +03:00
|
|
|
}
|
|
|
|
|
2023-06-01 23:16:40 +03:00
|
|
|
func validateReceiptsTarget(val string) (string, error) {
|
|
|
|
if val == "" {
|
2023-06-03 00:11:49 +03:00
|
|
|
val = ReceiptsTargetDebugGetRawReceipts
|
2023-06-01 23:16:40 +03:00
|
|
|
}
|
|
|
|
switch val {
|
2023-06-03 00:11:49 +03:00
|
|
|
case ReceiptsTargetDebugGetRawReceipts,
|
|
|
|
ReceiptsTargetAlchemyGetTransactionReceipts,
|
|
|
|
ReceiptsTargetEthGetTransactionReceipts,
|
|
|
|
ReceiptsTargetParityGetTransactionReceipts:
|
2023-06-01 23:16:40 +03:00
|
|
|
return val, nil
|
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("invalid receipts target: %s", val)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-05 00:51:24 +03:00
|
|
|
func secondsToDuration(seconds int) time.Duration {
|
|
|
|
return time.Duration(seconds) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
func configureBackendTLS(cfg *BackendConfig) (*tls.Config, error) {
|
|
|
|
if cfg.CAFile == "" {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsConfig, err := CreateTLSClient(cfg.CAFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if cfg.ClientCertFile != "" && cfg.ClientKeyFile != "" {
|
|
|
|
cert, err := ParseKeyPair(cfg.ClientCertFile, cfg.ClientKeyFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
tlsConfig.Certificates = []tls.Certificate{cert}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tlsConfig, nil
|
|
|
|
}
|