Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9502135

Browse files
committedAug 9, 2015
improve nodebuilder code to not depend on NewIPFSNode
License: MIT Signed-off-by: Jeromy <jeromyj@gmail.com>
1 parent d1366cd commit 9502135

File tree

10 files changed

+107
-99
lines changed

10 files changed

+107
-99
lines changed
 

‎cmd/ipfs/init.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ func addDefaultAssets(out io.Writer, repoRoot string) error {
157157
return err
158158
}
159159

160-
nd, err := core.NewIPFSNode(ctx, core.Offline(r))
160+
nd, err := core.NewNodeBuilder().Offline().SetRepo(r).Build(ctx)
161161
if err != nil {
162162
return err
163163
}
@@ -191,7 +191,7 @@ func initializeIpnsKeyspace(repoRoot string) error {
191191
return err
192192
}
193193

194-
nd, err := core.NewIPFSNode(ctx, core.Offline(r))
194+
nd, err := core.NewNodeBuilder().Offline().SetRepo(r).Build(ctx)
195195
if err != nil {
196196
return err
197197
}

‎cmd/ipfs/main.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ func (i *cmdInvocation) constructNodeFunc(ctx context.Context) func() (*core.Ipf
204204

205205
// ok everything is good. set it on the invocation (for ownership)
206206
// and return it.
207-
n, err := core.NewIPFSNode(ctx, core.Standard(r, cmdctx.Online))
207+
nb := core.NewNodeBuilder().SetRepo(r)
208+
if cmdctx.Online {
209+
nb.Online()
210+
}
211+
n, err := nb.Build(ctx)
208212
if err != nil {
209213
return nil, err
210214
}

‎cmd/ipfswatch/main.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ func run(ipfsPath, watchPath string) error {
7171
// TODO handle case: repo doesn't exist or isn't initialized
7272
return err
7373
}
74-
node, err := core.NewIPFSNode(context.Background(), core.Online(r))
74+
75+
node, err := core.NewNodeBuilder().Online().SetRepo(r).Build(context.Background())
7576
if err != nil {
7677
return err
7778
}

‎core/builder.go

+67-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,17 @@ import (
77

88
ds "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore"
99
dsync "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-datastore/sync"
10+
goprocessctx "github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/jbenet/goprocess/context"
1011
context "github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
12+
bstore "github.com/ipfs/go-ipfs/blocks/blockstore"
1113
key "github.com/ipfs/go-ipfs/blocks/key"
14+
bserv "github.com/ipfs/go-ipfs/blockservice"
15+
offline "github.com/ipfs/go-ipfs/exchange/offline"
16+
dag "github.com/ipfs/go-ipfs/merkledag"
1217
ci "github.com/ipfs/go-ipfs/p2p/crypto"
18+
peer "github.com/ipfs/go-ipfs/p2p/peer"
19+
path "github.com/ipfs/go-ipfs/path"
20+
pin "github.com/ipfs/go-ipfs/pin"
1321
repo "github.com/ipfs/go-ipfs/repo"
1422
cfg "github.com/ipfs/go-ipfs/repo/config"
1523
)
@@ -109,6 +117,63 @@ func (nb *NodeBuilder) Build(ctx context.Context) (*IpfsNode, error) {
109117
}
110118
nb.repo = r
111119
}
112-
conf := standardWithRouting(nb.repo, nb.online, nb.routing, nb.peerhost)
113-
return NewIPFSNode(ctx, conf)
120+
121+
n := &IpfsNode{
122+
mode: offlineMode,
123+
Repo: nb.repo,
124+
ctx: ctx,
125+
Peerstore: peer.NewPeerstore(),
126+
}
127+
if nb.online {
128+
n.mode = onlineMode
129+
}
130+
131+
// TODO: this is a weird circular-ish dependency, rework it
132+
n.proc = goprocessctx.WithContextAndTeardown(ctx, n.teardown)
133+
134+
success := false
135+
defer func() {
136+
if !success {
137+
n.teardown()
138+
}
139+
}()
140+
141+
// setup local peer ID (private key is loaded in online setup)
142+
if err := n.loadID(); err != nil {
143+
return nil, err
144+
}
145+
146+
var err error
147+
n.Blockstore, err = bstore.WriteCached(bstore.NewBlockstore(n.Repo.Datastore()), kSizeBlockstoreWriteCache)
148+
if err != nil {
149+
return nil, err
150+
}
151+
152+
if nb.online {
153+
do := setupDiscoveryOption(n.Repo.Config().Discovery)
154+
if err := n.startOnlineServices(ctx, nb.routing, nb.peerhost, do); err != nil {
155+
return nil, err
156+
}
157+
} else {
158+
n.Exchange = offline.Exchange(n.Blockstore)
159+
}
160+
161+
n.Blocks, err = bserv.New(n.Blockstore, n.Exchange)
162+
if err != nil {
163+
return nil, err
164+
}
165+
166+
n.DAG = dag.NewDAGService(n.Blocks)
167+
n.Pinning, err = pin.LoadPinner(n.Repo.Datastore(), n.DAG)
168+
if err != nil {
169+
// TODO: we should move towards only running 'NewPinner' explicity on
170+
// node init instead of implicitly here as a result of the pinner keys
171+
// not being found in the datastore.
172+
// this is kinda sketchy and could cause data loss
173+
n.Pinning = pin.NewPinner(n.Repo.Datastore(), n.DAG)
174+
}
175+
n.Resolver = &path.Resolver{DAG: n.DAG}
176+
177+
success = true
178+
return n, nil
114179
}

‎core/core.go

+7-75
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ import (
4646
exchange "github.com/ipfs/go-ipfs/exchange"
4747
bitswap "github.com/ipfs/go-ipfs/exchange/bitswap"
4848
bsnet "github.com/ipfs/go-ipfs/exchange/bitswap/network"
49-
offline "github.com/ipfs/go-ipfs/exchange/offline"
5049
rp "github.com/ipfs/go-ipfs/exchange/reprovide"
5150

5251
mount "github.com/ipfs/go-ipfs/fuse/mount"
@@ -124,7 +123,9 @@ type Mounts struct {
124123

125124
type ConfigOption func(ctx context.Context) (*IpfsNode, error)
126125

126+
// DEPRECATED
127127
func NewIPFSNode(ctx context.Context, option ConfigOption) (*IpfsNode, error) {
128+
log.Error("Using deprecated node construction method, please update to NodeBuilder")
128129
node, err := option(ctx)
129130
if err != nil {
130131
return nil, err
@@ -175,80 +176,8 @@ func NewIPFSNode(ctx context.Context, option ConfigOption) (*IpfsNode, error) {
175176
return node, nil
176177
}
177178

178-
func Offline(r repo.Repo) ConfigOption {
179-
return Standard(r, false)
180-
}
181-
182-
func OnlineWithOptions(r repo.Repo, router RoutingOption, ho HostOption) ConfigOption {
183-
return standardWithRouting(r, true, router, ho)
184-
}
185-
186-
func Online(r repo.Repo) ConfigOption {
187-
return Standard(r, true)
188-
}
179+
func (n *IpfsNode) setupHost(ctx context.Context, hostOption HostOption) {
189180

190-
// DEPRECATED: use Online, Offline functions
191-
func Standard(r repo.Repo, online bool) ConfigOption {
192-
return standardWithRouting(r, online, DHTOption, DefaultHostOption)
193-
}
194-
195-
// TODO refactor so maybeRouter isn't special-cased in this way
196-
func standardWithRouting(r repo.Repo, online bool, routingOption RoutingOption, hostOption HostOption) ConfigOption {
197-
return func(ctx context.Context) (n *IpfsNode, err error) {
198-
// FIXME perform node construction in the main constructor so it isn't
199-
// necessary to perform this teardown in this scope.
200-
success := false
201-
defer func() {
202-
if !success && n != nil {
203-
n.teardown()
204-
}
205-
}()
206-
207-
// TODO move as much of node initialization as possible into
208-
// NewIPFSNode. The larger these config options are, the harder it is
209-
// to test all node construction code paths.
210-
211-
if r == nil {
212-
return nil, fmt.Errorf("repo required")
213-
}
214-
n = &IpfsNode{
215-
mode: func() mode {
216-
if online {
217-
return onlineMode
218-
}
219-
return offlineMode
220-
}(),
221-
Repo: r,
222-
}
223-
224-
n.ctx = ctx
225-
n.proc = goprocessctx.WithContextAndTeardown(ctx, n.teardown)
226-
227-
// setup Peerstore
228-
n.Peerstore = peer.NewPeerstore()
229-
230-
// setup local peer ID (private key is loaded in online setup)
231-
if err := n.loadID(); err != nil {
232-
return nil, err
233-
}
234-
235-
n.Blockstore, err = bstore.WriteCached(bstore.NewBlockstore(n.Repo.Datastore()), kSizeBlockstoreWriteCache)
236-
if err != nil {
237-
return nil, err
238-
}
239-
240-
if online {
241-
do := setupDiscoveryOption(n.Repo.Config().Discovery)
242-
if err := n.startOnlineServices(ctx, routingOption, hostOption, do); err != nil {
243-
return nil, err
244-
}
245-
} else {
246-
n.Exchange = offline.Exchange(n.Blockstore)
247-
}
248-
249-
success = true
250-
return n, nil
251-
}
252181
}
253182

254183
func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption RoutingOption, hostOption HostOption, do DiscoveryOption) error {
@@ -378,10 +307,13 @@ func (n *IpfsNode) teardown() error {
378307
// owned objects are closed in this teardown to ensure that they're closed
379308
// regardless of which constructor was used to add them to the node.
380309
closers := []io.Closer{
381-
n.Exchange,
382310
n.Repo,
383311
}
384312

313+
if n.Exchange != nil {
314+
closers = append(closers, n.Exchange)
315+
}
316+
385317
if n.Mounts.Ipfs != nil {
386318
closers = append(closers, mount.Closer(n.Mounts.Ipfs))
387319
}

‎core/core_test.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ func TestInitialization(t *testing.T) {
4848
C: *c,
4949
D: testutil.ThreadSafeCloserMapDatastore(),
5050
}
51-
n, err := NewIPFSNode(ctx, Standard(r, false))
51+
52+
n, err := NewNodeBuilder().Offline().SetRepo(r).Build(ctx)
5253
if n == nil || err != nil {
5354
t.Error("Should have constructed.", i, err)
5455
}
@@ -59,7 +60,8 @@ func TestInitialization(t *testing.T) {
5960
C: *c,
6061
D: testutil.ThreadSafeCloserMapDatastore(),
6162
}
62-
n, err := NewIPFSNode(ctx, Standard(r, false))
63+
64+
n, err := NewNodeBuilder().Offline().SetRepo(r).Build(ctx)
6365
if n != nil || err == nil {
6466
t.Error("Should have failed to construct.", i)
6567
}

‎core/corehttp/gateway_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func newNodeWithMockNamesys(t *testing.T, ns mockNamesys) *core.IpfsNode {
4747
C: c,
4848
D: testutil.ThreadSafeCloserMapDatastore(),
4949
}
50-
n, err := core.NewIPFSNode(context.Background(), core.Offline(r))
50+
n, err := core.NewNodeBuilder().Offline().SetRepo(r).Build(context.Background())
5151
if err != nil {
5252
t.Fatal(err)
5353
}

‎core/coreunix/add_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ func TestAddRecursive(t *testing.T) {
2525
},
2626
D: testutil.ThreadSafeCloserMapDatastore(),
2727
}
28-
node, err := core.NewIPFSNode(context.Background(), core.Offline(r))
28+
29+
node, err := core.NewNodeBuilder().Offline().SetRepo(r).Build(context.Background())
2930
if err != nil {
3031
t.Fatal(err)
3132
}

‎core/mock/mock.go

+6-4
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,11 @@ func NewMockNode() (*core.IpfsNode, error) {
4444
},
4545
}
4646

47-
nd, err := core.Offline(&repo.Mock{
47+
r := &repo.Mock{
4848
C: c,
4949
D: ds2.CloserWrap(syncds.MutexWrap(datastore.NewMapDatastore())),
50-
})(ctx)
50+
}
51+
nd, err := core.NewNodeBuilder().Offline().SetRepo(r).Build(ctx)
5152
if err != nil {
5253
return nil, err
5354
}
@@ -100,10 +101,11 @@ func MockCmdsCtx() (commands.Context, error) {
100101
},
101102
}
102103

103-
node, err := core.NewIPFSNode(context.Background(), core.Offline(&repo.Mock{
104+
r := &repo.Mock{
104105
D: ds2.CloserWrap(syncds.MutexWrap(datastore.NewMapDatastore())),
105106
C: conf,
106-
}))
107+
}
108+
node, err := core.NewNodeBuilder().Offline().SetRepo(r).Build(context.Background())
107109

108110
return commands.Context{
109111
Online: true,

‎test/supernode_client/main.go

+11-10
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,13 @@ func run() error {
9393
})
9494
}
9595

96-
node, err := core.NewIPFSNode(
97-
ctx,
98-
core.OnlineWithOptions(
99-
repo,
100-
corerouting.SupernodeClient(infos...),
101-
core.DefaultHostOption,
102-
),
103-
)
96+
nb := core.NewNodeBuilder()
97+
nb.Online()
98+
nb.SetRepo(repo)
99+
nb.SetRouting(corerouting.SupernodeClient(infos...))
100+
nb.SetHost(core.DefaultHostOption)
101+
102+
node, err := nb.Build(ctx)
104103
if err != nil {
105104
return err
106105
}
@@ -168,10 +167,12 @@ func runFileCattingWorker(ctx context.Context, n *core.IpfsNode) error {
168167
return err
169168
}
170169

171-
dummy, err := core.NewIPFSNode(ctx, core.Offline(&repo.Mock{
170+
nb := core.NewNodeBuilder().Offline()
171+
nb.SetRepo(&repo.Mock{
172172
D: ds2.CloserWrap(syncds.MutexWrap(datastore.NewMapDatastore())),
173173
C: *conf,
174-
}))
174+
})
175+
dummy, err := nb.Build(ctx)
175176
if err != nil {
176177
return err
177178
}

0 commit comments

Comments
 (0)
Please sign in to comment.