diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index 344e0659015e..c03342efd510 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -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 diff --git a/core/coreapi/dht.go b/core/coreapi/dht.go index e7900569d5fc..6eb41b7aae42 100644 --- a/core/coreapi/dht.go +++ b/core/coreapi/dht.go @@ -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) diff --git a/core/coreapi/dht_test.go b/core/coreapi/dht_test.go index ed2ee542f085..6a9c96649c4f 100644 --- a/core/coreapi/dht_test.go +++ b/core/coreapi/dht_test.go @@ -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" @@ -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) } @@ -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) } @@ -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) } diff --git a/core/coreapi/interface/dht.go b/core/coreapi/interface/dht.go index ce8509e01a8b..c49903bee763 100644 --- a/core/coreapi/interface/dht.go +++ b/core/coreapi/interface/dht.go @@ -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 } diff --git a/core/coreapi/interface/options/dht.go b/core/coreapi/interface/options/dht.go index 3867e32c075f..6674a30a5831 100644 --- a/core/coreapi/interface/options/dht.go +++ b/core/coreapi/interface/options/dht.go @@ -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