Files
ethdo/grpc/beaconnode.go
Jim McDonald fc9c4cfe0c Tidy-ups
2020-07-20 13:38:51 +01:00

86 lines
2.8 KiB
Go

// Copyright © 2020 Weald Technology Trading
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package grpc
import (
"context"
"github.com/pkg/errors"
"github.com/spf13/viper"
"google.golang.org/grpc"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
wtypes "github.com/wealdtech/go-eth2-wallet-types/v2"
)
// FetchValidatorIndex fetches the index of a validator.
func FetchValidatorIndex(conn *grpc.ClientConn, account wtypes.Account) (uint64, error) {
if conn == nil {
return 0, errors.New("no connection to beacon node")
}
validatorClient := ethpb.NewBeaconNodeValidatorClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("timeout"))
defer cancel()
var pubKey []byte
if pubKeyProvider, ok := account.(wtypes.AccountCompositePublicKeyProvider); ok {
pubKey = pubKeyProvider.CompositePublicKey().Marshal()
} else if pubKeyProvider, ok := account.(wtypes.AccountPublicKeyProvider); ok {
pubKey = pubKeyProvider.PublicKey().Marshal()
} else {
return 0, errors.New("Unable to obtain public key")
}
// Fetch the account.
req := &ethpb.ValidatorIndexRequest{
PublicKey: pubKey,
}
resp, err := validatorClient.ValidatorIndex(ctx, req)
if err != nil {
return 0, err
}
return resp.Index, nil
}
// FetchValidatorState fetches the state of a validator.
func FetchValidatorState(conn *grpc.ClientConn, account wtypes.Account) (ethpb.ValidatorStatus, error) {
if conn == nil {
return ethpb.ValidatorStatus_UNKNOWN_STATUS, errors.New("no connection to beacon node")
}
validatorClient := ethpb.NewBeaconNodeValidatorClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), viper.GetDuration("timeout"))
defer cancel()
var pubKey []byte
if pubKeyProvider, ok := account.(wtypes.AccountCompositePublicKeyProvider); ok {
pubKey = pubKeyProvider.CompositePublicKey().Marshal()
} else if pubKeyProvider, ok := account.(wtypes.AccountPublicKeyProvider); ok {
pubKey = pubKeyProvider.PublicKey().Marshal()
} else {
return ethpb.ValidatorStatus_UNKNOWN_STATUS, errors.New("Unable to obtain public key")
}
// Fetch the account.
req := &ethpb.ValidatorStatusRequest{
PublicKey: pubKey,
}
resp, err := validatorClient.ValidatorStatus(ctx, req)
if err != nil {
return ethpb.ValidatorStatus_UNKNOWN_STATUS, err
}
return resp.Status, nil
}