Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memory/validator/<valindices> endpoint changes #211

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 52 additions & 14 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -509,33 +509,66 @@ 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")
return
}

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) {
Expand Down Expand Up @@ -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)
Expand Down
Loading