mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-25 12:57:18 +00:00
700f5fee8c
* add context to beacon APIs * add TODO to merge GET and POST methods * fix linter action Co-authored-by: kasey <489222+kasey@users.noreply.github.com> Co-authored-by: james-prysm <90280386+james-prysm@users.noreply.github.com>
126 lines
3.7 KiB
Go
126 lines
3.7 KiB
Go
package beacon_api
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"testing"
|
|
|
|
"github.com/golang/mock/gomock"
|
|
"github.com/prysmaticlabs/prysm/v3/beacon-chain/rpc/apimiddleware"
|
|
"github.com/prysmaticlabs/prysm/v3/testing/assert"
|
|
"github.com/prysmaticlabs/prysm/v3/testing/require"
|
|
"github.com/prysmaticlabs/prysm/v3/validator/client/beacon-api/mock"
|
|
)
|
|
|
|
func TestGetBeaconBlock_RequestFailed(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
ctx := context.Background()
|
|
|
|
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
|
|
jsonRestHandler.EXPECT().GetRestJsonResponse(
|
|
ctx,
|
|
gomock.Any(),
|
|
gomock.Any(),
|
|
).Return(
|
|
nil,
|
|
errors.New("foo error"),
|
|
).Times(1)
|
|
|
|
validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
|
|
_, err := validatorClient.getBeaconBlock(ctx, 1, []byte{1}, []byte{2})
|
|
assert.ErrorContains(t, "failed to query GET REST endpoint", err)
|
|
assert.ErrorContains(t, "foo error", err)
|
|
}
|
|
|
|
func TestGetBeaconBlock_Error(t *testing.T) {
|
|
phase0BeaconBlockBytes, err := json.Marshal(apimiddleware.BeaconBlockJson{})
|
|
require.NoError(t, err)
|
|
altairBeaconBlockBytes, err := json.Marshal(apimiddleware.BeaconBlockAltairJson{})
|
|
require.NoError(t, err)
|
|
bellatrixBeaconBlockBytes, err := json.Marshal(apimiddleware.BeaconBlockBellatrixJson{})
|
|
require.NoError(t, err)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
beaconBlock interface{}
|
|
expectedErrorMessage string
|
|
consensusVersion string
|
|
data json.RawMessage
|
|
}{
|
|
{
|
|
name: "phase0 block decoding failed",
|
|
expectedErrorMessage: "failed to decode phase0 block response json",
|
|
consensusVersion: "phase0",
|
|
data: []byte{},
|
|
},
|
|
{
|
|
name: "phase0 block conversion failed",
|
|
expectedErrorMessage: "failed to get phase0 block",
|
|
consensusVersion: "phase0",
|
|
data: phase0BeaconBlockBytes,
|
|
},
|
|
{
|
|
name: "altair block decoding failed",
|
|
expectedErrorMessage: "failed to decode altair block response json",
|
|
consensusVersion: "altair",
|
|
data: []byte{},
|
|
},
|
|
{
|
|
name: "altair block conversion failed",
|
|
expectedErrorMessage: "failed to get altair block",
|
|
consensusVersion: "altair",
|
|
data: altairBeaconBlockBytes,
|
|
},
|
|
{
|
|
name: "bellatrix block decoding failed",
|
|
expectedErrorMessage: "failed to decode bellatrix block response json",
|
|
beaconBlock: "foo",
|
|
consensusVersion: "bellatrix",
|
|
data: []byte{},
|
|
},
|
|
{
|
|
name: "bellatrix block conversion failed",
|
|
expectedErrorMessage: "failed to get bellatrix block",
|
|
consensusVersion: "bellatrix",
|
|
data: bellatrixBeaconBlockBytes,
|
|
},
|
|
{
|
|
name: "unsupported consensus version",
|
|
expectedErrorMessage: "unsupported consensus version `foo`",
|
|
consensusVersion: "foo",
|
|
},
|
|
}
|
|
|
|
for _, testCase := range testCases {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
ctrl := gomock.NewController(t)
|
|
defer ctrl.Finish()
|
|
|
|
ctx := context.Background()
|
|
|
|
jsonRestHandler := mock.NewMockjsonRestHandler(ctrl)
|
|
jsonRestHandler.EXPECT().GetRestJsonResponse(
|
|
ctx,
|
|
gomock.Any(),
|
|
&abstractProduceBlockResponseJson{},
|
|
).SetArg(
|
|
2,
|
|
abstractProduceBlockResponseJson{
|
|
Version: testCase.consensusVersion,
|
|
Data: testCase.data,
|
|
},
|
|
).Return(
|
|
nil,
|
|
nil,
|
|
).Times(1)
|
|
|
|
validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
|
|
_, err := validatorClient.getBeaconBlock(ctx, 1, []byte{1}, []byte{2})
|
|
assert.ErrorContains(t, testCase.expectedErrorMessage, err)
|
|
})
|
|
}
|
|
}
|