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

Decode peer ID properly when fetching peer #8883

Merged
merged 2 commits into from
May 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 beacon-chain/rpc/nodev1/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func (ns *Server) GetPeer(ctx context.Context, req *ethpb.PeerRequest) (*ethpb.P
defer span.End()

peerStatus := ns.PeersFetcher.Peers()
id, err := peer.IDFromString(req.PeerId)
id, err := peer.Decode(req.PeerId)
if err != nil {
return nil, status.Error(codes.InvalidArgument, "Invalid peer ID: "+req.PeerId)
return nil, status.Errorf(codes.Internal, "Could not decode peer ID: %v", err)
}
enr, err := peerStatus.ENR(id)
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions beacon-chain/rpc/nodev1/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,10 +172,10 @@ func TestSyncStatus(t *testing.T) {
}

func TestGetPeer(t *testing.T) {
const rawId = "16Uiu2HAkvyYtoQXZNTsthjgLHjEnv7kvwzEmjvsJjWXpbhtqpSUN"
ctx := context.Background()
decodedId, err := peer.Decode("16Uiu2HAkvyYtoQXZNTsthjgLHjEnv7kvwzEmjvsJjWXpbhtqpSUN")
decodedId, err := peer.Decode(rawId)
require.NoError(t, err)
peerId := string(decodedId)
enrRecord := &enr.Record{}
err = enrRecord.SetSig(dummyIdentity{1}, []byte{42})
require.NoError(t, err)
Expand All @@ -190,9 +190,9 @@ func TestGetPeer(t *testing.T) {
peerFetcher.Peers().Add(enrRecord, decodedId, p2pMultiAddr, network.DirInbound)

t.Run("OK", func(t *testing.T) {
resp, err := s.GetPeer(ctx, &ethpb.PeerRequest{PeerId: peerId})
resp, err := s.GetPeer(ctx, &ethpb.PeerRequest{PeerId: rawId})
require.NoError(t, err)
assert.Equal(t, peerId, resp.Data.PeerId)
assert.Equal(t, rawId, resp.Data.PeerId)
assert.Equal(t, p2pAddr, resp.Data.Address)
assert.Equal(t, "enr:yoABgmlwhAcHBwc=", resp.Data.Enr)
assert.Equal(t, ethpb.ConnectionState_DISCONNECTED, resp.Data.State)
Expand All @@ -201,11 +201,11 @@ func TestGetPeer(t *testing.T) {

t.Run("Invalid ID", func(t *testing.T) {
_, err = s.GetPeer(ctx, &ethpb.PeerRequest{PeerId: "foo"})
assert.ErrorContains(t, "Invalid peer ID: foo", err)
assert.ErrorContains(t, "Could not decode peer ID", err)
})

t.Run("Peer not found", func(t *testing.T) {
generatedId := string(libp2ptest.GeneratePeerIDs(1)[0])
generatedId := "16Uiu2HAmQqFdEcHbSmQTQuLoAhnMUrgoWoraKK4cUJT6FuuqHqTU"
_, err = s.GetPeer(ctx, &ethpb.PeerRequest{PeerId: generatedId})
assert.ErrorContains(t, "Peer not found", err)
})
Expand Down