@cryptotaxi247 / kubo / commits / 3dac4609a

split 'mode' into IsOnline and IsDaemon flags

1. They don't _have_ to be mutually exclusive. 2. local, mode, etc is _really_ confusing. License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>

Steven Allen committed Mar 4, 2019 at 18:59 UTC 3dac4609a86996fe43215b72bb62c657befc6b11
16 files changed +25 -60
cmd/ipfs/daemon.go
+2 -2
@@ -332,7 +332,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
332 log.Error("error from node construction: ", err)
333 return err
334 }
335 - node.SetLocal(false)
335 + node.IsDaemon = true
336
337 if node.PNetFingerprint != nil {
338 fmt.Println("Swarm is limited to private network of peers with the swarm key")
@@ -517,7 +517,7 @@ func serveHTTPApi(req *cmds.Request, cctx *oldcmds.Context) (<-chan error, error
517
518 // printSwarmAddrs prints the addresses of the host
519 func printSwarmAddrs(node *core.IpfsNode) {
520 - if !node.OnlineMode() {
520 + if !node.IsOnline {
521 fmt.Println("Swarm not listening, running in offline mode.")
522 return
523 }
cmd/ipfs/main.go
-1
@@ -171,7 +171,6 @@ func mainRet() int {
171 return nil, err
172 }
173
174 - n.SetLocal(true)
174 return n, nil
175 },
176 }, nil
core/builder.go
+1 -5
@@ -142,7 +142,7 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
142 ctx = metrics.CtxScope(ctx, "ipfs")
143
144 n := &IpfsNode{
145 - mode: offlineMode,
145 + IsOnline: cfg.Online,
146 Repo: cfg.Repo,
147 ctx: ctx,
148 Peerstore: pstoremem.NewPeerstore(),
@@ -153,10 +153,6 @@ func NewNode(ctx context.Context, cfg *BuildCfg) (*IpfsNode, error) {
153 "ipns": ipns.Validator{KeyBook: n.Peerstore},
154 }
155
156 - if cfg.Online {
157 - n.mode = onlineMode
158 - }
159 -
156 // TODO: this is a weird circular-ish dependency, rework it
157 n.proc = goprocessctx.WithContextAndTeardown(ctx, n.teardown)
158
core/commands/bitswap.go
+4 -4
@@ -50,7 +50,7 @@ Print out all blocks currently on the bitswap wantlist for the local peer.`,
50 return err
51 }
52
53 - if !nd.OnlineMode() {
53 + if !nd.IsOnline {
54 return ErrNotOnline
55 }
56
@@ -100,7 +100,7 @@ var bitswapStatCmd = &cmds.Command{
100 return err
101 }
102
103 - if !nd.OnlineMode() {
103 + if !nd.IsOnline {
104 return cmdkit.Errorf(cmdkit.ErrClient, ErrNotOnline.Error())
105 }
106
@@ -163,7 +163,7 @@ prints the ledger associated with a given peer.
163 return err
164 }
165
166 - if !nd.OnlineMode() {
166 + if !nd.IsOnline {
167 return ErrNotOnline
168 }
169
@@ -206,7 +206,7 @@ Trigger reprovider to announce our data to network.
206 return err
207 }
208
209 - if !nd.OnlineMode() {
209 + if !nd.IsOnline {
210 return ErrNotOnline
211 }
212
core/commands/dht.go
+5 -5
@@ -141,7 +141,7 @@ var findProvidersDhtCmd = &cmds.Command{
141 return err
142 }
143
144 - if !n.OnlineMode() {
144 + if !n.IsOnline {
145 return ErrNotOnline
146 }
147
@@ -232,7 +232,7 @@ var provideRefDhtCmd = &cmds.Command{
232 return err
233 }
234
235 - if !nd.OnlineMode() {
235 + if !nd.IsOnline {
236 return ErrNotOnline
237 }
238
@@ -361,7 +361,7 @@ var findPeerDhtCmd = &cmds.Command{
361 return err
362 }
363
364 - if !nd.OnlineMode() {
364 + if !nd.IsOnline {
365 return ErrNotOnline
366 }
367
@@ -445,7 +445,7 @@ Different key types can specify other 'best' rules.
445 return err
446 }
447
448 - if !nd.OnlineMode() {
448 + if !nd.IsOnline {
449 return ErrNotOnline
450 }
451
@@ -539,7 +539,7 @@ NOTE: A value may not exceed 2048 bytes.
539 return err
540 }
541
542 - if !nd.OnlineMode() {
542 + if !nd.IsOnline {
543 return ErrNotOnline
544 }
545
core/commands/id.go
+1 -1
@@ -92,7 +92,7 @@ EXAMPLE:
92 }
93
94 // TODO handle offline mode with polymorphism instead of conditionals
95 - if !n.OnlineMode() {
95 + if !n.IsOnline {
96 return errors.New(offlineIdErrorMessage)
97 }
98
core/commands/mount_unix.go
+1 -1
@@ -92,7 +92,7 @@ baz
92 }
93
94 // error if we aren't running node in online mode
95 - if nd.LocalMode() {
95 + if !nd.IsOnline {
96 return ErrNotOnline
97 }
98
core/commands/p2p.go
+1 -1
@@ -535,7 +535,7 @@ func p2pGetNode(env cmds.Environment) (*core.IpfsNode, error) {
535 return nil, errors.New("libp2p stream mounting not enabled")
536 }
537
538 - if !nd.OnlineMode() {
538 + if !nd.IsOnline {
539 return nil, ErrNotOnline
540 }
541
core/commands/ping.go
+1 -1
@@ -56,7 +56,7 @@ trip latency information.
56 }
57
58 // Must be online!
59 - if !n.OnlineMode() {
59 + if !n.IsOnline {
60 return ErrNotOnline
61 }
62
core/commands/shutdown.go
+1 -1
@@ -17,7 +17,7 @@ var daemonShutdownCmd = &cmds.Command{
17 return err
18 }
19
20 - if nd.LocalMode() {
20 + if !nd.IsDaemon {
21 return cmdkit.Errorf(cmdkit.ErrClient, "daemon not running")
22 }
23
core/commands/stat.go
+1 -1
@@ -94,7 +94,7 @@ Example:
94 }
95
96 // Must be online!
97 - if !nd.OnlineMode() {
97 + if !nd.IsOnline {
98 return cmdkit.Errorf(cmdkit.ErrClient, ErrNotOnline.Error())
99 }
100
core/commands/sysdiag.go
+1 -1
@@ -47,7 +47,7 @@ Prints out information about your computer to aid in easier debugging.
47 return err
48 }
49
50 - err = netInfo(nd.OnlineMode(), info)
50 + err = netInfo(nd.IsOnline, info)
51 if err != nil {
52 return err
53 }
core/core.go
+3 -33
@@ -86,15 +86,6 @@ const DefaultIpnsCacheSize = 128
86
87 var log = logging.Logger("core")
88
89 -type mode int
90 -
91 -const (
92 - // zero value is not a valid mode, must be explicitly set
93 - localMode mode = iota
94 - offlineMode
95 - onlineMode
96 -)
97 -
89 func init() {
90 identify.ClientVersion = "go-ipfs/" + version.CurrentVersionNumber + "/" + version.CurrentCommit
91 }
@@ -145,8 +136,9 @@ type IpfsNode struct {
136 proc goprocess.Process
137 ctx context.Context
138
148 - mode mode
149 - localModeSet bool
139 + // Flags
140 + IsOnline bool // Online is set when networking is enabled.
141 + IsDaemon bool // Daemon is set when running on a long-running daemon.
142 }
143
144 // Mounts defines what the node's mount state is. This should
@@ -721,28 +713,6 @@ func (n *IpfsNode) teardown() error {
713 return nil
714 }
715
724 -// OnlineMode returns whether or not the IpfsNode is in OnlineMode.
725 -func (n *IpfsNode) OnlineMode() bool {
726 - return n.mode == onlineMode
727 -}
728 -
729 -// SetLocal will set the IpfsNode to local mode
730 -func (n *IpfsNode) SetLocal(isLocal bool) {
731 - if isLocal {
732 - n.mode = localMode
733 - }
734 - n.localModeSet = true
735 -}
736 -
737 -// LocalMode returns whether or not the IpfsNode is in LocalMode
738 -func (n *IpfsNode) LocalMode() bool {
739 - if !n.localModeSet {
740 - // programmer error should not happen
741 - panic("local mode not set")
742 - }
743 - return n.mode == localMode
744 -}
745 -
716 // Bootstrap will set and call the IpfsNodes bootstrap function.
717 func (n *IpfsNode) Bootstrap(cfg BootstrapConfig) error {
718 // TODO what should return value be when in offlineMode?
core/coreapi/coreapi.go
+1 -1
@@ -181,7 +181,7 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
181 }
182
183 subApi.checkOnline = func(allowOffline bool) error {
184 - if !n.OnlineMode() && !allowOffline {
184 + if !n.IsOnline && !allowOffline {
185 return coreiface.ErrOffline
186 }
187 return nil
core/corehttp/gateway_handler.go
+1 -1
@@ -155,7 +155,7 @@ func (i *gatewayHandler) getOrHeadHandler(ctx context.Context, w http.ResponseWr
155
156 // Resolve path to the final DAG node for the ETag
157 resolvedPath, err := i.api.ResolvePath(ctx, parsedPath)
158 - if err == coreiface.ErrOffline && !i.node.OnlineMode() {
158 + if err == coreiface.ErrOffline && !i.node.IsOnline {
159 webError(w, "ipfs resolve -r "+escapedURLPath, err, http.StatusServiceUnavailable)
160 return
161 } else if err != nil {
fuse/node/mount_unix.go
+1 -1
@@ -75,7 +75,7 @@ func doMount(node *core.IpfsNode, fsdir, nsdir string) error {
75 fsmount, err1 = rofs.Mount(node, fsdir)
76 }()
77
78 - if node.OnlineMode() {
78 + if node.IsOnline {
79 wg.Add(1)
80 go func() {
81 defer wg.Done()