mirror of
https://github.com/OffchainLabs/prysm.git
synced 2026-01-08 23:18:15 -05:00
Use a single rest handler (#13446)
This commit is contained in:
@@ -3,6 +3,7 @@ package accounts
|
||||
import (
|
||||
"context"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
@@ -10,6 +11,7 @@ import (
|
||||
grpcutil "github.com/prysmaticlabs/prysm/v4/api/grpc"
|
||||
"github.com/prysmaticlabs/prysm/v4/crypto/bls"
|
||||
"github.com/prysmaticlabs/prysm/v4/validator/accounts/wallet"
|
||||
beaconApi "github.com/prysmaticlabs/prysm/v4/validator/client/beacon-api"
|
||||
iface "github.com/prysmaticlabs/prysm/v4/validator/client/iface"
|
||||
nodeClientFactory "github.com/prysmaticlabs/prysm/v4/validator/client/node-client-factory"
|
||||
validatorClientFactory "github.com/prysmaticlabs/prysm/v4/validator/client/validator-client-factory"
|
||||
@@ -80,14 +82,18 @@ func (acm *CLIManager) prepareBeaconClients(ctx context.Context) (*iface.Validat
|
||||
if err != nil {
|
||||
return nil, nil, errors.Wrapf(err, "could not dial endpoint %s", acm.beaconRPCProvider)
|
||||
}
|
||||
|
||||
conn := validatorHelpers.NewNodeConnection(
|
||||
grpcConn,
|
||||
acm.beaconApiEndpoint,
|
||||
acm.beaconApiTimeout,
|
||||
)
|
||||
|
||||
validatorClient := validatorClientFactory.NewValidatorClient(conn)
|
||||
nodeClient := nodeClientFactory.NewNodeClient(conn)
|
||||
restHandler := &beaconApi.BeaconApiJsonRestHandler{
|
||||
HttpClient: http.Client{Timeout: acm.beaconApiTimeout},
|
||||
Host: acm.beaconApiEndpoint,
|
||||
}
|
||||
validatorClient := validatorClientFactory.NewValidatorClient(conn, restHandler)
|
||||
nodeClient := nodeClientFactory.NewNodeClient(conn, restHandler)
|
||||
|
||||
return &validatorClient, &nodeClient, nil
|
||||
}
|
||||
|
||||
@@ -4,10 +4,8 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"reflect"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/golang/protobuf/ptypes/empty"
|
||||
@@ -357,12 +355,7 @@ func (c beaconApiBeaconChainClient) GetValidatorParticipation(ctx context.Contex
|
||||
panic("beaconApiBeaconChainClient.GetValidatorParticipation is not implemented. To use a fallback client, pass a fallback client as the last argument of NewBeaconApiBeaconChainClientWithFallback.")
|
||||
}
|
||||
|
||||
func NewBeaconApiBeaconChainClientWithFallback(host string, timeout time.Duration, fallbackClient iface.BeaconChainClient) iface.BeaconChainClient {
|
||||
jsonRestHandler := beaconApiJsonRestHandler{
|
||||
httpClient: http.Client{Timeout: timeout},
|
||||
host: host,
|
||||
}
|
||||
|
||||
func NewBeaconApiBeaconChainClientWithFallback(jsonRestHandler JsonRestHandler, fallbackClient iface.BeaconChainClient) iface.BeaconChainClient {
|
||||
return &beaconApiBeaconChainClient{
|
||||
jsonRestHandler: jsonRestHandler,
|
||||
fallbackClient: fallbackClient,
|
||||
|
||||
@@ -2,9 +2,7 @@ package beacon_api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
"github.com/golang/protobuf/ptypes/empty"
|
||||
@@ -100,12 +98,7 @@ func (c *beaconApiNodeClient) ListPeers(ctx context.Context, in *empty.Empty) (*
|
||||
panic("beaconApiNodeClient.ListPeers is not implemented. To use a fallback client, pass a fallback client as the last argument of NewBeaconApiNodeClientWithFallback.")
|
||||
}
|
||||
|
||||
func NewNodeClientWithFallback(host string, timeout time.Duration, fallbackClient iface.NodeClient) iface.NodeClient {
|
||||
jsonRestHandler := beaconApiJsonRestHandler{
|
||||
httpClient: http.Client{Timeout: timeout},
|
||||
host: host,
|
||||
}
|
||||
|
||||
func NewNodeClientWithFallback(jsonRestHandler JsonRestHandler, fallbackClient iface.NodeClient) iface.NodeClient {
|
||||
return &beaconApiNodeClient{
|
||||
jsonRestHandler: jsonRestHandler,
|
||||
fallbackClient: fallbackClient,
|
||||
|
||||
@@ -2,7 +2,6 @@ package beacon_api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common/hexutil"
|
||||
@@ -23,12 +22,7 @@ type beaconApiValidatorClient struct {
|
||||
prysmBeaconChainCLient iface.PrysmBeaconChainClient
|
||||
}
|
||||
|
||||
func NewBeaconApiValidatorClient(host string, timeout time.Duration) iface.ValidatorClient {
|
||||
jsonRestHandler := beaconApiJsonRestHandler{
|
||||
httpClient: http.Client{Timeout: timeout},
|
||||
host: host,
|
||||
}
|
||||
|
||||
func NewBeaconApiValidatorClient(jsonRestHandler JsonRestHandler) iface.ValidatorClient {
|
||||
return &beaconApiValidatorClient{
|
||||
genesisProvider: beaconApiGenesisProvider{jsonRestHandler: jsonRestHandler},
|
||||
dutiesProvider: beaconApiDutiesProvider{jsonRestHandler: jsonRestHandler},
|
||||
|
||||
@@ -13,29 +13,29 @@ import (
|
||||
)
|
||||
|
||||
type JsonRestHandler interface {
|
||||
Get(ctx context.Context, query string, resp interface{}) error
|
||||
Get(ctx context.Context, endpoint string, resp interface{}) error
|
||||
Post(ctx context.Context, endpoint string, headers map[string]string, data *bytes.Buffer, resp interface{}) error
|
||||
}
|
||||
|
||||
type beaconApiJsonRestHandler struct {
|
||||
httpClient http.Client
|
||||
host string
|
||||
type BeaconApiJsonRestHandler struct {
|
||||
HttpClient http.Client
|
||||
Host string
|
||||
}
|
||||
|
||||
// Get sends a GET request and decodes the response body as a JSON object into the passed in object.
|
||||
// If an HTTP error is returned, the body is decoded as a DefaultJsonError JSON object and returned as the first return value.
|
||||
func (c beaconApiJsonRestHandler) Get(ctx context.Context, endpoint string, resp interface{}) error {
|
||||
func (c BeaconApiJsonRestHandler) Get(ctx context.Context, endpoint string, resp interface{}) error {
|
||||
if resp == nil {
|
||||
return errors.New("resp is nil")
|
||||
}
|
||||
|
||||
url := c.host + endpoint
|
||||
url := c.Host + endpoint
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to create request for endpoint %s", url)
|
||||
}
|
||||
|
||||
httpResp, err := c.httpClient.Do(req)
|
||||
httpResp, err := c.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to perform request for endpoint %s", url)
|
||||
}
|
||||
@@ -50,7 +50,7 @@ func (c beaconApiJsonRestHandler) Get(ctx context.Context, endpoint string, resp
|
||||
|
||||
// Post sends a POST request and decodes the response body as a JSON object into the passed in object.
|
||||
// If an HTTP error is returned, the body is decoded as a DefaultJsonError JSON object and returned as the first return value.
|
||||
func (c beaconApiJsonRestHandler) Post(
|
||||
func (c BeaconApiJsonRestHandler) Post(
|
||||
ctx context.Context,
|
||||
apiEndpoint string,
|
||||
headers map[string]string,
|
||||
@@ -61,7 +61,7 @@ func (c beaconApiJsonRestHandler) Post(
|
||||
return errors.New("data is nil")
|
||||
}
|
||||
|
||||
url := c.host + apiEndpoint
|
||||
url := c.Host + apiEndpoint
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, data)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to create request for endpoint %s", url)
|
||||
@@ -72,7 +72,7 @@ func (c beaconApiJsonRestHandler) Post(
|
||||
}
|
||||
req.Header.Set("Content-Type", api.JsonMediaType)
|
||||
|
||||
httpResp, err := c.httpClient.Do(req)
|
||||
httpResp, err := c.HttpClient.Do(req)
|
||||
if err != nil {
|
||||
return errors.Wrapf(err, "failed to perform request for endpoint %s", url)
|
||||
}
|
||||
|
||||
@@ -40,9 +40,9 @@ func TestGet(t *testing.T) {
|
||||
server := httptest.NewServer(mux)
|
||||
defer server.Close()
|
||||
|
||||
jsonRestHandler := beaconApiJsonRestHandler{
|
||||
httpClient: http.Client{Timeout: time.Second * 5},
|
||||
host: server.URL,
|
||||
jsonRestHandler := BeaconApiJsonRestHandler{
|
||||
HttpClient: http.Client{Timeout: time.Second * 5},
|
||||
Host: server.URL,
|
||||
}
|
||||
resp := &beacon.GetGenesisResponse{}
|
||||
require.NoError(t, jsonRestHandler.Get(ctx, endpoint+"?arg1=abc&arg2=def", resp))
|
||||
@@ -86,9 +86,9 @@ func TestPost(t *testing.T) {
|
||||
server := httptest.NewServer(mux)
|
||||
defer server.Close()
|
||||
|
||||
jsonRestHandler := beaconApiJsonRestHandler{
|
||||
httpClient: http.Client{Timeout: time.Second * 5},
|
||||
host: server.URL,
|
||||
jsonRestHandler := BeaconApiJsonRestHandler{
|
||||
HttpClient: http.Client{Timeout: time.Second * 5},
|
||||
Host: server.URL,
|
||||
}
|
||||
resp := &beacon.GetGenesisResponse{}
|
||||
require.NoError(t, jsonRestHandler.Post(ctx, endpoint, headers, bytes.NewBuffer(dataBytes), resp))
|
||||
|
||||
@@ -3,11 +3,9 @@ package beacon_api
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
neturl "net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
"github.com/prysmaticlabs/prysm/v4/beacon-chain/rpc/prysm/validator"
|
||||
@@ -16,12 +14,7 @@ import (
|
||||
)
|
||||
|
||||
// NewPrysmBeaconChainClient returns implementation of iface.PrysmBeaconChainClient.
|
||||
func NewPrysmBeaconChainClient(host string, timeout time.Duration, nodeClient iface.NodeClient) iface.PrysmBeaconChainClient {
|
||||
jsonRestHandler := beaconApiJsonRestHandler{
|
||||
httpClient: http.Client{Timeout: timeout},
|
||||
host: host,
|
||||
}
|
||||
|
||||
func NewPrysmBeaconChainClient(jsonRestHandler JsonRestHandler, nodeClient iface.NodeClient) iface.PrysmBeaconChainClient {
|
||||
return prysmBeaconChainClient{
|
||||
jsonRestHandler: jsonRestHandler,
|
||||
nodeClient: nodeClient,
|
||||
|
||||
@@ -9,30 +9,18 @@ import (
|
||||
validatorHelpers "github.com/prysmaticlabs/prysm/v4/validator/helpers"
|
||||
)
|
||||
|
||||
func NewBeaconChainClient(validatorConn validatorHelpers.NodeConnection) iface.BeaconChainClient {
|
||||
func NewBeaconChainClient(validatorConn validatorHelpers.NodeConnection, jsonRestHandler beaconApi.JsonRestHandler) iface.BeaconChainClient {
|
||||
grpcClient := grpcApi.NewGrpcBeaconChainClient(validatorConn.GetGrpcClientConn())
|
||||
featureFlags := features.Get()
|
||||
|
||||
if featureFlags.EnableBeaconRESTApi {
|
||||
return beaconApi.NewBeaconApiBeaconChainClientWithFallback(
|
||||
validatorConn.GetBeaconApiUrl(),
|
||||
validatorConn.GetBeaconApiTimeout(),
|
||||
grpcClient,
|
||||
)
|
||||
if features.Get().EnableBeaconRESTApi {
|
||||
return beaconApi.NewBeaconApiBeaconChainClientWithFallback(jsonRestHandler, grpcClient)
|
||||
} else {
|
||||
return grpcClient
|
||||
}
|
||||
}
|
||||
|
||||
func NewPrysmBeaconClient(validatorConn validatorHelpers.NodeConnection) iface.PrysmBeaconChainClient {
|
||||
featureFlags := features.Get()
|
||||
|
||||
if featureFlags.EnableBeaconRESTApi {
|
||||
return beaconApi.NewPrysmBeaconChainClient(
|
||||
validatorConn.GetBeaconApiUrl(),
|
||||
validatorConn.GetBeaconApiTimeout(),
|
||||
nodeClientFactory.NewNodeClient(validatorConn),
|
||||
)
|
||||
func NewPrysmBeaconClient(validatorConn validatorHelpers.NodeConnection, jsonRestHandler beaconApi.JsonRestHandler) iface.PrysmBeaconChainClient {
|
||||
if features.Get().EnableBeaconRESTApi {
|
||||
return beaconApi.NewPrysmBeaconChainClient(jsonRestHandler, nodeClientFactory.NewNodeClient(validatorConn, jsonRestHandler))
|
||||
} else {
|
||||
return grpcApi.NewGrpcPrysmBeaconChainClient(validatorConn.GetGrpcClientConn())
|
||||
}
|
||||
|
||||
@@ -8,12 +8,10 @@ import (
|
||||
validatorHelpers "github.com/prysmaticlabs/prysm/v4/validator/helpers"
|
||||
)
|
||||
|
||||
func NewNodeClient(validatorConn validatorHelpers.NodeConnection) iface.NodeClient {
|
||||
func NewNodeClient(validatorConn validatorHelpers.NodeConnection, jsonRestHandler beaconApi.JsonRestHandler) iface.NodeClient {
|
||||
grpcClient := grpcApi.NewNodeClient(validatorConn.GetGrpcClientConn())
|
||||
featureFlags := features.Get()
|
||||
|
||||
if featureFlags.EnableBeaconRESTApi {
|
||||
return beaconApi.NewNodeClientWithFallback(validatorConn.GetBeaconApiUrl(), validatorConn.GetBeaconApiTimeout(), grpcClient)
|
||||
if features.Get().EnableBeaconRESTApi {
|
||||
return beaconApi.NewNodeClientWithFallback(jsonRestHandler, grpcClient)
|
||||
} else {
|
||||
return grpcClient
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -20,6 +21,7 @@ import (
|
||||
"github.com/prysmaticlabs/prysm/v4/consensus-types/primitives"
|
||||
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
||||
"github.com/prysmaticlabs/prysm/v4/validator/accounts/wallet"
|
||||
beaconApi "github.com/prysmaticlabs/prysm/v4/validator/client/beacon-api"
|
||||
beaconChainClientFactory "github.com/prysmaticlabs/prysm/v4/validator/client/beacon-chain-client-factory"
|
||||
"github.com/prysmaticlabs/prysm/v4/validator/client/iface"
|
||||
nodeClientFactory "github.com/prysmaticlabs/prysm/v4/validator/client/node-client-factory"
|
||||
@@ -189,15 +191,16 @@ func (v *ValidatorService) Start() {
|
||||
return
|
||||
}
|
||||
|
||||
validatorClient := validatorClientFactory.NewValidatorClient(v.conn)
|
||||
beaconClient := beaconChainClientFactory.NewBeaconChainClient(v.conn)
|
||||
prysmBeaconClient := beaconChainClientFactory.NewPrysmBeaconClient(v.conn)
|
||||
restHandler := &beaconApi.BeaconApiJsonRestHandler{
|
||||
HttpClient: http.Client{Timeout: v.conn.GetBeaconApiTimeout()},
|
||||
Host: v.conn.GetBeaconApiUrl(),
|
||||
}
|
||||
|
||||
valStruct := &validator{
|
||||
db: v.db,
|
||||
validatorClient: validatorClient,
|
||||
beaconClient: beaconClient,
|
||||
node: nodeClientFactory.NewNodeClient(v.conn),
|
||||
validatorClient: validatorClientFactory.NewValidatorClient(v.conn, restHandler),
|
||||
beaconClient: beaconChainClientFactory.NewBeaconChainClient(v.conn, restHandler),
|
||||
node: nodeClientFactory.NewNodeClient(v.conn, restHandler),
|
||||
graffiti: v.graffiti,
|
||||
logValidatorBalances: v.logValidatorBalances,
|
||||
emitAccountMetrics: v.emitAccountMetrics,
|
||||
@@ -221,7 +224,7 @@ func (v *ValidatorService) Start() {
|
||||
Web3SignerConfig: v.Web3SignerConfig,
|
||||
proposerSettings: v.proposerSettings,
|
||||
walletInitializedChannel: make(chan *wallet.Wallet, 1),
|
||||
prysmBeaconClient: prysmBeaconClient,
|
||||
prysmBeaconClient: beaconChainClientFactory.NewPrysmBeaconClient(v.conn, restHandler),
|
||||
validatorsRegBatchSize: v.validatorsRegBatchSize,
|
||||
}
|
||||
|
||||
|
||||
@@ -8,11 +8,12 @@ import (
|
||||
validatorHelpers "github.com/prysmaticlabs/prysm/v4/validator/helpers"
|
||||
)
|
||||
|
||||
func NewValidatorClient(validatorConn validatorHelpers.NodeConnection) iface.ValidatorClient {
|
||||
featureFlags := features.Get()
|
||||
|
||||
if featureFlags.EnableBeaconRESTApi {
|
||||
return beaconApi.NewBeaconApiValidatorClient(validatorConn.GetBeaconApiUrl(), validatorConn.GetBeaconApiTimeout())
|
||||
func NewValidatorClient(
|
||||
validatorConn validatorHelpers.NodeConnection,
|
||||
jsonRestHandler beaconApi.JsonRestHandler,
|
||||
) iface.ValidatorClient {
|
||||
if features.Get().EnableBeaconRESTApi {
|
||||
return beaconApi.NewBeaconApiValidatorClient(jsonRestHandler)
|
||||
} else {
|
||||
return grpcApi.NewGrpcValidatorClient(validatorConn.GetGrpcClientConn())
|
||||
}
|
||||
|
||||
@@ -49,6 +49,7 @@ go_library(
|
||||
"//validator/accounts/petnames:go_default_library",
|
||||
"//validator/accounts/wallet:go_default_library",
|
||||
"//validator/client:go_default_library",
|
||||
"//validator/client/beacon-api:go_default_library",
|
||||
"//validator/client/beacon-chain-client-factory:go_default_library",
|
||||
"//validator/client/iface:go_default_library",
|
||||
"//validator/client/node-client-factory:go_default_library",
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
package rpc
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
middleware "github.com/grpc-ecosystem/go-grpc-middleware"
|
||||
grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/retry"
|
||||
grpcopentracing "github.com/grpc-ecosystem/go-grpc-middleware/tracing/opentracing"
|
||||
@@ -9,6 +11,7 @@ import (
|
||||
grpcutil "github.com/prysmaticlabs/prysm/v4/api/grpc"
|
||||
ethpb "github.com/prysmaticlabs/prysm/v4/proto/prysm/v1alpha1"
|
||||
"github.com/prysmaticlabs/prysm/v4/validator/client"
|
||||
beaconApi "github.com/prysmaticlabs/prysm/v4/validator/client/beacon-api"
|
||||
beaconChainClientFactory "github.com/prysmaticlabs/prysm/v4/validator/client/beacon-chain-client-factory"
|
||||
nodeClientFactory "github.com/prysmaticlabs/prysm/v4/validator/client/node-client-factory"
|
||||
validatorClientFactory "github.com/prysmaticlabs/prysm/v4/validator/client/validator-client-factory"
|
||||
@@ -51,8 +54,13 @@ func (s *Server) registerBeaconClient() error {
|
||||
s.beaconApiTimeout,
|
||||
)
|
||||
|
||||
s.beaconChainClient = beaconChainClientFactory.NewBeaconChainClient(conn)
|
||||
s.beaconNodeClient = nodeClientFactory.NewNodeClient(conn)
|
||||
s.beaconNodeValidatorClient = validatorClientFactory.NewValidatorClient(conn)
|
||||
restHandler := &beaconApi.BeaconApiJsonRestHandler{
|
||||
HttpClient: http.Client{Timeout: s.beaconApiTimeout},
|
||||
Host: s.beaconApiEndpoint,
|
||||
}
|
||||
s.beaconChainClient = beaconChainClientFactory.NewBeaconChainClient(conn, restHandler)
|
||||
s.beaconNodeClient = nodeClientFactory.NewNodeClient(conn, restHandler)
|
||||
s.beaconNodeValidatorClient = validatorClientFactory.NewValidatorClient(conn, restHandler)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user