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

Problem: Validator projection catching up speed is slow #593

Merged
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
94 changes: 0 additions & 94 deletions config/config.mainnet.toml

This file was deleted.

9 changes: 6 additions & 3 deletions projection/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ func (projection *Validator) HandleEvents(height int64, events []event_entity.Ev
return fmt.Errorf("error querying active validators: %v", activeValidatorsQueryErr)
}

var mutActiveValidators []view.ValidatorRow
for _, activeValidator := range activeValidators {
mutActiveValidator := activeValidator
if commitmentMap[mutActiveValidator.ConsensusNodeAddress] {
Expand All @@ -210,9 +211,11 @@ func (projection *Validator) HandleEvents(height int64, events []event_entity.Ev
new(big.Float).SetInt64(mutActiveValidator.TotalActiveBlock),
)

if activeValidatorUpdateErr := validatorsView.Update(&mutActiveValidator); activeValidatorUpdateErr != nil {
return fmt.Errorf("error updating active validators up time data: %v", activeValidatorUpdateErr)
}
mutActiveValidators = append(mutActiveValidators, mutActiveValidator)
}

if activeValidatorUpdateErr := validatorsView.UpdateAllValidatorUpTime(mutActiveValidators); activeValidatorUpdateErr != nil {
return fmt.Errorf("error updating active validators up time data: %v", activeValidatorUpdateErr)
}

} else if votedEvent, ok := event.(*event_usecase.MsgVote); ok {
Expand Down
58 changes: 58 additions & 0 deletions projection/validator/view/validators.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,64 @@ func (validatorsView *Validators) Update(validator *ValidatorRow) error {
return nil
}

func (validatorsView *Validators) UpdateAllValidatorUpTime(validators []ValidatorRow) error {

pendingRowCount := 0
totalRowCount := len(validators)

sql := ""

for i, validator := range validators {

if pendingRowCount == 0 {

sql = `UPDATE view_validators AS view SET
total_signed_block = row.total_signed_block,
total_active_block = row.total_active_block,
imprecise_up_time = row.imprecise_up_time
FROM (VALUES
`
}

sql += fmt.Sprintf(
"(%d, %d, %d, %s),\n",
*validator.MaybeId,
validator.TotalSignedBlock,
validator.TotalActiveBlock,
validator.ImpreciseUpTime.String(),
)

pendingRowCount += 1

if pendingRowCount == 500 || i+1 == totalRowCount {

sql = strings.TrimSuffix(sql, ",\n")

sql += `) AS row(
id,
total_signed_block,
total_active_block,
imprecise_up_time
)
WHERE row.id = view.id;`

result, err := validatorsView.rdb.Exec(sql)
if err != nil {
return fmt.Errorf("error updating validators up time into the table: %v: %w", err, rdb.ErrWrite)
}
if result.RowsAffected() != int64(pendingRowCount) {
return fmt.Errorf("error updating validators up time into the table: wrong number of affected rows %d: %w", result.RowsAffected(), rdb.ErrWrite)
}

pendingRowCount = 0

}

}

return nil
}

type ValidatorsListFilter struct {
MaybeStatuses []constants.Status
}
Expand Down