prysm-pulse/validator/client/beacon-api/state_validators.go
Dhruv Bodani 700f5fee8c
Add context to beacon API REST implementation (#11847)
* add context to beacon APIs

* add TODO to merge GET and POST methods

* fix linter action

Co-authored-by: kasey <489222+kasey@users.noreply.github.com>
Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
2023-01-06 03:32:13 +00:00

67 lines
1.7 KiB
Go

package beacon_api
import (
"context"
neturl "net/url"
"strconv"
"github.com/pkg/errors"
rpcmiddleware "github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/apimiddleware"
)
type stateValidatorsProvider interface {
GetStateValidators(context.Context, []string, []int64, []string) (*rpcmiddleware.StateValidatorsResponseJson, error)
}
type beaconApiStateValidatorsProvider struct {
jsonRestHandler jsonRestHandler
}
func (c beaconApiStateValidatorsProvider) GetStateValidators(
ctx context.Context,
stringPubkeys []string,
indexes []int64,
statuses []string,
) (*rpcmiddleware.StateValidatorsResponseJson, error) {
params := neturl.Values{}
stringPubKeysSet := make(map[string]struct{}, len(stringPubkeys))
indexesSet := make(map[int64]struct{}, len(indexes))
for _, stringPubkey := range stringPubkeys {
if _, ok := stringPubKeysSet[stringPubkey]; !ok {
stringPubKeysSet[stringPubkey] = struct{}{}
params.Add("id", stringPubkey)
}
}
for _, index := range indexes {
if _, ok := indexesSet[index]; !ok {
indexesSet[index] = struct{}{}
params.Add("id", strconv.FormatInt(index, 10))
}
}
for _, status := range statuses {
params.Add("status", status)
}
url := buildURL(
"/eth/v1/beacon/states/head/validators",
params,
)
stateValidatorsJson := &rpcmiddleware.StateValidatorsResponseJson{}
_, err := c.jsonRestHandler.GetRestJsonResponse(ctx, url, stateValidatorsJson)
if err != nil {
return &rpcmiddleware.StateValidatorsResponseJson{}, errors.Wrap(err, "failed to get json response")
}
if stateValidatorsJson.Data == nil {
return &rpcmiddleware.StateValidatorsResponseJson{}, errors.New("stateValidatorsJson.Data is nil")
}
return stateValidatorsJson, nil
}