Skip to content

Commit 5cbb20e

Browse files
committed
use node identity from go-eth2-client
1 parent de79be8 commit 5cbb20e

9 files changed

Lines changed: 34 additions & 82 deletions

File tree

app/eth2wrap/eth2wrap_gen.go

Lines changed: 1 addition & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/eth2wrap/httpwrap.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ package eth2wrap
55
import (
66
"context"
77
"encoding/hex"
8-
"encoding/json"
9-
"fmt"
10-
"io"
118
"net/http"
129
"sync"
1310
"testing"
@@ -24,11 +21,6 @@ import (
2421
"github.com/obolnetwork/charon/eth2util"
2522
)
2623

27-
// NodeIdentity represents the identity of a beacon node.
28-
type NodeIdentity struct {
29-
PeerID string `json:"peer_id"`
30-
}
31-
3224
// NewHTTPAdapterForT returns a http adapter for testing non-eth2service methods as it is nil.
3325
func NewHTTPAdapterForT(_ *testing.T, address string, headers map[string]string, timeout time.Duration) Client {
3426
return newHTTPAdapter(nil, address, headers, timeout)
@@ -206,51 +198,3 @@ func (h *httpAdapter) ClientForAddress(_ string) Client {
206198
func (h *httpAdapter) Headers() map[string]string {
207199
return h.headers
208200
}
209-
210-
// NodeIdentity fetches the node identity from the beacon node.
211-
func (h *httpAdapter) NodeIdentity(ctx context.Context) (*NodeIdentity, error) {
212-
url := fmt.Sprintf("%s/eth/v1/node/identity", h.address)
213-
214-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
215-
if err != nil {
216-
return nil, errors.Wrap(err, "create request")
217-
}
218-
219-
// Add custom headers
220-
for k, v := range h.headers {
221-
req.Header.Set(k, v)
222-
}
223-
224-
client := &http.Client{
225-
Timeout: h.timeout,
226-
}
227-
228-
resp, err := client.Do(req)
229-
if err != nil {
230-
return nil, errors.Wrap(err, "fetch node identity")
231-
}
232-
defer resp.Body.Close()
233-
234-
if resp.StatusCode != http.StatusOK {
235-
body, _ := io.ReadAll(resp.Body)
236-
return nil, errors.New("node identity request failed",
237-
z.Int("status_code", resp.StatusCode),
238-
z.Str("body", string(body)),
239-
)
240-
}
241-
242-
body, err := io.ReadAll(resp.Body)
243-
if err != nil {
244-
return nil, errors.Wrap(err, "read response")
245-
}
246-
247-
var result struct {
248-
Data NodeIdentity `json:"data"`
249-
}
250-
251-
if err := json.Unmarshal(body, &result); err != nil {
252-
return nil, errors.Wrap(err, "unmarshal node identity")
253-
}
254-
255-
return &result.Data, nil
256-
}

app/eth2wrap/lazy.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@ import (
77
"sync"
88
"time"
99

10+
<<<<<<< HEAD
1011
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
1112
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
13+
=======
14+
eth2api "github.com/attestantio/go-eth2-client/api"
15+
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
16+
>>>>>>> 3130cbf8 (use node identity from go-eth2-client)
1217
)
1318

1419
//go:generate mockery --name=Client --output=mocks --outpkg=mocks --case=underscore
@@ -243,11 +248,11 @@ func (l *lazy) UpdateCacheIndices(ctx context.Context, idxs []eth2p0.ValidatorIn
243248
cl.UpdateCacheIndices(ctx, idxs)
244249
}
245250

246-
func (l *lazy) NodeIdentity(ctx context.Context) (*NodeIdentity, error) {
251+
func (l *lazy) NodeIdentity(ctx context.Context, opts *eth2api.NodeIdentityOpts) (*eth2api.Response[*eth2v1.NodeIdentity], error) {
247252
cl, err := l.getOrCreateClient(ctx)
248253
if err != nil {
249254
return nil, err
250255
}
251256

252-
return cl.NodeIdentity(ctx)
253-
}
257+
return cl.NodeIdentity(ctx, opts)
258+
}

app/eth2wrap/mocks/client.go

Lines changed: 10 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/eth2wrap/multi.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
eth2v1 "github.com/attestantio/go-eth2-client/api/v1"
1212
eth2p0 "github.com/attestantio/go-eth2-client/spec/phase0"
13+
eth2api "github.com/attestantio/go-eth2-client/api"
1314

1415
"github.com/obolnetwork/charon/app/errors"
1516
)
@@ -296,13 +297,13 @@ func (m multi) Proxy(ctx context.Context, req *http.Request) (*http.Response, er
296297
}
297298

298299
// NodeIdentity returns the identity of the beacon node.
299-
func (m multi) NodeIdentity(ctx context.Context) (*NodeIdentity, error) {
300+
func (m multi) NodeIdentity(ctx context.Context, opts *eth2api.NodeIdentityOpts) (*eth2api.Response[*eth2v1.NodeIdentity], error) {
300301
const label = "node_identity"
301302
defer incRequest(label)
302303

303304
res0, err := provide(ctx, m.clients, m.fallbacks,
304-
func(ctx context.Context, args provideArgs) (*NodeIdentity, error) {
305-
return args.client.NodeIdentity(ctx)
305+
func(ctx context.Context, args provideArgs) (*eth2api.Response[*eth2v1.NodeIdentity], error) {
306+
return args.client.NodeIdentity(ctx, opts)
306307
},
307308
nil, m.selector,
308309
)

app/monitoringapi.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,13 +287,13 @@ func beaconNodeVersionMetric(ctx context.Context, eth2Cl eth2wrap.Client, clock
287287
}
288288

289289
setNodePeerID := func() {
290-
identity, err := eth2Cl.NodeIdentity(ctx)
290+
response, err := eth2Cl.NodeIdentity(ctx, &eth2api.NodeIdentityOpts{})
291291
if err != nil {
292292
log.Error(ctx, "Failed to fetch beacon node identity. Check beacon node connectivity and API availability", err)
293293
return
294294
}
295295

296-
peerID := identity.PeerID
296+
peerID := response.Data.PeerID
297297

298298
beaconNodePeerIDGauge.Reset()
299299
beaconNodePeerIDGauge.WithLabelValues(peerID).Set(1)

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ require (
287287
replace github.com/coinbase/kryptology => github.com/ObolNetwork/kryptology v0.1.0
288288

289289
// We're replacing go-eth2-client with a branch off our fork. The branch is kept up to date with the latest attestantio versions.
290-
replace github.com/attestantio/go-eth2-client => github.com/ObolNetwork/go-eth2-client v0.28.0-obol //nolint
290+
replace github.com/attestantio/go-eth2-client => github.com/ObolNetwork/go-eth2-client v0.28.0-obol.1 //nolint
291291

292292
tool (
293293
github.com/bufbuild/buf/cmd/buf

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ github.com/DataDog/zstd v1.5.7 h1:ybO8RBeh29qrxIhCA9E8gKY6xfONU9T6G6aP9DTKfLE=
3434
github.com/DataDog/zstd v1.5.7/go.mod h1:g4AWEaM3yOg3HYfnJ3YIawPnVdXJh9QME85blwSAmyw=
3535
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
3636
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
37-
github.com/ObolNetwork/go-eth2-client v0.28.0-obol h1:3lFBuNmaxBI9fLDf+oza1p7VOmsECKaT6sx0EoFeAOk=
38-
github.com/ObolNetwork/go-eth2-client v0.28.0-obol/go.mod h1:PO9sHFCq+1RiG+Eh3eOR2GYvYV64Qzg7idM3kLgCs5k=
37+
github.com/ObolNetwork/go-eth2-client v0.28.0-obol.1 h1:72NAvJRe09mKHiKfB30r9nO7JZnV/5bPx5J1qxZ5Yow=
38+
github.com/ObolNetwork/go-eth2-client v0.28.0-obol.1/go.mod h1:PO9sHFCq+1RiG+Eh3eOR2GYvYV64Qzg7idM3kLgCs5k=
3939
github.com/ObolNetwork/kryptology v0.1.0 h1:AhoG4My70+xMhEJSpVaJay/t+T/vIUNHQYLjsDJHulI=
4040
github.com/ObolNetwork/kryptology v0.1.0/go.mod h1:/Wl7Js2f676GyXZDTaojf/O+l0fxFPWudbyjdFhkpSA=
4141
github.com/OffchainLabs/go-bitfield v0.0.0-20251031151322-f427d04d8506 h1:d/SJkN8/9Ca+1YmuDiUJxAiV4w/a9S8NcsG7GMQSrVI=

testutil/beaconmock/beaconmock.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,9 +509,12 @@ func (m Mock) ClientForAddress(_ string) eth2wrap.Client {
509509
}
510510

511511
// NodeIdentity returns a mock node identity.
512-
func (m Mock) NodeIdentity(_ context.Context) (*eth2wrap.NodeIdentity, error) {
513-
return &eth2wrap.NodeIdentity{
514-
PeerID: "16Uiu2HAm1234567890abcdefghijklmnopqrstuvwxyz",
512+
func (m Mock) NodeIdentity(_ context.Context, _ *eth2api.NodeIdentityOpts) (*eth2api.Response[*eth2v1.NodeIdentity], error) {
513+
return &eth2api.Response[*eth2v1.NodeIdentity]{
514+
Data: &eth2v1.NodeIdentity{
515+
PeerID: "16Uiu2HAm1234567890abcdefghijklmnopqrstuvwxyz",
516+
},
517+
Metadata: make(map[string]any),
515518
}, nil
516519
}
517520

0 commit comments

Comments
 (0)