diff --git a/core/coreapi/coreapi.go b/core/coreapi/coreapi.go index 702986608..bb7afd61a 100644 --- a/core/coreapi/coreapi.go +++ b/core/coreapi/coreapi.go @@ -65,5 +65,5 @@ 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) } diff --git a/core/coreapi/dht.go b/core/coreapi/dht.go index 4f3711116..107dc8ed5 100644 --- a/core/coreapi/dht.go +++ b/core/coreapi/dht.go @@ -18,10 +18,7 @@ import ( blockstore "gx/ipfs/Qmeg56ecxRnVv7VWViMrDeEMoBHaNFMs4vQnyQrJ79Zz7i/go-ipfs-blockstore" ) -type DhtAPI struct { - *CoreAPI - *caopts.DhtOptions -} +type DhtAPI CoreAPI func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) { pi, err := api.node.Routing.FindPeer(ctx, peer.ID(p)) @@ -38,7 +35,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ... return nil, err } - rp, err := api.ResolvePath(ctx, p) + rp, err := api.core().ResolvePath(ctx, p) if err != nil { return nil, err } @@ -62,7 +59,7 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao return errors.New("cannot provide in offline mode") } - rp, err := api.ResolvePath(ctx, path) + rp, err := api.core().ResolvePath(ctx, path) if err != nil { return err } @@ -127,3 +124,7 @@ func provideKeysRec(ctx context.Context, r routing.IpfsRouting, bs blockstore.Bl return nil } + +func (api *DhtAPI) core() coreiface.CoreAPI { + return (*CoreAPI)(api) +} diff --git a/core/coreapi/dht_test.go b/core/coreapi/dht_test.go index 2d24dda2e..7dcca66b6 100644 --- a/core/coreapi/dht_test.go +++ b/core/coreapi/dht_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/ipfs/go-ipfs/core/coreapi/interface" + "github.com/ipfs/go-ipfs/core/coreapi/interface/options" peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer" blocks "gx/ipfs/QmWAzSEoqZ6xU6pu8yL8e5WaMb7wtbfbhhN4p1DknUPtr3/go-block-format" @@ -50,7 +51,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.WithNumProviders(1)) if err != nil { t.Fatal(err) } @@ -79,7 +80,7 @@ func TestDhtProvide(t *testing.T) { nds[0].Blockstore.Put(b) p := iface.IpfsPath(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.WithNumProviders(1)) if err != nil { t.Fatal(err) } @@ -95,7 +96,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.WithNumProviders(1)) if err != nil { t.Fatal(err) } diff --git a/core/coreapi/interface/dht.go b/core/coreapi/interface/dht.go index 01b7d7367..f9a08df34 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 pstore.PeerInfo, error) - // 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 3867e32c0..f989fa5e7 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 + +// WithRecursive is an option for Dht.Provide which specifies whether to provide +// the given path recursively +func (dhtOpts) WithRecursive(recursive bool) DhtProvideOption { return func(settings *DhtProvideSettings) error { settings.Recursive = recursive return nil } } -func (api *DhtOptions) WithNumProviders(numProviders int) DhtFindProvidersOption { +// WithNumProviders is an option for Dht.FindProviders which specifies the +// number of peers to look for. Default is 20 +func (dhtOpts) WithNumProviders(numProviders int) DhtFindProvidersOption { return func(settings *DhtFindProvidersSettings) error { settings.NumProviders = numProviders return nil