Skip to content

Commit

Permalink
Merge pull request #219 from gobitfly/NOBIDS/ignore-invalid-valis
Browse files Browse the repository at this point in the history
ignore invalid validators in dashboard
  • Loading branch information
peterbitfly authored Apr 24, 2024
2 parents 0328edd + 1fc784f commit bf45100
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 18 deletions.
2 changes: 1 addition & 1 deletion backend/pkg/api/data_access/vdb_deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (d *DataAccessService) GetValidatorDashboardClDeposits(dashboardId t.VDBId,
for i, row := range data {
pubkeys[i] = hexutil.Encode(row.PublicKey)
}
indices, err := d.services.GetValidatorIndexOfPubkeySlice(pubkeys)
indices, err := d.services.GetValidatorIndicesOfPubkeySlice(pubkeys)
if err != nil {
return nil, nil, fmt.Errorf("failed to recover indices after query: %w", err)
}
Expand Down
8 changes: 4 additions & 4 deletions backend/pkg/api/data_access/vdb_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,19 @@ func (d *DataAccessService) GetValidatorsFromSlices(indices []uint64, publicKeys
return nil, nil
}

_, err := d.services.GetPubkeysOfValidatorIndexSlice(indices)
existingIndices, err := d.services.GetExistingValidatorIndices(indices)
if err != nil {
return nil, err
}

extraIndices, err := d.services.GetValidatorIndexOfPubkeySlice(publicKeys)
extraIndices, err := d.services.GetExistingValidatorIndexesOfPubkeySlice(publicKeys)
if err != nil {
return nil, err
}

// convert to t.VDBValidator slice
validators := make([]t.VDBValidator, len(indices)+len(publicKeys))
for i, index := range append(indices, extraIndices...) {
validators := make([]t.VDBValidator, len(existingIndices)+len(publicKeys))
for i, index := range append(existingIndices, extraIndices...) {
validators[i] = t.VDBValidator{Index: index}
}

Expand Down
27 changes: 19 additions & 8 deletions backend/pkg/api/handlers/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ var (
)

const (
maxNameLength uint64 = 50
maxNameLength = 50
maxValidatorsInList = 20
maxQueryLimit uint64 = 100
defaultReturnLimit uint64 = 10
sortOrderAscending = "asc"
Expand All @@ -62,6 +63,11 @@ const (
forbidEmpty = false
)

var (
errMsgParsingId = errors.New("error parsing parameter 'dashboard_id'")
errBadRequest = errors.New("bad request")
)

type Paging struct {
cursor string
limit uint64
Expand Down Expand Up @@ -90,7 +96,7 @@ func checkName(handlerErr *error, name string, minLength int) string {
if len(name) < minLength {
joinErr(handlerErr, fmt.Sprintf(`given value '%s' for parameter 'name' is too short, minimum length is %d`, name, minLength))
return name
} else if len(name) > 50 {
} else if len(name) > maxNameLength {
joinErr(handlerErr, fmt.Sprintf(`given value '%s' for parameter 'name' is too long, maximum length is %d`, name, maxNameLength))
return name
}
Expand Down Expand Up @@ -209,9 +215,6 @@ func parseDashboardId(id string) (interface{}, error) {
return nil, errors.New("invalid format for parameter 'dashboard_id'")
}
indexes, publicKeys := checkValidatorList(&err, string(decodedId))
if len(indexes)+len(publicKeys) > 20 {
return nil, errors.New("too many validators in the list, maximum is 20")
}
return validatorSet{Indexes: indexes, PublicKeys: publicKeys}, err
}

Expand All @@ -232,6 +235,12 @@ func (h *HandlerService) getDashboardId(dashboardIdParam interface{}) (*types.VD
if err != nil {
return nil, err
}
if len(validators) == 0 {
return nil, fmt.Errorf("%w: no validators found for given id", dataaccess.ErrNotFound)
}
if len(validators) > maxValidatorsInList {
return nil, fmt.Errorf("%w too many validators in list, maximum is %d", errBadRequest, maxValidatorsInList)
}
return &types.VDBId{Validators: validators}, nil
}
return nil, errMsgParsingId
Expand All @@ -243,7 +252,7 @@ func (h *HandlerService) handleDashboardId(param string) (*types.VDBId, error) {
// validate dashboard id param
dashboardIdParam, err := parseDashboardId(param)
if err != nil {
return nil, err
return nil, fmt.Errorf("%w: %w", errBadRequest, err)
}
// convert to VDBId
dashboardId, err := h.getDashboardId(dashboardIdParam)
Expand Down Expand Up @@ -467,15 +476,17 @@ func returnConflict(w http.ResponseWriter, err error) {
}

func returnInternalServerError(w http.ResponseWriter, err error) {
log.Error(err, "internal server error", 0, nil)
returnError(w, http.StatusInternalServerError, err)
}

func handleError(w http.ResponseWriter, err error) {
if errors.Is(err, dataaccess.ErrNotFound) {
returnNotFound(w, err)
return
} else if errors.Is(err, errBadRequest) {
returnBadRequest(w, err)
return
}
// TODO @recy21 define error types in data access package
// TODO @LuccaBitfly handle specific data access errors
returnInternalServerError(w, err)
}
2 changes: 0 additions & 2 deletions backend/pkg/api/handlers/internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ func (h *HandlerService) InternalPutAccountDashboardTransactionsSettings(w http.
// --------------------------------------
// Validator Dashboards

var errMsgParsingId = errors.New("error parsing parameter 'dashboard_id'")

func (h *HandlerService) InternalPostValidatorDashboards(w http.ResponseWriter, r *http.Request) {
var err error
user, err := h.getUser(r)
Expand Down
26 changes: 24 additions & 2 deletions backend/pkg/api/services/service_validator_mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,17 @@ func (s *Services) GetCurrentValidatorMapping() (*ValidatorMapping, func(), erro
return currentValidatorMapping, currentMappingMutex.RUnlock, nil
}

func (s *Services) GetExistingValidatorIndices(indices []uint64) ([]uint64, error) {
validIndices := []uint64{}
for _, index := range indices {
if index > uint64(lastValidatorIndex) {
continue
}
validIndices = append(validIndices, index)
}
return validIndices, nil
}

func (s *Services) GetPubkeysOfValidatorIndexSlice(indices []uint64) ([]string, error) {
res := make([]string, len(indices))
mapping, releaseLock, err := s.GetCurrentValidatorMapping()
Expand All @@ -170,7 +181,18 @@ func (s *Services) GetPubkeysOfValidatorIndexSlice(indices []uint64) ([]string,
return res, nil
}

func (s *Services) GetValidatorIndexOfPubkeySlice(pubkeys []string) ([]uint64, error) {
func (s *Services) GetValidatorIndicesOfPubkeySlice(pubkeys []string) ([]uint64, error) {
indices, err := s.GetExistingValidatorIndexesOfPubkeySlice(pubkeys)
if err != nil {
return nil, err
}
if len(indices) != len(pubkeys) {
return nil, fmt.Errorf("not all pubkeys could be mapped to indices")
}
return indices, nil
}

func (s *Services) GetExistingValidatorIndexesOfPubkeySlice(pubkeys []string) ([]uint64, error) {
res := make([]uint64, len(pubkeys))
mapping, releaseLock, err := s.GetCurrentValidatorMapping()
defer releaseLock()
Expand All @@ -180,7 +202,7 @@ func (s *Services) GetValidatorIndexOfPubkeySlice(pubkeys []string) ([]uint64, e
for i, pubkey := range pubkeys {
p, ok := mapping.ValidatorIndices[pubkey]
if !ok {
return nil, fmt.Errorf("pubkey not found in validator mapping: %s", pubkey)
continue
}
res[i] = *p
}
Expand Down
2 changes: 1 addition & 1 deletion backend/pkg/commons/utils/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func ReadConfig(cfg *types.Config, path string) error {
cfg.Chain.Id = cfg.Chain.ClConfig.DepositChainID

if cfg.RedisSessionStoreEndpoint == "" && cfg.RedisCacheEndpoint != "" {
log.Infof("using RedisCacheEndpoint %s as RedisSessionStoreEndpoint as no dedicated RedisSessionStoreEndpoint was provided", cfg.RedisCacheEndpoint)
log.Warnf("using RedisCacheEndpoint %s as RedisSessionStoreEndpoint as no dedicated RedisSessionStoreEndpoint was provided", cfg.RedisCacheEndpoint)
cfg.RedisSessionStoreEndpoint = cfg.RedisCacheEndpoint
}

Expand Down

0 comments on commit bf45100

Please sign in to comment.