From 905b49553fb0b78cc46c19463c2b106731951213 Mon Sep 17 00:00:00 2001 From: 0xTylerHolmes Date: Sat, 21 Oct 2023 13:38:39 -0500 Subject: [PATCH] nodepeer multi --- multi/nodepeers.go | 39 +++++++++++++++++++++++++++ multi/nodepeers_test.go | 60 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) create mode 100644 multi/nodepeers.go create mode 100644 multi/nodepeers_test.go diff --git a/multi/nodepeers.go b/multi/nodepeers.go new file mode 100644 index 00000000..de8bf659 --- /dev/null +++ b/multi/nodepeers.go @@ -0,0 +1,39 @@ +// Copyright © 2021, 2023 Attestant Limited. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package multi + +import ( + "context" + + consensusclient "github.com/attestantio/go-eth2-client" + "github.com/attestantio/go-eth2-client/api" + apiv1 "github.com/attestantio/go-eth2-client/api/v1" +) + +// NodePeers provides the peers of the node +func (s *Service) NodePeers(ctx context.Context, opts *api.PeerOpts) (*api.Response[*apiv1.Peers], error) { + res, err := s.doCall(ctx, func(ctx context.Context, client consensusclient.Service) (interface{}, error) { + nodePeers, err := client.(consensusclient.NodePeersProvider).NodePeers(ctx, opts) + if err != nil { + return nil, err + } + + return nodePeers, nil + }, nil) + if err != nil { + return nil, err + } + + return res.(*api.Response[*apiv1.Peers]), nil +} diff --git a/multi/nodepeers_test.go b/multi/nodepeers_test.go new file mode 100644 index 00000000..8bad87eb --- /dev/null +++ b/multi/nodepeers_test.go @@ -0,0 +1,60 @@ +// Copyright © 2021 Attestant Limited. +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package multi_test + +import ( + "context" + "github.com/attestantio/go-eth2-client/api" + "testing" + + consensusclient "github.com/attestantio/go-eth2-client" + "github.com/attestantio/go-eth2-client/mock" + "github.com/attestantio/go-eth2-client/multi" + "github.com/attestantio/go-eth2-client/testclients" + "github.com/rs/zerolog" + "github.com/stretchr/testify/require" +) + +func TestNodePeers(t *testing.T) { + ctx := context.Background() + + client1, err := mock.New(ctx, mock.WithName("mock 1")) + require.NoError(t, err) + erroringClient1, err := testclients.NewErroring(ctx, 0.1, client1) + require.NoError(t, err) + client2, err := mock.New(ctx, mock.WithName("mock 2")) + require.NoError(t, err) + erroringClient2, err := testclients.NewErroring(ctx, 0.1, client2) + require.NoError(t, err) + client3, err := mock.New(ctx, mock.WithName("mock 3")) + require.NoError(t, err) + + multiClient, err := multi.New(ctx, + multi.WithLogLevel(zerolog.Disabled), + multi.WithClients([]consensusclient.Service{ + erroringClient1, + erroringClient2, + client3, + }), + ) + require.NoError(t, err) + + for i := 0; i < 128; i++ { + res, err := multiClient.(consensusclient.NodePeersProvider).NodePeers(ctx, &api.PeerOpts{}) + require.NoError(t, err) + require.NotNil(t, res) + } + // At this point we expect mock 3 to be in active (unless probability hates us). + require.Equal(t, "mock 3", multiClient.Address()) +}