@@ -86,7 +86,7 @@ func createTempRepo() (string, error) {
86
86
/// ------ Spawning the node
87
87
88
88
// Creates an IPFS node and returns its coreAPI
89
- func createNode (ctx context.Context , repoPath string ) (icore. CoreAPI , error ) {
89
+ func createNode (ctx context.Context , repoPath string ) (* core. IpfsNode , error ) {
90
90
// Open the repo
91
91
repo , err := fsrepo .Open (repoPath )
92
92
if err != nil {
@@ -102,48 +102,36 @@ func createNode(ctx context.Context, repoPath string) (icore.CoreAPI, error) {
102
102
Repo : repo ,
103
103
}
104
104
105
- node , err := core .NewNode (ctx , nodeOptions )
106
- if err != nil {
107
- return nil , err
108
- }
109
-
110
- // Attach the Core API to the constructed node
111
- return coreapi .NewCoreAPI (node )
105
+ return core .NewNode (ctx , nodeOptions )
112
106
}
113
107
114
- // Spawns a node on the default repo location, if the repo exists
115
- func spawnDefault (ctx context.Context ) (icore.CoreAPI , error ) {
116
- defaultPath , err := config .PathRoot ()
117
- if err != nil {
118
- // shouldn't be possible
119
- return nil , err
120
- }
121
-
122
- if err := setupPlugins (defaultPath ); err != nil {
123
- return nil , err
124
-
125
- }
126
-
127
- return createNode (ctx , defaultPath )
128
- }
108
+ var loadPluginsOnce sync.Once
129
109
130
110
// Spawns a node to be used just for this run (i.e. creates a tmp repo)
131
- func spawnEphemeral (ctx context.Context ) (icore.CoreAPI , error ) {
132
- if err := setupPlugins ("" ); err != nil {
133
- return nil , err
111
+ func spawnEphemeral (ctx context.Context ) (icore.CoreAPI , * core.IpfsNode , error ) {
112
+ var onceErr error
113
+ loadPluginsOnce .Do (func () {
114
+ onceErr = setupPlugins ("" )
115
+ })
116
+ if onceErr != nil {
117
+ return nil , nil , onceErr
134
118
}
135
119
136
120
// Create a Temporary Repo
137
121
repoPath , err := createTempRepo ()
138
122
if err != nil {
139
- return nil , fmt .Errorf ("failed to create temp repo: %s" , err )
123
+ return nil , nil , fmt .Errorf ("failed to create temp repo: %s" , err )
140
124
}
141
125
142
- // Spawning an ephemeral IPFS node
143
- return createNode (ctx , repoPath )
144
- }
126
+ node , err := createNode (ctx , repoPath )
127
+ if err != nil {
128
+ return nil , nil , err
129
+ }
145
130
146
- //
131
+ api , err := coreapi .NewCoreAPI (node )
132
+
133
+ return api , node , err
134
+ }
147
135
148
136
func connectToPeers (ctx context.Context , ipfs icore.CoreAPI , peers []string ) error {
149
137
var wg sync.WaitGroup
@@ -179,26 +167,6 @@ func connectToPeers(ctx context.Context, ipfs icore.CoreAPI, peers []string) err
179
167
return nil
180
168
}
181
169
182
- func getUnixfsFile (path string ) (files.File , error ) {
183
- file , err := os .Open (path )
184
- if err != nil {
185
- return nil , err
186
- }
187
- defer file .Close ()
188
-
189
- st , err := file .Stat ()
190
- if err != nil {
191
- return nil , err
192
- }
193
-
194
- f , err := files .NewReaderPathFile (path , file , st )
195
- if err != nil {
196
- return nil , err
197
- }
198
-
199
- return f , nil
200
- }
201
-
202
170
func getUnixfsNode (path string ) (files.Node , error ) {
203
171
st , err := os .Stat (path )
204
172
if err != nil {
@@ -227,18 +195,23 @@ func main() {
227
195
ctx , cancel := context .WithCancel (context .Background ())
228
196
defer cancel ()
229
197
230
- /*
231
- // Spawn a node using the default path (~/.ipfs), assuming that a repo exists there already
232
- fmt.Println("Spawning node on default repo")
233
- ipfs, err := spawnDefault(ctx)
234
- if err != nil {
235
- panic(fmt.Errorf("failed to spawnDefault node: %s", err))
236
- }
237
- */
198
+ // Spawn a local peer using a temporary path, for testing purposes
199
+ ipfsA , nodeA , err := spawnEphemeral (ctx )
200
+ if err != nil {
201
+ panic (fmt .Errorf ("failed to spawn peer node: %s" , err ))
202
+ }
203
+
204
+ peerCidFile , err := ipfsA .Unixfs ().Add (ctx ,
205
+ files .NewBytesFile ([]byte ("hello from ipfs 101 in go-ipfs" )))
206
+ if err != nil {
207
+ panic (fmt .Errorf ("could not add File: %s" , err ))
208
+ }
209
+
210
+ fmt .Printf ("Added file to peer with CID %s\n " , peerCidFile .String ())
238
211
239
212
// Spawn a node using a temporary path, creating a temporary repo for the run
240
213
fmt .Println ("Spawning node on a temporary repo" )
241
- ipfs , err := spawnEphemeral (ctx )
214
+ ipfsB , _ , err := spawnEphemeral (ctx )
242
215
if err != nil {
243
216
panic (fmt .Errorf ("failed to spawn ephemeral node: %s" , err ))
244
217
}
@@ -255,24 +228,24 @@ func main() {
255
228
256
229
someFile , err := getUnixfsNode (inputPathFile )
257
230
if err != nil {
258
- panic (fmt .Errorf ("Could not get File: %s" , err ))
231
+ panic (fmt .Errorf ("could not get File: %s" , err ))
259
232
}
260
233
261
- cidFile , err := ipfs .Unixfs ().Add (ctx , someFile )
234
+ cidFile , err := ipfsB .Unixfs ().Add (ctx , someFile )
262
235
if err != nil {
263
- panic (fmt .Errorf ("Could not add File: %s" , err ))
236
+ panic (fmt .Errorf ("could not add File: %s" , err ))
264
237
}
265
238
266
239
fmt .Printf ("Added file to IPFS with CID %s\n " , cidFile .String ())
267
240
268
241
someDirectory , err := getUnixfsNode (inputPathDirectory )
269
242
if err != nil {
270
- panic (fmt .Errorf ("Could not get File: %s" , err ))
243
+ panic (fmt .Errorf ("could not get File: %s" , err ))
271
244
}
272
245
273
- cidDirectory , err := ipfs .Unixfs ().Add (ctx , someDirectory )
246
+ cidDirectory , err := ipfsB .Unixfs ().Add (ctx , someDirectory )
274
247
if err != nil {
275
- panic (fmt .Errorf ("Could not add Directory: %s" , err ))
248
+ panic (fmt .Errorf ("could not add Directory: %s" , err ))
276
249
}
277
250
278
251
fmt .Printf ("Added directory to IPFS with CID %s\n " , cidDirectory .String ())
@@ -287,26 +260,26 @@ func main() {
287
260
outputPathFile := outputBasePath + strings .Split (cidFile .String (), "/" )[2 ]
288
261
outputPathDirectory := outputBasePath + strings .Split (cidDirectory .String (), "/" )[2 ]
289
262
290
- rootNodeFile , err := ipfs .Unixfs ().Get (ctx , cidFile )
263
+ rootNodeFile , err := ipfsB .Unixfs ().Get (ctx , cidFile )
291
264
if err != nil {
292
- panic (fmt .Errorf ("Could not get file with CID: %s" , err ))
265
+ panic (fmt .Errorf ("could not get file with CID: %s" , err ))
293
266
}
294
267
295
268
err = files .WriteTo (rootNodeFile , outputPathFile )
296
269
if err != nil {
297
- panic (fmt .Errorf ("Could not write out the fetched CID: %s" , err ))
270
+ panic (fmt .Errorf ("could not write out the fetched CID: %s" , err ))
298
271
}
299
272
300
- fmt .Printf ("Got file back from IPFS (IPFS path: %s) and wrote it to %s\n " , cidFile .String (), outputPathFile )
273
+ fmt .Printf ("got file back from IPFS (IPFS path: %s) and wrote it to %s\n " , cidFile .String (), outputPathFile )
301
274
302
- rootNodeDirectory , err := ipfs .Unixfs ().Get (ctx , cidDirectory )
275
+ rootNodeDirectory , err := ipfsB .Unixfs ().Get (ctx , cidDirectory )
303
276
if err != nil {
304
- panic (fmt .Errorf ("Could not get file with CID: %s" , err ))
277
+ panic (fmt .Errorf ("could not get file with CID: %s" , err ))
305
278
}
306
279
307
280
err = files .WriteTo (rootNodeDirectory , outputPathDirectory )
308
281
if err != nil {
309
- panic (fmt .Errorf ("Could not write out the fetched CID: %s" , err ))
282
+ panic (fmt .Errorf ("could not write out the fetched CID: %s" , err ))
310
283
}
311
284
312
285
fmt .Printf ("Got directory back from IPFS (IPFS path: %s) and wrote it to %s\n " , cidDirectory .String (), outputPathDirectory )
@@ -315,49 +288,52 @@ func main() {
315
288
316
289
fmt .Println ("\n -- Going to connect to a few nodes in the Network as bootstrappers --" )
317
290
291
+ peerMa := fmt .Sprintf ("/ip4/127.0.0.1/udp/4010/p2p/%s" , nodeA .Identity .String ())
292
+
318
293
bootstrapNodes := []string {
319
294
// IPFS Bootstrapper nodes.
320
- "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN" ,
321
- "/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa" ,
322
- "/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb" ,
323
- "/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt" ,
295
+ // "/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
296
+ // "/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
297
+ // "/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
298
+ // "/dnsaddr/bootstrap.libp2p.io/p2p/QmcZf59bWwK5XFi76CZX8cbJ4BhTzzA3gU1ZjYZcYW3dwt",
324
299
325
300
// IPFS Cluster Pinning nodes
326
- "/ip4/138.201.67.219/tcp/4001/p2p/QmUd6zHcbkbcs7SMxwLs48qZVX3vpcM8errYS7xEczwRMA" ,
327
- "/ip4/138.201.67.219/udp/4001/quic/p2p/QmUd6zHcbkbcs7SMxwLs48qZVX3vpcM8errYS7xEczwRMA" ,
328
- "/ip4/138.201.67.220/tcp/4001/p2p/QmNSYxZAiJHeLdkBg38roksAR9So7Y5eojks1yjEcUtZ7i" ,
329
- "/ip4/138.201.67.220/udp/4001/quic/p2p/QmNSYxZAiJHeLdkBg38roksAR9So7Y5eojks1yjEcUtZ7i" ,
330
- "/ip4/138.201.68.74/tcp/4001/p2p/QmdnXwLrC8p1ueiq2Qya8joNvk3TVVDAut7PrikmZwubtR" ,
331
- "/ip4/138.201.68.74/udp/4001/quic/p2p/QmdnXwLrC8p1ueiq2Qya8joNvk3TVVDAut7PrikmZwubtR" ,
332
- "/ip4/94.130.135.167/tcp/4001/p2p/QmUEMvxS2e7iDrereVYc5SWPauXPyNwxcy9BXZrC1QTcHE" ,
333
- "/ip4/94.130.135.167/udp/4001/quic/p2p/QmUEMvxS2e7iDrereVYc5SWPauXPyNwxcy9BXZrC1QTcHE" ,
301
+ // "/ip4/138.201.67.219/tcp/4001/p2p/QmUd6zHcbkbcs7SMxwLs48qZVX3vpcM8errYS7xEczwRMA",
302
+ // "/ip4/138.201.67.219/udp/4001/quic/p2p/QmUd6zHcbkbcs7SMxwLs48qZVX3vpcM8errYS7xEczwRMA",
303
+ // "/ip4/138.201.67.220/tcp/4001/p2p/QmNSYxZAiJHeLdkBg38roksAR9So7Y5eojks1yjEcUtZ7i",
304
+ // "/ip4/138.201.67.220/udp/4001/quic/p2p/QmNSYxZAiJHeLdkBg38roksAR9So7Y5eojks1yjEcUtZ7i",
305
+ // "/ip4/138.201.68.74/tcp/4001/p2p/QmdnXwLrC8p1ueiq2Qya8joNvk3TVVDAut7PrikmZwubtR",
306
+ // "/ip4/138.201.68.74/udp/4001/quic/p2p/QmdnXwLrC8p1ueiq2Qya8joNvk3TVVDAut7PrikmZwubtR",
307
+ // "/ip4/94.130.135.167/tcp/4001/p2p/QmUEMvxS2e7iDrereVYc5SWPauXPyNwxcy9BXZrC1QTcHE",
308
+ // "/ip4/94.130.135.167/udp/4001/quic/p2p/QmUEMvxS2e7iDrereVYc5SWPauXPyNwxcy9BXZrC1QTcHE",
334
309
335
310
// You can add more nodes here, for example, another IPFS node you might have running locally, mine was:
336
311
// "/ip4/127.0.0.1/tcp/4010/p2p/QmZp2fhDLxjYue2RiUvLwT9MWdnbDxam32qYFnGmxZDh5L",
337
312
// "/ip4/127.0.0.1/udp/4010/quic/p2p/QmZp2fhDLxjYue2RiUvLwT9MWdnbDxam32qYFnGmxZDh5L",
313
+ peerMa ,
338
314
}
339
315
340
316
go func () {
341
- err := connectToPeers (ctx , ipfs , bootstrapNodes )
317
+ err := connectToPeers (ctx , ipfsB , bootstrapNodes )
342
318
if err != nil {
343
319
log .Printf ("failed connect to peers: %s" , err )
344
320
}
345
321
}()
346
322
347
- exampleCIDStr := "QmUaoioqU7bxezBQZkUcgcSyokatMY71sxsALxQmRRrHrj"
323
+ exampleCIDStr := peerCidFile . Cid (). String ()
348
324
349
325
fmt .Printf ("Fetching a file from the network with CID %s\n " , exampleCIDStr )
350
326
outputPath := outputBasePath + exampleCIDStr
351
327
testCID := icorepath .New (exampleCIDStr )
352
328
353
- rootNode , err := ipfs .Unixfs ().Get (ctx , testCID )
329
+ rootNode , err := ipfsB .Unixfs ().Get (ctx , testCID )
354
330
if err != nil {
355
- panic (fmt .Errorf ("Could not get file with CID: %s" , err ))
331
+ panic (fmt .Errorf ("could not get file with CID: %s" , err ))
356
332
}
357
333
358
334
err = files .WriteTo (rootNode , outputPath )
359
335
if err != nil {
360
- panic (fmt .Errorf ("Could not write out the fetched CID: %s" , err ))
336
+ panic (fmt .Errorf ("could not write out the fetched CID: %s" , err ))
361
337
}
362
338
363
339
fmt .Printf ("Wrote the file to %s\n " , outputPath )
0 commit comments