Skip to content
This repository has been archived by the owner on Jun 20, 2023. It is now read-only.

update for routing interface changes #5

Merged
merged 2 commits into from
Jun 2, 2018
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
2 changes: 1 addition & 1 deletion .gx/lastpubver
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.6: QmUUnw51qbtCvxayyMAGM6P1cjmgkhtoQ91pAp98XtnkS2
0.0.7: QmPuPdzoG4b5uyYSQCjLEHB8NM593m3BW19UHX2jZ6Wzfm
15 changes: 3 additions & 12 deletions mock/centralized_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
pstore "github.com/libp2p/go-libp2p-peerstore"
dhtpb "github.com/libp2p/go-libp2p-record/pb"
routing "github.com/libp2p/go-libp2p-routing"
ropts "github.com/libp2p/go-libp2p-routing/options"
"github.com/libp2p/go-testutil"
ma "github.com/multiformats/go-multiaddr"
)
Expand All @@ -28,7 +29,7 @@ type client struct {
}

// 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) error {
func (c *client) PutValue(ctx context.Context, key string, val []byte, opts ...ropts.Option) error {
log.Debugf("PutValue: %s", key)
rec := new(dhtpb.Record)
rec.Value = val
Expand All @@ -43,7 +44,7 @@ func (c *client) PutValue(ctx context.Context, key string, val []byte) error {
}

// FIXME(brian): is this method meant to simulate getting a value from the network?
func (c *client) GetValue(ctx context.Context, key string) ([]byte, error) {
func (c *client) GetValue(ctx context.Context, key string, opts ...ropts.Option) ([]byte, error) {
log.Debugf("GetValue: %s", key)
v, err := c.datastore.Get(dshelp.NewKeyFromBinary([]byte(key)))
if err != nil {
Expand All @@ -64,16 +65,6 @@ func (c *client) GetValue(ctx context.Context, key string) ([]byte, error) {
return rec.GetValue(), nil
}

func (c *client) GetValues(ctx context.Context, key string, count int) ([]routing.RecvdVal, error) {
log.Debugf("GetValues: %s", key)
data, err := c.GetValue(ctx, key)
if err != nil {
return nil, err
}

return []routing.RecvdVal{{Val: data, From: c.peer.ID()}}, nil
}

func (c *client) FindProviders(ctx context.Context, key *cid.Cid) ([]pstore.PeerInfo, error) {
return c.server.Providers(key), nil
}
Expand Down
12 changes: 5 additions & 7 deletions none/none_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,22 @@ import (
p2phost "github.com/libp2p/go-libp2p-host"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
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) error {
func (c *nilclient) PutValue(_ context.Context, _ string, _ []byte, _ ...ropts.Option) error {
return nil
}

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

func (c *nilclient) GetValues(_ context.Context, _ string, _ int) ([]routing.RecvdVal, error) {
return nil, errors.New("tried GetValues from nil routing")
}

func (c *nilclient) FindPeer(_ context.Context, _ peer.ID) (pstore.PeerInfo, error) {
return pstore.PeerInfo{}, nil
}
Expand All @@ -47,7 +45,7 @@ func (c *nilclient) Bootstrap(_ context.Context) error {
}

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

Expand Down
26 changes: 3 additions & 23 deletions offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
record "github.com/libp2p/go-libp2p-record"
pb "github.com/libp2p/go-libp2p-record/pb"
routing "github.com/libp2p/go-libp2p-routing"
ropts "github.com/libp2p/go-libp2p-routing/options"
)

// ErrOffline is returned when trying to perform operations that
Expand All @@ -41,7 +42,7 @@ type offlineRouting struct {
sk ci.PrivKey
}

func (c *offlineRouting) PutValue(ctx context.Context, key string, val []byte) error {
func (c *offlineRouting) PutValue(ctx context.Context, key string, val []byte, _ ...ropts.Option) error {
rec := record.MakePutRecord(key, val)
data, err := proto.Marshal(rec)
if err != nil {
Expand All @@ -51,7 +52,7 @@ func (c *offlineRouting) PutValue(ctx context.Context, key string, val []byte) e
return c.datastore.Put(dshelp.NewKeyFromBinary([]byte(key)), data)
}

func (c *offlineRouting) GetValue(ctx context.Context, key string) ([]byte, error) {
func (c *offlineRouting) GetValue(ctx context.Context, key string, _ ...ropts.Option) ([]byte, error) {
v, err := c.datastore.Get(dshelp.NewKeyFromBinary([]byte(key)))
if err != nil {
return nil, err
Expand All @@ -70,27 +71,6 @@ func (c *offlineRouting) GetValue(ctx context.Context, key string) ([]byte, erro
return rec.GetValue(), nil
}

func (c *offlineRouting) GetValues(ctx context.Context, key string, _ int) ([]routing.RecvdVal, error) {
v, err := c.datastore.Get(dshelp.NewKeyFromBinary([]byte(key)))
if err != nil {
return nil, err
}

byt, ok := v.([]byte)
if !ok {
return nil, errors.New("value stored in datastore not []byte")
}
rec := new(pb.Record)
err = proto.Unmarshal(byt, rec)
if err != nil {
return nil, err
}

return []routing.RecvdVal{
{Val: rec.GetValue()},
}, nil
}

func (c *offlineRouting) FindPeer(ctx context.Context, pid peer.ID) (pstore.PeerInfo, error) {
return pstore.PeerInfo{}, ErrOffline
}
Expand Down
6 changes: 3 additions & 3 deletions offline/offline_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

cid "github.com/ipfs/go-cid"
ds "github.com/ipfs/go-datastore"
ropt "github.com/libp2p/go-libp2p-routing/options"
testutil "github.com/libp2p/go-testutil"
mh "github.com/multiformats/go-multihash"
)
Expand Down Expand Up @@ -35,17 +36,16 @@ func TestOfflineRouterStorage(t *testing.T) {
t.Fatal("Router should throw errors for unfound records")
}

recVal, err := offline.GetValues(ctx, "key", 0)
local, err := offline.GetValue(ctx, "key", ropt.Offline)
if err != nil {
t.Fatal(err)
}

_, err = offline.GetValues(ctx, "notHere", 0)
_, err = offline.GetValue(ctx, "notHere", ropt.Offline)
if err == nil {
t.Fatal("Router should throw errors for unfound records")
}

local := recVal[0].Val
if !bytes.Equal([]byte("testing 1 2 3"), local) {
t.Fatal("OfflineRouter does not properly store")
}
Expand Down
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
"version": "2.1.7"
},
{
"hash": "QmZix3EdeAdc4wnRksRXWEQ6kbqiFAP16h3Sq9JnEiP71N",
"hash": "QmUHRKTeaoASDvDj7cTAXsmjAY7KQ13ErtzkQHZQq6uFUz",
"name": "go-libp2p-routing",
"version": "2.2.22"
"version": "2.3.0"
},
{
"author": "whyrusleeping",
Expand Down Expand Up @@ -55,9 +55,9 @@
"version": "1.2.1"
},
{
"hash": "QmZ9V14gpwKsTUG7y5mHZDnHSF4Fa4rKsXNx7jSTEQ4JWs",
"hash": "QmTUyK82BVPA6LmSzEJpfEunk9uBaQzWtMsNP917tVj4sT",
"name": "go-libp2p-record",
"version": "4.0.1"
"version": "4.1.0"
},
{
"author": "hsanjuan",
Expand All @@ -71,6 +71,6 @@
"license": "MIT",
"name": "go-ipfs-routing",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.0.6"
"version": "0.0.7"
}