Skip to content

Commit

Permalink
Update validator statuses during parsing (#50)
Browse files Browse the repository at this point in the history
* Update validator statuses during parsing

* Use constant instead of int in SaveValidatorStatus

* Append new validators from block to parsing

* Initially create an array of validators from db only

* Start with an empty array before merging db and chain fetched validators
  • Loading branch information
alinetskyi authored Jan 23, 2024
1 parent 8b21e36 commit a3cc328
Showing 1 changed file with 47 additions and 4 deletions.
51 changes: 47 additions & 4 deletions modules/staking/utils_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package staking

import (
"fmt"
"strings"

"github.com/forbole/bdjuno/v4/modules/staking/keybase"
"github.com/forbole/bdjuno/v4/types"
"google.golang.org/grpc/codes"

"github.com/rs/zerolog/log"

Expand Down Expand Up @@ -80,14 +82,40 @@ func (m *Module) convertValidatorDescription(
// RefreshAllValidatorInfos refreshes the info of all the validators at the given height
func (m *Module) RefreshAllValidatorInfos(height int64) error {
// Get all validators
validators, err := m.source.GetValidatorsWithStatus(height, "")
validatorsFromDb, err := m.db.GetValidators()
if err != nil {
return fmt.Errorf("error while getting validators: %s", err)
return fmt.Errorf("error while getting validators from db: %s", err)
}

validatorsFromHeight, err := m.source.GetValidatorsWithStatus(height, "")
if err != nil {
return fmt.Errorf("error while getting validators from db: %s", err)
}

validators := make([]types.Validator, 0)
validatorsMap := make(map[string]bool)

for _, validator := range validatorsFromDb {
validators = append(validators, validator)
validatorsMap[validator.GetOperator()] = true
}

for _, validator := range validatorsFromHeight {
// Check if the validator already exists in validatorsFromDb
if _, exists := validatorsMap[validator.OperatorAddress]; !exists {
// Convert and append the validator if it doesn't exist in validatorsFromDb
convertedValidator, err := m.convertValidator(height, validator)
if err != nil {
return fmt.Errorf("error while converting validator: %s", err)
}
validators = append(validators, convertedValidator)
validatorsMap[validator.OperatorAddress] = true
}
}

// Refresh each validator
for _, validator := range validators {
err = m.RefreshValidatorInfos(height, validator.OperatorAddress)
err = m.RefreshValidatorInfos(height, validator.GetOperator())
if err != nil {
return fmt.Errorf("error while refreshing validator: %s", err)
}
Expand All @@ -98,12 +126,27 @@ func (m *Module) RefreshAllValidatorInfos(height int64) error {

// RefreshValidatorInfos refreshes the info for the validator with the given operator address at the provided height
func (m *Module) RefreshValidatorInfos(height int64, valOper string) error {
validator := types.NewValidator(valOper, "", "", "", nil, nil, height)
stakingValidator, err := m.source.GetValidator(height, valOper)
if err != nil {
if strings.Contains(err.Error(), codes.NotFound.String()) {
validator, err = m.db.GetValidator(valOper)
if err != nil {
return fmt.Errorf("error while getting validator from db: %s", err)
}

err = m.db.SaveValidatorsStatuses([]types.ValidatorStatus{types.NewValidatorStatus(validator.GetConsAddr(), validator.GetConsPubKey(), int(stakingtypes.Unbonded), true, height)})
if err != nil {
return fmt.Errorf("error while saving validator status to db: %s", err)
}

return nil
}

return err
}

validator, err := m.convertValidator(height, stakingValidator)
validator, err = m.convertValidator(height, stakingValidator)
if err != nil {
return fmt.Errorf("error while converting validator: %s", err)
}
Expand Down

0 comments on commit a3cc328

Please sign in to comment.