Skip to content

Commit

Permalink
Merge pull request ipfs/go-ipfs-routing#22 from ipfs/migrate-libp2p
Browse files Browse the repository at this point in the history
migrate to go-libp2p-core

This commit was moved from ipfs/go-ipfs-routing@d508458
  • Loading branch information
Stebalien committed May 31, 2019
2 parents d75ccd6 + fc50e9a commit 7447e95
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 82 deletions.
32 changes: 16 additions & 16 deletions routing/mock/centralized_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (

cid "github.com/ipfs/go-cid"
logging "github.com/ipfs/go-log"
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"
"github.com/libp2p/go-testutil"

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

ma "github.com/multiformats/go-multiaddr"
)

Expand All @@ -19,37 +19,37 @@ var log = logging.Logger("mockrouter")
type client struct {
vs routing.ValueStore
server server
peer testutil.Identity
peer tnet.Identity
}

// FIXME(brian): is this method meant to simulate putting a value into the network?
func (c *client) PutValue(ctx context.Context, key string, val []byte, opts ...ropts.Option) error {
func (c *client) PutValue(ctx context.Context, key string, val []byte, opts ...routing.Option) error {
log.Debugf("PutValue: %s", key)
return c.vs.PutValue(ctx, key, val, opts...)
}

// FIXME(brian): is this method meant to simulate getting a value from the network?
func (c *client) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
func (c *client) GetValue(ctx context.Context, key string, opts ...routing.Option) ([]byte, error) {
log.Debugf("GetValue: %s", key)
return c.vs.GetValue(ctx, key, opts...)
}

func (c *client) SearchValue(ctx context.Context, key string, opts ...ropts.Option) (<-chan []byte, error) {
func (c *client) SearchValue(ctx context.Context, key string, opts ...routing.Option) (<-chan []byte, error) {
log.Debugf("SearchValue: %s", key)
return c.vs.SearchValue(ctx, key, opts...)
}

func (c *client) FindProviders(ctx context.Context, key cid.Cid) ([]pstore.PeerInfo, error) {
func (c *client) FindProviders(ctx context.Context, key cid.Cid) ([]peer.AddrInfo, error) {
return c.server.Providers(key), nil
}

func (c *client) FindPeer(ctx context.Context, pid peer.ID) (pstore.PeerInfo, error) {
func (c *client) FindPeer(ctx context.Context, pid peer.ID) (peer.AddrInfo, error) {
log.Debugf("FindPeer: %s", pid)
return pstore.PeerInfo{}, nil
return peer.AddrInfo{}, nil
}

func (c *client) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan pstore.PeerInfo {
out := make(chan pstore.PeerInfo)
func (c *client) FindProvidersAsync(ctx context.Context, k cid.Cid, max int) <-chan peer.AddrInfo {
out := make(chan peer.AddrInfo)
go func() {
defer close(out)
for i, p := range c.server.Providers(k) {
Expand All @@ -72,7 +72,7 @@ func (c *client) Provide(_ context.Context, key cid.Cid, brd bool) error {
if !brd {
return nil
}
info := pstore.PeerInfo{
info := peer.AddrInfo{
ID: c.peer.ID(),
Addrs: []ma.Multiaddr{c.peer.Address()},
}
Expand All @@ -87,4 +87,4 @@ func (c *client) Bootstrap(context.Context) error {
return nil
}

var _ routing.IpfsRouting = &client{}
var _ routing.Routing = &client{}
22 changes: 11 additions & 11 deletions routing/mock/centralized_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@ import (
cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
"github.com/libp2p/go-testutil"

"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-testing/net"

offline "github.com/ipfs/go-ipfs-routing/offline"
)

// server is the mockrouting.Client's private interface to the routing server
type server interface {
Announce(pstore.PeerInfo, cid.Cid) error
Providers(cid.Cid) []pstore.PeerInfo
Announce(peer.AddrInfo, cid.Cid) error
Providers(cid.Cid) []peer.AddrInfo

Server
}
Expand All @@ -33,11 +33,11 @@ type s struct {
}

type providerRecord struct {
Peer pstore.PeerInfo
Peer peer.AddrInfo
Created time.Time
}

func (rs *s) Announce(p pstore.PeerInfo, c cid.Cid) error {
func (rs *s) Announce(p peer.AddrInfo, c cid.Cid) error {
rs.lock.Lock()
defer rs.lock.Unlock()

Expand All @@ -54,14 +54,14 @@ func (rs *s) Announce(p pstore.PeerInfo, c cid.Cid) error {
return nil
}

func (rs *s) Providers(c cid.Cid) []pstore.PeerInfo {
func (rs *s) Providers(c cid.Cid) []peer.AddrInfo {
rs.delayConf.Query.Wait() // before locking

rs.lock.RLock()
defer rs.lock.RUnlock()
k := c.KeyString()

var ret []pstore.PeerInfo
var ret []peer.AddrInfo
records, ok := rs.providers[k]
if !ok {
return ret
Expand All @@ -80,11 +80,11 @@ func (rs *s) Providers(c cid.Cid) []pstore.PeerInfo {
return ret
}

func (rs *s) Client(p testutil.Identity) Client {
func (rs *s) Client(p tnet.Identity) Client {
return rs.ClientWithDatastore(context.Background(), p, dssync.MutexWrap(ds.NewMapDatastore()))
}

func (rs *s) ClientWithDatastore(_ context.Context, p testutil.Identity, datastore ds.Datastore) Client {
func (rs *s) ClientWithDatastore(_ context.Context, p tnet.Identity, datastore ds.Datastore) Client {
return &client{
peer: p,
vs: offline.NewOfflineRouter(datastore, MockValidator{}),
Expand Down
21 changes: 11 additions & 10 deletions routing/mock/centralized_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import (
cid "github.com/ipfs/go-cid"
delay "github.com/ipfs/go-ipfs-delay"
u "github.com/ipfs/go-ipfs-util"
pstore "github.com/libp2p/go-libp2p-peerstore"
testutil "github.com/libp2p/go-testutil"

"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-testing/net"
)

func TestKeyNotFound(t *testing.T) {

var pi = testutil.RandIdentityOrFatal(t)
var pi = tnet.RandIdentityOrFatal(t)
var key = cid.NewCidV0(u.Hash([]byte("mock key")))
var ctx = context.Background()

Expand All @@ -27,7 +28,7 @@ func TestKeyNotFound(t *testing.T) {
}

func TestClientFindProviders(t *testing.T) {
pi := testutil.RandIdentityOrFatal(t)
pi := tnet.RandIdentityOrFatal(t)
rs := NewServer()
client := rs.Client(pi)

Expand Down Expand Up @@ -58,15 +59,15 @@ func TestClientOverMax(t *testing.T) {
k := cid.NewCidV0(u.Hash([]byte("hello")))
numProvidersForHelloKey := 100
for i := 0; i < numProvidersForHelloKey; i++ {
pi := testutil.RandIdentityOrFatal(t)
pi := tnet.RandIdentityOrFatal(t)
err := rs.Client(pi).Provide(context.Background(), k, true)
if err != nil {
t.Fatal(err)
}
}

max := 10
pi := testutil.RandIdentityOrFatal(t)
pi := tnet.RandIdentityOrFatal(t)
client := rs.Client(pi)

providersFromClient := client.FindProvidersAsync(context.Background(), k, max)
Expand Down Expand Up @@ -101,7 +102,7 @@ func TestCanceledContext(t *testing.T) {
default:
}

pi, err := testutil.RandIdentity()
pi, err := tnet.RandIdentity()
if err != nil {
t.Error(err)
}
Expand All @@ -113,7 +114,7 @@ func TestCanceledContext(t *testing.T) {
}
}()

local := testutil.RandIdentityOrFatal(t)
local := tnet.RandIdentityOrFatal(t)
client := rs.Client(local)

t.Log("warning: max is finite so this test is non-deterministic")
Expand Down Expand Up @@ -141,7 +142,7 @@ func TestValidAfter(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

pi := testutil.RandIdentityOrFatal(t)
pi := tnet.RandIdentityOrFatal(t)
key := cid.NewCidV0(u.Hash([]byte("mock key")))
conf := DelayConfig{
ValueVisibility: delay.Fixed(1 * time.Hour),
Expand All @@ -152,7 +153,7 @@ func TestValidAfter(t *testing.T) {

rs.Client(pi).Provide(ctx, key, true)

var providers []pstore.PeerInfo
var providers []peer.AddrInfo
max := 100
providersChan := rs.Client(pi).FindProvidersAsync(ctx, key, max)
for p := range providersChan {
Expand Down
17 changes: 9 additions & 8 deletions routing/mock/interface.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Package mockrouting provides a virtual routing server. To use it,
// create a virtual routing server and use the Client() method to get a
// routing client (IpfsRouting). The server quacks like a DHT but is
// routing client (Routing). The server quacks like a DHT but is
// really a local in-memory hash table.
package mockrouting

Expand All @@ -9,9 +9,10 @@ import (

ds "github.com/ipfs/go-datastore"
delay "github.com/ipfs/go-ipfs-delay"
peer "github.com/libp2p/go-libp2p-peer"
routing "github.com/libp2p/go-libp2p-routing"
"github.com/libp2p/go-testutil"

"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/routing"
"github.com/libp2p/go-libp2p-testing/net"
)

// MockValidator is a record validator that always returns success.
Expand All @@ -22,13 +23,13 @@ func (MockValidator) Select(_ string, _ [][]byte) (int, error) { return 0, nil }

// Server provides mockrouting Clients
type Server interface {
Client(p testutil.Identity) Client
ClientWithDatastore(context.Context, testutil.Identity, ds.Datastore) Client
Client(p tnet.Identity) Client
ClientWithDatastore(context.Context, tnet.Identity, ds.Datastore) Client
}

// Client implements IpfsRouting
// Client implements Routing
type Client interface {
routing.IpfsRouting
routing.Routing
}

// NewServer returns a mockrouting Server
Expand Down
30 changes: 15 additions & 15 deletions routing/none/none_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ import (

cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
p2phost "github.com/libp2p/go-libp2p-host"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"

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

record "github.com/libp2p/go-libp2p-record"
routing "github.com/libp2p/go-libp2p-routing"
ropts "github.com/libp2p/go-libp2p-routing/options"
)

type nilclient struct {
}

func (c *nilclient) PutValue(_ context.Context, _ string, _ []byte, _ ...ropts.Option) error {
func (c *nilclient) PutValue(_ context.Context, _ string, _ []byte, _ ...routing.Option) error {
return nil
}

func (c *nilclient) GetValue(_ context.Context, _ string, _ ...ropts.Option) ([]byte, error) {
func (c *nilclient) GetValue(_ context.Context, _ string, _ ...routing.Option) ([]byte, error) {
return nil, errors.New("tried GetValue from nil routing")
}

func (c *nilclient) SearchValue(_ context.Context, _ string, _ ...ropts.Option) (<-chan []byte, error) {
func (c *nilclient) SearchValue(_ context.Context, _ string, _ ...routing.Option) (<-chan []byte, error) {
return nil, errors.New("tried SearchValue from nil routing")
}

func (c *nilclient) FindPeer(_ context.Context, _ peer.ID) (pstore.PeerInfo, error) {
return pstore.PeerInfo{}, nil
func (c *nilclient) FindPeer(_ context.Context, _ peer.ID) (peer.AddrInfo, error) {
return peer.AddrInfo{}, nil
}

func (c *nilclient) FindProvidersAsync(_ context.Context, _ cid.Cid, _ int) <-chan pstore.PeerInfo {
out := make(chan pstore.PeerInfo)
func (c *nilclient) FindProvidersAsync(_ context.Context, _ cid.Cid, _ int) <-chan peer.AddrInfo {
out := make(chan peer.AddrInfo)
defer close(out)
return out
}
Expand All @@ -48,10 +48,10 @@ func (c *nilclient) Bootstrap(_ context.Context) error {
return nil
}

// ConstructNilRouting creates an IpfsRouting client which does nothing.
func ConstructNilRouting(_ context.Context, _ p2phost.Host, _ ds.Batching, _ record.Validator) (routing.IpfsRouting, error) {
// ConstructNilRouting creates an Routing client which does nothing.
func ConstructNilRouting(_ context.Context, _ host.Host, _ ds.Batching, _ record.Validator) (routing.Routing, error) {
return &nilclient{}, nil
}

// ensure nilclient satisfies interface
var _ routing.IpfsRouting = &nilclient{}
var _ routing.Routing = &nilclient{}
Loading

0 comments on commit 7447e95

Please sign in to comment.