prysm-pulse/validator/accounts/status.go
Michael Huang 62b617fa9a
Command to fetch validator statuses + MultipleValidatorStatus (#5784)
* Add method to fetch account statuses
* Add status command
* gofmt
* Add comment about sorting
* Close conneciton when finished
* Fix error
* Refactor FetchAccountStatuses
* Add status_test.go
* Move sorting out of FetchAccountStatuses
* Remove unnecessary casting
* Expect ValidatorStatus to be called
* Wrap long comment
* Comment out sorting
* Add all necessary dial options
* Close connection before evaluating error from fetch
* Small changes
* Fix lint issues
* Merge branch 'master' into validator-client-account-statuses
* Update dependencies for docker images
* Rename multipleValidatorStatus to activationStatus
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into validator-client-account-statuses
* Update commit hash for ethereumapis
* Implement MultipleValidatorStatus
* Tests for MultipleValidatorStatus
* Fix bugs
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into validator-client-account-statuses
* Add export comment for MultipleValidatorStatus
* Run `go fmt` in prysm (#5815)

* go fmt
* Add fix for nil state in InitializeFromProto (#5817)

* Add nil check and tests
* Add unsafe test
* Update tools/genesis-state-gen/main.go
* Undo genesis state gen changes
* Merge branch 'proto-fuzz-fix' of github.com:prysmaticlabs/prysm into proto-fuzz-fix
* gaz
* Fix
* Add export comment for MultipleValidatorStatus
* Clean up comments
* Update mock files for beacon_node_validator_service
* Merge branch 'validator-client-account-statuses' of https://github.com/michaelhly/prysm into validator-client-account-statuses
* Run gazelle
* Fix mock issues
* Fetch statuses in batches
* Simplify public key generation for status_test
* Sort validator statuses by status type
* Format validator statuses and print to console
* Fix lint issues
* Delimit with commas
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into validator-client-account-statuses
* Rename otheropts to extraopts
* Merge branch 'master' into validator-client-account-statuses
* Merge branch 'validator-client-account-statuses' of https://github.com/michaelhly/prysm into validator-client-account-statuses
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into validator-client-account-statuses
* Clean up MultipleValidatorStatus tests
* Add sync checker to MultipleValidatorStatus
* Update formatting
* Prepend 0x to validator keys
* Check number of status blocks recieved in status_test
* Move sorting to goroutine
* Capitalize constants
* Fix typo
* Use mock reponses in sort test
* Remove byteutils
* Fix ugly format
* Add comment on MultipleValidatorStatus test
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into validator-client-account-statuses
* Create entrypoint to run status command, and make unexported functions internal
* Move merge step into FetchAccountStatuses
* Revert service.go
* Remove responseToSortedMetadata
* Simplify mergeTwo
* Replace fmt output with logrus
* Fix typo
* Merge branch 'master' into validator-client-account-statuses
* Update comment
* Merge branch 'validator-client-account-statuses' of https://github.com/michaelhly/prysm into validator-client-account-statuses
* Return error on bad credentials
* Merge branch 'master' into validator-client-account-statuses
* Merge branch 'master' into validator-client-account-statuses
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into validator-client-account-statuses
* Skip merge step on error
* Fix conflicts
* Fix mock paths
* Add comments
* Convert some sprintfs to wrapfs
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into validator-client-account-statuses
* Rename ExtractPublicKeys to ExtractPublicKeysFromKeyStore and move to account.go
* Add support for keymanager
* Add supported flags to flags list
* Log warning on intermediary errors
* Update output
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into validator-client-account-statuses
* Merge branch 'master' into validator-client-account-statuses
* Fix conflicts
* Merge branch 'master' of https://github.com/prysmaticlabs/prysm into validator-client-account-statuses
* Merge branch 'validator-client-account-statuses' of https://github.com/michaelhly/prysm into validator-client-account-statuses
* Set context timeout for FetchAccountStatuses
* Remove deprecated grpc.WithTimeout
* gofmt
* Remove getters
* Remove parallel stuff
* Move grpc dialing out of status.go
* Update logging based on feedback
* Update validator/accounts/status.go
2020-05-19 05:13:37 +00:00

89 lines
2.5 KiB
Go

package accounts
import (
"context"
"encoding/hex"
"fmt"
"sort"
"time"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
)
// ValidatorStatusMetadata holds all status information about a validator.
type ValidatorStatusMetadata struct {
PublicKey []byte
Metadata *ethpb.ValidatorStatusResponse
}
// RunStatusCommand is the entry point to the `validator status` command.
func RunStatusCommand(
pubkeys [][]byte, beaconNodeRPCProvider ethpb.BeaconNodeValidatorClient) error {
statuses, err := FetchAccountStatuses(
context.Background(), beaconNodeRPCProvider, pubkeys)
if err != nil {
return errors.Wrap(err, "Could not fetch account statuses from the beacon node")
}
printStatuses(statuses)
return nil
}
// FetchAccountStatuses fetches validator statuses from the BeaconNodeValidatorClient
// for each validator public key.
func FetchAccountStatuses(
ctx context.Context,
beaconNodeRPCProvider ethpb.BeaconNodeValidatorClient,
pubkeys [][]byte) ([]ValidatorStatusMetadata, error) {
ctx, span := trace.StartSpan(ctx, "accounts.FetchAccountStatuses")
defer span.End()
ctx, cancel := context.WithTimeout(ctx, 30*time.Second /* Cancel if running over thirty seconds. */)
defer cancel()
req := &ethpb.MultipleValidatorStatusRequest{PublicKeys: pubkeys}
resp, err := beaconNodeRPCProvider.MultipleValidatorStatus(ctx, req)
if err != nil {
return nil, err
}
respKeys := resp.GetPublicKeys()
statuses := make([]ValidatorStatusMetadata, len(respKeys))
for i, status := range resp.GetStatuses() {
statuses[i] = ValidatorStatusMetadata{
PublicKey: respKeys[i],
Metadata: status,
}
}
sort.Slice(statuses, func(i, j int) bool {
return statuses[i].Metadata.Status < statuses[j].Metadata.Status
})
return statuses, nil
}
func printStatuses(validatorStatuses []ValidatorStatusMetadata) {
for _, v := range validatorStatuses {
m := v.Metadata
key := v.PublicKey
log.WithFields(
logrus.Fields{
"PublicKey": hex.EncodeToString(key),
"ActivationEpoch": fieldToString(m.ActivationEpoch),
"DepositInclusionSlot": fieldToString(m.DepositInclusionSlot),
"Eth1DepositBlockNumber": fieldToString(m.Eth1DepositBlockNumber),
"PositionInActivationQueue": fieldToString(m.PositionInActivationQueue),
},
).Infof("Status: %s", m.Status.String())
}
}
func fieldToString(field uint64) string {
// Field is missing
if field == 0 {
return "NA"
}
return fmt.Sprintf("%d", field)
}