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 <[email protected]>
  • Loading branch information
magik6k committed Mar 30, 2018
1 parent d4b6cee commit 5f75628
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 20 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
11 changes: 6 additions & 5 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 Expand Up @@ -93,7 +90,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ...
return nil, ErrNotDHT
}

p, err = api.ResolvePath(ctx, p)
p, err = api.core().ResolvePath(ctx, p)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -238,3 +235,7 @@ func provideKeysRec(ctx context.Context, r routing.IpfsRouting, dserv ipld.DAGSe

return nil
}

func (api *DhtAPI) core() coreiface.CoreAPI {
return (*CoreAPI)(api)
}
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 5f75628

Please sign in to comment.