From 2eda72e7238a433dea8de6aa40fde10dfee8c8c3 Mon Sep 17 00:00:00 2001 From: remoterami <142154971+remoterami@users.noreply.github.com> Date: Thu, 19 Dec 2024 11:47:56 +0100 Subject: [PATCH] refactor(API): using config field --- backend/pkg/api/handlers/handler_service.go | 22 ++++++++++----------- backend/pkg/api/handlers/machine_metrics.go | 2 +- backend/pkg/api/handlers/public.go | 4 ++++ backend/pkg/api/router.go | 2 +- 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/backend/pkg/api/handlers/handler_service.go b/backend/pkg/api/handlers/handler_service.go index 472914017..3bbe6dfb1 100644 --- a/backend/pkg/api/handlers/handler_service.go +++ b/backend/pkg/api/handlers/handler_service.go @@ -12,7 +12,6 @@ import ( "strings" "github.com/gobitfly/beaconchain/pkg/commons/log" - "github.com/gobitfly/beaconchain/pkg/commons/utils" "github.com/invopop/jsonschema" "github.com/alexedwards/scs/v2" @@ -24,13 +23,14 @@ import ( ) type HandlerService struct { - daService dataaccess.DataAccessor - daDummy dataaccess.DataAccessor - scs *scs.SessionManager - isPostMachineMetricsEnabled bool // if more config options are needed, consider having the whole config in here + daService dataaccess.DataAccessor + daDummy dataaccess.DataAccessor + scs *scs.SessionManager + + cfg *commontypes.Config } -func NewHandlerService(dataAccessor dataaccess.DataAccessor, dummy dataaccess.DataAccessor, sessionManager *scs.SessionManager, enablePostMachineMetrics bool) *HandlerService { +func NewHandlerService(dataAccessor dataaccess.DataAccessor, dummy dataaccess.DataAccessor, sessionManager *scs.SessionManager, cfg *commontypes.Config) *HandlerService { if allNetworks == nil { networks, err := dataAccessor.GetAllNetworks() if err != nil { @@ -40,10 +40,10 @@ func NewHandlerService(dataAccessor dataaccess.DataAccessor, dummy dataaccess.Da } return &HandlerService{ - daService: dataAccessor, - daDummy: dummy, - scs: sessionManager, - isPostMachineMetricsEnabled: enablePostMachineMetrics, + daService: dataAccessor, + daDummy: dummy, + scs: sessionManager, + cfg: cfg, } } @@ -193,7 +193,7 @@ func (h *HandlerService) getCurrentChartTimeLimitsForDashboard(ctx context.Conte return limits, err } limits.MinAllowedTs = limits.LatestExportedTs - min(maxAge, limits.LatestExportedTs) // min to prevent underflow - secondsPerEpoch := utils.Config.Chain.ClConfig.SlotsPerEpoch * utils.Config.Chain.ClConfig.SecondsPerSlot // TODO: fetch dashboards chain id and use correct value for network once available + secondsPerEpoch := h.cfg.Chain.ClConfig.SlotsPerEpoch * h.cfg.Chain.ClConfig.SecondsPerSlot // TODO: fetch dashboards chain id and use correct value for network once available limits.MaxAllowedInterval = chartDatapointLimit*uint64(aggregation.Duration(secondsPerEpoch).Seconds()) - 1 // -1 to make sure we don't go over the limit return limits, nil diff --git a/backend/pkg/api/handlers/machine_metrics.go b/backend/pkg/api/handlers/machine_metrics.go index 9bef843d5..f2562355b 100644 --- a/backend/pkg/api/handlers/machine_metrics.go +++ b/backend/pkg/api/handlers/machine_metrics.go @@ -72,7 +72,7 @@ func (h *HandlerService) LegacyPostUserMachineMetrics(w http.ResponseWriter, r * apiKey = r.Header.Get("apikey") } - if !h.isPostMachineMetricsEnabled { + if h.cfg.Frontend.DisableStatsInserts { returnError(w, r, http.StatusServiceUnavailable, fmt.Errorf("machine metrics pushing is temporarily disabled")) return } diff --git a/backend/pkg/api/handlers/public.go b/backend/pkg/api/handlers/public.go index cd6b7f7a3..3c88122a4 100644 --- a/backend/pkg/api/handlers/public.go +++ b/backend/pkg/api/handlers/public.go @@ -1350,6 +1350,10 @@ func (h *HandlerService) PublicGetValidatorDashboardRewardsChart(w http.Response handleErr(w, r, v) return } + if afterTs < chartLimits.MinAllowedTs || beforeTs < chartLimits.MinAllowedTs { + returnConflict(w, r, fmt.Errorf("requested time range is too old, minimum timestamp for dashboard owner's premium subscription for this aggregation is %v", chartLimits.MinAllowedTs)) + return + } data, err := h.getDataAccessor(r).GetValidatorDashboardRewardsChart(r.Context(), *dashboardId, groupIds, protocolModes, aggregation, afterTs, beforeTs) if err != nil { diff --git a/backend/pkg/api/router.go b/backend/pkg/api/router.go index ecaa07c38..99b4fcc7e 100644 --- a/backend/pkg/api/router.go +++ b/backend/pkg/api/router.go @@ -33,7 +33,7 @@ func NewApiRouter(dataAccessor dataaccess.DataAccessor, dummy dataaccess.DataAcc if !(cfg.Frontend.CsrfInsecure || cfg.Frontend.Debug) { internalRouter.Use(getCsrfProtectionMiddleware(cfg), csrfInjecterMiddleware) } - handlerService := handlers.NewHandlerService(dataAccessor, dummy, sessionManager, !cfg.Frontend.DisableStatsInserts) + handlerService := handlers.NewHandlerService(dataAccessor, dummy, sessionManager, cfg) // store user id in context, if available publicRouter.Use(handlerService.StoreUserIdByApiKeyMiddleware)