prysm-pulse/validator/client/beacon-api/index.go
Manu NALEPA 0a5c65e29c
Add REST implementation for Validator's ValidatorIndex (#11712)
* Add GetAttestationData

* Add tests

* Add many more tests and refactor

* Fix logic

* Address PR comments

* Address PR comments

* Add jsonRestHandler and decouple http logic from rest of the code

* Add buildURL tests

* Remove handlers_test.go

* Improve tests

* Implement `ValidatorIndex` of `beaconApiValidatorClient` using Beacon API

* Implement getStateValidators

* `validatorIndex`: Use `getStateValidators`

Co-authored-by: Patrice Vignola <vignola.patrice@gmail.com>
2022-12-06 12:27:26 +00:00

36 lines
1.0 KiB
Go

//go:build use_beacon_api
// +build use_beacon_api
package beacon_api
import (
"strconv"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/pkg/errors"
types "github.com/prysmaticlabs/prysm/v3/consensus-types/primitives"
ethpb "github.com/prysmaticlabs/prysm/v3/proto/prysm/v1alpha1"
)
func (c beaconApiValidatorClient) validatorIndex(in *ethpb.ValidatorIndexRequest) (*ethpb.ValidatorIndexResponse, error) {
stringPubKey := hexutil.Encode(in.PublicKey)
stateValidator, err := c.getStateValidators([]string{stringPubKey})
if err != nil {
return nil, errors.Wrap(err, "failed to get validator state")
}
if len(stateValidator.Data) == 0 {
return nil, errors.Errorf("could not find validator index for public key `%s`", stringPubKey)
}
stringValidatorIndex := stateValidator.Data[0].Index
index, err := strconv.ParseUint(stringValidatorIndex, 10, 64)
if err != nil {
return nil, errors.Wrap(err, "failed to parse validator index")
}
return &ethpb.ValidatorIndexResponse{Index: types.ValidatorIndex(index)}, nil
}