Skip to content

Commit

Permalink
Update voting powers when validator is created (#51)
Browse files Browse the repository at this point in the history
  • Loading branch information
wojtek-coreum authored Jan 26, 2024
1 parent 05e430f commit 4368eee
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions database/schema/03-staking.sql
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ CREATE TABLE validator_voting_power
(
validator_address TEXT NOT NULL REFERENCES validator (consensus_address) PRIMARY KEY,
voting_power BIGINT NOT NULL,
height BIGINT NOT NULL REFERENCES block (height)
height BIGINT NULL REFERENCES block (height)
);
CREATE INDEX validator_voting_power_height_index ON validator_voting_power (height);

Expand Down Expand Up @@ -108,4 +108,4 @@ CREATE TABLE double_sign_evidence
vote_a_id BIGINT NOT NULL REFERENCES double_sign_vote (id),
vote_b_id BIGINT NOT NULL REFERENCES double_sign_vote (id)
);
CREATE INDEX double_sign_evidence_height_index ON double_sign_evidence (height);
CREATE INDEX double_sign_evidence_height_index ON double_sign_evidence (height);
6 changes: 5 additions & 1 deletion database/staking_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,11 @@ func (db *Db) SaveValidatorsVotingPowers(entries []types.ValidatorVotingPower) e
for i, entry := range entries {
pi := i * 3
stmt += fmt.Sprintf("($%d,$%d,$%d),", pi+1, pi+2, pi+3)
params = append(params, entry.ConsensusAddress, entry.VotingPower, entry.Height)
if entry.Height == 0 {
params = append(params, entry.ConsensusAddress, entry.VotingPower, nil)
} else {
params = append(params, entry.ConsensusAddress, entry.VotingPower, entry.Height)
}
}

stmt = stmt[:len(stmt)-1]
Expand Down
5 changes: 4 additions & 1 deletion modules/staking/handle_genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ func (m *Module) saveValidators(doc *tmtypes.GenesisDoc, validators stakingtypes
vals[i] = validator
}

return m.db.SaveValidatorsData(vals)
if err := m.db.SaveValidatorsData(vals); err != nil {
return err
}
return m.UpdateValidatorStatuses()
}

// saveValidatorDescription saves the description for the given validators
Expand Down
2 changes: 1 addition & 1 deletion modules/staking/handle_msg.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (m *Module) handleMsgCreateValidator(height int64, msg *stakingtypes.MsgCre
if err != nil {
return fmt.Errorf("error while refreshing validator from MsgCreateValidator: %s", err)
}
return nil
return m.UpdateValidatorStatuses()
}

// handleEditValidator handles MsgEditValidator utils, updating the validator info
Expand Down
12 changes: 6 additions & 6 deletions modules/staking/utils_validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -313,10 +313,10 @@ func (m *Module) updateProposalValidatorStatusSnapshot(
// updateValidatorStatusAndVP updates validators status
// and validators voting power
func (m *Module) updateValidatorStatusAndVP(height int64, validators []stakingtypes.Validator) error {
votingPowers := make([]types.ValidatorVotingPower, len(validators))
statuses := make([]types.ValidatorStatus, len(validators))
votingPowers := make([]types.ValidatorVotingPower, 0, len(validators))
statuses := make([]types.ValidatorStatus, 0, len(validators))

for index, validator := range validators {
for _, validator := range validators {
consAddr, err := validator.GetConsAddr()
if err != nil {
return err
Expand All @@ -331,15 +331,15 @@ func (m *Module) updateValidatorStatusAndVP(height int64, validators []stakingty
return err
}

votingPowers[index] = types.NewValidatorVotingPower(consAddr.String(), validator.Tokens.Int64(), height)
votingPowers = append(votingPowers, types.NewValidatorVotingPower(consAddr.String(), validator.Tokens.Int64(), height))

statuses[index] = types.NewValidatorStatus(
statuses = append(statuses, types.NewValidatorStatus(
consAddr.String(),
consPubKey.String(),
int(validator.GetStatus()),
validator.IsJailed(),
height,
)
))
}

log.Debug().Str("module", "staking").Msg("refreshing validator voting power")
Expand Down

0 comments on commit 4368eee

Please sign in to comment.