From bfc6ceff493a6b4f25c42ab76124f6c0efbc5f6d Mon Sep 17 00:00:00 2001 From: Sownak Roy <20537268+sownak@users.noreply.github.com> Date: Mon, 27 Jan 2025 17:07:45 +0530 Subject: [PATCH 1/3] fix: Update non-matching validators [DEV-4671] (#142) * fix update non-matching validators Signed-off-by: Sownak Roy --- database/staking_validators.go | 33 +++++++++++++++++++++++- modules/staking/utils_validators.go | 40 +++++++++++++++++++++++++++++ 2 files changed, 72 insertions(+), 1 deletion(-) diff --git a/database/staking_validators.go b/database/staking_validators.go index 3023ad163..c238e4caa 100644 --- a/database/staking_validators.go +++ b/database/staking_validators.go @@ -4,6 +4,7 @@ import ( "fmt" "github.com/forbole/callisto/v4/types" + "github.com/lib/pq" dbtypes "github.com/forbole/callisto/v4/database/types" @@ -420,7 +421,7 @@ func (db *Db) SaveValidatorsStatuses(statuses []types.ValidatorStatus) error { } validatorStmt = validatorStmt[:len(validatorStmt)-1] - validatorStmt += "ON CONFLICT DO NOTHING" + validatorStmt += " ON CONFLICT DO NOTHING" _, err := db.SQL.Exec(validatorStmt, valParams...) if err != nil { return fmt.Errorf("error while storing validators: %s", err) @@ -441,6 +442,36 @@ WHERE validator_status.height <= excluded.height` return nil } +// GetAllValidatorsWithStatus returns the list of validators having the given status. +func (db *Db) GetAllValidatorsWithStatus(status int) ([]string, error) { + query := `SELECT validator_address FROM validator_status WHERE status = $1` + rows, err := db.SQL.Query(query, status) + if err != nil { + return nil, err + } + defer rows.Close() + + var addresses []string + for rows.Next() { + var address string + if err := rows.Scan(&address); err != nil { + return nil, err + } + addresses = append(addresses, address) + } + return addresses, nil +} + +// UpdateValidators updates the given status of the validators having the given addresses +func (db *Db) UpdateValidators(addresses []string, status int, height int64) error { + query := ` + UPDATE validator_status + SET status = $1, jailed = true + WHERE validator_address = ANY($3) AND height <= $2` + _, err := db.SQL.Exec(query, status, height, pq.Array(addresses)) + return err +} + // saveDoubleSignVote saves the given vote inside the database, returning the row id func (db *Db) saveDoubleSignVote(vote types.DoubleSignVote) (int64, error) { stmt := ` diff --git a/modules/staking/utils_validators.go b/modules/staking/utils_validators.go index 8bafc2f1d..d037e4e30 100644 --- a/modules/staking/utils_validators.go +++ b/modules/staking/utils_validators.go @@ -223,6 +223,11 @@ func (m *Module) UpdateValidatorStatuses() error { return fmt.Errorf("error while updating validators status and voting power: %s", err) } + // update non-matching validators statuses to Unbonded + err = m.updateNonMatchingValidatorStatuses(block.Height, validators, int(stakingtypes.Unbonded)) + if err != nil { + return fmt.Errorf("error while updating non-matching validators status: %s", err) + } // get all active proposals IDs from db ids, err := m.db.GetOpenProposalsIds(block.BlockTimestamp) if err != nil { @@ -321,6 +326,41 @@ func (m *Module) updateValidatorStatusAndVP(height int64, validators []stakingty return nil } +func (m *Module) updateNonMatchingValidatorStatuses(height int64, validators []stakingtypes.Validator, defaultStatus int) error { + // Create a map of validator consensus addresses from the input list + validatorSet := make(map[string]bool, len(validators)) + for _, validator := range validators { + consAddr, err := validator.GetConsAddr() + if err != nil { + return fmt.Errorf("error getting consensus address: %w", err) + } + validatorSet[consAddr.String()] = true + } + // Fetch all validators in the database that have status == Bonded + dbValidators, err := m.db.GetAllValidatorsWithStatus(int(stakingtypes.Bonded)) + if err != nil { + return fmt.Errorf("error fetching validators with status != 1: %w", err) + } + // Identify non-matching validators + nonMatchingValidators := []string{} + for _, dbValidator := range dbValidators { + if _, exists := validatorSet[dbValidator]; !exists { + nonMatchingValidators = append(nonMatchingValidators, dbValidator) + } + } + + // Update the status of non-matching validators + if len(nonMatchingValidators) > 0 { + err := m.db.UpdateValidators(nonMatchingValidators, defaultStatus, height) + if err != nil { + return fmt.Errorf("error updating non-matching validators: %w", err) + } + log.Debug().Int("count", len(nonMatchingValidators)).Msg("Updated non-matching validator statuses") + } + + return nil +} + func (m *Module) GetValidatorsVotingPowers(height int64, vals *tmctypes.ResultValidators) ([]types.ValidatorVotingPower, error) { stakingVals, _, err := m.getValidators(height) if err != nil { From 0da953c93055d8cdf91c41cde5c0bcc2c7d75400 Mon Sep 17 00:00:00 2001 From: Sownak Roy <20537268+sownak@users.noreply.github.com> Date: Tue, 28 Jan 2025 15:32:36 +0530 Subject: [PATCH 2/3] ci: Bump CodeQL version (#144) update codeql version Signed-off-by: Sownak Roy --- .github/workflows/codeql.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index da59e9a54..73c86ca1b 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -23,7 +23,7 @@ jobs: steps: - name: Setup CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: 'go' queries: security-and-quality From a3f680bbe7eebc7ace3f03ba8e84a2439ab9441b Mon Sep 17 00:00:00 2001 From: Sownak Roy Date: Wed, 29 Jan 2025 14:55:54 +0530 Subject: [PATCH 3/3] fix: update codeql action version Signed-off-by: Sownak Roy --- .github/workflows/codeql.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index 73c86ca1b..6fad251fc 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -21,18 +21,18 @@ jobs: contents: read security-events: write - steps: + steps: - name: Setup CodeQL uses: github/codeql-action/init@v3 with: languages: 'go' queries: security-and-quality - - - uses: actions/setup-go@v4 + + - uses: actions/setup-go@v5 with: - go-version: 1.20 + go-version: 1.22 - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 with: fetch-depth: 0 # Required to fetch version @@ -42,4 +42,4 @@ jobs: make build - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v3