Implement GetHealth in the node API (#8217)

* Implement GetHealth in the node API

* repair fuzz mock

Co-authored-by: Raul Jordan <raul@prysmaticlabs.com>
This commit is contained in:
Radosław Kapka 2021-01-06 21:11:20 +01:00 committed by GitHub
parent da59fdd22b
commit 9b3e1eb643
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 46 additions and 4 deletions

View File

@ -18,7 +18,9 @@ go_library(
"@com_github_gogo_protobuf//types:go_default_library",
"@com_github_pkg_errors//:go_default_library",
"@com_github_prysmaticlabs_ethereumapis//eth/v1:go_default_library",
"@io_opencensus_go//trace:go_default_library",
"@org_golang_google_grpc//:go_default_library",
"@org_golang_google_grpc//status:go_default_library",
],
)
@ -30,6 +32,7 @@ go_test(
],
embed = [":go_default_library"],
deps = [
"//beacon-chain/sync/initial-sync/testing:go_default_library",
"//shared/testutil/assert:go_default_library",
"//shared/testutil/require:go_default_library",
"//shared/version:go_default_library",

View File

@ -3,12 +3,15 @@ package nodev1
import (
"context"
"fmt"
"net/http"
"runtime"
ptypes "github.com/gogo/protobuf/types"
"github.com/pkg/errors"
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1"
"github.com/prysmaticlabs/prysm/shared/version"
"go.opencensus.io/trace"
"google.golang.org/grpc/status"
)
// GetIdentity retrieves data about the node's network presence.
@ -28,7 +31,10 @@ func (ns *Server) ListPeers(ctx context.Context, _ *ptypes.Empty) (*ethpb.PeersR
// GetVersion requests that the beacon node identify information about its implementation in a
// format similar to a HTTP User-Agent field.
func (ns *Server) GetVersion(_ context.Context, _ *ptypes.Empty) (*ethpb.VersionResponse, error) {
func (ns *Server) GetVersion(ctx context.Context, _ *ptypes.Empty) (*ethpb.VersionResponse, error) {
ctx, span := trace.StartSpan(ctx, "nodev1.GetVersion")
defer span.End()
v := fmt.Sprintf("Prysm/%s (%s %s)", version.GetSemanticVersion(), runtime.GOOS, runtime.GOARCH)
return &ethpb.VersionResponse{
Data: &ethpb.Version{
@ -52,5 +58,11 @@ func (ns *Server) GetSyncStatus(ctx context.Context, _ *ptypes.Empty) (*ethpb.Sy
// "503":
// description: Node not initialized or having issues
func (ns *Server) GetHealth(ctx context.Context, _ *ptypes.Empty) (*ptypes.Empty, error) {
return nil, errors.New("unimplemented")
ctx, span := trace.StartSpan(ctx, "nodev1.GetHealth")
defer span.End()
if ns.SyncChecker.Syncing() || ns.SyncChecker.Initialized() {
return &ptypes.Empty{}, nil
}
return &ptypes.Empty{}, status.Error(http.StatusInternalServerError, "node not initialized or having issues")
}

View File

@ -7,6 +7,7 @@ import (
"testing"
ptypes "github.com/gogo/protobuf/types"
syncmock "github.com/prysmaticlabs/prysm/beacon-chain/sync/initial-sync/testing"
"github.com/prysmaticlabs/prysm/shared/testutil/assert"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/prysmaticlabs/prysm/shared/version"
@ -23,3 +24,20 @@ func TestGetVersion(t *testing.T) {
assert.Equal(t, true, strings.Contains(v, os))
assert.Equal(t, true, strings.Contains(v, arch))
}
func TestGetHealth(t *testing.T) {
ctx := context.Background()
checker := &syncmock.Sync{}
s := &Server{
SyncChecker: checker,
}
_, err := s.GetHealth(ctx, &ptypes.Empty{})
require.ErrorContains(t, "node not initialized or having issues", err)
checker.IsInitialized = true
_, err = s.GetHealth(ctx, &ptypes.Empty{})
require.NoError(t, err)
checker.IsInitialized = false
checker.IsSyncing = true
require.NoError(t, err)
}

View File

@ -4,7 +4,8 @@ package testing
// Sync defines a mock for the sync service.
type Sync struct {
IsSyncing bool
IsSyncing bool
IsInitialized bool
}
// Syncing --
@ -12,6 +13,11 @@ func (s *Sync) Syncing() bool {
return s.IsSyncing
}
// Initialized --
func (s *Sync) Initialized() bool {
return s.IsInitialized
}
// Status --
func (s *Sync) Status() error {
return nil

View File

@ -294,6 +294,7 @@ func (s *Service) markForChainStart() {
// Checker defines a struct which can verify whether a node is currently
// synchronizing a chain with the rest of peers in the network.
type Checker interface {
Initialized() bool
Syncing() bool
Status() error
Resync() error

View File

@ -89,10 +89,12 @@ type fakeChecker struct{}
func (fakeChecker) Syncing() bool {
return false
}
func (fakeChecker) Initialized() bool {
return false
}
func (fakeChecker) Status() error {
return nil
}
func (fakeChecker) Resync() error {
return nil
}