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

Don't use BeaconState in Validators call #1301

Merged
merged 8 commits into from
Feb 4, 2024
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
57 changes: 2 additions & 55 deletions beacon/goclient/goclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ package goclient

import (
"context"
"encoding/json"
"fmt"
"math"
"net/http"
"strings"
"sync"
"time"
Expand All @@ -32,8 +30,8 @@ const (
DataVersionNil spec.DataVersion = math.MaxUint64

// Client timeouts.
DefaultCommonTimeout = time.Second * 5 // For dialing and most requests.
DefaultLongTimeout = time.Second * 120 // For long requests.
DefaultCommonTimeout = time.Second * 5 // For dialing and most requests.
DefaultLongTimeout = time.Second * 10 // For long requests.
)

type beaconNodeStatus int32
Expand Down Expand Up @@ -208,15 +206,6 @@ func New(logger *zap.Logger, opt beaconprotocol.Options, operatorID spectypes.Op
zap.String("version", client.nodeVersion),
)

// If it's a Prysm, check that the debug endpoints are enabled, otherwise
// the Validators method might fail when calling BeaconState.
// TODO: remove this once Prysm enables debug endpoints by default.
if client.nodeClient == NodePrysm {
if err := client.checkPrysmDebugEndpoints(); err != nil {
return nil, err
}
}

// Start registration submitter.
go client.registrationSubmitter(slotTickerProvider)

Expand Down Expand Up @@ -275,45 +264,3 @@ func (gc *goClient) slotStartTime(slot phase0.Slot) time.Time {
func (gc *goClient) Events(ctx context.Context, topics []string, handler eth2client.EventHandlerFunc) error {
return gc.client.Events(ctx, topics, handler)
}

func (gc *goClient) checkPrysmDebugEndpoints() error {
start := time.Now()
address := strings.TrimSuffix(gc.client.Address(), "/")
if !strings.HasPrefix(address, "http") {
address = fmt.Sprintf("http://%s", address)
}

ctx, cancel := context.WithTimeout(context.Background(), DefaultCommonTimeout)
defer cancel()

req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/eth/v2/debug/beacon/heads", address), nil)
if err != nil {
return fmt.Errorf("failed to create beacon heads request: %w", err)
}
resp, err := (&http.Client{}).Do(req)
if err != nil {
return fmt.Errorf("failed to get beacon heads: %w", err)
}
if resp.StatusCode == http.StatusNotFound {
return fmt.Errorf("Prysm node doesn't have debug endpoints enabled, please enable them with --enable-debug-rpc-endpoints")
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("failed to get beacon heads: %s", resp.Status)
}
var data struct {
Data []struct {
Root phase0.Root `json:"root"`
} `json:"data"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return fmt.Errorf("failed to decode beacon heads response: %w", err)
}
if len(data.Data) == 0 {
return fmt.Errorf("no beacon heads found")
}
if data.Data[0].Root == (phase0.Root{}) {
return fmt.Errorf("beacon head is empty")
}
gc.log.Debug("Prysm debug endpoints are enabled", zap.Duration("took", time.Since(start)))
return nil
}
5 changes: 3 additions & 2 deletions beacon/goclient/goclient_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ func TestTimeouts(t *testing.T) {
// Too slow to respond to the Validators request.
{
unresponsiveServer := mockServer(t, delays{
BeaconStateDelay: longTimeout * 2,
ValidatorsDelay: longTimeout * 2,
BeaconStateDelay: longTimeout / 2,
})
client, err := mockClient(t, ctx, unresponsiveServer.URL, commonTimeout, longTimeout)
require.NoError(t, err)

require.NoError(t, err)
_, err = client.(*goClient).GetValidatorData(nil) // Should call BeaconState internally.
_, err = client.(*goClient).GetValidatorData(nil) // Shouldn't call BeaconState internally.
require.ErrorContains(t, err, "context deadline exceeded")

duties, err := client.(*goClient).ProposerDuties(ctx, mockServerEpoch, nil)
Expand Down
Loading
Loading