IPFS Cluster Scala Client

Happy devSecOps

(λx.x)eranga
Effectz.AI
3 min readApr 12, 2022

--

Background

IPFS introduced IPFS Cluster which enables automatically replicating and pinning the data throughout the IPFS network of nodes. Whenever a new data is generated and pinned all the cluster nodes receive it. The pin replication handles by the IPFS cluster. It prevent the single point of failure in IPFS. IPFS Cluster is a distributed application that works as a sidecar to IPFS nodes. It runs alongside IPFS node as a separate service and needs to be installed on all the IPFS nodes that make your cluster. In this post I’m gonna discuss about building IPFS Client application with Scala functional programming language and akka-http Client.

IPFS Cluster Deployment

In my previous posts I have discussed detailed information about deploying IPFS Cluster using Docker and Kubernetes. Please follow these posts and get some background information about IPFS Cluster and it’s deployments. In here I have deployed IPFS Cluster with Docker using the following docker-compose.yaml file. Instead of native Docker on macOS, I have run the Docker using Minikube.

I can start the IPFS Cluster as a daemon with docker-compose up -d command. It will starts three IPFS nodes and three IPFS Cluster nodes.

Sbt Dependency

IPFS exposes HTTP API on port 5001 and IPFS Cluster exposes HTTP API on port 9094. I have used akka-http client interface to interact with these HTTP API from the Scala application. Following is the build.sbt dependency file with akka-http, akka-streams and other dependencies.

Scala Client

Following are the various functions which related to the IPFS and IPFS Cluster that I have implemented in Scala client application. Please note that, when connecting to the IPFS/IPFS Cluster I have used 192.168.64.48(minikube ip) since I have run the Docker with Minikube on macOS.

1. Retrive Peer Information

ipfs-cluster-ctl id command used to get the IPFS Cluster peer information. Following is the way execute that command via IPFS Cluster HTTP API on port 9094.

2. List Cluster Peers

ipfs-cluster-ctl peers ls command used to list the IPFS Cluster peers . Following is the way execute that command via IPFS Cluster HTTP API on port 9094.

3. Add Content to IPFS

ipfs-cluster-ctl add command(which is similar to the ipfs add command) used to add the files to IPFS Cluster. ipfs add command only adds to the local IPFS daemon, the ipfs-cluster-ctl add command will add to several cluster peers at the same time. Since ipfs-cluster-ctl add command adding file to several cluster peers it is slower than ipfs add. As an alternative, we can add file to IPFS local daemon via ipfs add command and then pinned normally. The arrival of the pin will make ipfs retrieve the file faster than it would have been to send each block individually, so this way is more appropriate for larger files. Following is the way to add file to local IPFS daemon via IPFS HTTP API on port 5001. In here I have added a file .ipfs/block0001 to the IPFS. It will return the CID of the added file.

4. View Added IPFS Item

ipfs cat command used to view an item in the IPFS. Following is the way execute that command via IPFS HTTP API on port 5001.

5. Pin CID in the Cluster

Above ipfs add command will returns the CID which points to the uploaded file in the IPFS. CIDs can be pinned in the Cluster via ipfs-cluster-ctl pin add command. Following is the way execute that command via IPFS Cluster HTTP API on port 9094.

6. List Pinned Items

ipfs-cluster-ctl pin ls command used to list the pinned items in the IPFS Cluster. Following is the way execute that command via IPFS Cluster HTTP API on port 9094.

7. View Status of Pinned CID

ipfs-cluster-ctl status command used to view the status of the pinned item in the IPFS Cluster. Following is the way execute that command via IPFS Cluster HTTP API on port 9094.

Reference

  1. https://medium.com/rahasak/ipfs-cluster-with-docker-db2ec20a6cc1
  2. https://medium.com/rahasak/deploy-ipfs-cluster-with-kubernetes-c4cd8d64b7c8
  3. https://sovereign-individual.xyz/posts/ipfs-content-identifiers/
  4. https://blockonomi.com/interplanetary-file-system/
  5. https://cluster.ipfs.io/
  6. https://labs.eleks.com/2019/03/ipfs-network-data-replication.html

--

--