Decentralized Storage Explained


– In this lesson, we’ll learn
about decentralized storage
and some services that are available today
like IPFS and Swarm.
You can think of decentralized storage as
a peer-to-peer network where members
pool together their disk space to create
a shared global memory,
kind of like Dropbox, but decentralized.
Both IPFS and Swarm are similar in that
they are open source,
decentralized storage solutions,
but they differ in
implementation and approach.
IPFS, which stands for
InterPlanetary File System,
is a protocol that was
published by an organization
called Protocol Labs.
The first implementation
of the IPFS protocol
was written in the Go language
and published in early 2015,
and today there are several
alpha release implementations available,
including in JavaScript.
Swarm is also a protocol and is part of
the Ethereum Holy Trinity that makes up
the serverless world computer,
which consists of Swarm,
Ethereum, and Whisper.
These three infrastructure
projects are part of
a broader vision that
provides a completely
decentralized alternative to
the currently centralized web.
Ethereum would provide
the computation power,
Swarm would provide the storage layer,
and Whisper would provide
a messaging layer.
Swarm and Whisper are
not quite as far along
in their roadmaps as Ethereum itself,
but a proof of concept,
Swarm implementation,
written in the Go language,
is included as part of
the Geth client since version 1.5.
You may have noticed I
said that both projects
are either in alpha or
proof of concept stages,
meaning they’re not quite yet ready
for the production spotlight.
But, understanding the role
of decentralized storage
in the future web is important,
so you should know how to
make use of these tools.
As these projects evolve and mature,
they will become increasingly
production-ready.
Let’s start by look at how to use Swarm.
Since Swarm is already
part of the Ethereum stack,
a proof of concept, Go
language implementation
exists as part of Geth.
So to activate Swarm, we
need to download and build
Geth and Swarm from source code.
To do this, we need to have Git and Go
installed in our environment.
You can do this easily using the command
“brew install go git.”
Then you need to make
sure your Go environment
is set up correctly.
You can do this by
creating a folder called
“Go” in your home folder
and then exporting
an environment variable called “GOPATH”
which points to this folder.
We’ll then install from
source by downloading
the Go Ethereum source
code from their GitHub.
And now we can compile
Geth and Swarm from source.
We’re now ready to run the Swarm Daemon
by using the binary in our GoPath.
You can add this bin folder
to your path for convenience.
If you run “swarm
version,” we can see that
we are on version 1 point 8.
To use Swarm, you need to
have an Ethereum account.
You can do this quickly
using “geth account new”
and after entering a
password, this will print out
the new account address that we will use
to join the Swarm network.
You can copy the public
address to your clipboard.
To run Swarm, we first need to make sure
we have Geth running.
Then, in a separate tab, we run Swarm
and point it to our Ethereum account using
the “bzzaccount” option, and
pasting in our public address.
It will prompt for your password to unlock
the key file for Swarm, and then start up
a local Swarm node on port 8500.
Now we can easily upload files using
Swarm command line tools by simply calling
“swarm up” followed by
the path of the file
you want to upload.
Swarm doesn’t support encryption yet,
so make sure you don’t
upload any sensitive data.
After uploading, you’ll get a unique hash
printed out for this
file that points to it
on the Swarm network.
Let’s upload our index
dot html from our previous
block chain explorer example.
To view this HTML file, we can navigate
to our local Swarm node in the browser
by going to localhost
port 8500 slash b-z-z
followed by the hash of the file.
You can see the headers
of our table appear
but not any data.
This is because our
Ethers App needs to run
inside an ethers dot io
environment to function properly.
We can quickly spin up a
local ethers dot io server
using the “ethers-build serve” command.
Remember that our ethers dot io container
can point to any app URL,
so let’s point it to our HTML page
by passing our local Swarm
node URL with the hash.
And this will render our
block chain explorer DAP,
hosted on decentralized storage.
Awesome.
Now, let’s see how we
can do something similar
using IPFS.
The easiest way to install
the Go implementation of IPFS
is by using the installer
from the IPFS website.
If you navigate to IPFS dot
io in your favorite browser
and go to the install page,
you can download the appropriate installer
which comes with instructions
in a “read me” file.
So for Mac OS, I can run
“sudo install dot sh”
to move the IPFS binary
somewhere in my path.
To test that it’s working,
we can run “ipfs version.”
To get started, we run
the “ipfs init” command,
which will initialize a local repo for us
at our home folder slash dot IPFS.
This also generates a key pair,
which will be used to identify
us on the IPFS network,
known as your peer identity.
It also prints out a sample
command for us to get started.
We use “ipfs cat” to view
the contents of a file.
Let’s view the “read me”
file mentioned over here.
We see a welcome message and some links
to other files in this directory.
You should probably give
the quick start guide a read as well.
To view the contents of a directory,
we can’t use “ipfs cat.”
Instead, we would use
“ipfs ls” followed by
the directory hash, which would print out
a list of the files in this directory.
Now we’re going to upload the directory
from our block chain explorer
to the IPFS storage network.
Navigate to the directory
that you want to upload
and make sure there aren’t
any sensitive files being uploaded.
Type “ipfs add dash r”
and “dot” for this folder.
The “dash r” option is to recursively add
contents of this directory,
including subdirectories.
This prints out a list of
hashes for each file uploaded
as well as one for the
entire directory itself.
To view our rendered HTML
file, we can access it
through an IPFS node.
A publicly available node is running at
ipfs dot io slash ipfs,
through which we can request any hash.
The first time we request
this hash from this gateway,
it will take a few minutes to load.
This is because the
files need a few minutes
to propagate through the
network when first uploaded.
Once propagated, the load
time will be much faster.
We can also spin up an IPFS node locally
by invoking “ipfs daemon.”
But before we do this, let’s change
the default port for the
daemon from 8080 to 8081
so it doesn’t conflict
with our Ethers server.
We can do this easily by invoking
“ipfs config Addresses dot Gateway”
and then setting the path to 8081.
Now invoke “ipfs daemon.”
This prints out the local nodes address
running on port 8081,
through which we can access
our local files much quicker.
The URL in this case would be
localhost colon 8081 slash IPFS slash
followed by the hash.
Our HTML table headers
appear as we expect,
but the data from the
block chain is not rendered
because, again, we need an
ethers dot io container.
Let’s use our local ethers dot io server
and point it to our IPFS URL.
And voila, we see data from the main net
rendered on the screen.
You might have noticed that
our IPFS and Swarm URLs
aren’t very easy to remember
and pretty difficult to
pass around to friends
because of the really long hash.
It’s not as simple as, say, Google dot com
or Apple dot com.
In our next lesson,
we’ll see how to access
our front end files
stored on IPFS or Swarm
using a human-readable domain name,
much like regular websites today.
To learn more about building
Blockchain applications
check out our online guides
and courses
available at blockgeeks.com

Add a Comment

Your email address will not be published. Required fields are marked *