2020-04-29 17:40:33 +00:00
|
|
|
// Package node defines a gRPC node service implementation, providing
|
|
|
|
// useful endpoints for checking a node's sync status, peer info,
|
|
|
|
// genesis data, and version information.
|
2019-11-12 17:01:27 +00:00
|
|
|
package node
|
2019-07-23 02:19:55 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-11-30 05:36:02 +00:00
|
|
|
"fmt"
|
2019-07-23 02:19:55 +00:00
|
|
|
"sort"
|
2020-05-29 01:57:03 +00:00
|
|
|
"time"
|
2019-07-23 02:19:55 +00:00
|
|
|
|
|
|
|
ptypes "github.com/gogo/protobuf/types"
|
2019-11-30 05:36:02 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/network"
|
2019-11-27 05:08:18 +00:00
|
|
|
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
|
2019-09-16 20:45:03 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/blockchain"
|
2019-07-23 02:19:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2019-11-30 05:36:02 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/p2p"
|
2019-08-21 20:58:38 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/sync"
|
2019-07-23 02:19:55 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/version"
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2019-11-12 17:01:27 +00:00
|
|
|
// Server defines a server implementation of the gRPC Node service,
|
2019-07-23 02:19:55 +00:00
|
|
|
// providing RPC endpoints for verifying a beacon node's sync status, genesis and
|
|
|
|
// version information, and services the node implements and runs.
|
2019-11-12 17:01:27 +00:00
|
|
|
type Server struct {
|
|
|
|
SyncChecker sync.Checker
|
|
|
|
Server *grpc.Server
|
2020-01-13 17:02:20 +00:00
|
|
|
BeaconDB db.ReadOnlyDatabase
|
2019-11-30 05:36:02 +00:00
|
|
|
PeersFetcher p2p.PeersProvider
|
2020-02-02 01:42:29 +00:00
|
|
|
GenesisTimeFetcher blockchain.TimeFetcher
|
2020-04-27 06:31:50 +00:00
|
|
|
GenesisFetcher blockchain.GenesisFetcher
|
2019-07-23 02:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetSyncStatus checks the current network sync status of the node.
|
2019-11-12 17:01:27 +00:00
|
|
|
func (ns *Server) GetSyncStatus(ctx context.Context, _ *ptypes.Empty) (*ethpb.SyncStatus, error) {
|
2019-07-23 02:19:55 +00:00
|
|
|
return ðpb.SyncStatus{
|
2019-11-12 17:01:27 +00:00
|
|
|
Syncing: ns.SyncChecker.Syncing(),
|
2019-07-23 02:19:55 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-05-29 01:57:03 +00:00
|
|
|
// GetGenesis fetches genesis chain information of Ethereum 2.0. Returns unix timestamp 0
|
|
|
|
// if a genesis time has yet to be determined.
|
2019-11-12 17:01:27 +00:00
|
|
|
func (ns *Server) GetGenesis(ctx context.Context, _ *ptypes.Empty) (*ethpb.Genesis, error) {
|
|
|
|
contractAddr, err := ns.BeaconDB.DepositContractAddress(ctx)
|
2019-09-16 20:45:03 +00:00
|
|
|
if err != nil {
|
2019-11-13 21:03:12 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "Could not retrieve contract address from db: %v", err)
|
2019-09-16 20:45:03 +00:00
|
|
|
}
|
2019-11-12 17:01:27 +00:00
|
|
|
genesisTime := ns.GenesisTimeFetcher.GenesisTime()
|
2020-05-29 01:57:03 +00:00
|
|
|
var defaultGenesisTime time.Time
|
|
|
|
var gt *ptypes.Timestamp
|
|
|
|
if genesisTime == defaultGenesisTime {
|
|
|
|
gt, err = ptypes.TimestampProto(time.Unix(0, 0))
|
|
|
|
} else {
|
|
|
|
gt, err = ptypes.TimestampProto(genesisTime)
|
|
|
|
}
|
2019-09-16 20:45:03 +00:00
|
|
|
if err != nil {
|
2019-11-13 21:03:12 +00:00
|
|
|
return nil, status.Errorf(codes.Internal, "Could not convert genesis time to proto: %v", err)
|
2019-09-16 20:45:03 +00:00
|
|
|
}
|
2020-05-29 01:57:03 +00:00
|
|
|
|
2020-04-27 06:31:50 +00:00
|
|
|
genValRoot := ns.GenesisFetcher.GenesisValidatorRoot()
|
2019-09-16 20:45:03 +00:00
|
|
|
return ðpb.Genesis{
|
|
|
|
GenesisTime: gt,
|
|
|
|
DepositContractAddress: contractAddr,
|
2020-04-27 06:31:50 +00:00
|
|
|
GenesisValidatorsRoot: genValRoot[:],
|
2019-09-16 20:45:03 +00:00
|
|
|
}, nil
|
2019-07-23 02:19:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetVersion checks the version information of the beacon node.
|
2019-11-12 17:01:27 +00:00
|
|
|
func (ns *Server) GetVersion(ctx context.Context, _ *ptypes.Empty) (*ethpb.Version, error) {
|
2019-07-23 02:19:55 +00:00
|
|
|
return ðpb.Version{
|
|
|
|
Version: version.GetVersion(),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListImplementedServices lists the services implemented and enabled by this node.
|
|
|
|
//
|
|
|
|
// Any service not present in this list may return UNIMPLEMENTED or
|
|
|
|
// PERMISSION_DENIED. The server may also support fetching services by grpc
|
|
|
|
// reflection.
|
2019-11-12 17:01:27 +00:00
|
|
|
func (ns *Server) ListImplementedServices(ctx context.Context, _ *ptypes.Empty) (*ethpb.ImplementedServices, error) {
|
|
|
|
serviceInfo := ns.Server.GetServiceInfo()
|
2019-07-23 02:19:55 +00:00
|
|
|
serviceNames := make([]string, 0, len(serviceInfo))
|
|
|
|
for svc := range serviceInfo {
|
|
|
|
serviceNames = append(serviceNames, svc)
|
|
|
|
}
|
|
|
|
sort.Strings(serviceNames)
|
|
|
|
return ðpb.ImplementedServices{
|
|
|
|
Services: serviceNames,
|
|
|
|
}, nil
|
|
|
|
}
|
2019-11-29 16:44:51 +00:00
|
|
|
|
2019-11-30 05:36:02 +00:00
|
|
|
// ListPeers lists the peers connected to this node.
|
2019-11-29 16:44:51 +00:00
|
|
|
func (ns *Server) ListPeers(ctx context.Context, _ *ptypes.Empty) (*ethpb.Peers, error) {
|
2019-12-11 10:31:36 +00:00
|
|
|
res := make([]*ethpb.Peer, 0)
|
|
|
|
for _, pid := range ns.PeersFetcher.Peers().Connected() {
|
|
|
|
multiaddr, err := ns.PeersFetcher.Peers().Address(pid)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
2019-11-30 05:36:02 +00:00
|
|
|
}
|
2019-12-11 10:31:36 +00:00
|
|
|
direction, err := ns.PeersFetcher.Peers().Direction(pid)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
address := fmt.Sprintf("%s/p2p/%s", multiaddr.String(), pid.Pretty())
|
|
|
|
pbDirection := ethpb.PeerDirection_UNKNOWN
|
|
|
|
switch direction {
|
|
|
|
case network.DirInbound:
|
|
|
|
pbDirection = ethpb.PeerDirection_INBOUND
|
|
|
|
case network.DirOutbound:
|
|
|
|
pbDirection = ethpb.PeerDirection_OUTBOUND
|
|
|
|
}
|
|
|
|
res = append(res, ðpb.Peer{
|
|
|
|
Address: address,
|
|
|
|
Direction: pbDirection,
|
|
|
|
})
|
2019-11-30 05:36:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ðpb.Peers{
|
2019-12-11 10:31:36 +00:00
|
|
|
Peers: res,
|
2019-11-30 05:36:02 +00:00
|
|
|
}, nil
|
2019-11-29 16:44:51 +00:00
|
|
|
}
|