@cryptotaxi247 / kubo / commits / 0fd87de1a

coreapi name: accept namesys options

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

Łukasz Magiera committed Sep 19, 2018 at 11:51 UTC 0fd87de1a4afc9ec90896c533811646ca54a2213
3 files changed +21 -49
core/commands/resolve.go
+8 -5
@@ -11,8 +11,9 @@ import (
11 e "github.com/ipfs/go-ipfs/core/commands/e"
12 ncmd "github.com/ipfs/go-ipfs/core/commands/name"
13 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
14 - "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
14 + options "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
15 ns "github.com/ipfs/go-ipfs/namesys"
16 + nsopts "github.com/ipfs/go-ipfs/namesys/opts"
17 path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
18
19 "gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
@@ -93,12 +94,14 @@ Resolve the value of an IPFS DAG path:
94
95 // the case when ipns is resolved step by step
96 if strings.HasPrefix(name, "/ipns/") && !recursive {
96 - rc, rcok := req.Options["dht-record-count"].(int)
97 + rc, rcok := req.Options["dht-record-count"].(uint)
98 dhtt, dhttok := req.Options["dht-timeout"].(string)
98 - ropts := []options.NameResolveOption{options.Name.Depth(1)}
99 + ropts := []options.NameResolveOption{
100 + options.Name.ResolveOption(nsopts.Depth(1)),
101 + }
102
103 if rcok {
101 - ropts = append(ropts, options.Name.DhtRecordCount(rc))
104 + ropts = append(ropts, options.Name.ResolveOption(nsopts.DhtRecordCount(rc)))
105 }
106 if dhttok {
107 d, err := time.ParseDuration(dhtt)
@@ -110,7 +113,7 @@ Resolve the value of an IPFS DAG path:
113 res.SetError(errors.New("DHT timeout value must be >= 0"), cmdkit.ErrNormal)
114 return
115 }
113 - ropts = append(ropts, options.Name.DhtTimeout(d))
116 + ropts = append(ropts, options.Name.ResolveOption(nsopts.DhtTimeout(d)))
117 }
118 p, err := api.Name().Resolve(req.Context, name, ropts...)
119 // ErrResolveRecursion is fine
core/coreapi/interface/options/name.go
+6 -30
@@ -2,6 +2,8 @@ package options
2
3 import (
4 "time"
5 +
6 + ropts "github.com/ipfs/go-ipfs/namesys/opts"
7 )
8
9 const (
@@ -14,12 +16,10 @@ type NamePublishSettings struct {
16 }
17
18 type NameResolveSettings struct {
17 - Depth int
19 Local bool
20 Cache bool
21
21 - DhtRecordCount int
22 - DhtTimeout time.Duration
22 + ResolveOpts []ropts.ResolveOpt
23 }
24
25 type NamePublishOption func(*NamePublishSettings) error
@@ -43,12 +43,8 @@ func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error)
43
44 func NameResolveOptions(opts ...NameResolveOption) (*NameResolveSettings, error) {
45 options := &NameResolveSettings{
46 - Depth: 1,
46 Local: false,
47 Cache: true,
49 -
50 - DhtRecordCount: 16,
51 - DhtTimeout: time.Minute,
48 }
49
50 for _, opt := range opts {
@@ -86,15 +82,6 @@ func (nameOpts) Key(key string) NamePublishOption {
82 }
83 }
84
89 -// Depth is an option for Name.Resolve which specifies the maximum depth of a
90 -// recursive lookup. Default value is false
91 -func (nameOpts) Depth(depth int) NameResolveOption {
92 - return func(settings *NameResolveSettings) error {
93 - settings.Depth = depth
94 - return nil
95 - }
96 -}
97 -
85 // Local is an option for Name.Resolve which specifies if the lookup should be
86 // offline. Default value is false
87 func (nameOpts) Local(local bool) NameResolveOption {
@@ -113,21 +100,10 @@ func (nameOpts) Cache(cache bool) NameResolveOption {
100 }
101 }
102
116 -// DhtRecordCount is an option for Name.Resolve which specifies how many records
117 -// we want to validate before selecting the best one (newest). Note that setting
118 -// this value too low will have security implications
119 -func (nameOpts) DhtRecordCount(rc int) NameResolveOption {
120 - return func(settings *NameResolveSettings) error {
121 - settings.DhtRecordCount = rc
122 - return nil
123 - }
124 -}
125 -
126 -// DhtTimeout is an option for Name.Resolve which specifies timeout for
127 -// DHT lookup
128 -func (nameOpts) DhtTimeout(timeout time.Duration) NameResolveOption {
103 +//
104 +func (nameOpts) ResolveOption(opt ropts.ResolveOpt) NameResolveOption {
105 return func(settings *NameResolveSettings) error {
130 - settings.DhtTimeout = timeout
106 + settings.ResolveOpts = append(settings.ResolveOpts, opt)
107 return nil
108 }
109 }
core/coreapi/name.go
+7 -14
@@ -7,17 +7,16 @@ import (
7 "strings"
8 "time"
9
10 - core "github.com/ipfs/go-ipfs/core"
10 + "github.com/ipfs/go-ipfs/core"
11 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
12 caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
13 - keystore "github.com/ipfs/go-ipfs/keystore"
14 - namesys "github.com/ipfs/go-ipfs/namesys"
15 - nsopts "github.com/ipfs/go-ipfs/namesys/opts"
13 + "github.com/ipfs/go-ipfs/keystore"
14 + "github.com/ipfs/go-ipfs/namesys"
15 ipath "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
16
18 - crypto "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
19 - peer "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
20 - offline "gx/ipfs/QmSNe4MWVxZWk6UxxW2z2EKofFo4GdFzud1vfn1iVby3mj/go-ipfs-routing/offline"
17 + "gx/ipfs/QmPvyPwuCgJ7pDmrKDxRtsScJgBaM5h4EpRL2qQJsmXf4n/go-libp2p-crypto"
18 + "gx/ipfs/QmQsErDt8Qgw1XrsXf2BpEzDgGWtB1YLsTAARBup5b6B9W/go-libp2p-peer"
19 + "gx/ipfs/QmSNe4MWVxZWk6UxxW2z2EKofFo4GdFzud1vfn1iVby3mj/go-ipfs-routing/offline"
20 )
21
22 type NameAPI CoreAPI
@@ -119,13 +118,7 @@ func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.Nam
118 name = "/ipns/" + name
119 }
120
122 - ropts := []nsopts.ResolveOpt{
123 - nsopts.Depth(uint(options.Depth)),
124 - nsopts.DhtRecordCount(uint(options.DhtRecordCount)),
125 - nsopts.DhtTimeout(options.DhtTimeout),
126 - }
127 -
128 - output, err := resolver.Resolve(ctx, name, ropts...)
121 + output, err := resolver.Resolve(ctx, name, options.ResolveOpts...)
122 if err != nil {
123 return nil, err
124 }