Learn Tutorial Use cases

Deploying RChain on a Raspberry Pi: Coin faucet part 2

This is the second of 5 articles about a project using RChain deployed to Raspberry Pi 3, interacting with phones in the area over Bluetooth LE 4.  If you missed part 1, see Coin Faucet Origins: The Quest for Free Beer

Deploying RChain on the Raspberry Pi 3

The variety of disposable electronics (Internet-of-Things, IoT) has grown substantially over the last decade, as prices are falling, and the selection becomes incredibly powerful.  Blockchains typically require significant storage and processing resources for all of the cryptography used in the transactions they store. One of the most common, inexpensive, and powerful IoT devices is the Raspberry Pi, at $35, featuring 1GB of RAM, a 4-core processor, 802.11n WiFi, 1 network port, 4 USB ports, and Bluetooth 4 LE. The Raspberry Pi is a fully functioning computer, and it can run many different versions of Linux. In this article, we will cover the overall process for setting up a Raspberry Pi with a running RChain node on Linux.  A more detailed process is captured on our GitHub webpage.

Hardware Setup

When we are selecting our hardware, we have a few requirements to consider for RChain (or any blockchain). RChain relies heavily on cryptography, so it will need something with a reasonable amount of processing power, and it will need to handle very large numbers. We have chosen the Raspberry Pi 3, 3B, and 3B+, because they all offer 4-core processors and feature a 64-bit architecture.  Blockchains are essentially very sophisticated databases, and they are expected to store an increasing amount of transaction data, so we have selected a 32GB microSD Card as our hard-drive. While you are setting up your Raspberry Pi, you will need a monitor (HDMI only), WiFi router, and a keyboard and mouse. Total price for our Raspberry Pi and microSD Card is $46.
Raspberry Pi 3B (Amazon, $35) – http://a.co/d/4wXC5Td
32GB microSD   (Amazon, $11) – http://a.co/d/8OseQmo

Operating System Setup

During our testing, we tried multiple Linux distributions and configurations, and eventually settled on openSUSE Leap 42.3 with an XFCE desktop. The Raspberry Pi 3 uses an ARMv8 64-bit processor, and very few Linux distributions have been released for this architecture as of today. To begin our setup, we will need to download openSUSE, located here:
We are using a laptop with Ubuntu 18.04 to write openSUSE onto the microSD card.  Once we plug the microSD card into the laptop, we will need to determine what device name it is using. In a Terminal on Linux, you can run the command “lsblk” to get a list of devices:

You can see our microSD card in the list of devices with name “mmcblk0”, and it has 1 partition called “mmcblk0p1”. We are going to overwrite the entire device, so we will use “xzcat” to decompress openSUSE, “dd” to write to the entire microSD card, and “sync” to verify everything.  Execute the following single-line command in Terminal:
WARNING: If you use the wrong device for the “of” parameter, you may erase your computer.

xzcat <image_name>.raw.xz | sudo dd bs=4M of=/dev/<MicroSD_name> iflag=fullblock oflag=direct; sync

This step can take quite a while to complete (11 minutes on my computer), and you will not see any updates until it finishes. Once it is finished, you can take the microSD card out of your laptop, plug it into the Raspberry Pi, and then turn it on. After a few minutes, you will be greeted with the openSUSE login screen (username=root / password=linux).  You will be taken to the desktop, and you will need to connect to your WiFi by clicking the network icon in the bottom-right corner.  
NOTE: Make sure that you have plugged your Raspberry Pi into a strong, consistent power source, like a wall outlet or your laptop. If the power source is unstable, you may lose your Wifi connection randomly.  Also, there is a bug in openSUSE Leap 42.3 that affects the Wifi network config, and will prevent it from working after you restart. To fix this, you will need to run the following in XFCE Terminal and restart the RasPi:

# /usr/sbin/install-brcmfmac
# sed -i ‘s/sdhci-iproc //’ /etc/dracut.conf.d/raspberrypi_modules.conf
# mkinitrd
# shutdown -r now

After the RasPi restarts, and your internet connection is working consistently, we have one final command that will pull in some initial setup files needed to install RChain.  Run the following in XFCE Terminal:

# curl -sL https://git.io/fAMQP| sh

Congratulations! Your Raspberry Pi is set up with Linux and ready to install RChain.

Building and Installing RChain

We have packaged the RChain installation process into 4 scripts, that will each conclude by restarting the Raspberry Pi. Building RChain and its dependencies requires significant resources, so we will need to disable the desktop and run everything from a text console.  These 4 scripts may take hours to complete, and you will continuously get feedback on what they are doing until they finish with a summary of what was accomplished. The final script will re-enable the desktop after RChain is built, but it is recommended that you limit your activity while RChain is running so that it has access to as many resources as possible. At the end of this process, you will have a local RChain node built specifically for your Raspberry Pi, as well as a Docker container that you can use to run multiple nodes at the same time.

Step 1: Disable GUI

This step is quick, and it will make sure you have a large (2 GB) swap file to help with building everything later, and it will disable the Desktop so we have more RAM for building RChain.  You will need to run the following commands in XFCE Terminal:  

# cd /coinFaucet/raspi-rchain
# ./step1_DISABLE-GUI.sh

You will be prompted to “Press 1 to abort, or any other key to restart”. This prompt is presented after each script is run, so you would only press 1 if you wanted to review things on the RasPi and then manually restart on your own. Otherwise, press any other key, the RasPi will restart, and you will be ready for the next step. When the RasPi restarts, you will not have a desktop anymore, and you will need to log in to the text console to continue.

Step 2: Install Prereqs

By far the longest running script (almost 4 hours), this script will install several tools and libraries used to run RChain. The full list of prerequisite components is detailed on the GitHub page for this project along with versions, and the largest amount of time will involve building Netty (Tomcat). You will need to run the following commands in the text console:

# cd /coinFaucet/raspi-rchain
# ./step2_INSTALL-PREREQS.sh

Again, this script will end by prompting you to press any key to restart.  All of the prerequisite components have been built and installed, so we are ready to proceed to building RChain.  

Step 3: Build RChain

This step will also take quite a while to complete (almost 3 hours), and it will build and package a native copy of an RChain node for the Raspberry Pi. RChain is a collection of multiple projects that are all built using Scala. Some of the projects, such as Rholang and RSpace will involve special build processes, so we have packaged all of these processes in this script:

# cd /coinFaucet/raspi-rchain
# ./step3_BUILD-RCHAIN.sh

When this step completes, you will have an RChain node that you can use immediately for testing.  If you want to test this out, press ‘1’ to abort the restart, and then running the following command:

rnode --version

This has been quite a journey, and your RasPi is now equipped to run RChain natively. To run RChain nodes, you either need to run the ‘rnode’ command with all of its configuration details as parameters, or you will need to use the configuration file ‘rnode.toml’ that is part of the Docker setup. In the next step, we will package the RChain node as a Docker container, and we will deploy the configuration files so that you can easily start and stop nodes.

Step 4: Deploy Docker

This final step is optional, but it really is the recommended way to manage your RChain nodes in a consistent, reproducible form. During this step, we will package the Docker images, setup configuration for a small RChain network, and then load a network as a set of Docker containers. To deploy RChain as a Docker image, you need to run the following:

# cd /coinFaucet/raspi-rchain
# ./step4_DEPLOY-DOCKER.sh

You now have a Docker image that you can use for building an RChain network. The final command in this script re-enabled your Desktop, so after you restart the RasPi this last time, you will be greeted by the normal openSUSE login screen. If you ever need to disable the Desktop again, you can use the following commands to switch between modes:
Disable GUI Desktop = systemctl set-default multi-user.target
Enable GUI Desktop = systemctl set-default graphical.target

Testing your Environment

After all the waiting and restarting, RChain has been built and installed specifically for your Raspberry Pi, and it has even been packaged as a Docker image so that the environment is more self-contained. To test you deployment, we recommend that you start a small, private RChain network using ‘docker-compose’. The configuration files for RChain have been installed under /root/rchain/docker. You will also find the Docker Compose file used to start your network. Run the following command and observe your network starting for the first time:

# cd /root/rchain/docker
# docker-compose -f docker-compose.yml up

If everything starts correctly, you will see 2 RChain nodes start: bootstrap & node0. To keep things simple, this is your entire blockchain network. The resources on the Raspberry Pi are somewhat limited, so we do not recommend running more than 2 nodes at once. You can go through some of the more advanced examples and setups to learn more about how to connect your Raspberry Pi to the Test Net.  https://developer.rchain.coop/

What’s next…

In our next article, we will be doing a deep dive into writing smart contracts on RChain using its custom language, Rholang. We will show how to build a distributed application on the blockchain that is capable of massive concurrency, while solving a practical, everyday business problem: issuing and redeeming coupons with customers.
This project is a collaboration between BlockSpaces, LLC and RChain Cooperative
Code is open source and available on https://github.com/BlockSpaces/coin-faucet