@cryptotaxi247 / kubo / commits / f915730e6

resolve cmd: use coreapi

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

Łukasz Magiera committed Sep 18, 2018 at 04:30 UTC f915730e6832e26120f8e767d9319f7fe6724577
5 files changed +77 -52
core/commands/resolve.go
+26 -18
@@ -7,18 +7,16 @@ import (
7 "strings"
8 "time"
9
10 - "github.com/ipfs/go-ipfs/core"
10 cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
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"
15 ns "github.com/ipfs/go-ipfs/namesys"
15 - nsopts "github.com/ipfs/go-ipfs/namesys/opts"
16 path "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
17
18 - uio "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/io"
18 "gx/ipfs/QmPTfgFTo9PFr1PvPKyKoeMgBvYPh6cX3aDP7DHKVbnCbi/go-ipfs-cmds"
19 "gx/ipfs/QmSP88ryZkHSRn1fnngAaV2Vcn63WUJzAavnRM9CVdU1Ky/go-ipfs-cmdkit"
21 - resolver "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path/resolver"
20 )
21
22 var ResolveCmd = &cmds.Command{
@@ -66,16 +64,30 @@ Resolve the value of an IPFS DAG path:
64 },
65 Options: []cmdkit.Option{
66 cmdkit.BoolOption("recursive", "r", "Resolve until the result is an IPFS name."),
69 - cmdkit.UintOption("dht-record-count", "dhtrc", "Number of records to request for DHT resolution."),
67 + cmdkit.IntOption("dht-record-count", "dhtrc", "Number of records to request for DHT resolution."),
68 cmdkit.StringOption("dht-timeout", "dhtt", "Max time to collect values during DHT resolution eg \"30s\". Pass 0 for no timeout."),
69 },
70 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
71 + api, err := cmdenv.GetApi(env)
72 + if err != nil {
73 + res.SetError(err, cmdkit.ErrNormal)
74 + return
75 + }
76 +
77 n, err := cmdenv.GetNode(env)
78 if err != nil {
79 res.SetError(err, cmdkit.ErrNormal)
80 return
81 }
82
83 + if !n.OnlineMode() {
84 + err := n.SetupOfflineRouting()
85 + if err != nil {
86 + res.SetError(err, cmdkit.ErrNormal)
87 + return
88 + }
89 + }
90 +
91 name := req.Arguments[0]
92 recursive, _ := req.Options["recursive"].(bool)
93
@@ -83,9 +95,10 @@ Resolve the value of an IPFS DAG path:
95 if strings.HasPrefix(name, "/ipns/") && !recursive {
96 rc, rcok := req.Options["dht-record-count"].(int)
97 dhtt, dhttok := req.Options["dht-timeout"].(string)
86 - ropts := []nsopts.ResolveOpt{nsopts.Depth(1)}
98 + ropts := []options.NameResolveOption{options.Name.Depth(1)}
99 +
100 if rcok {
88 - ropts = append(ropts, nsopts.DhtRecordCount(uint(rc)))
101 + ropts = append(ropts, options.Name.DhtRecordCount(rc))
102 }
103 if dhttok {
104 d, err := time.ParseDuration(dhtt)
@@ -97,37 +110,32 @@ Resolve the value of an IPFS DAG path:
110 res.SetError(errors.New("DHT timeout value must be >= 0"), cmdkit.ErrNormal)
111 return
112 }
100 - ropts = append(ropts, nsopts.DhtTimeout(d))
113 + ropts = append(ropts, options.Name.DhtTimeout(d))
114 }
102 - p, err := n.Namesys.Resolve(req.Context, name, ropts...)
115 + p, err := api.Name().Resolve(req.Context, name, ropts...)
116 // ErrResolveRecursion is fine
117 if err != nil && err != ns.ErrResolveRecursion {
118 res.SetError(err, cmdkit.ErrNormal)
119 return
120 }
108 - cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: p})
121 + cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: path.Path(p.String())})
122 return
123 }
124
125 // else, ipfs path or ipns with recursive flag
113 - p, err := path.ParsePath(name)
126 + p, err := coreiface.ParsePath(name)
127 if err != nil {
128 res.SetError(err, cmdkit.ErrNormal)
129 return
130 }
131
119 - r := &resolver.Resolver{
120 - DAG: n.DAG,
121 - ResolveOnce: uio.ResolveUnixfsOnce,
122 - }
123 -
124 - node, err := core.Resolve(req.Context, n.Namesys, r, p)
132 + rp, err := api.ResolvePath(req.Context, p)
133 if err != nil {
134 res.SetError(err, cmdkit.ErrNormal)
135 return
136 }
137
130 - c := node.Cid()
138 + c := rp.Cid()
139
140 cmds.EmitOnce(res, &ncmd.ResolvedPath{Path: path.FromCid(c)})
141 },
core/coreapi/interface/options/name.go
+34 -9
@@ -14,9 +14,12 @@ type NamePublishSettings struct {
14 }
15
16 type NameResolveSettings struct {
17 - Recursive bool
18 - Local bool
19 - Cache bool
17 + Depth int
18 + Local bool
19 + Cache bool
20 +
21 + DhtRecordCount int
22 + DhtTimeout time.Duration
23 }
24
25 type NamePublishOption func(*NamePublishSettings) error
@@ -40,9 +43,12 @@ func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error)
43
44 func NameResolveOptions(opts ...NameResolveOption) (*NameResolveSettings, error) {
45 options := &NameResolveSettings{
43 - Recursive: false,
44 - Local: false,
45 - Cache: true,
46 + Depth: 1,
47 + Local: false,
48 + Cache: true,
49 +
50 + DhtRecordCount: 16,
51 + DhtTimeout: time.Minute,
52 }
53
54 for _, opt := range opts {
@@ -80,11 +86,11 @@ func (nameOpts) Key(key string) NamePublishOption {
86 }
87 }
88
83 -// Recursive is an option for Name.Resolve which specifies whether to perform a
89 +// Depth is an option for Name.Resolve which specifies the maximum depth of a
90 // recursive lookup. Default value is false
85 -func (nameOpts) Recursive(recursive bool) NameResolveOption {
91 +func (nameOpts) Depth(depth int) NameResolveOption {
92 return func(settings *NameResolveSettings) error {
87 - settings.Recursive = recursive
93 + settings.Depth = depth
94 return nil
95 }
96 }
@@ -106,3 +112,22 @@ func (nameOpts) Cache(cache bool) NameResolveOption {
112 return nil
113 }
114 }
115 +
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 {
129 + return func(settings *NameResolveSettings) error {
130 + settings.DhtTimeout = timeout
131 + return nil
132 + }
133 +}
core/coreapi/name.go
+4 -3
@@ -119,9 +119,10 @@ func (api *NameAPI) Resolve(ctx context.Context, name string, opts ...caopts.Nam
119 name = "/ipns/" + name
120 }
121
122 - var ropts []nsopts.ResolveOpt
123 - if !options.Recursive {
124 - ropts = append(ropts, nsopts.Depth(1))
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...)
core/coreapi/path.go
+12 -21
@@ -1,53 +1,44 @@
1 package coreapi
2
3 import (
4 - context "context"
5 - fmt "fmt"
4 + "context"
5 + "fmt"
6 gopath "path"
7
8 - core "github.com/ipfs/go-ipfs/core"
8 + "github.com/ipfs/go-ipfs/core"
9 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
10 - namesys "github.com/ipfs/go-ipfs/namesys"
10 uio "gx/ipfs/QmPL8bYtbACcSFFiSr4s2du7Na382NxRADR8hC7D9FkEA2/go-unixfs/io"
11 ipfspath "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path"
13 - resolver "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path/resolver"
12 + "gx/ipfs/QmX7uSbkNz76yNwBhuwYwRbhihLnJqM73VTCjS3UMJud9A/go-path/resolver"
13
15 - cid "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
14 + "gx/ipfs/QmPSQnBKM9g7BaUcZCvswUJVscQ1ipjmwxN5PXCjkp9EQ7/go-cid"
15 ipld "gx/ipfs/QmdDXJs4axxefSPgK6Y1QhpJWKuDPnGJiqgq4uncb4rFHL/go-ipld-format"
16 )
17
18 // ResolveNode resolves the path `p` using Unixfs resolver, gets and returns the
19 // resolved Node.
20 func (api *CoreAPI) ResolveNode(ctx context.Context, p coreiface.Path) (ipld.Node, error) {
22 - return resolveNode(ctx, api.node.DAG, api.node.Namesys, p)
23 -}
24 -
25 -// ResolvePath resolves the path `p` using Unixfs resolver, returns the
26 -// resolved path.
27 -func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreiface.ResolvedPath, error) {
28 - return resolvePath(ctx, api.node.DAG, api.node.Namesys, p)
29 -}
30 -
31 -func resolveNode(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (ipld.Node, error) {
32 - rp, err := resolvePath(ctx, ng, nsys, p)
21 + rp, err := api.ResolvePath(ctx, p)
22 if err != nil {
23 return nil, err
24 }
25
37 - node, err := ng.Get(ctx, rp.Cid())
26 + node, err := api.node.DAG.Get(ctx, rp.Cid())
27 if err != nil {
28 return nil, err
29 }
30 return node, nil
31 }
32
44 -func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSystem, p coreiface.Path) (coreiface.ResolvedPath, error) {
33 +// ResolvePath resolves the path `p` using Unixfs resolver, returns the
34 +// resolved path.
35 +func (api *CoreAPI) ResolvePath(ctx context.Context, p coreiface.Path) (coreiface.ResolvedPath, error) {
36 if _, ok := p.(coreiface.ResolvedPath); ok {
37 return p.(coreiface.ResolvedPath), nil
38 }
39
40 ipath := ipfspath.Path(p.String())
50 - ipath, err := core.ResolveIPNS(ctx, nsys, ipath)
41 + ipath, err := core.ResolveIPNS(ctx, api.node.Namesys, ipath)
42 if err == core.ErrNoNamesys {
43 return nil, coreiface.ErrOffline
44 } else if err != nil {
@@ -66,7 +57,7 @@ func resolvePath(ctx context.Context, ng ipld.NodeGetter, nsys namesys.NameSyste
57 }
58
59 r := &resolver.Resolver{
69 - DAG: ng,
60 + DAG: api.node.DAG,
61 ResolveOnce: resolveOnce,
62 }
63
core/coreapi/unixfs.go
+1 -1
@@ -32,7 +32,7 @@ func (api *UnixfsAPI) Add(ctx context.Context, r io.Reader) (coreiface.ResolvedP
32 func (api *UnixfsAPI) Cat(ctx context.Context, p coreiface.Path) (coreiface.Reader, error) {
33 dget := api.node.DAG // TODO: use a session here once routing perf issues are resolved
34
35 - dagnode, err := resolveNode(ctx, dget, api.node.Namesys, p)
35 + dagnode, err := api.core().ResolveNode(ctx, p)
36 if err != nil {
37 return nil, err
38 }