From 48063cdc45b39df9bd828245313f1916e26bb133 Mon Sep 17 00:00:00 2001 From: Marketen Date: Thu, 11 Jan 2024 16:36:42 +0100 Subject: [PATCH] add endpoint changes --- api/api.go | 66 ++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 52 insertions(+), 14 deletions(-) diff --git a/api/api.go b/api/api.go index 171d25a..2b024e6 100644 --- a/api/api.go +++ b/api/api.go @@ -60,7 +60,7 @@ const ( // Memory endpoints: what the oracle knows pathMemoryValidators = "/memory/validators" - pathMemoryValidatorByIndex = "/memory/validator/{valindex}" + pathMemoryValidatorByIndices = "/memory/validator/{valindices}" pathMemoryValidatorsByWithdrawal = "/memory/validators/{withdrawalAddress}" pathMemoryFeesInfo = "/memory/feesinfo" pathMemoryAllBlocks = "/memory/allblocks" @@ -194,7 +194,7 @@ func (m *ApiService) getRouter() http.Handler { // Memory endpoints r.HandleFunc(pathMemoryValidators, m.handleMemoryValidators).Methods(http.MethodGet) - r.HandleFunc(pathMemoryValidatorByIndex, m.handleMemoryValidatorInfo).Methods(http.MethodGet) + r.HandleFunc(pathMemoryValidatorByIndices, m.handleMemoryValidatorInfo).Methods(http.MethodGet) r.HandleFunc(pathMemoryValidatorsByWithdrawal, m.handleMemoryValidatorsByWithdrawal).Methods(http.MethodGet) r.HandleFunc(pathMemoryFeesInfo, m.handleMemoryFeesInfo).Methods(http.MethodGet) r.HandleFunc(pathMemoryPoolStatistics, m.handleMemoryStatistics).Methods(http.MethodGet) @@ -509,6 +509,7 @@ func (m *ApiService) handleMemoryValidators(w http.ResponseWriter, req *http.Req m.respondOK(w, validatorsResp) } +// Returns 1 or more validators, defined by index func (m *ApiService) handleMemoryValidatorInfo(w http.ResponseWriter, req *http.Request) { if !m.OracleReady(MaxSlotsBehind) { m.respondError(w, http.StatusServiceUnavailable, "Oracle node is currently syncing and not serving requests") @@ -516,26 +517,58 @@ func (m *ApiService) handleMemoryValidatorInfo(w http.ResponseWriter, req *http. } vars := mux.Vars(req) - valIndexStr := vars["valindex"] - valIndex, ok := IsValidIndex(valIndexStr) + valIndicesStr := vars["valindices"] - if !ok { - m.respondError(w, http.StatusBadRequest, "invalid validator index: "+valIndexStr) + // Validate the valIndicesStr format + if !isValidIndicesFormat(valIndicesStr) { + m.respondError(w, http.StatusBadRequest, "invalid format for validator indices: "+valIndicesStr) return } - validator, found := m.oracle.State().Validators[valIndex] - if !found { - m.respondError(w, http.StatusBadRequest, fmt.Sprint("could not find validator with index: ", valIndex)) - return + valIndices := strings.Split(valIndicesStr, ",") + + var validators []*oracle.ValidatorInfo + var notFoundIndices []string + for _, valIndexStr := range valIndices { + valIndex, ok := IsValidIndex(valIndexStr) + if !ok { + m.respondError(w, http.StatusBadRequest, "invalid validator index: "+valIndexStr) + return + } + + validator, found := m.oracle.State().Validators[valIndex] + if found { + validators = append(validators, validator) + } else { + notFoundIndices = append(notFoundIndices, valIndexStr) + } + } + + // Make bigNumber of the return array a string + validatorsResp := make([]httpOkValidatorInfo, 0) + for _, v := range validators { + validatorsResp = append(validatorsResp, httpOkValidatorInfo{ + ValidatorStatus: v.ValidatorStatus.String(), + AccumulatedRewardsWei: v.AccumulatedRewardsWei.String(), + PendingRewardsWei: v.PendingRewardsWei.String(), + CollateralWei: v.CollateralWei.String(), + WithdrawalAddress: v.WithdrawalAddress, + ValidatorIndex: v.ValidatorIndex, + ValidatorKey: v.ValidatorKey, + SubscriptionType: v.SubscriptionType.String(), + }) } - // TODO: Temporal, remove in production. - if validator.ValidatorIndex != valIndex { - validator.ValidatorIndex = valIndex + // Respond with found validators and a list of not found indices + response := struct { + FoundValidators []httpOkValidatorInfo `json:"found_validators"` + NotFoundIndices []string `json:"not_found_validators"` + }{ + FoundValidators: validatorsResp, + NotFoundIndices: notFoundIndices, } - m.respondOK(w, validator) + m.respondOK(w, response) } func (m *ApiService) handleMemoryValidatorsByWithdrawal(w http.ResponseWriter, req *http.Request) { @@ -969,6 +1002,11 @@ func IsValidIndex(v string) (uint64, bool) { return val, true } +func isValidIndicesFormat(valIndicesStr string) bool { + re := regexp.MustCompile(`^\d+(,\d+)*$`) + return re.MatchString(valIndicesStr) +} + func IsValidAddress(v string) bool { re := regexp.MustCompile("^0x[0-9a-fA-F]{40}$") return re.MatchString(v)