Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 2bc3457

Browse files
authored
Implementation of delegated routing based on the Edelweiss compiler and Reframe spec (#11)
1 parent a4e74c4 commit 2bc3457

19 files changed

+5520
-331
lines changed

client/client.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

client/client_test.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

client/contentrouting.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package client
2+
3+
import (
4+
"context"
5+
6+
"github.com/ipfs/go-cid"
7+
"github.com/libp2p/go-libp2p-core/peer"
8+
"github.com/libp2p/go-libp2p-core/routing"
9+
)
10+
11+
type ContentRoutingClient struct {
12+
client DelegatedRoutingClient
13+
}
14+
15+
var _ routing.ContentRouting = (*ContentRoutingClient)(nil)
16+
17+
func NewContentRoutingClient(c DelegatedRoutingClient) *ContentRoutingClient {
18+
return &ContentRoutingClient{client: c}
19+
}
20+
21+
func (c *ContentRoutingClient) Provide(context.Context, cid.Cid, bool) error {
22+
return routing.ErrNotSupported
23+
}
24+
25+
func (c *ContentRoutingClient) FindProvidersAsync(ctx context.Context, key cid.Cid, numResults int) <-chan peer.AddrInfo {
26+
addrInfoCh := make(chan peer.AddrInfo)
27+
resultCh, err := c.client.FindProvidersAsync(ctx, key)
28+
if err != nil {
29+
close(addrInfoCh)
30+
return addrInfoCh
31+
}
32+
go func() {
33+
numProcessed := 0
34+
closed := false
35+
for asyncResult := range resultCh {
36+
if asyncResult.Err != nil {
37+
logger.Infof("find providers async emitted a transient error (%v)", asyncResult.Err)
38+
} else {
39+
for _, peerAddr := range asyncResult.AddrInfo {
40+
if numResults <= 0 || numProcessed < numResults {
41+
addrInfoCh <- peerAddr
42+
}
43+
numProcessed++
44+
if numProcessed == numResults {
45+
close(addrInfoCh)
46+
closed = true
47+
}
48+
}
49+
}
50+
}
51+
if !closed {
52+
close(addrInfoCh)
53+
}
54+
}()
55+
return addrInfoCh
56+
}

client/contentrouting_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package client
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/ipfs/go-cid"
8+
"github.com/libp2p/go-libp2p-core/peer"
9+
)
10+
11+
type TestDelegatedRoutingClient struct {
12+
NumResults int
13+
}
14+
15+
func (t TestDelegatedRoutingClient) FindProviders(ctx context.Context, key cid.Cid) ([]peer.AddrInfo, error) {
16+
panic("not supported")
17+
}
18+
19+
func (t TestDelegatedRoutingClient) FindProvidersAsync(ctx context.Context, key cid.Cid) (<-chan FindProvidersAsyncResult, error) {
20+
ch := make(chan FindProvidersAsyncResult)
21+
go func() {
22+
defer close(ch)
23+
for i := 0; i < t.NumResults; i++ {
24+
ch <- FindProvidersAsyncResult{
25+
AddrInfo: []peer.AddrInfo{{}},
26+
}
27+
}
28+
}()
29+
return ch, nil
30+
}
31+
32+
func (t TestDelegatedRoutingClient) GetIPNS(ctx context.Context, id []byte) ([]byte, error) {
33+
panic("not supported")
34+
}
35+
36+
func (t TestDelegatedRoutingClient) GetIPNSAsync(ctx context.Context, id []byte) (<-chan GetIPNSAsyncResult, error) {
37+
panic("not supported")
38+
}
39+
40+
func (t TestDelegatedRoutingClient) PutIPNS(ctx context.Context, id []byte, record []byte) error {
41+
panic("not supported")
42+
}
43+
44+
func (t TestDelegatedRoutingClient) PutIPNSAsync(ctx context.Context, id []byte, record []byte) (<-chan PutIPNSAsyncResult, error) {
45+
panic("not supported")
46+
}
47+
48+
// TestContentRoutingFindProvidersUnlimitedResults is testing that ContentRoutingClient.FindProvidersAsync
49+
// correctly wraps DelegatedRoutingClient.FindProvidersAsync in the regime when the former allows for unlimited results.
50+
// This is a test of async semantics only. This is why values are not checked for validity.
51+
// Non-test implementations of DelegatedRoutingClient are responsible for returning valid values.
52+
func TestContentRoutingFindProvidersUnlimitedResults(t *testing.T) {
53+
providedResults := 5
54+
c := NewContentRoutingClient(TestDelegatedRoutingClient{providedResults})
55+
ch := c.FindProvidersAsync(context.Background(), cid.Cid{}, 0)
56+
num := 0
57+
for range ch {
58+
num++
59+
}
60+
if num != providedResults {
61+
t.Errorf("expecting %v results, got %v", providedResults, num)
62+
}
63+
}
64+
65+
// TestContentRoutingFindProvidersFewerResults is testing that ContentRoutingClient.FindProvidersAsync
66+
// correctly wraps DelegatedRoutingClient.FindProvidersAsync in the regime when the former allows for
67+
// fewer results than are available.
68+
// This is a test of async semantics only. This is why values are not checked for validity.
69+
// Non-test implementations of DelegatedRoutingClient are responsible for returning valid values.
70+
func TestContentRoutingFindProvidersFewerResults(t *testing.T) {
71+
providedResults := 5
72+
wantResults := 3
73+
c := NewContentRoutingClient(TestDelegatedRoutingClient{providedResults})
74+
ch := c.FindProvidersAsync(context.Background(), cid.Cid{}, wantResults)
75+
num := 0
76+
for range ch {
77+
num++
78+
}
79+
if num != wantResults {
80+
t.Errorf("expecting %v results, got %v", wantResults, num)
81+
}
82+
}
83+
84+
// TestContentRoutingFindProvidersMoreResults is testing that ContentRoutingClient.FindProvidersAsync
85+
// correctly wraps DelegatedRoutingClient.FindProvidersAsync in the regime when the former allows for
86+
// more results than are available.
87+
// This is a test of async semantics only. This is why values are not checked for validity.
88+
// Non-test implementations of DelegatedRoutingClient are responsible for returning valid values.
89+
func TestContentRoutingFindProvidersMoreResults(t *testing.T) {
90+
providedResults := 5
91+
wantResults := 7
92+
c := NewContentRoutingClient(TestDelegatedRoutingClient{providedResults})
93+
ch := c.FindProvidersAsync(context.Background(), cid.Cid{}, wantResults)
94+
num := 0
95+
for range ch {
96+
num++
97+
}
98+
if num != providedResults {
99+
t.Errorf("expecting %v results, got %v", providedResults, num)
100+
}
101+
}

0 commit comments

Comments
 (0)