Add REST implementation of node version endpoint (#12809)

This commit is contained in:
Dhruv Bodani 2023-08-25 18:44:08 +05:30 committed by GitHub
parent 30eaddf48f
commit 4ef29a24e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 76 additions and 5 deletions

View File

@ -75,13 +75,19 @@ func (c *beaconApiNodeClient) GetGenesis(ctx context.Context, _ *empty.Empty) (*
}, nil
}
func (c *beaconApiNodeClient) GetVersion(ctx context.Context, in *empty.Empty) (*ethpb.Version, error) {
if c.fallbackClient != nil {
return c.fallbackClient.GetVersion(ctx, in)
func (c *beaconApiNodeClient) GetVersion(ctx context.Context, _ *empty.Empty) (*ethpb.Version, error) {
var versionResponse apimiddleware.VersionResponseJson
if _, err := c.jsonRestHandler.GetRestJsonResponse(ctx, "/eth/v1/node/version", &versionResponse); err != nil {
return nil, errors.Wrapf(err, "failed to query node version")
}
// TODO: Implement me
panic("beaconApiNodeClient.GetVersion is not implemented. To use a fallback client, pass a fallback client as the last argument of NewBeaconApiNodeClientWithFallback.")
if versionResponse.Data == nil || versionResponse.Data.Version == "" {
return nil, errors.New("empty version response")
}
return &ethpb.Version{
Version: versionResponse.Data.Version,
}, nil
}
func (c *beaconApiNodeClient) ListPeers(ctx context.Context, in *empty.Empty) (*ethpb.Peers, error) {

View File

@ -228,3 +228,68 @@ func TestGetSyncStatus(t *testing.T) {
})
}
}
func TestGetVersion(t *testing.T) {
const versionEndpoint = "/eth/v1/node/version"
testCases := []struct {
name string
restEndpointResponse apimiddleware.VersionResponseJson
restEndpointError error
expectedResponse *ethpb.Version
expectedError string
}{
{
name: "fails to query REST endpoint",
restEndpointError: errors.New("foo error"),
expectedError: "failed to query node version",
},
{
name: "returns nil version data",
restEndpointResponse: apimiddleware.VersionResponseJson{Data: nil},
expectedError: "empty version response",
},
{
name: "returns proper version response",
restEndpointResponse: apimiddleware.VersionResponseJson{
Data: &apimiddleware.VersionJson{
Version: "prysm/local",
},
},
expectedResponse: &ethpb.Version{
Version: "prysm/local",
},
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background()
var versionResponse apimiddleware.VersionResponseJson
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
jsonRestHandler.EXPECT().GetRestJsonResponse(
ctx,
versionEndpoint,
&versionResponse,
).Return(
nil,
testCase.restEndpointError,
).SetArg(
2,
testCase.restEndpointResponse,
)
nodeClient := &beaconApiNodeClient{jsonRestHandler: jsonRestHandler}
version, err := nodeClient.GetVersion(ctx, &emptypb.Empty{})
if testCase.expectedResponse == nil {
assert.ErrorContains(t, testCase.expectedError, err)
} else {
assert.DeepEqual(t, testCase.expectedResponse, version)
}
})
}
}