Skip to content

Commit 061228a

Browse files
authored
migrate to consolidated types. (#20)
1 parent dfa3fdb commit 061228a

14 files changed

+191
-202
lines changed

bootstrap_test.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import (
55
"errors"
66
"testing"
77

8+
"github.com/libp2p/go-libp2p-core/routing"
9+
810
errwrap "github.com/hashicorp/errwrap"
9-
routing "github.com/libp2p/go-libp2p-routing"
1011
)
1112

1213
type bootstrapRouter struct {
@@ -21,9 +22,9 @@ func (bs *bootstrapRouter) Bootstrap(ctx context.Context) error {
2122
func TestBootstrap(t *testing.T) {
2223
pings := make([]bool, 6)
2324
d := Parallel{
24-
Routers: []routing.IpfsRouting{
25+
Routers: []routing.Routing{
2526
Tiered{
26-
Routers: []routing.IpfsRouting{
27+
Routers: []routing.Routing{
2728
&bootstrapRouter{
2829
bs: func() error {
2930
pings[0] = true
@@ -33,7 +34,7 @@ func TestBootstrap(t *testing.T) {
3334
},
3435
},
3536
Tiered{
36-
Routers: []routing.IpfsRouting{
37+
Routers: []routing.Routing{
3738
&bootstrapRouter{
3839
bs: func() error {
3940
pings[1] = true
@@ -95,9 +96,9 @@ func TestBootstrap(t *testing.T) {
9596
}
9697
func TestBootstrapErr(t *testing.T) {
9798
d := Parallel{
98-
Routers: []routing.IpfsRouting{
99+
Routers: []routing.Routing{
99100
Tiered{
100-
Routers: []routing.IpfsRouting{
101+
Routers: []routing.Routing{
101102
&bootstrapRouter{
102103
bs: func() error {
103104
return errors.New("err1")
@@ -106,7 +107,7 @@ func TestBootstrapErr(t *testing.T) {
106107
},
107108
},
108109
Tiered{
109-
Routers: []routing.IpfsRouting{
110+
Routers: []routing.Routing{
110111
&bootstrapRouter{
111112
bs: func() error {
112113
return nil

composed.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ package routinghelpers
33
import (
44
"context"
55

6+
ci "github.com/libp2p/go-libp2p-core/crypto"
7+
"github.com/libp2p/go-libp2p-core/peer"
8+
"github.com/libp2p/go-libp2p-core/routing"
9+
610
multierror "github.com/hashicorp/go-multierror"
711
cid "github.com/ipfs/go-cid"
8-
ci "github.com/libp2p/go-libp2p-crypto"
9-
peer "github.com/libp2p/go-libp2p-peer"
10-
pstore "github.com/libp2p/go-libp2p-peerstore"
11-
routing "github.com/libp2p/go-libp2p-routing"
12-
ropts "github.com/libp2p/go-libp2p-routing/options"
1312
)
1413

1514
// Compose composes the components into a single router. Not specifying a
@@ -29,23 +28,23 @@ type Compose struct {
2928
// functionality.
3029

3130
// PutValue adds value corresponding to given Key.
32-
func (cr *Compose) PutValue(ctx context.Context, key string, value []byte, opts ...ropts.Option) error {
31+
func (cr *Compose) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
3332
if cr.ValueStore == nil {
3433
return routing.ErrNotSupported
3534
}
3635
return cr.ValueStore.PutValue(ctx, key, value, opts...)
3736
}
3837

3938
// GetValue searches for the value corresponding to given Key.
40-
func (cr *Compose) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
39+
func (cr *Compose) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
4140
if cr.ValueStore == nil {
4241
return nil, routing.ErrNotFound
4342
}
4443
return cr.ValueStore.GetValue(ctx, key, opts...)
4544
}
4645

4746
// SearchValue searches for the value corresponding to given Key.
48-
func (cr *Compose) SearchValue(ctx context.Context, key string, opts ...ropts.Option) (<-chan []byte, error) {
47+
func (cr *Compose) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
4948
if cr.ValueStore == nil {
5049
out := make(chan []byte)
5150
close(out)
@@ -65,20 +64,20 @@ func (cr *Compose) Provide(ctx context.Context, c cid.Cid, local bool) error {
6564
}
6665

6766
// FindProvidersAsync searches for peers who are able to provide a given key
68-
func (cr *Compose) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan pstore.PeerInfo {
67+
func (cr *Compose) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan peer.AddrInfo {
6968
if cr.ContentRouting == nil {
70-
ch := make(chan pstore.PeerInfo)
69+
ch := make(chan peer.AddrInfo)
7170
close(ch)
7271
return ch
7372
}
7473
return cr.ContentRouting.FindProvidersAsync(ctx, c, count)
7574
}
7675

77-
// FindPeer searches for a peer with given ID, returns a pstore.PeerInfo
76+
// FindPeer searches for a peer with given ID, returns a peer.AddrInfo
7877
// with relevant addresses.
79-
func (cr *Compose) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
78+
func (cr *Compose) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
8079
if cr.PeerRouting == nil {
81-
return pstore.PeerInfo{}, routing.ErrNotFound
80+
return peer.AddrInfo{}, routing.ErrNotFound
8281
}
8382
return cr.PeerRouting.FindPeer(ctx, p)
8483
}
@@ -119,5 +118,5 @@ func (cr *Compose) Bootstrap(ctx context.Context) error {
119118
return me.ErrorOrNil()
120119
}
121120

122-
var _ routing.IpfsRouting = (*Compose)(nil)
121+
var _ routing.Routing = (*Compose)(nil)
123122
var _ routing.PubKeyFetcher = (*Compose)(nil)

dummy_test.go

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,30 @@ import (
66
"strings"
77
"sync"
88

9+
"github.com/libp2p/go-libp2p-core/peer"
10+
"github.com/libp2p/go-libp2p-core/routing"
11+
912
cid "github.com/ipfs/go-cid"
10-
peer "github.com/libp2p/go-libp2p-peer"
11-
pstore "github.com/libp2p/go-libp2p-peerstore"
12-
routing "github.com/libp2p/go-libp2p-routing"
13-
ropts "github.com/libp2p/go-libp2p-routing/options"
1413
)
1514

1615
type failValueStore struct{}
1716

1817
var failValueErr = errors.New("fail valuestore error")
1918

20-
func (f failValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...ropts.Option) error {
19+
func (f failValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
2120
return failValueErr
2221
}
23-
func (f failValueStore) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
22+
func (f failValueStore) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
2423
return nil, failValueErr
2524
}
2625

27-
func (f failValueStore) SearchValue(ctx context.Context, key string, opts ...ropts.Option) (<-chan []byte, error) {
26+
func (f failValueStore) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
2827
return nil, failValueErr
2928
}
3029

3130
type dummyValueStore sync.Map
3231

33-
func (d *dummyValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...ropts.Option) error {
32+
func (d *dummyValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
3433
if strings.HasPrefix(key, "/notsupported/") {
3534
return routing.ErrNotSupported
3635
}
@@ -45,7 +44,7 @@ func (d *dummyValueStore) PutValue(ctx context.Context, key string, value []byte
4544
return nil
4645
}
4746

48-
func (d *dummyValueStore) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
47+
func (d *dummyValueStore) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
4948
if strings.HasPrefix(key, "/error/") {
5049
return nil, errors.New(key[len("/error/"):])
5150
}
@@ -59,7 +58,7 @@ func (d *dummyValueStore) GetValue(ctx context.Context, key string, opts ...ropt
5958
return nil, routing.ErrNotFound
6059
}
6160

62-
func (d *dummyValueStore) SearchValue(ctx context.Context, key string, opts ...ropts.Option) (<-chan []byte, error) {
61+
func (d *dummyValueStore) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
6362
out := make(chan []byte)
6463
if strings.HasPrefix(key, "/error/") {
6564
return nil, errors.New(key[len("/error/"):])
@@ -80,12 +79,12 @@ func (d *dummyValueStore) SearchValue(ctx context.Context, key string, opts ...r
8079

8180
type dummyProvider map[string][]peer.ID
8281

83-
func (d dummyProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan pstore.PeerInfo {
82+
func (d dummyProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan peer.AddrInfo {
8483
peers := d[c.KeyString()]
8584
if len(peers) > count {
8685
peers = peers[:count]
8786
}
88-
out := make(chan pstore.PeerInfo)
87+
out := make(chan peer.AddrInfo)
8988
go func() {
9089
defer close(out)
9190
for _, p := range peers {
@@ -94,7 +93,7 @@ func (d dummyProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count
9493
return
9594
}
9695
select {
97-
case out <- pstore.PeerInfo{ID: p}:
96+
case out <- peer.AddrInfo{ID: p}:
9897
case <-ctx.Done():
9998
}
10099
}
@@ -112,17 +111,17 @@ func (d cbProvider) Provide(ctx context.Context, c cid.Cid, local bool) error {
112111
return d(c, local)
113112
}
114113

115-
func (d cbProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan pstore.PeerInfo {
116-
ch := make(chan pstore.PeerInfo)
114+
func (d cbProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan peer.AddrInfo {
115+
ch := make(chan peer.AddrInfo)
117116
close(ch)
118117
return ch
119118
}
120119

121120
type dummyPeerRouter map[peer.ID]struct{}
122121

123-
func (d dummyPeerRouter) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
122+
func (d dummyPeerRouter) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
124123
if _, ok := d[p]; ok {
125-
return pstore.PeerInfo{ID: p}, nil
124+
return peer.AddrInfo{ID: p}, nil
126125
}
127-
return pstore.PeerInfo{}, routing.ErrNotFound
126+
return peer.AddrInfo{}, routing.ErrNotFound
128127
}

go.mod

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,8 @@ module github.com/libp2p/go-libp2p-routing-helpers
33
require (
44
github.com/hashicorp/errwrap v1.0.0
55
github.com/hashicorp/go-multierror v1.0.0
6-
github.com/ipfs/go-cid v0.0.1
7-
github.com/libp2p/go-libp2p-crypto v0.0.1
8-
github.com/libp2p/go-libp2p-peer v0.0.1
9-
github.com/libp2p/go-libp2p-peerstore v0.0.1
10-
github.com/libp2p/go-libp2p-record v0.0.1
11-
github.com/libp2p/go-libp2p-routing v0.0.1
12-
github.com/multiformats/go-multihash v0.0.1
6+
github.com/ipfs/go-cid v0.0.2
7+
github.com/libp2p/go-libp2p-core v0.0.1
8+
github.com/libp2p/go-libp2p-record v0.1.0
9+
github.com/multiformats/go-multihash v0.0.5
1310
)

0 commit comments

Comments
 (0)