@cryptotaxi247 / kubo / commits / 77ceb7546

coreapi: dht: refactor options after rebase

License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>

Łukasz Magiera committed Jul 19, 2018 at 13:27 UTC 77ceb7546b3465c9e5f024eff9f0be3c507f8ba2
5 files changed +21 -21
core/coreapi/coreapi.go
+1 -1
@@ -65,5 +65,5 @@ func (api *CoreAPI) Pin() coreiface.PinAPI {
65
66 // Dht returns the DhtAPI interface implementation backed by the go-ipfs node
67 func (api *CoreAPI) Dht() coreiface.DhtAPI {
68 - return &DhtAPI{api, nil}
68 + return (*DhtAPI)(api)
69 }
core/coreapi/dht.go
+7 -6
@@ -18,10 +18,7 @@ import (
18 blockstore "gx/ipfs/Qmeg56ecxRnVv7VWViMrDeEMoBHaNFMs4vQnyQrJ79Zz7i/go-ipfs-blockstore"
19 )
20
21 -type DhtAPI struct {
22 - *CoreAPI
23 - *caopts.DhtOptions
24 -}
21 +type DhtAPI CoreAPI
22
23 func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
24 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 ...
35 return nil, err
36 }
37
41 - rp, err := api.ResolvePath(ctx, p)
38 + rp, err := api.core().ResolvePath(ctx, p)
39 if err != nil {
40 return nil, err
41 }
@@ -62,7 +59,7 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao
59 return errors.New("cannot provide in offline mode")
60 }
61
65 - rp, err := api.ResolvePath(ctx, path)
62 + rp, err := api.core().ResolvePath(ctx, path)
63 if err != nil {
64 return err
65 }
@@ -127,3 +124,7 @@ func provideKeysRec(ctx context.Context, r routing.IpfsRouting, bs blockstore.Bl
124
125 return nil
126 }
127 +
128 +func (api *DhtAPI) core() coreiface.CoreAPI {
129 + return (*CoreAPI)(api)
130 +}
core/coreapi/dht_test.go
+4 -3
@@ -7,6 +7,7 @@ import (
7 "testing"
8
9 "github.com/ipfs/go-ipfs/core/coreapi/interface"
10 + "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
11
12 peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
13 blocks "gx/ipfs/QmWAzSEoqZ6xU6pu8yL8e5WaMb7wtbfbhhN4p1DknUPtr3/go-block-format"
@@ -50,7 +51,7 @@ func TestDhtFindProviders(t *testing.T) {
51 t.Fatal(err)
52 }
53
53 - out, err := apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1))
54 + out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.WithNumProviders(1))
55 if err != nil {
56 t.Fatal(err)
57 }
@@ -79,7 +80,7 @@ func TestDhtProvide(t *testing.T) {
80 nds[0].Blockstore.Put(b)
81 p := iface.IpfsPath(b.Cid())
82
82 - out, err := apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1))
83 + out, err := apis[2].Dht().FindProviders(ctx, p, options.Dht.WithNumProviders(1))
84 if err != nil {
85 t.Fatal(err)
86 }
@@ -95,7 +96,7 @@ func TestDhtProvide(t *testing.T) {
96 t.Fatal(err)
97 }
98
98 - out, err = apis[2].Dht().FindProviders(ctx, p, apis[2].Dht().WithNumProviders(1))
99 + out, err = apis[2].Dht().FindProviders(ctx, p, options.Dht.WithNumProviders(1))
100 if err != nil {
101 t.Fatal(err)
102 }
core/coreapi/interface/dht.go
-8
@@ -19,14 +19,6 @@ type DhtAPI interface {
19 // given a key.
20 FindProviders(context.Context, Path, ...options.DhtFindProvidersOption) (<-chan pstore.PeerInfo, error)
21
22 - // WithNumProviders is an option for FindProviders which specifies the
23 - // number of peers to look for. Default is 20
24 - WithNumProviders(numProviders int) options.DhtFindProvidersOption
25 -
22 // Provide announces to the network that you are providing given values
23 Provide(context.Context, Path, ...options.DhtProvideOption) error
28 -
29 - // WithRecursive is an option for Provide which specifies whether to provide
30 - // the given path recursively
31 - WithRecursive(recursive bool) options.DhtProvideOption
24 }
core/coreapi/interface/options/dht.go
+9 -3
@@ -39,16 +39,22 @@ func DhtFindProvidersOptions(opts ...DhtFindProvidersOption) (*DhtFindProvidersS
39 return options, nil
40 }
41
42 -type DhtOptions struct{}
42 +type dhtOpts struct{}
43
44 -func (api *DhtOptions) WithRecursive(recursive bool) DhtProvideOption {
44 +var Dht dhtOpts
45 +
46 +// WithRecursive is an option for Dht.Provide which specifies whether to provide
47 +// the given path recursively
48 +func (dhtOpts) WithRecursive(recursive bool) DhtProvideOption {
49 return func(settings *DhtProvideSettings) error {
50 settings.Recursive = recursive
51 return nil
52 }
53 }
54
51 -func (api *DhtOptions) WithNumProviders(numProviders int) DhtFindProvidersOption {
55 +// WithNumProviders is an option for Dht.FindProviders which specifies the
56 +// number of peers to look for. Default is 20
57 +func (dhtOpts) WithNumProviders(numProviders int) DhtFindProvidersOption {
58 return func(settings *DhtFindProvidersSettings) error {
59 settings.NumProviders = numProviders
60 return nil