@@ -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
0 commit comments