diff --git a/app/eth2wrap/eth2wrap.go b/app/eth2wrap/eth2wrap.go index a1f39f90c..368f2f577 100644 --- a/app/eth2wrap/eth2wrap.go +++ b/app/eth2wrap/eth2wrap.go @@ -169,12 +169,12 @@ func (m multi) BlockAttestations(ctx context.Context, stateID string) ([]*eth2p0 return res, err } -func (m multi) NodePeerCount(ctx context.Context) (PeerCount, error) { +func (m multi) NodePeerCount(ctx context.Context) (int, error) { const label = "node_peer_count" defer latency(label)() res, err := provide(ctx, m.clients, - func(ctx context.Context, cl Client) (PeerCount, error) { + func(ctx context.Context, cl Client) (int, error) { return cl.NodePeerCount(ctx) }, nil, diff --git a/app/eth2wrap/httpwrap.go b/app/eth2wrap/httpwrap.go index 6aacc633a..9a4f7f235 100644 --- a/app/eth2wrap/httpwrap.go +++ b/app/eth2wrap/httpwrap.go @@ -41,20 +41,12 @@ type BlockAttestationsProvider interface { BlockAttestations(ctx context.Context, stateID string) ([]*eth2p0.Attestation, error) } -// PeerCount is the response for querying beacon node peer count (/eth/v1/node/peer_count). -type PeerCount struct { - Disconnected int - Connecting int - Connected int - Disconnecting int -} - // NodePeerCountProvider is the interface for providing node peer count. // It is a standard beacon API endpoint not implemented by eth2client. // See https://ethereum.github.io/beacon-APIs/#/Node/getPeerCount. type NodePeerCountProvider interface { // NodePeerCount provides peer count of the beacon node. - NodePeerCount(ctx context.Context) (PeerCount, error) + NodePeerCount(ctx context.Context) (int, error) } // NewHTTPAdapterForT returns a http adapter for testing non-eth2service methods as it is nil. @@ -150,21 +142,21 @@ func (h *httpAdapter) BlockAttestations(ctx context.Context, stateID string) ([] // NodePeerCount provides the peer count of the beacon node. // See https://ethereum.github.io/beacon-APIs/#/Node/getPeerCount. -func (h *httpAdapter) NodePeerCount(ctx context.Context) (PeerCount, error) { +func (h *httpAdapter) NodePeerCount(ctx context.Context) (int, error) { path := "/eth/v1/node/peer_count" respBody, statusCode, err := httpGet(ctx, h.address, path, h.timeout) if err != nil { - return PeerCount{}, errors.Wrap(err, "request beacon node peer count") + return 0, errors.Wrap(err, "request beacon node peer count") } else if statusCode != http.StatusOK { - return PeerCount{}, errors.New("request beacon node peer count failed", z.Int("status", statusCode), z.Str("body", string(respBody))) + return 0, errors.New("request beacon node peer count failed", z.Int("status", statusCode), z.Str("body", string(respBody))) } var resp peerCountJSON if err := json.Unmarshal(respBody, &resp); err != nil { - return PeerCount{}, errors.Wrap(err, "failed to parse beacon node peer count response") + return 0, errors.Wrap(err, "failed to parse beacon node peer count response") } - return resp.Data, nil + return resp.Data.Connected, nil } type submitBeaconCommitteeSelectionsJSON struct { @@ -180,7 +172,9 @@ type attestationsJSON struct { } type peerCountJSON struct { - Data PeerCount `json:"data"` + Data struct { + Connected int `json:"connected,string"` + } `json:"data"` } func httpPost(ctx context.Context, base string, endpoint string, body io.Reader, timeout time.Duration) ([]byte, error) { diff --git a/app/monitoringapi.go b/app/monitoringapi.go index 9080ae8be..63e7245d8 100644 --- a/app/monitoringapi.go +++ b/app/monitoringapi.go @@ -226,7 +226,7 @@ func beaconNodePeerCount(ctx context.Context, eth2Cl eth2wrap.Client) (int, erro return 0, errors.Wrap(err, "get beacon node peer count") } - return peerCount.Connected, nil + return peerCount, nil } // quorumPeersConnected returns true if quorum peers are currently connected. diff --git a/testutil/beaconmock/beaconmock.go b/testutil/beaconmock/beaconmock.go index 8669473a2..a414c112c 100644 --- a/testutil/beaconmock/beaconmock.go +++ b/testutil/beaconmock/beaconmock.go @@ -134,7 +134,7 @@ type Mock struct { AttestationDataFunc func(context.Context, eth2p0.Slot, eth2p0.CommitteeIndex) (*eth2p0.AttestationData, error) AttesterDutiesFunc func(context.Context, eth2p0.Epoch, []eth2p0.ValidatorIndex) ([]*eth2v1.AttesterDuty, error) BlockAttestationsFunc func(ctx context.Context, stateID string) ([]*eth2p0.Attestation, error) - NodePeerCountFunc func(ctx context.Context) (eth2wrap.PeerCount, error) + NodePeerCountFunc func(ctx context.Context) (int, error) BlindedBeaconBlockProposalFunc func(ctx context.Context, slot eth2p0.Slot, randaoReveal eth2p0.BLSSignature, graffiti []byte) (*eth2api.VersionedBlindedBeaconBlock, error) BeaconCommitteesFunc func(ctx context.Context, stateID string) ([]*eth2v1.BeaconCommittee, error) BeaconBlockProposalFunc func(ctx context.Context, slot eth2p0.Slot, randaoReveal eth2p0.BLSSignature, graffiti []byte) (*spec.VersionedBeaconBlock, error) @@ -169,7 +169,7 @@ func (m Mock) BlockAttestations(ctx context.Context, stateID string) ([]*eth2p0. return m.BlockAttestationsFunc(ctx, stateID) } -func (m Mock) NodePeerCount(ctx context.Context) (eth2wrap.PeerCount, error) { +func (m Mock) NodePeerCount(ctx context.Context) (int, error) { return m.NodePeerCountFunc(ctx) } diff --git a/testutil/beaconmock/options.go b/testutil/beaconmock/options.go index 4c137c368..b91a7d32c 100644 --- a/testutil/beaconmock/options.go +++ b/testutil/beaconmock/options.go @@ -34,7 +34,6 @@ import ( "github.com/prysmaticlabs/go-bitfield" "github.com/obolnetwork/charon/app/errors" - "github.com/obolnetwork/charon/app/eth2wrap" "github.com/obolnetwork/charon/app/log" "github.com/obolnetwork/charon/core" "github.com/obolnetwork/charon/eth2util/eth2exp" @@ -490,8 +489,8 @@ func defaultMock(httpMock HTTPMock, httpServer *http.Server, clock clockwork.Clo BlockAttestationsFunc: func(ctx context.Context, stateID string) ([]*eth2p0.Attestation, error) { return []*eth2p0.Attestation{}, nil }, - NodePeerCountFunc: func(ctx context.Context) (eth2wrap.PeerCount, error) { - return eth2wrap.PeerCount{}, nil + NodePeerCountFunc: func(ctx context.Context) (int, error) { + return 0, nil }, AttestationDataFunc: func(ctx context.Context, slot eth2p0.Slot, index eth2p0.CommitteeIndex) (*eth2p0.AttestationData, error) { return attStore.NewAttestationData(ctx, slot, index)