2022-12-06 12:27:26 +00:00
|
|
|
package beacon_api
|
|
|
|
|
|
|
|
import (
|
2023-01-06 03:32:13 +00:00
|
|
|
"context"
|
2023-12-08 04:30:50 +00:00
|
|
|
"fmt"
|
2022-12-06 12:27:26 +00:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
"github.com/pkg/errors"
|
2024-02-15 05:46:47 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
|
|
|
|
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
|
2022-12-06 12:27:26 +00:00
|
|
|
)
|
|
|
|
|
2023-12-08 04:30:50 +00:00
|
|
|
// IndexNotFoundError represents an error scenario where no validator index matches a pubkey.
|
|
|
|
type IndexNotFoundError struct {
|
|
|
|
message string
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewIndexNotFoundError creates a new error instance.
|
|
|
|
func NewIndexNotFoundError(pubkey string) IndexNotFoundError {
|
|
|
|
return IndexNotFoundError{
|
|
|
|
message: fmt.Sprintf("could not find validator index for public key `%s`", pubkey),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Error returns the underlying error message.
|
|
|
|
func (e *IndexNotFoundError) Error() string {
|
|
|
|
return e.message
|
|
|
|
}
|
|
|
|
|
2023-01-06 03:32:13 +00:00
|
|
|
func (c beaconApiValidatorClient) validatorIndex(ctx context.Context, in *ethpb.ValidatorIndexRequest) (*ethpb.ValidatorIndexResponse, error) {
|
2022-12-06 12:27:26 +00:00
|
|
|
stringPubKey := hexutil.Encode(in.PublicKey)
|
|
|
|
|
2023-01-06 03:32:13 +00:00
|
|
|
stateValidator, err := c.stateValidatorsProvider.GetStateValidators(ctx, []string{stringPubKey}, nil, nil)
|
2022-12-06 12:27:26 +00:00
|
|
|
if err != nil {
|
2022-12-14 12:58:36 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to get state validator")
|
2022-12-06 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(stateValidator.Data) == 0 {
|
2023-12-08 04:30:50 +00:00
|
|
|
e := NewIndexNotFoundError(stringPubKey)
|
|
|
|
return nil, &e
|
2022-12-06 12:27:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2023-01-26 14:40:12 +00:00
|
|
|
return ðpb.ValidatorIndexResponse{Index: primitives.ValidatorIndex(index)}, nil
|
2022-12-06 12:27:26 +00:00
|
|
|
}
|