prysm-pulse/slasher/beaconclient/validator_retrieval.go
Raul Jordan 5aac06f04e
Move EthereumAPIs Into Prysm (#8968)
* begin move

* use same import path

* imports

* regen protos

* regen

* no rename

* generate ssz

* gaz

* fmt

* edit build file

* imports

* modify

* remove generated files

* remove protos

* edit imports in prysm

* beacon chain all builds

* edit script

* add generated pbs

* add replace rules

* license for ethereumapis protos

* change visibility

* fmt

* update build files to gaz ignore

* use proper form

* edit imports

* wrap block

* revert scripts

* revert go mod
2021-06-02 18:49:52 -05:00

62 lines
1.6 KiB
Go

package beaconclient
import (
"context"
"github.com/pkg/errors"
types "github.com/prysmaticlabs/eth2-types"
ethpb "github.com/prysmaticlabs/prysm/proto/eth/v1alpha1"
"go.opencensus.io/trace"
)
// FindOrGetPublicKeys gets public keys from cache or request validators public
// keys from a beacon node via gRPC.
func (s *Service) FindOrGetPublicKeys(
ctx context.Context,
validatorIndices []types.ValidatorIndex,
) (map[types.ValidatorIndex][]byte, error) {
ctx, span := trace.StartSpan(ctx, "beaconclient.FindOrGetPublicKeys")
defer span.End()
validators := make(map[types.ValidatorIndex][]byte, len(validatorIndices))
notFound := 0
for _, validatorIndex := range validatorIndices {
pub, exists := s.publicKeyCache.Get(validatorIndex)
if exists {
validators[validatorIndex] = pub
continue
}
// inline removal of cached elements from slice
validatorIndices[notFound] = validatorIndex
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
}
vc, err := s.cfg.BeaconClient.ListValidators(ctx, &ethpb.ListValidatorsRequest{
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
s.publicKeyCache.Set(v.Index, v.Validator.PublicKey)
}
log.Tracef(
"Retrieved validators id public key map: %v",
validators,
)
return validators, nil
}