Skip to content

Commit

Permalink
Multiple BN HTTP resolver (#13433)
Browse files Browse the repository at this point in the history
* http resolver

* Redo

* Revert "Redo"

This reverts commit 5437c44.

* Revert "http resolver"

This reverts commit 206207b.

* Add host change to ValidatorClient + Validator

* Update mockgen

* Tidy

* Add mock validator

* Update gomock

* Gaz

* Solve interface issues

* Fix host

* Fix test

* Add tests

* Add endpoint change log

* Fix log

* Gen mock

* Fix test

* Fix deepsource

* Lint + deepsource

* Move to healthCheckRoutine

* Fix build errors

* Switch host to string

* Forgot a couple

* Radek' review

* Add PushProposerSettings to goroutine

* Radek' review

* James' review + test fix

* Radek' suggestion

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

* Check if new node is healthy

* Fix linter errors

* Add host switch logic to ChangeHost

* Lint + comment

* Fix messy merge

* rename ChangeHost to SetHost

* improve log

* remove log

* switch one node

* rename param

---------

Co-authored-by: Radosław Kapka <rkapka@wp.pl>
Co-authored-by: rkapka <radoslaw.kapka@gmail.com>
  • Loading branch information
3 people authored May 29, 2024
1 parent 43c7659 commit 6fddd13
Show file tree
Hide file tree
Showing 18 changed files with 267 additions and 37 deletions.
20 changes: 20 additions & 0 deletions testing/mock/node_service_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 10 additions & 16 deletions testing/validator-mock/node_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions testing/validator-mock/validator_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion validator/accounts/cli_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ func (acm *CLIManager) prepareBeaconClients(ctx context.Context) (*iface.Validat
acm.beaconApiTimeout,
)

restHandler := beaconApi.NewBeaconApiJsonRestHandler(http.Client{Timeout: acm.beaconApiTimeout}, acm.beaconApiEndpoint)
restHandler := beaconApi.NewBeaconApiJsonRestHandler(
http.Client{Timeout: acm.beaconApiTimeout},
acm.beaconApiEndpoint,
)
validatorClient := validatorClientFactory.NewValidatorClient(conn, restHandler)
nodeClient := nodeClientFactory.NewNodeClient(conn, restHandler)

Expand Down
8 changes: 8 additions & 0 deletions validator/accounts/testing/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,11 @@ func (*Validator) EventStreamIsRunning() bool {
func (*Validator) HealthTracker() *beacon.NodeHealthTracker {
panic("implement me")
}

func (*Validator) Host() string {
panic("implement me")
}

func (*Validator) ChangeHost() {
panic("implement me")
}
8 changes: 8 additions & 0 deletions validator/client/beacon-api/beacon_api_validator_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,11 @@ func wrapInMetrics[Resp any](action string, f func() (Resp, error)) (Resp, error
}
return resp, err
}

func (c *beaconApiValidatorClient) Host() string {
return c.jsonRestHandler.Host()
}

func (c *beaconApiValidatorClient) SetHost(host string) {
c.jsonRestHandler.SetHost(host)
}
29 changes: 29 additions & 0 deletions validator/client/beacon-api/beacon_api_validator_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,3 +202,32 @@ func TestBeaconApiValidatorClient_ProposeBeaconBlockError(t *testing.T) {
assert.ErrorContains(t, expectedErr.Error(), err)
assert.DeepEqual(t, expectedResp, resp)
}

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

hosts := []string{"http://localhost:8080", "http://localhost:8081"}
jsonRestHandler := mock.NewMockJsonRestHandler(ctrl)
jsonRestHandler.EXPECT().SetHost(
hosts[0],
).Times(1)
jsonRestHandler.EXPECT().Host().Return(
hosts[0],
).Times(1)

validatorClient := beaconApiValidatorClient{jsonRestHandler: jsonRestHandler}
validatorClient.SetHost(hosts[0])
host := validatorClient.Host()
require.Equal(t, hosts[0], host)

jsonRestHandler.EXPECT().SetHost(
hosts[1],
).Times(1)
jsonRestHandler.EXPECT().Host().Return(
hosts[1],
).Times(1)
validatorClient.SetHost(hosts[1])
host = validatorClient.Host()
require.Equal(t, hosts[1], host)
}
17 changes: 11 additions & 6 deletions validator/client/beacon-api/json_rest_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type JsonRestHandler interface {
Post(ctx context.Context, endpoint string, headers map[string]string, data *bytes.Buffer, resp interface{}) error
HttpClient() *http.Client
Host() string
SetHost(host string)
}

type BeaconApiJsonRestHandler struct {
Expand All @@ -33,19 +34,19 @@ func NewBeaconApiJsonRestHandler(client http.Client, host string) JsonRestHandle
}
}

// GetHttpClient returns the underlying HTTP client of the handler
func (c BeaconApiJsonRestHandler) HttpClient() *http.Client {
// HttpClient returns the underlying HTTP client of the handler
func (c *BeaconApiJsonRestHandler) HttpClient() *http.Client {
return &c.client
}

// GetHost returns the underlying HTTP host
func (c BeaconApiJsonRestHandler) Host() string {
// Host returns the underlying HTTP host
func (c *BeaconApiJsonRestHandler) Host() string {
return c.host
}

// 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 {
url := c.host + endpoint
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
Expand All @@ -67,7 +68,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,
Expand Down Expand Up @@ -134,3 +135,7 @@ func decodeResp(httpResp *http.Response, resp interface{}) error {

return nil
}

func (c *BeaconApiJsonRestHandler) SetHost(host string) {
c.host = host
}
50 changes: 41 additions & 9 deletions validator/client/beacon-api/mock/json_rest_handler_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions validator/client/grpc-api/grpc_validator_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,11 @@ func (c *grpcValidatorClient) AggregatedSigAndAggregationBits(
return c.beaconNodeValidatorClient.AggregatedSigAndAggregationBits(ctx, in)
}

func (grpcValidatorClient) GetAggregatedSelections(context.Context, []iface.BeaconCommitteeSelection) ([]iface.BeaconCommitteeSelection, error) {
func (*grpcValidatorClient) GetAggregatedSelections(context.Context, []iface.BeaconCommitteeSelection) ([]iface.BeaconCommitteeSelection, error) {
return nil, iface.ErrNotSupported
}

func (grpcValidatorClient) GetAggregatedSyncSelections(context.Context, []iface.SyncCommitteeSelection) ([]iface.SyncCommitteeSelection, error) {
func (*grpcValidatorClient) GetAggregatedSyncSelections(context.Context, []iface.SyncCommitteeSelection) ([]iface.SyncCommitteeSelection, error) {
return nil, iface.ErrNotSupported
}

Expand Down Expand Up @@ -245,3 +245,12 @@ func (c *grpcValidatorClient) StartEventStream(ctx context.Context, topics []str
func (c *grpcValidatorClient) EventStreamIsRunning() bool {
return c.isEventStreamRunning
}

func (*grpcValidatorClient) Host() string {
log.Warn(iface.ErrNotSupported)
return ""
}

func (*grpcValidatorClient) SetHost(_ string) {
log.Warn(iface.ErrNotSupported)
}
4 changes: 3 additions & 1 deletion validator/client/iface/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ type Validator interface {
SetGraffiti(ctx context.Context, pubKey [fieldparams.BLSPubkeyLength]byte, graffiti []byte) error
DeleteGraffiti(ctx context.Context, pubKey [fieldparams.BLSPubkeyLength]byte) error
HealthTracker() *beacon.NodeHealthTracker
Host() string
ChangeHost()
}

// SigningFunc interface defines a type for the a function that signs a message
// SigningFunc interface defines a type for the function that signs a message
type SigningFunc func(context.Context, *validatorpb.SignRequest) (bls.Signature, error)
2 changes: 2 additions & 0 deletions validator/client/iface/validator_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,6 @@ type ValidatorClient interface {
EventStreamIsRunning() bool
GetAggregatedSelections(ctx context.Context, selections []BeaconCommitteeSelection) ([]BeaconCommitteeSelection, error)
GetAggregatedSyncSelections(ctx context.Context, selections []SyncCommitteeSelection) ([]SyncCommitteeSelection, error)
Host() string
SetHost(host string)
}
Loading

0 comments on commit 6fddd13

Please sign in to comment.