Skip to content

Commit

Permalink
nodepeer multi
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTylerHolmes committed Oct 21, 2023
1 parent d4db36a commit 905b495
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
39 changes: 39 additions & 0 deletions multi/nodepeers.go
Original file line number Diff line number Diff line change
@@ -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

Check failure on line 24 in multi/nodepeers.go

View workflow job for this annotation

GitHub Actions / lint

Comment should end in a period (godot)
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
}
60 changes: 60 additions & 0 deletions multi/nodepeers_test.go
Original file line number Diff line number Diff line change
@@ -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())
}

0 comments on commit 905b495

Please sign in to comment.