Skip to content

Commit

Permalink
coreapi: update for always loaded privkey
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
  • Loading branch information
magik6k committed Dec 17, 2018
1 parent d3a6941 commit 6de9b03
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 50 deletions.
2 changes: 1 addition & 1 deletion core/commands/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ var CatCmd = &cmds.Command{
cmdkit.Int64Option(lengthOptionName, "l", "Maximum number of bytes to read."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion core/commands/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Resolve the value of an IPFS DAG path:
cmdkit.StringOption(resolveDhtTimeoutOptionName, "dhtt", "Max time to collect values during DHT resolution eg \"30s\". Pass 0 for no timeout."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
api, err := cmdenv.GetApi(env)
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
Expand Down
59 changes: 23 additions & 36 deletions core/coreapi/coreapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"context"
"errors"
"fmt"

"github.com/ipfs/go-ipfs/core"
coreiface "github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
Expand All @@ -26,14 +25,14 @@ import (
"github.com/ipfs/go-ipfs/repo"

ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
exchange "gx/ipfs/QmP2g3VxmC7g7fyRJDj1VJ72KHZbJ9UW24YjSWEj1XTb4H/go-ipfs-exchange-interface"
"gx/ipfs/QmP2g3VxmC7g7fyRJDj1VJ72KHZbJ9UW24YjSWEj1XTb4H/go-ipfs-exchange-interface"
bserv "gx/ipfs/QmPoh3SrQzFBWtdGK6qmHDV4EanKR6kYPj4DD3J2NLoEmZ/go-blockservice"
routing "gx/ipfs/QmRASJXJUFygM5qU4YrH7k7jD6S4Hg8nJmgqJ4bYJvLatd/go-libp2p-routing"
blockstore "gx/ipfs/QmS2aqUZLJp8kF1ihE5rvDGE5LvmKDPnx32w9Z1BW9xLV5/go-ipfs-blockstore"
peer "gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
"gx/ipfs/QmRASJXJUFygM5qU4YrH7k7jD6S4Hg8nJmgqJ4bYJvLatd/go-libp2p-routing"
"gx/ipfs/QmS2aqUZLJp8kF1ihE5rvDGE5LvmKDPnx32w9Z1BW9xLV5/go-ipfs-blockstore"
"gx/ipfs/QmY5Grm8pJdiSSVsYxx4uNRgweY72EmYwuSDbRnbFok3iY/go-libp2p-peer"
offlinexch "gx/ipfs/QmYZwey1thDTynSrvd6qQkX24UpTka6TFhQ2v569UpoqxD/go-ipfs-exchange-offline"
pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
pubsub "gx/ipfs/QmaTfHazBrintpyALv8MzmCvGyGg3XWY7vDrsVfGVnpd1j/go-libp2p-pubsub"
"gx/ipfs/QmaTfHazBrintpyALv8MzmCvGyGg3XWY7vDrsVfGVnpd1j/go-libp2p-pubsub"
ipld "gx/ipfs/QmcKKBwfz6FyQdHR2jsXrrF6XeSBXYL86anmWNewpFpoF5/go-ipld-format"
logging "gx/ipfs/QmcuXC5cxs79ro2cUuHs4HQ2bkDLJUYokwL8aivcX6HW3C/go-log"
dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
Expand Down Expand Up @@ -64,13 +63,14 @@ type CoreAPI struct {
exchange exchange.Interface

namesys namesys.NameSystem
routing func(bool) (routing.IpfsRouting, error)
routing routing.IpfsRouting

pubSub *pubsub.PubSub

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

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

pubSub: n.PubSub,

nd: n,
parentOpts: settings,
}

subApi.routing = func(allowOffline bool) (routing.IpfsRouting, error) {
if !n.OnlineMode() {
if !allowOffline {
return nil, coreiface.ErrOffline
}
if err := n.SetupOfflineRouting(); err != nil {
return nil, err
}
subApi.privateKey = n.PrivateKey
subApi.namesys = n.Namesys
return n.Routing, nil
}
if !settings.Offline {
return n.Routing, nil
subApi.isOnline = func(allowOffline bool) error {
if !n.OnlineMode() && !allowOffline {
return coreiface.ErrOffline
}
if !allowOffline {
return nil, coreiface.ErrOffline
return nil
}

subApi.isPublishAllowed = func() error {
if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
return errors.New("cannot manually publish while IPNS is mounted")
}
return nil
}

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

offroute := offlineroute.NewOfflineRouter(subApi.repo.Datastore(), subApi.recordValidator)
subApi.namesys = namesys.NewNameSystem(offroute, subApi.repo.Datastore(), cs)
subApi.routing = offlineroute.NewOfflineRouter(subApi.repo.Datastore(), subApi.recordValidator)
subApi.namesys = namesys.NewNameSystem(subApi.routing, subApi.repo.Datastore(), cs)

return offroute, nil
}

subApi.isPublishAllowed = func() error {
if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
return errors.New("cannot manually publish while IPNS is mounted")
}
return nil
}

if settings.Offline {
subApi.peerstore = nil
subApi.peerHost = nil
subApi.namesys = nil
Expand All @@ -231,6 +217,7 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
subApi.exchange = offlinexch.Exchange(subApi.blockstore)
subApi.blocks = bserv.New(api.blockstore, subApi.exchange)
subApi.dag = dag.NewDAGService(subApi.blocks)

}

return subApi, nil
Expand Down
14 changes: 7 additions & 7 deletions core/coreapi/dht.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (
type DhtAPI CoreAPI

func (api *DhtAPI) FindPeer(ctx context.Context, p peer.ID) (pstore.PeerInfo, error) {
r, err := api.routing(false)
err := api.isOnline(false)
if err != nil {
return pstore.PeerInfo{}, err
}

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

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

pchan := r.FindProvidersAsync(ctx, rp.Cid(), numProviders)
pchan := api.routing.FindProvidersAsync(ctx, rp.Cid(), numProviders)
return pchan, nil
}

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

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

if settings.Recursive {
err = provideKeysRec(ctx, r, api.blockstore, []cid.Cid{c})
err = provideKeysRec(ctx, api.routing, api.blockstore, []cid.Cid{c})
} else {
err = provideKeys(ctx, r, []cid.Cid{c})
err = provideKeys(ctx, api.routing, []cid.Cid{c})
}
if err != nil {
return err
Expand Down
6 changes: 3 additions & 3 deletions core/coreapi/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
return nil, err
}

_, err = api.routing(options.AllowOffline)
err = api.isOnline(options.AllowOffline)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -87,15 +87,15 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
return nil, err
}

r, err := api.routing(true)
err = api.isOnline(true)
if err != nil {
return nil, err
}

var resolver namesys.Resolver = api.namesys

if !options.Cache {
resolver = namesys.NewNameSystem(r, api.repo.Datastore(), 0)
resolver = namesys.NewNameSystem(api.routing, api.repo.Datastore(), 0)
}

if !strings.HasPrefix(name, "/ipns/") {
Expand Down
4 changes: 2 additions & 2 deletions core/coreapi/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ func (api *PubSubAPI) checkNode() (routing.IpfsRouting, error) {
return nil, errors.New("experimental pubsub feature not enabled. Run daemon with --enable-pubsub-experiment to use.")
}

r, err := api.routing(false)
err := api.isOnline(false)
if err != nil {
return nil, err
}

return r, nil
return api.routing, nil
}

func (sub *pubSubSubscription) Close() error {
Expand Down

0 comments on commit 6de9b03

Please sign in to comment.