Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

REST VC: Use POST to fetch validators #13239

Merged
merged 4 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions validator/client/beacon-api/activation_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package beacon_api

import (
"bytes"
"context"
"strings"
"encoding/json"
"testing"
"time"

Expand Down Expand Up @@ -61,15 +62,6 @@ func TestActivation_Nominal(t *testing.T) {
}

pubKeys := make([][]byte, len(stringPubKeys))

url := strings.Join([]string{
"/eth/v1/beacon/states/head/validators?",
"id=0x8000091c2ae64ee414a54c1cc1fc67dec663408bc636cb86756e0200e41a75c8f86603f104f02c856983d2783116be13&",
"id=0x80000e851c0f53c3246ff726d7ff7766661ca5e12a07c45c114d208d54f0f8233d4380b2e9aff759d69795d1df905526&",
"id=0x424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242424242&",
"id=0x800015473bdc3a7f45ef8eb8abc598bc20021e55ad6e6ad1d745aaef9730dd2c28ec08bf42df18451de94dd4a6d24ec5",
}, "")

for i, stringPubKey := range stringPubKeys {
pubKey, err := hexutil.Decode(stringPubKey)
require.NoError(t, err)
Expand Down Expand Up @@ -115,16 +107,25 @@ func TestActivation_Nominal(t *testing.T) {

jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)

req := &beacon.GetValidatorsRequest{
Ids: stringPubKeys,
Statuses: []string{},
}
reqBytes, err := json.Marshal(req)
require.NoError(t, err)

// Get does not return any result for non existing key
jsonRestHandler.EXPECT().Get(
jsonRestHandler.EXPECT().Post(
ctx,
url,
"/eth/v1/beacon/states/head/validators",
nil,
bytes.NewBuffer(reqBytes),
&stateValidatorsResponseJson,
).Return(
nil,
nil,
).SetArg(
2,
4,
beacon.GetValidatorsResponse{
Data: []*beacon.ValidatorContainer{
{
Expand Down Expand Up @@ -239,15 +240,17 @@ func TestActivation_InvalidData(t *testing.T) {
ctx := context.Background()

jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)
jsonRestHandler.EXPECT().Get(
jsonRestHandler.EXPECT().Post(
ctx,
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(
nil,
nil,
).SetArg(
2,
4,
beacon.GetValidatorsResponse{
Data: testCase.data,
},
Expand Down Expand Up @@ -279,10 +282,12 @@ func TestActivation_JsonResponseError(t *testing.T) {
ctx := context.Background()

jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)
jsonRestHandler.EXPECT().Get(
jsonRestHandler.EXPECT().Post(
ctx,
gomock.Any(),
gomock.Any(),
gomock.Any(),
gomock.Any(),
).Return(
nil,
errors.New("some specific json error"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
type beaconApiBeaconChainClient struct {
fallbackClient iface.BeaconChainClient
jsonRestHandler JsonRestHandler
stateValidatorsProvider stateValidatorsProvider
stateValidatorsProvider StateValidatorsProvider
}

const getValidatorPerformanceEndpoint = "/prysm/validators/performance"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestListValidators(t *testing.T) {
defer ctrl.Finish()
ctx := context.Background()

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)
stateValidatorsProvider.EXPECT().GetStateValidatorsForSlot(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(
nil,
errors.New("foo error"),
Expand All @@ -79,7 +79,7 @@ func TestListValidators(t *testing.T) {
defer ctrl.Finish()
ctx := context.Background()

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)
stateValidatorsProvider.EXPECT().GetStateValidatorsForSlot(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(
nil,
errors.New("bar error"),
Expand All @@ -97,7 +97,7 @@ func TestListValidators(t *testing.T) {
defer ctrl.Finish()
ctx := context.Background()

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)
stateValidatorsProvider.EXPECT().GetStateValidatorsForHead(ctx, gomock.Any(), gomock.Any(), gomock.Any()).Return(
nil,
errors.New("foo error"),
Expand All @@ -115,7 +115,7 @@ func TestListValidators(t *testing.T) {
defer ctrl.Finish()
ctx := context.Background()

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)
stateValidatorsProvider.EXPECT().GetStateValidatorsForHead(ctx, gomock.Any(), gomock.Any(), gomock.Any()).Return(
nil,
nil,
Expand Down Expand Up @@ -191,7 +191,7 @@ func TestListValidators(t *testing.T) {
defer ctrl.Finish()
ctx := context.Background()

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)
stateValidatorsProvider.EXPECT().GetStateValidatorsForHead(ctx, gomock.Any(), gomock.Any(), gomock.Any()).Return(
nil,
nil,
Expand Down Expand Up @@ -333,7 +333,7 @@ func TestListValidators(t *testing.T) {
defer ctrl.Finish()
ctx := context.Background()

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)
stateValidatorsProvider.EXPECT().GetStateValidatorsForSlot(ctx, gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(
testCase.generateStateValidatorsResponse(),
nil,
Expand Down Expand Up @@ -561,7 +561,7 @@ func TestListValidators(t *testing.T) {
defer ctrl.Finish()
ctx := context.Background()

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)
stateValidatorsProvider.EXPECT().GetStateValidatorsForSlot(ctx, primitives.Slot(0), make([]string, 0), []primitives.ValidatorIndex{}, nil).Return(
testCase.generateJsonStateValidatorsResponse(),
nil,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
type beaconApiValidatorClient struct {
genesisProvider GenesisProvider
dutiesProvider dutiesProvider
stateValidatorsProvider stateValidatorsProvider
stateValidatorsProvider StateValidatorsProvider
jsonRestHandler JsonRestHandler
beaconBlockConverter beaconBlockConverter
prysmBeaconChainCLient iface.PrysmBeaconChainClient
Expand Down
4 changes: 2 additions & 2 deletions validator/client/beacon-api/doppelganger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func TestCheckDoppelGanger_Nominal(t *testing.T) {
}
}

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)

if testCase.getStateValidatorsInterface != nil {
stateValidatorsProvider.EXPECT().GetStateValidators(
Expand Down Expand Up @@ -784,7 +784,7 @@ func TestCheckDoppelGanger_Errors(t *testing.T) {
).Times(1)
}

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)

if testCase.getStateValidatorsInterface != nil {
stateValidatorsProvider.EXPECT().GetStateValidators(
Expand Down
6 changes: 3 additions & 3 deletions validator/client/beacon-api/duties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1148,7 +1148,7 @@ func TestGetDuties_Valid(t *testing.T) {
).Times(2)
}

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)
stateValidatorsProvider.EXPECT().GetStateValidators(
ctx,
gomock.Any(),
Expand Down Expand Up @@ -1314,7 +1314,7 @@ func TestGetDuties_GetValidatorStatusFailed(t *testing.T) {

ctx := context.Background()

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)
stateValidatorsProvider.EXPECT().GetStateValidators(
ctx,
gomock.Any(),
Expand Down Expand Up @@ -1343,7 +1343,7 @@ func TestGetDuties_GetDutiesForEpochFailed(t *testing.T) {

ctx := context.Background()

stateValidatorsProvider := mock.NewMockstateValidatorsProvider(ctrl)
stateValidatorsProvider := mock.NewMockStateValidatorsProvider(ctrl)
stateValidatorsProvider.EXPECT().GetStateValidators(
ctx,
gomock.Any(),
Expand Down
55 changes: 33 additions & 22 deletions validator/client/beacon-api/index_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package beacon_api

import (
"bytes"
"context"
"fmt"
"encoding/json"
"testing"

"github.com/ethereum/go-ethereum/common/hexutil"
Expand All @@ -18,35 +19,39 @@ import (

const stringPubKey = "0x8000091c2ae64ee414a54c1cc1fc67dec663408bc636cb86756e0200e41a75c8f86603f104f02c856983d2783116be13"

func getPubKeyAndURL(t *testing.T) ([]byte, string) {
baseUrl := "/eth/v1/beacon/states/head/validators"
url := fmt.Sprintf("%s?id=%s", baseUrl, stringPubKey)

func getPubKeyAndReqBuffer(t *testing.T) ([]byte, *bytes.Buffer) {
pubKey, err := hexutil.Decode(stringPubKey)
require.NoError(t, err)

return pubKey, url
req := beacon.GetValidatorsRequest{
Ids: []string{stringPubKey},
Statuses: []string{},
}
reqBytes, err := json.Marshal(req)
require.NoError(t, err)
return pubKey, bytes.NewBuffer(reqBytes)
}

func TestIndex_Nominal(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

pubKey, url := getPubKeyAndURL(t)
pubKey, reqBuffer := getPubKeyAndReqBuffer(t)
ctx := context.Background()

stateValidatorsResponseJson := beacon.GetValidatorsResponse{}
jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)

jsonRestHandler.EXPECT().Get(
jsonRestHandler.EXPECT().Post(
ctx,
url,
"/eth/v1/beacon/states/head/validators",
nil,
reqBuffer,
&stateValidatorsResponseJson,
).Return(
nil,
nil,
).SetArg(
2,
4,
beacon.GetValidatorsResponse{
Data: []*beacon.ValidatorContainer{
{
Expand Down Expand Up @@ -81,21 +86,23 @@ func TestIndex_UnexistingValidator(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

pubKey, url := getPubKeyAndURL(t)
pubKey, reqBuffer := getPubKeyAndReqBuffer(t)
ctx := context.Background()

stateValidatorsResponseJson := beacon.GetValidatorsResponse{}
jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)

jsonRestHandler.EXPECT().Get(
jsonRestHandler.EXPECT().Post(
ctx,
url,
"/eth/v1/beacon/states/head/validators",
nil,
reqBuffer,
&stateValidatorsResponseJson,
).Return(
nil,
nil,
).SetArg(
2,
4,
beacon.GetValidatorsResponse{
Data: []*beacon.ValidatorContainer{},
},
Expand All @@ -122,21 +129,23 @@ func TestIndex_BadIndexError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

pubKey, url := getPubKeyAndURL(t)
pubKey, reqBuffer := getPubKeyAndReqBuffer(t)
ctx := context.Background()

stateValidatorsResponseJson := beacon.GetValidatorsResponse{}
jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)

jsonRestHandler.EXPECT().Get(
jsonRestHandler.EXPECT().Post(
ctx,
url,
"/eth/v1/beacon/states/head/validators",
nil,
reqBuffer,
&stateValidatorsResponseJson,
).Return(
nil,
nil,
).SetArg(
2,
4,
beacon.GetValidatorsResponse{
Data: []*beacon.ValidatorContainer{
{
Expand Down Expand Up @@ -170,15 +179,17 @@ func TestIndex_JsonResponseError(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

pubKey, url := getPubKeyAndURL(t)
pubKey, reqBuffer := getPubKeyAndReqBuffer(t)
ctx := context.Background()

stateValidatorsResponseJson := beacon.GetValidatorsResponse{}
jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)

jsonRestHandler.EXPECT().Get(
jsonRestHandler.EXPECT().Post(
ctx,
url,
"/eth/v1/beacon/states/head/validators",
nil,
reqBuffer,
&stateValidatorsResponseJson,
).Return(
nil,
Expand Down
Loading
Loading