Skip to content

Commit e55729f

Browse files
authored
feat(exporter): increase validator status update query speed (#899)
1 parent 6482f60 commit e55729f

File tree

1 file changed

+22
-4
lines changed
  • backend/pkg/exporter/db

1 file changed

+22
-4
lines changed

backend/pkg/exporter/db/db.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,14 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie
523523
return fmt.Errorf("error preparing insert validator statement: %w", err)
524524
}
525525

526+
validatorStatusUpdateStmt, err := tx.Prepare(`UPDATE validators SET status = $1 WHERE validatorindex = $2;`)
527+
if err != nil {
528+
return fmt.Errorf("error preparing update validator status statement: %w", err)
529+
}
530+
531+
log.Info("updating validator status and metadata")
532+
valiudatorUpdateTs := time.Now()
533+
526534
updates := 0
527535
for _, v := range validators {
528536
// exchange farFutureEpoch with the corresponding max sql value
@@ -610,10 +618,14 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie
610618
}
611619

612620
if c.Status != v.Status {
613-
log.Infof("Status changed for validator %v from %v to %v", v.Index, c.Status, v.Status)
614-
log.Infof("v.ActivationEpoch %v, latestEpoch %v, lastAttestationSlots[v.Index] %v, thresholdSlot %v, lastGlobalAttestedEpoch: %v, lastValidatorAttestedEpoch: %v", v.ActivationEpoch, latestEpoch, lastAttestationSlot, thresholdSlot, lastGlobalAttestedEpoch, lastValidatorAttestedEpoch)
615-
queries.WriteString(fmt.Sprintf("UPDATE validators SET status = '%s' WHERE validatorindex = %d;\n", v.Status, c.Index))
616-
updates++
621+
log.Debugf("Status changed for validator %v from %v to %v", v.Index, c.Status, v.Status)
622+
log.Debugf("v.ActivationEpoch %v, latestEpoch %v, lastAttestationSlots[v.Index] %v, thresholdSlot %v, lastGlobalAttestedEpoch: %v, lastValidatorAttestedEpoch: %v", v.ActivationEpoch, latestEpoch, lastAttestationSlot, thresholdSlot, lastGlobalAttestedEpoch, lastValidatorAttestedEpoch)
623+
//queries.WriteString(fmt.Sprintf("UPDATE validators SET status = '%s' WHERE validatorindex = %d;\n", v.Status, c.Index))
624+
_, err := validatorStatusUpdateStmt.Exec(v.Status, c.Index)
625+
if err != nil {
626+
return fmt.Errorf("error updating validator status: %w", err)
627+
}
628+
//updates++
617629
}
618630
// if c.Balance != v.Balance {
619631
// // log.LogInfo("Balance changed for validator %v from %v to %v", v.Index, c.Balance, v.Balance)
@@ -658,6 +670,11 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie
658670
}
659671
}
660672

673+
err = validatorStatusUpdateStmt.Close()
674+
if err != nil {
675+
return fmt.Errorf("error closing validator status update statement: %w", err)
676+
}
677+
661678
err = insertStmt.Close()
662679
if err != nil {
663680
return fmt.Errorf("error closing insert validator statement: %w", err)
@@ -673,6 +690,7 @@ func SaveValidators(epoch uint64, validators []*types.Validator, client rpc.Clie
673690
}
674691
log.Infof("validator table update completed, took %v", time.Since(updateStart))
675692
}
693+
log.Infof("updating validator status and metadata completed, took %v", time.Since(valiudatorUpdateTs))
676694

677695
s := time.Now()
678696
newValidators := []struct {

0 commit comments

Comments
 (0)