mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-04-19 03:01:06 -04:00
**What type of PR is this?** Other **What does this PR do? Why is it needed?** Follow up to https://github.com/OffchainLabs/prysm/pull/16215 this pr improves logging, fixes stuttering in package naming, adds additional unit tests, and deduplicates fallback node code. **Which issues(s) does this PR fix?** fixes a potential race if reconnecting to the same host very quickly which has a stale connection still. **Other notes for review** **Acknowledgements** - [x] I have read [CONTRIBUTING.md](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md). - [x] I have included a uniquely named [changelog fragment file](https://github.com/prysmaticlabs/prysm/blob/develop/CONTRIBUTING.md#maintaining-changelogmd). - [x] I have added a description with sufficient context for reviewers to understand this PR. - [x] I have tested that my changes work as expected and I added a testing plan to the PR description (if applicable).
54 lines
1.6 KiB
Go
54 lines
1.6 KiB
Go
package grpc_api
|
|
|
|
import (
|
|
"context"
|
|
|
|
ethpb "github.com/OffchainLabs/prysm/v7/proto/prysm/v1alpha1"
|
|
"github.com/OffchainLabs/prysm/v7/validator/client/iface"
|
|
validatorHelpers "github.com/OffchainLabs/prysm/v7/validator/helpers"
|
|
"github.com/golang/protobuf/ptypes/empty"
|
|
)
|
|
|
|
var (
|
|
_ = iface.NodeClient(&grpcNodeClient{})
|
|
)
|
|
|
|
type grpcNodeClient struct {
|
|
*grpcClientManager[ethpb.NodeClient]
|
|
}
|
|
|
|
func (c *grpcNodeClient) SyncStatus(ctx context.Context, in *empty.Empty) (*ethpb.SyncStatus, error) {
|
|
return c.getClient().GetSyncStatus(ctx, in)
|
|
}
|
|
|
|
func (c *grpcNodeClient) Genesis(ctx context.Context, in *empty.Empty) (*ethpb.Genesis, error) {
|
|
return c.getClient().GetGenesis(ctx, in)
|
|
}
|
|
|
|
func (c *grpcNodeClient) Version(ctx context.Context, in *empty.Empty) (*ethpb.Version, error) {
|
|
return c.getClient().GetVersion(ctx, in)
|
|
}
|
|
|
|
func (c *grpcNodeClient) Peers(ctx context.Context, in *empty.Empty) (*ethpb.Peers, error) {
|
|
return c.getClient().ListPeers(ctx, in)
|
|
}
|
|
|
|
func (c *grpcNodeClient) IsReady(ctx context.Context) bool {
|
|
// GetHealth returns 200 OK only if node is synced and not optimistic.
|
|
// otherwise it will throw an error
|
|
_, err := c.getClient().GetHealth(ctx, ðpb.HealthRequest{})
|
|
if err != nil {
|
|
log.WithError(err).WithField("url", c.conn.GetGrpcConnectionProvider().CurrentHost()).Debug("Node is not ready")
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
// NewNodeClient creates a new gRPC node client that supports
|
|
// dynamic connection switching via the NodeConnection's GrpcConnectionProvider.
|
|
func NewNodeClient(conn validatorHelpers.NodeConnection) iface.NodeClient {
|
|
return &grpcNodeClient{
|
|
grpcClientManager: newGrpcClientManager(conn, ethpb.NewNodeClient),
|
|
}
|
|
}
|