2020-04-02 03:08:23 +00:00
|
|
|
package beaconclient
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
2021-02-23 00:14:50 +00:00
|
|
|
types "github.com/prysmaticlabs/eth2-types"
|
2021-06-02 23:49:52 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
|
2020-04-02 03:08:23 +00:00
|
|
|
"go.opencensus.io/trace"
|
|
|
|
)
|
|
|
|
|
|
|
|
// FindOrGetPublicKeys gets public keys from cache or request validators public
|
|
|
|
// keys from a beacon node via gRPC.
|
2021-01-20 14:39:07 +00:00
|
|
|
func (s *Service) FindOrGetPublicKeys(
|
2020-04-02 03:08:23 +00:00
|
|
|
ctx context.Context,
|
2021-02-23 00:14:50 +00:00
|
|
|
validatorIndices []types.ValidatorIndex,
|
|
|
|
) (map[types.ValidatorIndex][]byte, error) {
|
2020-04-02 03:08:23 +00:00
|
|
|
ctx, span := trace.StartSpan(ctx, "beaconclient.FindOrGetPublicKeys")
|
|
|
|
defer span.End()
|
|
|
|
|
2021-02-23 00:14:50 +00:00
|
|
|
validators := make(map[types.ValidatorIndex][]byte, len(validatorIndices))
|
2020-04-02 03:08:23 +00:00
|
|
|
notFound := 0
|
2021-02-23 01:40:58 +00:00
|
|
|
for _, validatorIndex := range validatorIndices {
|
|
|
|
pub, exists := s.publicKeyCache.Get(validatorIndex)
|
2020-04-02 03:08:23 +00:00
|
|
|
if exists {
|
2021-02-23 01:40:58 +00:00
|
|
|
validators[validatorIndex] = pub
|
2020-04-02 03:08:23 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// inline removal of cached elements from slice
|
2021-02-23 01:40:58 +00:00
|
|
|
validatorIndices[notFound] = validatorIndex
|
2020-04-02 03:08:23 +00:00
|
|
|
notFound++
|
|
|
|
}
|
|
|
|
// trim the slice to its new size
|
|
|
|
validatorIndices = validatorIndices[:notFound]
|
|
|
|
|
|
|
|
if len(validators) > 0 {
|
|
|
|
log.Tracef(
|
|
|
|
"Retrieved validators public keys from cache: %v",
|
|
|
|
validators,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if notFound == 0 {
|
|
|
|
return validators, nil
|
|
|
|
}
|
2021-03-21 17:53:17 +00:00
|
|
|
vc, err := s.cfg.BeaconClient.ListValidators(ctx, ðpb.ListValidatorsRequest{
|
2020-04-02 03:08:23 +00:00
|
|
|
Indices: validatorIndices,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "could not request validators public key: %d", validatorIndices)
|
|
|
|
}
|
|
|
|
for _, v := range vc.ValidatorList {
|
|
|
|
validators[v.Index] = v.Validator.PublicKey
|
2021-01-20 14:39:07 +00:00
|
|
|
s.publicKeyCache.Set(v.Index, v.Validator.PublicKey)
|
2020-04-02 03:08:23 +00:00
|
|
|
}
|
|
|
|
log.Tracef(
|
|
|
|
"Retrieved validators id public key map: %v",
|
|
|
|
validators,
|
|
|
|
)
|
|
|
|
return validators, nil
|
|
|
|
}
|