Skip to content

Commit

Permalink
eth2wrap: initialize fork version on lazy
Browse files Browse the repository at this point in the history
Instead of relying on the `lazy` interface to set the Eth2CL fork version, initialize it immediately *before* returning it as a `lazy`.

Fixes exits through VCs.
  • Loading branch information
gsora committed Jun 11, 2024
1 parent f286523 commit dc40d06
Show file tree
Hide file tree
Showing 10 changed files with 22 additions and 31 deletions.
4 changes: 1 addition & 3 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,13 +850,11 @@ func newETH2Client(ctx context.Context, conf Config, life *lifecycle.Manager,
return nil, errors.New("beacon node endpoints empty")
}

eth2Cl, err := eth2wrap.NewMultiHTTP(eth2ClientTimeout, conf.BeaconNodeAddrs...)
eth2Cl, err := eth2wrap.NewMultiHTTP(eth2ClientTimeout, [4]byte(forkVersion), conf.BeaconNodeAddrs...)
if err != nil {
return nil, errors.Wrap(err, "new eth2 http client")
}

eth2Cl.SetForkVersion([4]byte(forkVersion))

if conf.SyntheticBlockProposals {
log.Info(ctx, "Synthetic block proposals enabled")
eth2Cl = eth2wrap.WithSyntheticDuties(eth2Cl)
Expand Down
7 changes: 5 additions & 2 deletions app/eth2wrap/eth2wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func WithSyntheticDuties(cl Client) Client {
}

// NewMultiHTTP returns a new instrumented multi eth2 http client.
func NewMultiHTTP(timeout time.Duration, addresses ...string) (Client, error) {
func NewMultiHTTP(timeout time.Duration, forkVersion [4]byte, addresses ...string) (Client, error) {
var clients []Client
for _, address := range addresses {
address := address // Capture range variable.
Expand All @@ -93,7 +93,10 @@ func NewMultiHTTP(timeout time.Duration, addresses ...string) (Client, error) {
return nil, errors.New("invalid eth2 http service")
}

return AdaptEth2HTTP(eth2Http, timeout), nil
adaptedCl := AdaptEth2HTTP(eth2Http, timeout)
adaptedCl.SetForkVersion(forkVersion)

return adaptedCl, nil
})

clients = append(clients, cl)
Expand Down
16 changes: 8 additions & 8 deletions app/eth2wrap/eth2wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ func TestErrors(t *testing.T) {
ctx := context.Background()

t.Run("network dial error", func(t *testing.T) {
cl, err := eth2wrap.NewMultiHTTP(time.Hour, "localhost:22222")
cl, err := eth2wrap.NewMultiHTTP(time.Hour, [4]byte{}, "localhost:22222")
require.NoError(t, err)

_, err = cl.SlotsPerEpoch(ctx)
Expand All @@ -184,7 +184,7 @@ func TestErrors(t *testing.T) {
}))

t.Run("http timeout", func(t *testing.T) {
cl, err := eth2wrap.NewMultiHTTP(time.Millisecond, srv.URL)
cl, err := eth2wrap.NewMultiHTTP(time.Millisecond, [4]byte{}, srv.URL)
require.NoError(t, err)

_, err = cl.SlotsPerEpoch(ctx)
Expand All @@ -197,7 +197,7 @@ func TestErrors(t *testing.T) {
ctx, cancel := context.WithCancel(ctx)
cancel()

cl, err := eth2wrap.NewMultiHTTP(time.Millisecond, srv.URL)
cl, err := eth2wrap.NewMultiHTTP(time.Millisecond, [4]byte{}, srv.URL)
require.NoError(t, err)

_, err = cl.SlotsPerEpoch(ctx)
Expand Down Expand Up @@ -249,7 +249,7 @@ func TestCtxCancel(t *testing.T) {

bmock, err := beaconmock.New()
require.NoError(t, err)
eth2Cl, err := eth2wrap.NewMultiHTTP(time.Second, bmock.Address())
eth2Cl, err := eth2wrap.NewMultiHTTP(time.Second, [4]byte{}, bmock.Address())
require.NoError(t, err)

cancel() // Cancel context before calling method.
Expand Down Expand Up @@ -308,7 +308,7 @@ func TestOneError(t *testing.T) {
bmock.Address(), // Valid
}

eth2Cl, err := eth2wrap.NewMultiHTTP(time.Second, addresses...)
eth2Cl, err := eth2wrap.NewMultiHTTP(time.Second, [4]byte{}, addresses...)
require.NoError(t, err)

eth2Resp, err := eth2Cl.Spec(ctx, &eth2api.SpecOpts{})
Expand Down Expand Up @@ -339,7 +339,7 @@ func TestOneTimeout(t *testing.T) {
bmock.Address(), // Valid
}

eth2Cl, err := eth2wrap.NewMultiHTTP(time.Minute, addresses...)
eth2Cl, err := eth2wrap.NewMultiHTTP(time.Minute, [4]byte{}, addresses...)
require.NoError(t, err)

eth2Resp, err := eth2Cl.Spec(ctx, &eth2api.SpecOpts{})
Expand All @@ -362,7 +362,7 @@ func TestOnlyTimeout(t *testing.T) {
defer srv.Close()
defer cancel() // Cancel the context before stopping the server.

eth2Cl, err := eth2wrap.NewMultiHTTP(time.Minute, srv.URL)
eth2Cl, err := eth2wrap.NewMultiHTTP(time.Minute, [4]byte{}, srv.URL)
require.NoError(t, err)

// Start goroutine that is blocking trying to create the client.
Expand Down Expand Up @@ -424,7 +424,7 @@ func TestLazy(t *testing.T) {
httputil.NewSingleHostReverseProxy(target).ServeHTTP(w, r)
}))

eth2Cl, err := eth2wrap.NewMultiHTTP(time.Second, srv1.URL, srv2.URL)
eth2Cl, err := eth2wrap.NewMultiHTTP(time.Second, [4]byte{}, srv1.URL, srv2.URL)
require.NoError(t, err)

// Both proxies are disabled, so this should fail.
Expand Down
4 changes: 2 additions & 2 deletions cmd/exit.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ func bindExitFlags(cmd *cobra.Command, config *exitConfig, flags []exitCLIFlag)
}
}

func eth2Client(ctx context.Context, u []string, timeout time.Duration) (eth2wrap.Client, error) {
cl, err := eth2wrap.NewMultiHTTP(timeout, u...)
func eth2Client(ctx context.Context, u []string, timeout time.Duration, forkVersion [4]byte) (eth2wrap.Client, error) {
cl, err := eth2wrap.NewMultiHTTP(timeout, forkVersion, u...)
if err != nil {
return nil, err
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/exit_broadcast.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,11 @@ func runBcastFullExit(ctx context.Context, config exitConfig) error {

ctx = log.WithCtx(ctx, z.Str("validator", validator.String()))

eth2Cl, err := eth2Client(ctx, config.BeaconNodeEndpoints, config.BeaconNodeTimeout)
eth2Cl, err := eth2Client(ctx, config.BeaconNodeEndpoints, config.BeaconNodeTimeout, [4]byte(cl.GetForkVersion()))
if err != nil {
return errors.Wrap(err, "cannot create eth2 client for specified beacon node")
}

eth2Cl.SetForkVersion([4]byte(cl.GetForkVersion()))

var fullExit eth2p0.SignedVoluntaryExit
maybeExitFilePath := strings.TrimSpace(config.ExitFromFilePath)

Expand Down
4 changes: 1 addition & 3 deletions cmd/exit_broadcast_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,9 @@ func testRunBcastFullExitCmdFlow(t *testing.T, fromFile bool) {
require.NoError(t, beaconMock.Close())
}()

eth2Cl, err := eth2Client(ctx, []string{beaconMock.Address()}, 10*time.Second)
eth2Cl, err := eth2Client(ctx, []string{beaconMock.Address()}, 10*time.Second, [4]byte(lock.ForkVersion))
require.NoError(t, err)

eth2Cl.SetForkVersion([4]byte(lock.ForkVersion))

handler, addLockFiles := obolapimock.MockServer(false, eth2Cl)
srv := httptest.NewServer(handler)
addLockFiles(lock)
Expand Down
4 changes: 1 addition & 3 deletions cmd/exit_fetch_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,9 @@ func Test_runFetchExitFullFlow(t *testing.T) {
require.NoError(t, beaconMock.Close())
}()

eth2Cl, err := eth2Client(ctx, []string{beaconMock.Address()}, 10*time.Second)
eth2Cl, err := eth2Client(ctx, []string{beaconMock.Address()}, 10*time.Second, [4]byte(lock.ForkVersion))
require.NoError(t, err)

eth2Cl.SetForkVersion([4]byte(lock.ForkVersion))

handler, addLockFiles := obolapimock.MockServer(false, eth2Cl)
srv := httptest.NewServer(handler)
addLockFiles(lock)
Expand Down
2 changes: 1 addition & 1 deletion cmd/exit_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func listActiveVals(ctx context.Context, config exitConfig) ([]string, error) {
return nil, errors.Wrap(err, "could not load cluster-lock.json")
}

eth2Cl, err := eth2Client(ctx, config.BeaconNodeEndpoints, config.BeaconNodeTimeout)
eth2Cl, err := eth2Client(ctx, config.BeaconNodeEndpoints, config.BeaconNodeTimeout, [4]byte{}) // fine to avoid initializing a fork version, we're just querying the BN
if err != nil {
return nil, errors.Wrap(err, "cannot create eth2 client for specified beacon node")
}
Expand Down
4 changes: 1 addition & 3 deletions cmd/exit_sign.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,11 @@ func runSignPartialExit(ctx context.Context, config exitConfig) error {
}
}

eth2Cl, err := eth2Client(ctx, config.BeaconNodeEndpoints, config.BeaconNodeTimeout)
eth2Cl, err := eth2Client(ctx, config.BeaconNodeEndpoints, config.BeaconNodeTimeout, [4]byte(cl.GetForkVersion()))
if err != nil {
return errors.Wrap(err, "cannot create eth2 client for specified beacon node")
}

eth2Cl.SetForkVersion([4]byte(cl.GetForkVersion()))

if !config.ExpertMode {
rawValData, err := eth2Cl.Validators(ctx, valAPICallOpts)
if err != nil {
Expand Down
4 changes: 1 addition & 3 deletions cmd/exit_sign_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,9 @@ func runSubmitPartialExitFlowTest(t *testing.T, useValIdx bool, expertMode bool,
require.NoError(t, beaconMock.Close())
}()

eth2Cl, err := eth2Client(ctx, []string{beaconMock.Address()}, 10*time.Second)
eth2Cl, err := eth2Client(ctx, []string{beaconMock.Address()}, 10*time.Second, [4]byte(lock.ForkVersion))
require.NoError(t, err)

eth2Cl.SetForkVersion([4]byte(lock.ForkVersion))

handler, addLockFiles := obolapimock.MockServer(false, eth2Cl)
srv := httptest.NewServer(handler)
addLockFiles(lock)
Expand Down

0 comments on commit dc40d06

Please sign in to comment.