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

feat(exporter): write validators status counts to dedicated table #914

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-- +goose Up
-- +goose StatementBegin
SELECT 'up SQL query';
CREATE TABLE IF NOT EXISTS validators_status_counts (status varchar(20) primary key, validator_count int not null);
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin
SELECT 'down SQL query';
DROP TABLE IF EXISTS validators_status_counts;
-- +goose StatementEnd
19 changes: 18 additions & 1 deletion backend/pkg/exporter/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,8 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie
log.Info("updating validator status and metadata")
valiudatorUpdateTs := time.Now()

validatorStatusCounts := make(map[string]int)

updates := 0
for _, v := range validators {
// exchange farFutureEpoch with the corresponding max sql value
Expand Down Expand Up @@ -572,6 +574,7 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie
if err != nil {
log.Error(err, "error saving new validator", 0, map[string]interface{}{"index": v.Index})
}
validatorStatusCounts[v.Status]++
} else {
// status =
// CASE
Expand Down Expand Up @@ -617,6 +620,7 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie
v.Status = string(constypes.DbActiveOnline)
}

validatorStatusCounts[v.Status]++
if c.Status != v.Status {
log.Debugf("Status changed for validator %v from %v to %v", v.Index, c.Status, v.Status)
log.Debugf("v.ActivationEpoch %v, latestEpoch %v, lastAttestationSlots[v.Index] %v, thresholdSlot %v, lastGlobalAttestedEpoch: %v, lastValidatorAttestedEpoch: %v", v.ActivationEpoch, latestEpoch, lastAttestationSlot, thresholdSlot, lastGlobalAttestedEpoch, lastValidatorAttestedEpoch)
Expand Down Expand Up @@ -752,9 +756,22 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie
return fmt.Errorf("error updating activation epoch balance for validator %v: %w", newValidator.Validatorindex, err)
}
}

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

log.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)
}
}
log.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
Loading