Skip to content

Commit a4cc162

Browse files
ajnavarrolidel
authored andcommitted
fix(ci): make go-ipfs-as-a-library work without external peers (#8978)
* Do not connect to external nodes on ipfs as a lib example. It was causing some build timeouts error because CircleCI was throttling WAN connections. It closes #8956 * style: rename node vars since this is example, this should make things easier to follow Co-authored-by: Marcin Rataj <[email protected]>
1 parent 883bd8a commit a4cc162

File tree

1 file changed

+66
-90
lines changed
  • docs/examples/go-ipfs-as-a-library

1 file changed

+66
-90
lines changed

docs/examples/go-ipfs-as-a-library/main.go

+66-90
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func createTempRepo() (string, error) {
8686
/// ------ Spawning the node
8787

8888
// 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) {
9090
// Open the repo
9191
repo, err := fsrepo.Open(repoPath)
9292
if err != nil {
@@ -102,48 +102,36 @@ func createNode(ctx context.Context, repoPath string) (icore.CoreAPI, error) {
102102
Repo: repo,
103103
}
104104

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)
112106
}
113107

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
129109

130110
// 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
134118
}
135119

136120
// Create a Temporary Repo
137121
repoPath, err := createTempRepo()
138122
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)
140124
}
141125

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+
}
145130

146-
//
131+
api, err := coreapi.NewCoreAPI(node)
132+
133+
return api, node, err
134+
}
147135

148136
func connectToPeers(ctx context.Context, ipfs icore.CoreAPI, peers []string) error {
149137
var wg sync.WaitGroup
@@ -179,26 +167,6 @@ func connectToPeers(ctx context.Context, ipfs icore.CoreAPI, peers []string) err
179167
return nil
180168
}
181169

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-
202170
func getUnixfsNode(path string) (files.Node, error) {
203171
st, err := os.Stat(path)
204172
if err != nil {
@@ -227,18 +195,23 @@ func main() {
227195
ctx, cancel := context.WithCancel(context.Background())
228196
defer cancel()
229197

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())
238211

239212
// Spawn a node using a temporary path, creating a temporary repo for the run
240213
fmt.Println("Spawning node on a temporary repo")
241-
ipfs, err := spawnEphemeral(ctx)
214+
ipfsB, _, err := spawnEphemeral(ctx)
242215
if err != nil {
243216
panic(fmt.Errorf("failed to spawn ephemeral node: %s", err))
244217
}
@@ -255,24 +228,24 @@ func main() {
255228

256229
someFile, err := getUnixfsNode(inputPathFile)
257230
if err != nil {
258-
panic(fmt.Errorf("Could not get File: %s", err))
231+
panic(fmt.Errorf("could not get File: %s", err))
259232
}
260233

261-
cidFile, err := ipfs.Unixfs().Add(ctx, someFile)
234+
cidFile, err := ipfsB.Unixfs().Add(ctx, someFile)
262235
if err != nil {
263-
panic(fmt.Errorf("Could not add File: %s", err))
236+
panic(fmt.Errorf("could not add File: %s", err))
264237
}
265238

266239
fmt.Printf("Added file to IPFS with CID %s\n", cidFile.String())
267240

268241
someDirectory, err := getUnixfsNode(inputPathDirectory)
269242
if err != nil {
270-
panic(fmt.Errorf("Could not get File: %s", err))
243+
panic(fmt.Errorf("could not get File: %s", err))
271244
}
272245

273-
cidDirectory, err := ipfs.Unixfs().Add(ctx, someDirectory)
246+
cidDirectory, err := ipfsB.Unixfs().Add(ctx, someDirectory)
274247
if err != nil {
275-
panic(fmt.Errorf("Could not add Directory: %s", err))
248+
panic(fmt.Errorf("could not add Directory: %s", err))
276249
}
277250

278251
fmt.Printf("Added directory to IPFS with CID %s\n", cidDirectory.String())
@@ -287,26 +260,26 @@ func main() {
287260
outputPathFile := outputBasePath + strings.Split(cidFile.String(), "/")[2]
288261
outputPathDirectory := outputBasePath + strings.Split(cidDirectory.String(), "/")[2]
289262

290-
rootNodeFile, err := ipfs.Unixfs().Get(ctx, cidFile)
263+
rootNodeFile, err := ipfsB.Unixfs().Get(ctx, cidFile)
291264
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))
293266
}
294267

295268
err = files.WriteTo(rootNodeFile, outputPathFile)
296269
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))
298271
}
299272

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)
301274

302-
rootNodeDirectory, err := ipfs.Unixfs().Get(ctx, cidDirectory)
275+
rootNodeDirectory, err := ipfsB.Unixfs().Get(ctx, cidDirectory)
303276
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))
305278
}
306279

307280
err = files.WriteTo(rootNodeDirectory, outputPathDirectory)
308281
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))
310283
}
311284

312285
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() {
315288

316289
fmt.Println("\n-- Going to connect to a few nodes in the Network as bootstrappers --")
317290

291+
peerMa := fmt.Sprintf("/ip4/127.0.0.1/udp/4010/p2p/%s", nodeA.Identity.String())
292+
318293
bootstrapNodes := []string{
319294
// 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",
324299

325300
// 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",
334309

335310
// You can add more nodes here, for example, another IPFS node you might have running locally, mine was:
336311
// "/ip4/127.0.0.1/tcp/4010/p2p/QmZp2fhDLxjYue2RiUvLwT9MWdnbDxam32qYFnGmxZDh5L",
337312
// "/ip4/127.0.0.1/udp/4010/quic/p2p/QmZp2fhDLxjYue2RiUvLwT9MWdnbDxam32qYFnGmxZDh5L",
313+
peerMa,
338314
}
339315

340316
go func() {
341-
err := connectToPeers(ctx, ipfs, bootstrapNodes)
317+
err := connectToPeers(ctx, ipfsB, bootstrapNodes)
342318
if err != nil {
343319
log.Printf("failed connect to peers: %s", err)
344320
}
345321
}()
346322

347-
exampleCIDStr := "QmUaoioqU7bxezBQZkUcgcSyokatMY71sxsALxQmRRrHrj"
323+
exampleCIDStr := peerCidFile.Cid().String()
348324

349325
fmt.Printf("Fetching a file from the network with CID %s\n", exampleCIDStr)
350326
outputPath := outputBasePath + exampleCIDStr
351327
testCID := icorepath.New(exampleCIDStr)
352328

353-
rootNode, err := ipfs.Unixfs().Get(ctx, testCID)
329+
rootNode, err := ipfsB.Unixfs().Get(ctx, testCID)
354330
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))
356332
}
357333

358334
err = files.WriteTo(rootNode, outputPath)
359335
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))
361337
}
362338

363339
fmt.Printf("Wrote the file to %s\n", outputPath)

0 commit comments

Comments
 (0)