@cryptotaxi247 / kubo / commits / bf06e54d4

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 <lidel@lidel.org> (cherry picked from commit e8f1ce07b3d8076da5baedd772d80368804e57a7)

Antonio Navarro Perez committed May 25, 2022 at 02:36 UTC bf06e54d44f838f35dd7683aa2047c16692e9ad5
1 file changed +66 -90
docs/examples/go-ipfs-as-a-library/main.go
+66 -90
@@ -86,7 +86,7 @@ func createTempRepo() (string, error) {
86 /// ------ Spawning the node
87
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 // Open the repo
91 repo, err := fsrepo.Open(repoPath)
92 if err != nil {
@@ -102,48 +102,36 @@ func createNode(ctx context.Context, repoPath string) (icore.CoreAPI, error) {
102 Repo: repo,
103 }
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)
106 }
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
109
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
118 }
119
120 // Create a Temporary Repo
121 repoPath, err := createTempRepo()
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)
124 }
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 + }
130
146 -//
131 + api, err := coreapi.NewCoreAPI(node)
132 +
133 + return api, node, err
134 +}
135
136 func connectToPeers(ctx context.Context, ipfs icore.CoreAPI, peers []string) error {
137 var wg sync.WaitGroup
@@ -179,26 +167,6 @@ func connectToPeers(ctx context.Context, ipfs icore.CoreAPI, peers []string) err
167 return nil
168 }
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 -
170 func getUnixfsNode(path string) (files.Node, error) {
171 st, err := os.Stat(path)
172 if err != nil {
@@ -227,18 +195,23 @@ func main() {
195 ctx, cancel := context.WithCancel(context.Background())
196 defer cancel()
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())
211
212 // Spawn a node using a temporary path, creating a temporary repo for the run
213 fmt.Println("Spawning node on a temporary repo")
241 - ipfs, err := spawnEphemeral(ctx)
214 + ipfsB, _, err := spawnEphemeral(ctx)
215 if err != nil {
216 panic(fmt.Errorf("failed to spawn ephemeral node: %s", err))
217 }
@@ -255,24 +228,24 @@ func main() {
228
229 someFile, err := getUnixfsNode(inputPathFile)
230 if err != nil {
258 - panic(fmt.Errorf("Could not get File: %s", err))
231 + panic(fmt.Errorf("could not get File: %s", err))
232 }
233
261 - cidFile, err := ipfs.Unixfs().Add(ctx, someFile)
234 + cidFile, err := ipfsB.Unixfs().Add(ctx, someFile)
235 if err != nil {
263 - panic(fmt.Errorf("Could not add File: %s", err))
236 + panic(fmt.Errorf("could not add File: %s", err))
237 }
238
239 fmt.Printf("Added file to IPFS with CID %s\n", cidFile.String())
240
241 someDirectory, err := getUnixfsNode(inputPathDirectory)
242 if err != nil {
270 - panic(fmt.Errorf("Could not get File: %s", err))
243 + panic(fmt.Errorf("could not get File: %s", err))
244 }
245
273 - cidDirectory, err := ipfs.Unixfs().Add(ctx, someDirectory)
246 + cidDirectory, err := ipfsB.Unixfs().Add(ctx, someDirectory)
247 if err != nil {
275 - panic(fmt.Errorf("Could not add Directory: %s", err))
248 + panic(fmt.Errorf("could not add Directory: %s", err))
249 }
250
251 fmt.Printf("Added directory to IPFS with CID %s\n", cidDirectory.String())
@@ -287,26 +260,26 @@ func main() {
260 outputPathFile := outputBasePath + strings.Split(cidFile.String(), "/")[2]
261 outputPathDirectory := outputBasePath + strings.Split(cidDirectory.String(), "/")[2]
262
290 - rootNodeFile, err := ipfs.Unixfs().Get(ctx, cidFile)
263 + rootNodeFile, err := ipfsB.Unixfs().Get(ctx, cidFile)
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))
266 }
267
268 err = files.WriteTo(rootNodeFile, outputPathFile)
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))
271 }
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)
274
302 - rootNodeDirectory, err := ipfs.Unixfs().Get(ctx, cidDirectory)
275 + rootNodeDirectory, err := ipfsB.Unixfs().Get(ctx, cidDirectory)
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))
278 }
279
280 err = files.WriteTo(rootNodeDirectory, outputPathDirectory)
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))
283 }
284
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() {
288
289 fmt.Println("\n-- Going to connect to a few nodes in the Network as bootstrappers --")
290
291 + peerMa := fmt.Sprintf("/ip4/127.0.0.1/udp/4010/p2p/%s", nodeA.Identity.String())
292 +
293 bootstrapNodes := []string{
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",
299
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",
309
310 // You can add more nodes here, for example, another IPFS node you might have running locally, mine was:
311 // "/ip4/127.0.0.1/tcp/4010/p2p/QmZp2fhDLxjYue2RiUvLwT9MWdnbDxam32qYFnGmxZDh5L",
312 // "/ip4/127.0.0.1/udp/4010/quic/p2p/QmZp2fhDLxjYue2RiUvLwT9MWdnbDxam32qYFnGmxZDh5L",
313 + peerMa,
314 }
315
316 go func() {
341 - err := connectToPeers(ctx, ipfs, bootstrapNodes)
317 + err := connectToPeers(ctx, ipfsB, bootstrapNodes)
318 if err != nil {
319 log.Printf("failed connect to peers: %s", err)
320 }
321 }()
322
347 - exampleCIDStr := "QmUaoioqU7bxezBQZkUcgcSyokatMY71sxsALxQmRRrHrj"
323 + exampleCIDStr := peerCidFile.Cid().String()
324
325 fmt.Printf("Fetching a file from the network with CID %s\n", exampleCIDStr)
326 outputPath := outputBasePath + exampleCIDStr
327 testCID := icorepath.New(exampleCIDStr)
328
353 - rootNode, err := ipfs.Unixfs().Get(ctx, testCID)
329 + rootNode, err := ipfsB.Unixfs().Get(ctx, testCID)
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))
332 }
333
334 err = files.WriteTo(rootNode, outputPath)
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))
337 }
338
339 fmt.Printf("Wrote the file to %s\n", outputPath)