@cryptotaxi247 / kubo / commits / 190728f04

coreapi: update for always loaded privkey

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

Łukasz Magiera committed Dec 17, 2018 at 09:14 UTC 190728f04a13860f2275b116187873ef87180e64
6 files changed +36 -49
core/commands/cat.go
+1 -1
@@ -34,7 +34,7 @@ var CatCmd = &cmds.Command{
34 cmdkit.Int64Option(lengthOptionName, "l", "Maximum number of bytes to read."),
35 },
36 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
37 - api, err := cmdenv.GetApi(env)
37 + api, err := cmdenv.GetApi(env, req)
38 if err != nil {
39 return err
40 }
core/commands/resolve.go
+1 -1
@@ -74,7 +74,7 @@ Resolve the value of an IPFS DAG path:
74 cmdkit.StringOption(resolveDhtTimeoutOptionName, "dhtt", "Max time to collect values during DHT resolution eg \"30s\". Pass 0 for no timeout."),
75 },
76 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
77 - api, err := cmdenv.GetApi(env)
77 + api, err := cmdenv.GetApi(env, req)
78 if err != nil {
79 return err
80 }
core/coreapi/coreapi.go
+22 -35
@@ -17,7 +17,6 @@ import (
17 "context"
18 "errors"
19 "fmt"
20 -
20 "github.com/ipfs/go-ipfs/core"
21 coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
22 "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
@@ -26,11 +25,11 @@ import (
25 "github.com/ipfs/go-ipfs/repo"
26
27 ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
29 - exchange "gx/ipfs/QmP2g3VxmC7g7fyRJDj1VJ72KHZbJ9UW24YjSWEj1XTb4H/go-ipfs-exchange-interface"
28 + "gx/ipfs/QmP2g3VxmC7g7fyRJDj1VJ72KHZbJ9UW24YjSWEj1XTb4H/go-ipfs-exchange-interface"
29 bserv "gx/ipfs/QmPoh3SrQzFBWtdGK6qmHDV4EanKR6kYPj4DD3J2NLoEmZ/go-blockservice"
31 - routing "gx/ipfs/QmRASJXJUFygM5qU4YrH7k7jD6S4Hg8nJmgqJ4bYJvLatd/go-libp2p-routing"
32 - blockstore "gx/ipfs/QmS2aqUZLJp8kF1ihE5rvDGE5LvmKDPnx32w9Z1BW9xLV5/go-ipfs-blockstore"
33 - peer "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
30 + "gx/ipfs/QmRASJXJUFygM5qU4YrH7k7jD6S4Hg8nJmgqJ4bYJvLatd/go-libp2p-routing"
31 + "gx/ipfs/QmS2aqUZLJp8kF1ihE5rvDGE5LvmKDPnx32w9Z1BW9xLV5/go-ipfs-blockstore"
32 + "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
33 offlinexch "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
34 pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
35 pubsub "gx/ipfs/QmaqGyUhWLsJbVo1QAujSu13mxNjFJ98Kt2VWGSnShGE1Q/go-libp2p-pubsub"
@@ -64,13 +63,14 @@ type CoreAPI struct {
63 exchange exchange.Interface
64
65 namesys namesys.NameSystem
67 - routing func(bool) (routing.IpfsRouting, error)
66 + routing routing.IpfsRouting
67
68 pubSub *pubsub.PubSub
69
70 // TODO: this can be generalized to all functions when we implement some
71 // api based security mechanism
72 isPublishAllowed func() error
73 + isOnline func(allowOffline bool) error
74
75 // ONLY for re-applying options in WithOptions, DO NOT USE ANYWHERE ELSE
76 nd *core.IpfsNode
@@ -170,6 +170,7 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
170 namesys: n.Namesys,
171 recordValidator: n.RecordValidator,
172 exchange: n.Exchange,
173 + routing: n.Routing,
174
175 pubSub: n.PubSub,
176
@@ -177,25 +178,21 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
178 parentOpts: settings,
179 }
180
180 - subApi.routing = func(allowOffline bool) (routing.IpfsRouting, error) {
181 - if !n.OnlineMode() {
182 - if !allowOffline {
183 - return nil, coreiface.ErrOffline
184 - }
185 - if err := n.SetupOfflineRouting(); err != nil {
186 - return nil, err
187 - }
188 - subApi.privateKey = n.PrivateKey
189 - subApi.namesys = n.Namesys
190 - return n.Routing, nil
191 - }
192 - if !settings.Offline {
193 - return n.Routing, nil
181 + subApi.isOnline = func(allowOffline bool) error {
182 + if !n.OnlineMode() && !allowOffline {
183 + return coreiface.ErrOffline
184 }
195 - if !allowOffline {
196 - return nil, coreiface.ErrOffline
185 + return nil
186 + }
187 +
188 + subApi.isPublishAllowed = func() error {
189 + if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
190 + return errors.New("cannot manually publish while IPNS is mounted")
191 }
192 + return nil
193 + }
194
195 + if settings.Offline {
196 cfg, err := n.Repo.Config()
197 if err != nil {
198 return nil, err
@@ -209,20 +206,9 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
206 return nil, fmt.Errorf("cannot specify negative resolve cache size")
207 }
208
212 - offroute := offlineroute.NewOfflineRouter(subApi.repo.Datastore(), subApi.recordValidator)
213 - subApi.namesys = namesys.NewNameSystem(offroute, subApi.repo.Datastore(), cs)
209 + subApi.routing = offlineroute.NewOfflineRouter(subApi.repo.Datastore(), subApi.recordValidator)
210 + subApi.namesys = namesys.NewNameSystem(subApi.routing, subApi.repo.Datastore(), cs)
211
215 - return offroute, nil
216 - }
217 -
218 - subApi.isPublishAllowed = func() error {
219 - if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
220 - return errors.New("cannot manually publish while IPNS is mounted")
221 - }
222 - return nil
223 - }
224 -
225 - if settings.Offline {
212 subApi.peerstore = nil
213 subApi.peerHost = nil
214 subApi.namesys = nil
@@ -231,6 +217,7 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
217 subApi.exchange = offlinexch.Exchange(subApi.blockstore)
218 subApi.blocks = bserv.New(api.blockstore, subApi.exchange)
219 subApi.dag = dag.NewDAGService(subApi.blocks)
220 +
221 }
222
223 return subApi, nil
core/coreapi/dht.go
+7 -7
@@ -21,12 +21,12 @@ import (
21 type DhtAPI CoreAPI
22
23 func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
24 - r, err := api.routing(false)
24 + err := api.isOnline(false)
25 if err != nil {
26 return pstore.PeerInfo{}, err
27 }
28
29 - pi, err := r.FindPeer(ctx, peer.ID(p))
29 + pi, err := api.routing.FindPeer(ctx, peer.ID(p))
30 if err != nil {
31 return pstore.PeerInfo{}, err
32 }
@@ -40,7 +40,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ...
40 return nil, err
41 }
42
43 - r, err := api.routing(false)
43 + err = api.isOnline(false)
44 if err != nil {
45 return nil, err
46 }
@@ -55,7 +55,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ...
55 return nil, fmt.Errorf("number of providers must be greater than 0")
56 }
57
58 - pchan := r.FindProvidersAsync(ctx, rp.Cid(), numProviders)
58 + pchan := api.routing.FindProvidersAsync(ctx, rp.Cid(), numProviders)
59 return pchan, nil
60 }
61
@@ -65,7 +65,7 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao
65 return err
66 }
67
68 - r, err := api.routing(false)
68 + err = api.isOnline(false)
69 if err != nil {
70 return err
71 }
@@ -87,9 +87,9 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao
87 }
88
89 if settings.Recursive {
90 - err = provideKeysRec(ctx, r, api.blockstore, []cid.Cid{c})
90 + err = provideKeysRec(ctx, api.routing, api.blockstore, []cid.Cid{c})
91 } else {
92 - err = provideKeys(ctx, r, []cid.Cid{c})
92 + err = provideKeys(ctx, api.routing, []cid.Cid{c})
93 }
94 if err != nil {
95 return err
core/coreapi/name.go
+3 -3
@@ -45,7 +45,7 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
45 return nil, err
46 }
47
48 - _, err = api.routing(options.AllowOffline)
48 + err = api.isOnline(options.AllowOffline)
49 if err != nil {
50 return nil, err
51 }
@@ -87,7 +87,7 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
87 return nil, err
88 }
89
90 - r, err := api.routing(true)
90 + err = api.isOnline(true)
91 if err != nil {
92 return nil, err
93 }
@@ -95,7 +95,7 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
95 var resolver namesys.Resolver = api.namesys
96
97 if !options.Cache {
98 - resolver = namesys.NewNameSystem(r, api.repo.Datastore(), 0)
98 + resolver = namesys.NewNameSystem(api.routing, api.repo.Datastore(), 0)
99 }
100
101 if !strings.HasPrefix(name, "/ipns/") {
core/coreapi/pubsub.go
+2 -2
@@ -127,12 +127,12 @@ func (api *PubSubAPI) checkNode() (routing.IpfsRouting, error) {
127 return nil, errors.New("experimental pubsub feature not enabled. Run daemon with --enable-pubsub-experiment to use.")
128 }
129
130 - r, err := api.routing(false)
130 + err := api.isOnline(false)
131 if err != nil {
132 return nil, err
133 }
134
135 - return r, nil
135 + return api.routing, nil
136 }
137
138 func (sub *pubSubSubscription) Close() error {