Skip to content

Commit

Permalink
coreapi/dht: refactor options
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Mar 30, 2018
1 parent d4b6cee commit 8aaf2e1
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (api *CoreAPI) Pin() coreiface.PinAPI {

// Dht returns the DhtAPI interface implementation backed by the go-ipfs node
func (api *CoreAPI) Dht() coreiface.DhtAPI {
return &DhtAPI{api, nil}
return (*DhtAPI)(api)
}

// ResolveNode resolves the path `p` using Unixfx resolver, gets and returns the
Expand Down
5 changes: 1 addition & 4 deletions core/coreapi/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ import (

var ErrNotDHT = errors.New("routing service is not a DHT")

type DhtAPI struct {
*CoreAPI
*caopts.DhtOptions
}
type DhtAPI CoreAPI

func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (<-chan ma.Multiaddr, error) {
dht, ok := api.node.Routing.(*ipdht.IpfsDHT)
Expand Down
7 changes: 4 additions & 3 deletions core/coreapi/dht_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"testing"

coreapi "github.com/ipfs/go-ipfs/core/coreapi"
options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"

peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
blocks "gx/ipfs/Qmej7nf81hi2x2tvjRBF3mcp74sQyuDH4VMYDGd1YtXjb2/go-block-format"
Expand Down Expand Up @@ -54,7 +55,7 @@ func TestDhtFindProviders(t *testing.T) {
t.Fatal(err)
}

out, err := apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1))
out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1))
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -83,7 +84,7 @@ func TestDhtProvide(t *testing.T) {
nds[0].Blockstore.Put(b)
p := coreapi.ParseCid(b.Cid())

out, err := apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1))
out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1))
if err != nil {
t.Fatal(err)
}
Expand All @@ -99,7 +100,7 @@ func TestDhtProvide(t *testing.T) {
t.Fatal(err)
}

out, err = apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1))
out, err = apis[2].Dht().FindProviders(ctx, p, options.Dht.NumProviders(1))
if err != nil {
t.Fatal(err)
}
Expand Down
8 changes: 0 additions & 8 deletions core/coreapi/interface/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ type DhtAPI interface {
// given a key.
FindProviders(context.Context, Path, ...options.DhtFindProvidersOption) (<-chan peer.ID, error) //TODO: is path the right choice here?

// WithNumProviders is an option for FindProviders which specifies the
// number of peers to look for. Default is 20
WithNumProviders(numProviders int) options.DhtFindProvidersOption

// Provide announces to the network that you are providing given values
Provide(context.Context, Path, ...options.DhtProvideOption) error

// WithRecursive is an option for Provide which specifies whether to provide
// the given path recursively
WithRecursive(recursive bool) options.DhtProvideOption
}
12 changes: 9 additions & 3 deletions core/coreapi/interface/options/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,22 @@ func DhtFindProvidersOptions(opts ...DhtFindProvidersOption) (*DhtFindProvidersS
return options, nil
}

type DhtOptions struct{}
type dhtOpts struct{}

func (api *DhtOptions) WithRecursive(recursive bool) DhtProvideOption {
var Dht dhtOpts

// Recursive is an option for Provide which specifies whether to provide
// the given path recursively
func (dhtOpts) Recursive(recursive bool) DhtProvideOption {
return func(settings *DhtProvideSettings) error {
settings.Recursive = recursive
return nil
}
}

func (api *DhtOptions) WithNumProviders(numProviders int) DhtFindProvidersOption {
// Providers is an option for Dht.FindProviders which specifies the
// number of peers to look for. Default is 20
func (dhtOpts) NumProviders(numProviders int) DhtFindProvidersOption {
return func(settings *DhtFindProvidersSettings) error {
settings.NumProviders = numProviders
return nil
Expand Down

0 comments on commit 8aaf2e1

Please sign in to comment.