Skip to content

Commit

Permalink
feat: contentrouter implements routing.PeerRouting
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Aug 8, 2023
1 parent 5bf3989 commit 27e4bb2
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 1 deletion.
42 changes: 42 additions & 0 deletions routing/http/contentrouter/contentrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ var logger = logging.Logger("routing/http/contentrouter")

type Client interface {
GetProviders(ctx context.Context, key cid.Cid) (iter.ResultIter[types.Record], error)
GetPeers(ctx context.Context, pid peer.ID) (peers iter.ResultIter[types.Record], err error)
}

type contentRouter struct {
client Client
}

var _ routing.ContentRouting = (*contentRouter)(nil)
var _ routing.PeerRouting = (*contentRouter)(nil)
var _ routinghelpers.ProvideManyRouter = (*contentRouter)(nil)

type option func(c *contentRouter)
Expand Down Expand Up @@ -100,3 +102,43 @@ func (c *contentRouter) FindProvidersAsync(ctx context.Context, key cid.Cid, num
go readProviderResponses(resultsIter, ch)
return ch
}

func (c *contentRouter) FindPeer(ctx context.Context, pid peer.ID) (peer.AddrInfo, error) {
iter, err := c.client.GetPeers(ctx, pid)
if err != nil {
return peer.AddrInfo{}, err
}

Check warning on line 110 in routing/http/contentrouter/contentrouter.go

View check run for this annotation

Codecov / codecov/patch

routing/http/contentrouter/contentrouter.go#L109-L110

Added lines #L109 - L110 were not covered by tests
defer iter.Close()

for iter.Next() {
res := iter.Val()
if res.Err != nil {
logger.Warnw("error iterating provider responses: %s", res.Err)
continue

Check warning on line 117 in routing/http/contentrouter/contentrouter.go

View check run for this annotation

Codecov / codecov/patch

routing/http/contentrouter/contentrouter.go#L116-L117

Added lines #L116 - L117 were not covered by tests
}
v := res.Val
if v.GetSchema() == types.SchemaPeer {
result, ok := v.(*types.PeerRecord)
if !ok {
logger.Errorw(
"problem casting find providers result",
"Schema", v.GetSchema(),
"Type", reflect.TypeOf(v).String(),
)
continue

Check warning on line 128 in routing/http/contentrouter/contentrouter.go

View check run for this annotation

Codecov / codecov/patch

routing/http/contentrouter/contentrouter.go#L123-L128

Added lines #L123 - L128 were not covered by tests
}

var addrs []multiaddr.Multiaddr
for _, a := range result.Addrs {
addrs = append(addrs, a.Multiaddr)
}

Check warning on line 134 in routing/http/contentrouter/contentrouter.go

View check run for this annotation

Codecov / codecov/patch

routing/http/contentrouter/contentrouter.go#L133-L134

Added lines #L133 - L134 were not covered by tests

return peer.AddrInfo{
ID: *result.ID,
Addrs: addrs,
}, nil
}
}

return peer.AddrInfo{}, err

Check warning on line 143 in routing/http/contentrouter/contentrouter.go

View check run for this annotation

Codecov / codecov/patch

routing/http/contentrouter/contentrouter.go#L143

Added line #L143 was not covered by tests
}
31 changes: 30 additions & 1 deletion routing/http/contentrouter/contentrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (m *mockClient) GetProviders(ctx context.Context, key cid.Cid) (iter.Result
args := m.Called(ctx, key)
return args.Get(0).(iter.ResultIter[types.Record]), args.Error(1)
}
func (m *mockClient) GetPeers(ctx context.Context, pid peer.ID) (iter.ResultIter[types.Record], error) {
args := m.Called(ctx, pid)
return args.Get(0).(iter.ResultIter[types.Record]), args.Error(1)
}
func (m *mockClient) Ready(ctx context.Context) (bool, error) {
args := m.Called(ctx)
return args.Bool(0), args.Error(1)
Expand All @@ -38,7 +42,7 @@ func makeCID() cid.Cid {
return c
}

func TestGetProvidersAsync(t *testing.T) {
func TestFindProvidersAsync(t *testing.T) {
key := makeCID()
ctx := context.Background()
client := &mockClient{}
Expand Down Expand Up @@ -79,3 +83,28 @@ func TestGetProvidersAsync(t *testing.T) {

require.Equal(t, expected, actualAIs)
}

func TestFindPeer(t *testing.T) {
ctx := context.Background()
client := &mockClient{}
crc := NewContentRoutingClient(client)

p1 := peer.ID("peer1")
ais := []types.Record{
&types.UnknownRecord{
Schema: "Unknown",
},
&types.PeerRecord{
Schema: types.SchemaPeer,
ID: &p1,
Protocols: []string{"transport-bitswap"},
},
}
aisIter := iter.ToResultIter[types.Record](iter.FromSlice(ais))

client.On("GetPeers", ctx, p1).Return(aisIter, nil)

peer, err := crc.FindPeer(ctx, p1)
require.NoError(t, err)
require.Equal(t, peer.ID, p1)
}

0 comments on commit 27e4bb2

Please sign in to comment.