package db import ( "context" "database/sql" "fmt" "time" ) // DiagnosticHold is an address held on a validator for manual diagnostics. type DiagnosticHold struct { Address string `json:"address"` ValidatorID string `json:"validator_id"` FIPID string `json:"fip_id"` Note string `json:"note"` CreatedAt time.Time `json:"created_at"` } // ClaimDiagnostic moves an idle validator to the diagnostic state and records // the hold, in one transaction. ErrNotFound: no such validator; ErrBusy: the // validator is not idle; ErrConflict: the address or validator is already held. func (d *DB) ClaimDiagnostic(ctx context.Context, validatorID, address, fipID, note string) error { tx, err := d.BeginTx(ctx, nil) if err != nil { return err } defer tx.Rollback() now := timeToDB(Now()) res, err := tx.ExecContext(ctx, ` UPDATE validators SET state=?, updated_at=? WHERE validator_id=? AND state=? AND current_ip_id IS NULL `, ValidatorDiagnostic, now, validatorID, ValidatorIdle) if err != nil { return err } if n, _ := res.RowsAffected(); n != 1 { var one int err := tx.QueryRowContext(ctx, `SELECT 1 FROM validators WHERE validator_id=?`, validatorID).Scan(&one) if err == sql.ErrNoRows { return fmt.Errorf("validator %q: %w", validatorID, ErrNotFound) } if err != nil { return err } return fmt.Errorf("validator %q is not idle: %w", validatorID, ErrBusy) } if _, err := tx.ExecContext(ctx, ` INSERT INTO diagnostic_holds (address, validator_id, fip_id, note, created_at) VALUES (?, ?, ?, ?, ?) `, address, validatorID, fipID, note, now); err != nil { return fmt.Errorf("address %q or validator %q is already held: %w", address, validatorID, ErrConflict) } return tx.Commit() } // ReleaseDiagnostic deletes the hold of an address and returns the validator // to idle. ErrNotFound if the address is not held. func (d *DB) ReleaseDiagnostic(ctx context.Context, address string) (*DiagnosticHold, error) { tx, err := d.BeginTx(ctx, nil) if err != nil { return nil, err } defer tx.Rollback() h, err := scanHold(tx.QueryRowContext(ctx, holdSelect+` WHERE address=?`, address)) if err == sql.ErrNoRows { return nil, fmt.Errorf("address %q is not on diagnostic: %w", address, ErrNotFound) } if err != nil { return nil, err } if _, err := tx.ExecContext(ctx, `DELETE FROM diagnostic_holds WHERE address=?`, address); err != nil { return nil, err } if _, err := tx.ExecContext(ctx, ` UPDATE validators SET state=?, updated_at=? WHERE validator_id=? AND state=? `, ValidatorIdle, timeToDB(Now()), h.ValidatorID, ValidatorDiagnostic); err != nil { return nil, err } return h, tx.Commit() } const holdSelect = `SELECT address, validator_id, fip_id, note, created_at FROM diagnostic_holds` func scanHold(row rowScanner) (*DiagnosticHold, error) { var h DiagnosticHold var created string if err := row.Scan(&h.Address, &h.ValidatorID, &h.FIPID, &h.Note, &created); err != nil { return nil, err } var err error if h.CreatedAt, err = dbToTime(created); err != nil { return nil, err } return &h, nil } // ListDiagnosticHolds returns all holds, oldest first. func (d *DB) ListDiagnosticHolds(ctx context.Context) ([]DiagnosticHold, error) { rows, err := d.QueryContext(ctx, holdSelect+` ORDER BY id`) if err != nil { return nil, err } defer rows.Close() out := []DiagnosticHold{} for rows.Next() { h, err := scanHold(rows) if err != nil { return nil, err } out = append(out, *h) } return out, rows.Err() }