Skip to content

migrate to consolidated types. #20

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

Merged
merged 1 commit into from
May 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
15 changes: 8 additions & 7 deletions bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"errors"
"testing"

"github.com/libp2p/go-libp2p-core/routing"

errwrap "github.com/hashicorp/errwrap"
routing "github.com/libp2p/go-libp2p-routing"
)

type bootstrapRouter struct {
Expand All @@ -21,9 +22,9 @@ func (bs *bootstrapRouter) Bootstrap(ctx context.Context) error {
func TestBootstrap(t *testing.T) {
pings := make([]bool, 6)
d := Parallel{
Routers: []routing.IpfsRouting{
Routers: []routing.Routing{
Tiered{
Routers: []routing.IpfsRouting{
Routers: []routing.Routing{
&bootstrapRouter{
bs: func() error {
pings[0] = true
Expand All @@ -33,7 +34,7 @@ func TestBootstrap(t *testing.T) {
},
},
Tiered{
Routers: []routing.IpfsRouting{
Routers: []routing.Routing{
&bootstrapRouter{
bs: func() error {
pings[1] = true
Expand Down Expand Up @@ -95,9 +96,9 @@ func TestBootstrap(t *testing.T) {
}
func TestBootstrapErr(t *testing.T) {
d := Parallel{
Routers: []routing.IpfsRouting{
Routers: []routing.Routing{
Tiered{
Routers: []routing.IpfsRouting{
Routers: []routing.Routing{
&bootstrapRouter{
bs: func() error {
return errors.New("err1")
Expand All @@ -106,7 +107,7 @@ func TestBootstrapErr(t *testing.T) {
},
},
Tiered{
Routers: []routing.IpfsRouting{
Routers: []routing.Routing{
&bootstrapRouter{
bs: func() error {
return nil
Expand Down
27 changes: 13 additions & 14 deletions composed.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@ package routinghelpers
import (
"context"

ci "github.com/libp2p/go-libp2p-core/crypto"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"

multierror "github.com/hashicorp/go-multierror"
cid "github.com/ipfs/go-cid"
ci "github.com/libp2p/go-libp2p-crypto"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
routing "github.com/libp2p/go-libp2p-routing"
ropts "github.com/libp2p/go-libp2p-routing/options"
)

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

// PutValue adds value corresponding to given Key.
func (cr *Compose) PutValue(ctx context.Context, key string, value []byte, opts ...ropts.Option) error {
func (cr *Compose) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
if cr.ValueStore == nil {
return routing.ErrNotSupported
}
return cr.ValueStore.PutValue(ctx, key, value, opts...)
}

// GetValue searches for the value corresponding to given Key.
func (cr *Compose) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
func (cr *Compose) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
if cr.ValueStore == nil {
return nil, routing.ErrNotFound
}
return cr.ValueStore.GetValue(ctx, key, opts...)
}

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

// FindProvidersAsync searches for peers who are able to provide a given key
func (cr *Compose) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan pstore.PeerInfo {
func (cr *Compose) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan peer.AddrInfo {
if cr.ContentRouting == nil {
ch := make(chan pstore.PeerInfo)
ch := make(chan peer.AddrInfo)
close(ch)
return ch
}
return cr.ContentRouting.FindProvidersAsync(ctx, c, count)
}

// FindPeer searches for a peer with given ID, returns a pstore.PeerInfo
// FindPeer searches for a peer with given ID, returns a peer.AddrInfo
// with relevant addresses.
func (cr *Compose) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
func (cr *Compose) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
if cr.PeerRouting == nil {
return pstore.PeerInfo{}, routing.ErrNotFound
return peer.AddrInfo{}, routing.ErrNotFound
}
return cr.PeerRouting.FindPeer(ctx, p)
}
Expand Down Expand Up @@ -119,5 +118,5 @@ func (cr *Compose) Bootstrap(ctx context.Context) error {
return me.ErrorOrNil()
}

var _ routing.IpfsRouting = (*Compose)(nil)
var _ routing.Routing = (*Compose)(nil)
var _ routing.PubKeyFetcher = (*Compose)(nil)
35 changes: 17 additions & 18 deletions dummy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,30 @@ import (
"strings"
"sync"

"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"

cid "github.com/ipfs/go-cid"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
routing "github.com/libp2p/go-libp2p-routing"
ropts "github.com/libp2p/go-libp2p-routing/options"
)

type failValueStore struct{}

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

func (f failValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...ropts.Option) error {
func (f failValueStore) PutValue(ctx context.Context, key string, value []byte, opts ...routing.Option) error {
return failValueErr
}
func (f failValueStore) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
func (f failValueStore) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
return nil, failValueErr
}

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

type dummyValueStore sync.Map

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

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

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

type dummyProvider map[string][]peer.ID

func (d dummyProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan pstore.PeerInfo {
func (d dummyProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan peer.AddrInfo {
peers := d[c.KeyString()]
if len(peers) > count {
peers = peers[:count]
}
out := make(chan pstore.PeerInfo)
out := make(chan peer.AddrInfo)
go func() {
defer close(out)
for _, p := range peers {
Expand All @@ -94,7 +93,7 @@ func (d dummyProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count
return
}
select {
case out <- pstore.PeerInfo{ID: p}:
case out <- peer.AddrInfo{ID: p}:
case <-ctx.Done():
}
}
Expand All @@ -112,17 +111,17 @@ func (d cbProvider) Provide(ctx context.Context, c cid.Cid, local bool) error {
return d(c, local)
}

func (d cbProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan pstore.PeerInfo {
ch := make(chan pstore.PeerInfo)
func (d cbProvider) FindProvidersAsync(ctx context.Context, c cid.Cid, count int) <-chan peer.AddrInfo {
ch := make(chan peer.AddrInfo)
close(ch)
return ch
}

type dummyPeerRouter map[peer.ID]struct{}

func (d dummyPeerRouter) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
func (d dummyPeerRouter) FindPeer(ctx context.Context, p peer.ID) (peer.AddrInfo, error) {
if _, ok := d[p]; ok {
return pstore.PeerInfo{ID: p}, nil
return peer.AddrInfo{ID: p}, nil
}
return pstore.PeerInfo{}, routing.ErrNotFound
return peer.AddrInfo{}, routing.ErrNotFound
}
11 changes: 4 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ module github.com/libp2p/go-libp2p-routing-helpers
require (
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/go-multierror v1.0.0
github.com/ipfs/go-cid v0.0.1
github.com/libp2p/go-libp2p-crypto v0.0.1
github.com/libp2p/go-libp2p-peer v0.0.1
github.com/libp2p/go-libp2p-peerstore v0.0.1
github.com/libp2p/go-libp2p-record v0.0.1
github.com/libp2p/go-libp2p-routing v0.0.1
github.com/multiformats/go-multihash v0.0.1
github.com/ipfs/go-cid v0.0.2
github.com/libp2p/go-libp2p-core v0.0.1
github.com/libp2p/go-libp2p-record v0.1.0
github.com/multiformats/go-multihash v0.0.5
)
Loading