Skip to content

Commit

Permalink
feat(validators): improve performance of validator status filters
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbitfly committed Oct 3, 2024
1 parent 08cc84f commit eace845
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 61 deletions.
34 changes: 20 additions & 14 deletions handlers/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,36 @@ func Validators(w http.ResponseWriter, r *http.Request) {

validatorsPageData := types.ValidatorsPageData{}

currentStateCounts := services.LatestValidatorStateCounts()
var currentStateCounts []*types.ValidatorStateCountRow
err := db.ReaderDb.Select(&currentStateCounts, "SELECT status, validator_count FROM validators_status_counts")
if err != nil {
utils.LogError(err, "error retrieving validators state counts", 0, nil)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}

for _, state := range *currentStateCounts {
switch state.Name {
for _, status := range currentStateCounts {
switch status.Name {
case "pending":
validatorsPageData.PendingCount = state.Count
validatorsPageData.PendingCount = status.Count
case "active_online":
validatorsPageData.ActiveOnlineCount = state.Count
validatorsPageData.ActiveOnlineCount = status.Count
case "active_offline":
validatorsPageData.ActiveOfflineCount = state.Count
validatorsPageData.ActiveOfflineCount = status.Count
case "slashing_online":
validatorsPageData.SlashingOnlineCount = state.Count
validatorsPageData.SlashingOnlineCount = status.Count
case "slashing_offline":
validatorsPageData.SlashingOfflineCount = state.Count
validatorsPageData.SlashingOfflineCount = status.Count
case "slashed":
validatorsPageData.Slashed = state.Count
validatorsPageData.Slashed = status.Count
case "exiting_online":
validatorsPageData.ExitingOnlineCount = state.Count
validatorsPageData.ExitingOnlineCount = status.Count
case "exiting_offline":
validatorsPageData.ExitingOfflineCount = state.Count
validatorsPageData.ExitingOfflineCount = status.Count
case "exited":
validatorsPageData.VoluntaryExitsCount = state.Count
validatorsPageData.VoluntaryExitsCount = status.Count
case "deposited":
validatorsPageData.DepositedCount = state.Count
validatorsPageData.DepositedCount = status.Count
}
}

Expand Down Expand Up @@ -352,7 +358,7 @@ func ValidatorsData(w http.ResponseWriter, r *http.Request) {
}
countFiltered := uint64(0)
if dataQuery.StateFilter != "" {
qry = fmt.Sprintf(`SELECT COUNT(*) FROM validators %s`, dataQuery.StateFilter)
qry = fmt.Sprintf(`SELECT SUM(validator_count) FROM validators_status_counts AS validators %s`, dataQuery.StateFilter)
err = db.ReaderDb.Get(&countFiltered, qry)
if err != nil {
utils.LogError(err, "error retrieving validators total count", 0, errFields)
Expand Down
45 changes: 0 additions & 45 deletions services/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,6 @@ func Init() {
ready.Add(1)
go latestExportedStatisticDayUpdater(ready)

ready.Add(1)
go validatorStateCountsUpdater(ready)

if utils.Config.RatelimitUpdater.Enabled {
go ratelimit.DBUpdater()
}
Expand Down Expand Up @@ -1769,45 +1766,3 @@ func LatestExportedStatisticDay() (uint64, error) {
}
return wanted, nil
}

func validatorStateCountsUpdater(wg *sync.WaitGroup) {
firstRun := true

for {
var currentStateCounts []types.ValidatorStateCountRow
qry := "SELECT status AS statename, COUNT(*) AS statecount FROM validators GROUP BY status"
err := db.ReaderDb.Select(&currentStateCounts, qry)

if err != nil {
logger.Errorf("error retrieving validator state counts from the database: %v", err)

if err.Error() == "sql: database is closed" {
logger.Fatalf("error retrieving validator state counts from the database: %v", err)
}
} else {
cacheKey := fmt.Sprintf("%d:frontend:validator_state_counts", utils.Config.Chain.ClConfig.DepositChainID)
err := cache.TieredCache.Set(cacheKey, currentStateCounts, utils.Day)
if err != nil {
logger.Errorf("error caching validator state counts: %v", err)
}
if firstRun {
logger.Info("initialized validator state counts updater")
wg.Done()
firstRun = false
}
}
ReportStatus("validatorStateCountsUpdater", "Running", nil)
time.Sleep(time.Minute * 60)
}
}

func LatestValidatorStateCounts() *[]types.ValidatorStateCountRow {
wanted := []types.ValidatorStateCountRow{}
cacheKey := fmt.Sprintf("%d:frontend:validator_state_counts", utils.Config.Chain.ClConfig.DepositChainID)
if wanted, err := cache.TieredCache.GetWithLocalTimeout(cacheKey, time.Minute, &wanted); err == nil {
return wanted.(*[]types.ValidatorStateCountRow)
} else {
logger.Errorf("error retrieving validator state count data from cache: %v", err)
}
return &wanted
}
4 changes: 2 additions & 2 deletions types/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,6 @@ type SearchValidatorsByEth1Result []struct {
}

type ValidatorStateCountRow struct {
Name string `db:"statename"`
Count uint64 `db:"statecount"`
Name string `db:"status"`
Count uint64 `db:"validator_count"`
}

0 comments on commit eace845

Please sign in to comment.