Skip to content

Commit

Permalink
Merge pull request #2964 from gobitfly/NOBIDS/source_validator_status…
Browse files Browse the repository at this point in the history
…_count_from_table

Nobids/source validator status count from table
  • Loading branch information
guybrush authored Oct 3, 2024
2 parents 91fecbc + 838bf05 commit 2054c64
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
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
11 changes: 11 additions & 0 deletions db/migrations/20241003062452_validator_status_counts.sql
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

0 comments on commit 2054c64

Please sign in to comment.