Skip to content

Commit

Permalink
feat: add endpoint to delete all validators from a dashboard
Browse files Browse the repository at this point in the history
  • Loading branch information
LuccaBitfly committed Jan 8, 2025
1 parent 3b2df71 commit a6b4236
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
1 change: 0 additions & 1 deletion backend/pkg/api/data_access/vdb_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -1056,7 +1056,6 @@ func (d *DataAccessService) getAddValidatorsQuery(uniqueValidatorIndexesQuery st
func (d *DataAccessService) RemoveValidatorDashboardValidators(ctx context.Context, dashboardId t.VDBIdPrimary, validators []t.VDBValidator) error {
if len(validators) == 0 {
// Remove all validators for the dashboard
// This is usually forbidden by API validation
_, err := d.alloyWriter.ExecContext(ctx, `
DELETE FROM users_val_dashboards_validators
WHERE dashboard_id = $1
Expand Down
8 changes: 6 additions & 2 deletions backend/pkg/api/handlers/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,10 @@ func (h *HandlerService) InternalPostValidatorDashboardValidators(w http.Respons
h.PublicPostValidatorDashboardValidators(w, r)
}

func (h *HandlerService) InternalDeleteValidatorDashboardValidators(w http.ResponseWriter, r *http.Request) {
h.PublicDeleteValidatorDashboardValidators(w, r)
}

func (h *HandlerService) InternalGetValidatorDashboardValidators(w http.ResponseWriter, r *http.Request) {
h.PublicGetValidatorDashboardValidators(w, r)
}
Expand Down Expand Up @@ -400,8 +404,8 @@ func (h *HandlerService) InternalGetValidatorDashboardMobileValidators(w http.Re
returnOk(w, r, response)
}

func (h *HandlerService) InternalDeleteValidatorDashboardValidators(w http.ResponseWriter, r *http.Request) {
h.PublicDeleteValidatorDashboardValidators(w, r)
func (h *HandlerService) InternalPostValidatorDashboardValidatorBulkDeletions(w http.ResponseWriter, r *http.Request) {
h.PublicPostValidatorDashboardValidatorBulkDeletions(w, r)
}

func (h *HandlerService) InternalPostValidatorDashboardPublicIds(w http.ResponseWriter, r *http.Request) {
Expand Down
50 changes: 38 additions & 12 deletions backend/pkg/api/handlers/public.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func (h *HandlerService) PublicGetValidatorDashboard(w http.ResponseWriter, r *h
returnOk(w, r, response)
}

// PublicPutValidatorDashboard godoc
// PublicDeleteValidatorDashboard godoc
//
// @Description Delete a specified validator dashboard.
// @Security ApiKeyInHeader || ApiKeyInQuery
Expand Down Expand Up @@ -450,7 +450,6 @@ func (h *HandlerService) PublicPutValidatorDashboardGroups(w http.ResponseWriter
// @Description Delete a group in a specified validator dashboard.
// @Tags Validator Dashboard Management
// @Security ApiKeyInHeader || ApiKeyInQuery
// @Accept json
// @Produce json
// @Param dashboard_id path integer true "The ID of the dashboard."
// @Param group_id path integer true "The ID of the group."
Expand Down Expand Up @@ -714,17 +713,44 @@ func (h *HandlerService) PublicGetValidatorDashboardValidators(w http.ResponseWr

// PublicDeleteValidatorDashboardValidators godoc
//
// @Description Remove validators from a specified dashboard.
// @Security ApiKeyInHeader || ApiKeyInQuery
// @Tags Validator Dashboard Management
// @Accept json
// @Produce json
// @Param dashboard_id path integer true "The ID of the dashboard."
// @Param request body handlers.PublicDeleteValidatorDashboardValidators.request true "`validators`: Provide an array of validator indices or public keys that should get removed from the dashboard."
// @Success 204 "Validators removed successfully."
// @Failure 400 {object} types.ApiErrorResponse
// @Router /validator-dashboards/{dashboard_id}/validators/bulk-deletions [post]
// @Description Remove all validators from a specified dashboard.
// @Security ApiKeyInHeader || ApiKeyInQuery
// @Tags Validator Dashboard Management
// @Produce json
// @Param dashboard_id path integer true "The ID of the dashboard."
// @Success 204 "Validators removed successfully."
// @Failure 400 {object} types.ApiErrorResponse
// @Router /validator-dashboards/{dashboard_id}/validators [delete]
func (h *HandlerService) PublicDeleteValidatorDashboardValidators(w http.ResponseWriter, r *http.Request) {
var v validationError
dashboardId := v.checkPrimaryDashboardId(mux.Vars(r)["dashboard_id"])
if v.hasErrors() {
handleErr(w, r, v)
return
}
ctx := r.Context()
err := h.getDataAccessor(r).RemoveValidatorDashboardValidators(ctx, dashboardId, []types.VDBValidator{}) // removes all validators
if err != nil {
handleErr(w, r, err)
return
}

returnNoContent(w, r)
}

// PublicPostValidatorDashboardValidatorBulkDeletions godoc
//
// @Description Remove specific validators from a specified dashboard in bulk.
// @Security ApiKeyInHeader || ApiKeyInQuery
// @Tags Validator Dashboard Management
// @Accept json
// @Produce json
// @Param dashboard_id path integer true "The ID of the dashboard."
// @Param request body handlers.PublicPostValidatorDashboardValidatorBulkDeletions.request true "`validators`: Provide an array of validator indices or public keys that should get removed from the dashboard."
// @Success 204 "Validators removed successfully."
// @Failure 400 {object} types.ApiErrorResponse
// @Router /validator-dashboards/{dashboard_id}/validators/bulk-deletions [post]
func (h *HandlerService) PublicPostValidatorDashboardValidatorBulkDeletions(w http.ResponseWriter, r *http.Request) {
var v validationError
dashboardId := v.checkPrimaryDashboardId(mux.Vars(r)["dashboard_id"])
type request struct {
Expand Down
3 changes: 2 additions & 1 deletion backend/pkg/api/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,8 @@ func addValidatorDashboardRoutes(hs *handlers.HandlerService, publicRouter, inte
{http.MethodDelete, "/{dashboard_id}/groups/{group_id}/validators", hs.PublicDeleteValidatorDashboardGroupValidators, hs.InternalDeleteValidatorDashboardGroupValidators},
{http.MethodPost, "/{dashboard_id}/validators", hs.PublicPostValidatorDashboardValidators, hs.InternalPostValidatorDashboardValidators},
{http.MethodGet, "/{dashboard_id}/validators", hs.PublicGetValidatorDashboardValidators, hs.InternalGetValidatorDashboardValidators},
{http.MethodPost, "/{dashboard_id}/validators/bulk-deletions", hs.PublicDeleteValidatorDashboardValidators, hs.InternalDeleteValidatorDashboardValidators},
{http.MethodDelete, "/{dashboard_id}/validators", hs.PublicDeleteValidatorDashboardValidators, hs.InternalDeleteValidatorDashboardValidators},
{http.MethodPost, "/{dashboard_id}/validators/bulk-deletions", hs.PublicPostValidatorDashboardValidatorBulkDeletions, hs.InternalPostValidatorDashboardValidatorBulkDeletions},
{http.MethodPost, "/{dashboard_id}/public-ids", hs.PublicPostValidatorDashboardPublicIds, hs.InternalPostValidatorDashboardPublicIds},
{http.MethodPut, "/{dashboard_id}/public-ids/{public_id}", hs.PublicPutValidatorDashboardPublicId, hs.InternalPutValidatorDashboardPublicId},
{http.MethodDelete, "/{dashboard_id}/public-ids/{public_id}", hs.PublicDeleteValidatorDashboardPublicId, hs.InternalDeleteValidatorDashboardPublicId},
Expand Down

0 comments on commit a6b4236

Please sign in to comment.