2022-12-06 12:27:26 +00:00
|
|
|
package beacon_api
|
|
|
|
|
|
|
|
import (
|
2023-01-06 03:32:13 +00:00
|
|
|
"context"
|
2022-12-06 12:27:26 +00:00
|
|
|
neturl "net/url"
|
2023-01-04 23:15:23 +00:00
|
|
|
"strconv"
|
2022-12-06 12:27:26 +00:00
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
rpcmiddleware "github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/apimiddleware"
|
|
|
|
)
|
|
|
|
|
2023-01-04 23:15:23 +00:00
|
|
|
type stateValidatorsProvider interface {
|
2023-01-06 03:32:13 +00:00
|
|
|
GetStateValidators(context.Context, []string, []int64, []string) (*rpcmiddleware.StateValidatorsResponseJson, error)
|
2023-01-04 23:15:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type beaconApiStateValidatorsProvider struct {
|
|
|
|
jsonRestHandler jsonRestHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c beaconApiStateValidatorsProvider) GetStateValidators(
|
2023-01-06 03:32:13 +00:00
|
|
|
ctx context.Context,
|
2022-12-14 12:58:36 +00:00
|
|
|
stringPubkeys []string,
|
2023-01-04 23:15:23 +00:00
|
|
|
indexes []int64,
|
2022-12-14 12:58:36 +00:00
|
|
|
statuses []string,
|
|
|
|
) (*rpcmiddleware.StateValidatorsResponseJson, error) {
|
2022-12-06 12:27:26 +00:00
|
|
|
params := neturl.Values{}
|
|
|
|
|
2023-01-04 23:15:23 +00:00
|
|
|
stringPubKeysSet := make(map[string]struct{}, len(stringPubkeys))
|
|
|
|
indexesSet := make(map[int64]struct{}, len(indexes))
|
|
|
|
|
2022-12-06 12:27:26 +00:00
|
|
|
for _, stringPubkey := range stringPubkeys {
|
2023-01-04 23:15:23 +00:00
|
|
|
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))
|
|
|
|
}
|
2022-12-06 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
2022-12-14 12:58:36 +00:00
|
|
|
for _, status := range statuses {
|
|
|
|
params.Add("status", status)
|
|
|
|
}
|
|
|
|
|
2022-12-06 12:27:26 +00:00
|
|
|
url := buildURL(
|
|
|
|
"/eth/v1/beacon/states/head/validators",
|
|
|
|
params,
|
|
|
|
)
|
|
|
|
|
|
|
|
stateValidatorsJson := &rpcmiddleware.StateValidatorsResponseJson{}
|
|
|
|
|
2023-01-06 03:32:13 +00:00
|
|
|
_, err := c.jsonRestHandler.GetRestJsonResponse(ctx, url, stateValidatorsJson)
|
2022-12-06 12:27:26 +00:00
|
|
|
if err != nil {
|
2023-01-04 23:15:23 +00:00
|
|
|
return &rpcmiddleware.StateValidatorsResponseJson{}, errors.Wrap(err, "failed to get json response")
|
2022-12-06 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if stateValidatorsJson.Data == nil {
|
2023-01-04 23:15:23 +00:00
|
|
|
return &rpcmiddleware.StateValidatorsResponseJson{}, errors.New("stateValidatorsJson.Data is nil")
|
2022-12-06 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return stateValidatorsJson, nil
|
|
|
|
}
|