Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Nobids/source validator status count from table #2964

Merged
merged 3 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,8 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie
return fmt.Errorf("error preparing insert validator statement: %w", err)
}

validatorStatusCounts := make(map[string]int)

updates := 0
for _, v := range validators {

Expand Down Expand Up @@ -1031,6 +1033,7 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie
if err != nil {
logger.Errorf("error saving new validator %v: %v", v.Index, err)
}
validatorStatusCounts[v.Status]++
} else {
// status =
// CASE
Expand Down Expand Up @@ -1071,6 +1074,7 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie
v.Status = "active_online"
}

validatorStatusCounts[v.Status]++
if c.Status != v.Status {
logger.Tracef("Status changed for validator %v from %v to %v", v.Index, c.Status, v.Status)
// logger.Tracef("v.ActivationEpoch %v, latestEpoch %v, lastAttestationSlots[v.Index] %v, thresholdSlot %v", v.ActivationEpoch, latestEpoch, lastAttestationSlots[v.Index], thresholdSlot)
Expand Down Expand Up @@ -1199,6 +1203,20 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie

logger.Infof("updating validator activation epoch balance completed, took %v", time.Since(s))

logger.Infof("updating validator status counts")
s = time.Now()
_, err = tx.Exec("TRUNCATE TABLE validators_status_counts;")
if err != nil {
return fmt.Errorf("error truncating validators_status_counts table: %w", err)
}
for status, count := range validatorStatusCounts {
_, err = tx.Exec("INSERT INTO validators_status_counts (status, validator_count) VALUES ($1, $2);", status, count)
if err != nil {
return fmt.Errorf("error updating validator status counts: %w", err)
}
}
logger.Infof("updating validator status counts completed, took %v", time.Since(s))

s = time.Now()
_, err = tx.Exec("ANALYZE (SKIP_LOCKED) validators;")
if err != nil {
Expand Down
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"`
}
Loading