prysm-pulse/validator/client/beacon-api/beacon_committee_selections_test.go
Dhruv Bodani 55a29a4670
Implement beacon committee selections (#13503)
* implement beacon committee selections

* fix build

* fix lint

* fix lint

* Update beacon-chain/rpc/eth/shared/structs.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update validator/client/beacon-api/beacon_committee_selections.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update validator/client/beacon-api/beacon_committee_selections.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* Update validator/client/beacon-api/beacon_committee_selections.go

Co-authored-by: Radosław Kapka <rkapka@wp.pl>

* move beacon committee selection structs to validator module

* fix bazel build files

* add support for POST and GET endpoints for get state validators query

* add a handler to return error from beacon node

* move beacon committee selection to validator top-level module

* fix bazel

* re-arrange fields to fix lint

* fix TestServer_InitializeRoutes

* fix build and lint

* fix build and lint

* fix TestSubmitAggregateAndProof_Distributed

---------

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
2024-02-05 15:43:51 +00:00

125 lines
3.0 KiB
Go

package beacon_api
import (
"bytes"
"context"
"encoding/json"
"testing"
"github.com/prysmaticlabs/prysm/v4/validator/client/iface"
"github.com/golang/mock/gomock"
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v4/testing/require"
"github.com/prysmaticlabs/prysm/v4/validator/client/beacon-api/mock"
test_helpers "github.com/prysmaticlabs/prysm/v4/validator/client/beacon-api/test-helpers"
)
func TestGetAggregatedSelections(t *testing.T) {
testcases := []struct {
name string
req []iface.BeaconCommitteeSelection
res []iface.BeaconCommitteeSelection
endpointError error
expectedErrorMessage string
}{
{
name: "valid",
req: []iface.BeaconCommitteeSelection{
{
SelectionProof: test_helpers.FillByteSlice(96, 82),
Slot: 75,
ValidatorIndex: 76,
},
},
res: []iface.BeaconCommitteeSelection{
{
SelectionProof: test_helpers.FillByteSlice(96, 100),
Slot: 75,
ValidatorIndex: 76,
},
},
},
{
name: "endpoint error",
req: []iface.BeaconCommitteeSelection{
{
SelectionProof: test_helpers.FillByteSlice(96, 82),
Slot: 75,
ValidatorIndex: 76,
},
},
endpointError: errors.New("bad request"),
expectedErrorMessage: "bad request",
},
{
name: "no response error",
req: []iface.BeaconCommitteeSelection{
{
SelectionProof: test_helpers.FillByteSlice(96, 82),
Slot: 75,
ValidatorIndex: 76,
},
},
expectedErrorMessage: "no aggregated selection returned",
},
{
name: "mismatch response",
req: []iface.BeaconCommitteeSelection{
{
SelectionProof: test_helpers.FillByteSlice(96, 82),
Slot: 75,
ValidatorIndex: 76,
},
{
SelectionProof: test_helpers.FillByteSlice(96, 102),
Slot: 75,
ValidatorIndex: 79,
},
},
res: []iface.BeaconCommitteeSelection{
{
SelectionProof: test_helpers.FillByteSlice(96, 100),
Slot: 75,
ValidatorIndex: 76,
},
},
expectedErrorMessage: "mismatching number of selections",
},
}
for _, test := range testcases {
t.Run(test.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)
reqBody, err := json.Marshal(test.req)
require.NoError(t, err)
ctx := context.Background()
jsonRestHandler.EXPECT().Post(
ctx,
"/eth/v1/validator/beacon_committee_selections",
nil,
bytes.NewBuffer(reqBody),
&aggregatedSelectionResponse{},
).SetArg(
4,
aggregatedSelectionResponse{Data: test.res},
).Return(
test.endpointError,
).Times(1)
validatorClient := &beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
res, err := validatorClient.GetAggregatedSelections(ctx, test.req)
if test.expectedErrorMessage != "" {
require.ErrorContains(t, test.expectedErrorMessage, err)
return
}
require.NoError(t, err)
require.DeepEqual(t, test.res, res)
})
}
}