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

*: cherrypick for v1.0.0-rc5 #3134

Merged
merged 3 commits into from
Jun 11, 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
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 @@
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...)

Check warning on line 853 in app/app.go

View check run for this annotation

Codecov / codecov/patch

app/app.go#L853

Added line #L853 was not covered by tests
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
86 changes: 78 additions & 8 deletions app/eth2wrap/eth2wrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package eth2wrap_test

import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"net"
Expand All @@ -25,6 +26,7 @@ import (
"github.com/obolnetwork/charon/app/errors"
"github.com/obolnetwork/charon/app/eth2wrap"
"github.com/obolnetwork/charon/app/log"
"github.com/obolnetwork/charon/eth2util"
"github.com/obolnetwork/charon/testutil"
"github.com/obolnetwork/charon/testutil/beaconmock"
)
Expand Down Expand Up @@ -169,7 +171,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 +186,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 +199,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 +251,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 +310,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 +341,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 +364,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 +426,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 All @@ -449,3 +451,71 @@ func TestLazy(t *testing.T) {

require.Equal(t, srv2.URL, eth2Cl.Address())
}

func TestLazyDomain(t *testing.T) {
tests := []struct {
name string
in string
expErr string
expRes string
}{
{
name: "mainnet fork",
in: eth2util.Mainnet.GenesisForkVersionHex[2:],
expRes: "040000008c6ebbceb21209e6af5ab7db4a3027998c412c0eb0e15fbc1ee75617",
},
{
name: "goerli fork",
in: eth2util.Goerli.GenesisForkVersionHex[2:],
expRes: "04000000628941ef21d1fe8c7134720add10bb91e3b02c007e0046d2472c6695",
},
{
name: "gnosis fork",
in: eth2util.Gnosis.GenesisForkVersionHex[2:],
expRes: "04000000398beb768264920602d7d79f88da05cac0550ae4108753fd846408b5",
},
{
name: "sepolia fork",
in: eth2util.Sepolia.GenesisForkVersionHex[2:],
expRes: "040000007191d9b3c210dbffc7810b6ccb436c1b3897b6772452924b20f6f5f2",
},
{
name: "holesky fork",
in: eth2util.Holesky.GenesisForkVersionHex[2:],
expRes: "040000002b3e2c2d17a0d820f3099580a72d1bc743b17616ff7851f32aa303ad",
},
{
name: "unknown fork",
in: "00000001",
expErr: "beacon api domain: get domain: compute domain: invalid fork hash: no capella fork for specified fork",
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ctx := context.Background()

bmock, err := beaconmock.New()
require.NoError(t, err)

target := testutil.MustParseURL(t, bmock.Address())

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
httputil.NewSingleHostReverseProxy(target).ServeHTTP(w, r)
}))

forkVersionHex, err := hex.DecodeString(test.in)
require.NoError(t, err)
eth2Cl, err := eth2wrap.NewMultiHTTP(time.Second, [4]byte(forkVersionHex), srv.URL)
require.NoError(t, err)

voluntaryExitDomain := eth2p0.DomainType{0x04, 0x00, 0x00, 0x00}
f, err := eth2Cl.Domain(ctx, voluntaryExitDomain, testutil.RandomEpoch())

if test.expErr != "" {
require.ErrorContains(t, err, test.expErr)
} else {
require.Equal(t, test.expRes, hex.EncodeToString(f[:]))
}
})
}
}
10 changes: 5 additions & 5 deletions app/log/loki/lokipb/v1/loki.pb.go

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

8 changes: 4 additions & 4 deletions app/peerinfo/peerinfopb/v1/peerinfo.pb.go

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

22 changes: 11 additions & 11 deletions app/protonil/testdata/v1/test.pb.go

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

Loading
Loading