Skip to content

Commit 4eccfa9

Browse files
committed
coreapi: update for always loaded privkey
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
1 parent d3a6941 commit 4eccfa9

File tree

6 files changed

+37
-50
lines changed

6 files changed

+37
-50
lines changed

core/commands/cat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ var CatCmd = &cmds.Command{
3333
cmdkit.Int64Option(lengthOptionName, "l", "Maximum number of bytes to read."),
3434
},
3535
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
36-
api, err := cmdenv.GetApi(env)
36+
api, err := cmdenv.GetApi(env, req)
3737
if err != nil {
3838
return err
3939
}

core/commands/resolve.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ Resolve the value of an IPFS DAG path:
7474
cmdkit.StringOption(resolveDhtTimeoutOptionName, "dhtt", "Max time to collect values during DHT resolution eg \"30s\". Pass 0 for no timeout."),
7575
},
7676
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)
7878
if err != nil {
7979
return err
8080
}

core/coreapi/coreapi.go

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import (
1717
"context"
1818
"errors"
1919
"fmt"
20-
2120
"github.com/ipfs/go-ipfs/core"
2221
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
2322
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
@@ -26,14 +25,14 @@ import (
2625
"github.com/ipfs/go-ipfs/repo"
2726

2827
ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
29-
exchange "gx/ipfs/QmP2g3VxmC7g7fyRJDj1VJ72KHZbJ9UW24YjSWEj1XTb4H/go-ipfs-exchange-interface"
28+
"gx/ipfs/QmP2g3VxmC7g7fyRJDj1VJ72KHZbJ9UW24YjSWEj1XTb4H/go-ipfs-exchange-interface"
3029
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"
3433
offlinexch "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
3534
pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
36-
pubsub "gx/ipfs/QmaTfHazBrintpyALv8MzmCvGyGg3XWY7vDrsVfGVnpd1j/go-libp2p-pubsub"
35+
"gx/ipfs/QmaTfHazBrintpyALv8MzmCvGyGg3XWY7vDrsVfGVnpd1j/go-libp2p-pubsub"
3736
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
3837
logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
3938
dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
@@ -64,13 +63,14 @@ type CoreAPI struct {
6463
exchange exchange.Interface
6564

6665
namesys namesys.NameSystem
67-
routing func(bool) (routing.IpfsRouting, error)
66+
routing routing.IpfsRouting
6867

6968
pubSub *pubsub.PubSub
7069

7170
// TODO: this can be generalized to all functions when we implement some
7271
// api based security mechanism
7372
isPublishAllowed func() error
73+
isOnline func(allowOffline bool) error
7474

7575
// ONLY for re-applying options in WithOptions, DO NOT USE ANYWHERE ELSE
7676
nd *core.IpfsNode
@@ -170,32 +170,29 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
170170
namesys: n.Namesys,
171171
recordValidator: n.RecordValidator,
172172
exchange: n.Exchange,
173+
routing: n.Routing,
173174

174175
pubSub: n.PubSub,
175176

176177
nd: n,
177178
parentOpts: settings,
178179
}
179180

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
194184
}
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")
197191
}
192+
return nil
193+
}
198194

195+
if settings.Offline {
199196
cfg, err := n.Repo.Config()
200197
if err != nil {
201198
return nil, err
@@ -209,20 +206,9 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
209206
return nil, fmt.Errorf("cannot specify negative resolve cache size")
210207
}
211208

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)
214211

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 {
226212
subApi.peerstore = nil
227213
subApi.peerHost = nil
228214
subApi.namesys = nil
@@ -231,6 +217,7 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
231217
subApi.exchange = offlinexch.Exchange(subApi.blockstore)
232218
subApi.blocks = bserv.New(api.blockstore, subApi.exchange)
233219
subApi.dag = dag.NewDAGService(subApi.blocks)
220+
234221
}
235222

236223
return subApi, nil

core/coreapi/dht.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ import (
2121
type DhtAPI CoreAPI
2222

2323
func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
24-
r, err := api.routing(false)
24+
err := api.isOnline(false)
2525
if err != nil {
2626
return pstore.PeerInfo{}, err
2727
}
2828

29-
pi, err := r.FindPeer(ctx, peer.ID(p))
29+
pi, err := api.routing.FindPeer(ctx, peer.ID(p))
3030
if err != nil {
3131
return pstore.PeerInfo{}, err
3232
}
@@ -40,7 +40,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ...
4040
return nil, err
4141
}
4242

43-
r, err := api.routing(false)
43+
err = api.isOnline(false)
4444
if err != nil {
4545
return nil, err
4646
}
@@ -55,7 +55,7 @@ func (api *DhtAPI) FindProviders(ctx context.Context, p coreiface.Path, opts ...
5555
return nil, fmt.Errorf("number of providers must be greater than 0")
5656
}
5757

58-
pchan := r.FindProvidersAsync(ctx, rp.Cid(), numProviders)
58+
pchan := api.routing.FindProvidersAsync(ctx, rp.Cid(), numProviders)
5959
return pchan, nil
6060
}
6161

@@ -65,7 +65,7 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao
6565
return err
6666
}
6767

68-
r, err := api.routing(false)
68+
err = api.isOnline(false)
6969
if err != nil {
7070
return err
7171
}
@@ -87,9 +87,9 @@ func (api *DhtAPI) Provide(ctx context.Context, path coreiface.Path, opts ...cao
8787
}
8888

8989
if settings.Recursive {
90-
err = provideKeysRec(ctx, r, api.blockstore, []cid.Cid{c})
90+
err = provideKeysRec(ctx, api.routing, api.blockstore, []cid.Cid{c})
9191
} else {
92-
err = provideKeys(ctx, r, []cid.Cid{c})
92+
err = provideKeys(ctx, api.routing, []cid.Cid{c})
9393
}
9494
if err != nil {
9595
return err

core/coreapi/name.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
4545
return nil, err
4646
}
4747

48-
_, err = api.routing(options.AllowOffline)
48+
err = api.isOnline(options.AllowOffline)
4949
if err != nil {
5050
return nil, err
5151
}
@@ -87,15 +87,15 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
8787
return nil, err
8888
}
8989

90-
r, err := api.routing(true)
90+
err = api.isOnline(true)
9191
if err != nil {
9292
return nil, err
9393
}
9494

9595
var resolver namesys.Resolver = api.namesys
9696

9797
if !options.Cache {
98-
resolver = namesys.NewNameSystem(r, api.repo.Datastore(), 0)
98+
resolver = namesys.NewNameSystem(api.routing, api.repo.Datastore(), 0)
9999
}
100100

101101
if !strings.HasPrefix(name, "/ipns/") {

core/coreapi/pubsub.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,12 +127,12 @@ func (api *PubSubAPI) checkNode() (routing.IpfsRouting, error) {
127127
return nil, errors.New("experimental pubsub feature not enabled. Run daemon with --enable-pubsub-experiment to use.")
128128
}
129129

130-
r, err := api.routing(false)
130+
err := api.isOnline(false)
131131
if err != nil {
132132
return nil, err
133133
}
134134

135-
return r, nil
135+
return api.routing, nil
136136
}
137137

138138
func (sub *pubSubSubscription) Close() error {

0 commit comments

Comments
 (0)