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

eth2wrap: fix node peer count #1681

Merged
merged 5 commits into from
Jan 25, 2023
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: 2 additions & 2 deletions app/eth2wrap/eth2wrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
24 changes: 9 additions & 15 deletions app/eth2wrap/httpwrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand All @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion app/monitoringapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions testutil/beaconmock/beaconmock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down
5 changes: 2 additions & 3 deletions testutil/beaconmock/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down