Move prysm specific performance endpoint (#15062)

* adding in check for non prysm node and moving the prysm endpoint to the prysm beacon client

* fixing a bug connecting to a non prysm client and moving the prysm api call to the prysm beacon client

* changelog

* fixing linting

* Update validator/client/metrics.go

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

---------

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
This commit is contained in:
james-prysm
2025-03-18 10:02:54 -05:00
committed by GitHub
parent 34429368fe
commit 9d2273c514
11 changed files with 139 additions and 72 deletions

View File

@@ -106,6 +106,7 @@ go_test(
"propose_beacon_block_phase0_test.go",
"propose_beacon_block_test.go",
"propose_exit_test.go",
"prysm_beacon_chain_client_test.go",
"registration_test.go",
"state_validators_test.go",
"status_test.go",
@@ -116,7 +117,6 @@ go_test(
"subscribe_committee_subnets_test.go",
"sync_committee_selections_test.go",
"sync_committee_test.go",
"validator_count_test.go",
"wait_for_chain_start_test.go",
],
embed = [":go_default_library"],

View File

@@ -1,9 +1,7 @@
package beacon_api
import (
"bytes"
"context"
"encoding/json"
"reflect"
"strconv"
@@ -23,8 +21,6 @@ type beaconApiChainClient struct {
stateValidatorsProvider StateValidatorsProvider
}
const getValidatorPerformanceEndpoint = "/prysm/validators/performance"
func (c beaconApiChainClient) headBlockHeaders(ctx context.Context) (*structs.GetBlockHeaderResponse, error) {
blockHeader := structs.GetBlockHeaderResponse{}
err := c.jsonRestHandler.Get(ctx, "/eth/v1/beacon/headers/head", &blockHeader)
@@ -320,29 +316,12 @@ func (c beaconApiChainClient) ValidatorQueue(ctx context.Context, in *empty.Empt
}
func (c beaconApiChainClient) ValidatorPerformance(ctx context.Context, in *ethpb.ValidatorPerformanceRequest) (*ethpb.ValidatorPerformanceResponse, error) {
request, err := json.Marshal(structs.GetValidatorPerformanceRequest{
PublicKeys: in.PublicKeys,
Indices: in.Indices,
})
if err != nil {
return nil, errors.Wrap(err, "failed to marshal request")
}
resp := &structs.GetValidatorPerformanceResponse{}
if err = c.jsonRestHandler.Post(ctx, getValidatorPerformanceEndpoint, nil, bytes.NewBuffer(request), resp); err != nil {
return nil, err
if c.fallbackClient != nil {
return c.fallbackClient.ValidatorPerformance(ctx, in)
}
return &ethpb.ValidatorPerformanceResponse{
CurrentEffectiveBalances: resp.CurrentEffectiveBalances,
CorrectlyVotedSource: resp.CorrectlyVotedSource,
CorrectlyVotedTarget: resp.CorrectlyVotedTarget,
CorrectlyVotedHead: resp.CorrectlyVotedHead,
BalancesBeforeEpochTransition: resp.BalancesBeforeEpochTransition,
BalancesAfterEpochTransition: resp.BalancesAfterEpochTransition,
MissingValidators: resp.MissingValidators,
PublicKeys: resp.PublicKeys,
InactivityScores: resp.InactivityScores,
}, nil
// TODO: Implement me
panic("beaconApiChainClient.ValidatorPerformance is not implemented. To use a fallback client, pass a fallback client as the last argument of NewBeaconApiChainClientWithFallback.")
}
func (c beaconApiChainClient) ValidatorParticipation(ctx context.Context, in *ethpb.GetValidatorParticipationRequest) (*ethpb.ValidatorParticipationResponse, error) {

View File

@@ -1,9 +1,7 @@
package beacon_api
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"math"
@@ -13,7 +11,6 @@ import (
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
"github.com/prysmaticlabs/prysm/v5/consensus-types/primitives"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/testing/assert"
"github.com/prysmaticlabs/prysm/v5/testing/require"
@@ -919,43 +916,3 @@ func TestGetChainHead(t *testing.T) {
assert.DeepEqual(t, expectedChainHead, chainHead)
})
}
func Test_beaconApiBeaconChainClient_GetValidatorPerformance(t *testing.T) {
publicKeys := [][48]byte{
bytesutil.ToBytes48([]byte{1}),
bytesutil.ToBytes48([]byte{2}),
bytesutil.ToBytes48([]byte{3}),
}
ctx := context.Background()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
request, err := json.Marshal(structs.GetValidatorPerformanceRequest{
PublicKeys: [][]byte{publicKeys[0][:], publicKeys[2][:], publicKeys[1][:]},
})
require.NoError(t, err)
wantResponse := &structs.GetValidatorPerformanceResponse{}
want := &ethpb.ValidatorPerformanceResponse{}
jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)
jsonRestHandler.EXPECT().Post(
gomock.Any(),
getValidatorPerformanceEndpoint,
nil,
bytes.NewBuffer(request),
wantResponse,
).Return(
nil,
)
c := beaconApiChainClient{
jsonRestHandler: jsonRestHandler,
}
got, err := c.ValidatorPerformance(ctx, &ethpb.ValidatorPerformanceRequest{
PublicKeys: [][]byte{publicKeys[0][:], publicKeys[2][:], publicKeys[1][:]},
})
require.NoError(t, err)
require.DeepEqual(t, want.PublicKeys, got.PublicKeys)
}

View File

@@ -1,7 +1,9 @@
package beacon_api
import (
"bytes"
"context"
"encoding/json"
"fmt"
neturl "net/url"
"strconv"
@@ -10,6 +12,7 @@ import (
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
validator2 "github.com/prysmaticlabs/prysm/v5/consensus-types/validator"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/validator/client/iface"
)
@@ -72,3 +75,39 @@ func (c prysmChainClient) ValidatorCount(ctx context.Context, stateID string, st
return resp, nil
}
func (c prysmChainClient) ValidatorPerformance(ctx context.Context, in *ethpb.ValidatorPerformanceRequest) (*ethpb.ValidatorPerformanceResponse, error) {
// Check node version for prysm beacon node as it is a custom endpoint for prysm beacon node.
nodeVersion, err := c.nodeClient.Version(ctx, nil)
if err != nil {
return nil, errors.Wrap(err, "failed to get node version")
}
if !strings.Contains(strings.ToLower(nodeVersion.Version), "prysm") {
return nil, iface.ErrNotSupported
}
request, err := json.Marshal(structs.GetValidatorPerformanceRequest{
PublicKeys: in.PublicKeys,
Indices: in.Indices,
})
if err != nil {
return nil, errors.Wrap(err, "failed to marshal request")
}
resp := &structs.GetValidatorPerformanceResponse{}
if err = c.jsonRestHandler.Post(ctx, "/prysm/validators/performance", nil, bytes.NewBuffer(request), resp); err != nil {
return nil, err
}
return &ethpb.ValidatorPerformanceResponse{
CurrentEffectiveBalances: resp.CurrentEffectiveBalances,
CorrectlyVotedSource: resp.CorrectlyVotedSource,
CorrectlyVotedTarget: resp.CorrectlyVotedTarget,
CorrectlyVotedHead: resp.CorrectlyVotedHead,
BalancesBeforeEpochTransition: resp.BalancesBeforeEpochTransition,
BalancesAfterEpochTransition: resp.BalancesAfterEpochTransition,
MissingValidators: resp.MissingValidators,
PublicKeys: resp.PublicKeys,
InactivityScores: resp.InactivityScores,
}, nil
}

View File

@@ -1,12 +1,16 @@
package beacon_api
import (
"bytes"
"context"
"encoding/json"
"errors"
"testing"
"github.com/prysmaticlabs/prysm/v5/api/server/structs"
"github.com/prysmaticlabs/prysm/v5/consensus-types/validator"
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/testing/require"
"github.com/prysmaticlabs/prysm/v5/validator/client/beacon-api/mock"
"github.com/prysmaticlabs/prysm/v5/validator/client/iface"
@@ -156,5 +160,61 @@ func TestGetValidatorCount(t *testing.T) {
}
})
}
}
func Test_beaconApiBeaconChainClient_GetValidatorPerformance(t *testing.T) {
const nodeVersion = "prysm/v0.0.1"
publicKeys := [][48]byte{
bytesutil.ToBytes48([]byte{1}),
bytesutil.ToBytes48([]byte{2}),
bytesutil.ToBytes48([]byte{3}),
}
ctx := context.Background()
ctrl := gomock.NewController(t)
defer ctrl.Finish()
request, err := json.Marshal(structs.GetValidatorPerformanceRequest{
PublicKeys: [][]byte{publicKeys[0][:], publicKeys[2][:], publicKeys[1][:]},
})
require.NoError(t, err)
jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)
// Expect node version endpoint call.
var nodeVersionResponse structs.GetVersionResponse
jsonRestHandler.EXPECT().Get(
gomock.Any(),
"/eth/v1/node/version",
&nodeVersionResponse,
).Return(
nil,
).SetArg(
2,
structs.GetVersionResponse{
Data: &structs.Version{Version: nodeVersion},
},
)
wantResponse := &structs.GetValidatorPerformanceResponse{}
want := &ethpb.ValidatorPerformanceResponse{}
jsonRestHandler.EXPECT().Post(
gomock.Any(),
"/prysm/validators/performance",
nil,
bytes.NewBuffer(request),
wantResponse,
).Return(
nil,
)
var client iface.PrysmChainClient = &prysmChainClient{
nodeClient: &beaconApiNodeClient{jsonRestHandler: jsonRestHandler},
jsonRestHandler: jsonRestHandler,
}
got, err := client.ValidatorPerformance(ctx, &ethpb.ValidatorPerformanceRequest{
PublicKeys: [][]byte{publicKeys[0][:], publicKeys[2][:], publicKeys[1][:]},
})
require.NoError(t, err)
require.DeepEqual(t, want.PublicKeys, got.PublicKeys)
}

View File

@@ -91,6 +91,10 @@ func validatorCountByStatus(validators []*ethpb.Validator, statuses []validator.
return resp, nil
}
func (c *grpcPrysmChainClient) ValidatorPerformance(ctx context.Context, in *ethpb.ValidatorPerformanceRequest) (*ethpb.ValidatorPerformanceResponse, error) {
return c.chainClient.ValidatorPerformance(ctx, in)
}
func NewGrpcPrysmChainClient(cc grpc.ClientConnInterface) iface.PrysmChainClient {
return &grpcPrysmChainClient{chainClient: &grpcChainClient{ethpb.NewBeaconChainClient(cc)}}
}

View File

@@ -12,6 +12,6 @@ type ChainClient interface {
ValidatorBalances(ctx context.Context, in *ethpb.ListValidatorBalancesRequest) (*ethpb.ValidatorBalances, error)
Validators(ctx context.Context, in *ethpb.ListValidatorsRequest) (*ethpb.Validators, error)
ValidatorQueue(ctx context.Context, in *empty.Empty) (*ethpb.ValidatorQueue, error)
ValidatorPerformance(ctx context.Context, in *ethpb.ValidatorPerformanceRequest) (*ethpb.ValidatorPerformanceResponse, error)
ValidatorParticipation(ctx context.Context, in *ethpb.GetValidatorParticipationRequest) (*ethpb.ValidatorParticipationResponse, error)
ValidatorPerformance(context.Context, *ethpb.ValidatorPerformanceRequest) (*ethpb.ValidatorPerformanceResponse, error)
}

View File

@@ -5,6 +5,7 @@ import (
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/v5/consensus-types/validator"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
)
var ErrNotSupported = errors.New("endpoint not supported")
@@ -17,4 +18,5 @@ type ValidatorCount struct {
// PrysmChainClient defines an interface required to implement all the prysm specific custom endpoints.
type PrysmChainClient interface {
ValidatorCount(context.Context, string, []validator.Status) ([]ValidatorCount, error)
ValidatorPerformance(context.Context, *ethpb.ValidatorPerformanceRequest) (*ethpb.ValidatorPerformanceResponse, error)
}

View File

@@ -4,6 +4,7 @@ import (
"context"
"fmt"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
fieldparams "github.com/prysmaticlabs/prysm/v5/config/fieldparams"
@@ -12,6 +13,7 @@ import (
"github.com/prysmaticlabs/prysm/v5/encoding/bytesutil"
ethpb "github.com/prysmaticlabs/prysm/v5/proto/prysm/v1alpha1"
"github.com/prysmaticlabs/prysm/v5/time/slots"
"github.com/prysmaticlabs/prysm/v5/validator/client/iface"
"github.com/sirupsen/logrus"
)
@@ -229,8 +231,12 @@ func (v *validator) LogValidatorGainsAndLosses(ctx context.Context, slot primiti
req := &ethpb.ValidatorPerformanceRequest{
PublicKeys: pubKeys,
}
resp, err := v.chainClient.ValidatorPerformance(ctx, req)
resp, err := v.prysmChainClient.ValidatorPerformance(ctx, req)
if err != nil {
if errors.Is(err, iface.ErrNotSupported) {
log.WithError(err).Debug("Skipping validator performance metric for non-Prysm beacon node")
return nil
}
return err
}