Make your own NKM (New Kind of Miner) out of a Raspberry pi

#1

NKN (New Kind of Network) is probably the most exciting blockchain project in the year 2018. With NKN blockchainizing the whole network layer it reaches out to be the third pillar of internet structure besides computing and storageing.

In terms of mining that means that NKN tokens could be earned just by providing your internet bandwidth to the network. On the contrary to many other tokens you don’t need any big server farms or masses of storage — just a network device with an OS and a little bit of configuration. The NKN mining introduction video is a very good point to get started:

Now what’s important to run a node? Obviously you don’t need much computing power and only some space for storing the NKN blockchain on your device — so why don’t use a low-power consuming computer like the raspberry pi? This tutorial describes how to turn it into a NKM (New Kind of Miner).

What you’ll need

  • A Raspberry pi with a network interface (Raspberry pi 1 Model B and up)or Raspberry pi zero W with a suitable Micro SD Card (16GB+)
  • A personal computer with some sort of SSH Terminal (on Windows I recommend Putty https://www.putty.org/)
  • A way to format your micro SD card with the Raspian Image (recommended: Etcher https://etcher.io/)
  • Your internet connection

Getting started

Before we can start configuring our miner we have to get our raspi ready. The next part describes how to get your device running with the latest Raspberry OS (Raspian) and enable all needed things so you are able to connect to it through SSH.

1. Install Raspian OS

Installing an OS to your Raspberry is very easy. First of all we need to download the OS Image. In this tutorial we are using the latest Raspian Stretch Lite Image. You can get it at https://www.raspberrypi.org/downloads/raspbian/. Store it to the directory of your choice.

After download is finished put the micro SD card to your computer and start up your formatting software. Choose the downloaded image, flash it to your card and wait until it’s finished. Some software like Etcher automatically eject your device. Because we’re not finished yet you need to reinsert it in that case.

If everything went right an external device named “Boot” appears in your file explorer. Depending on the model of your pi continue to the according headless installation step.

2a. Headless installation on Raspberry pi 1 Model B and up

Probably the easiest step of this tutorial. If you got a Raspberry pi 1 Model B and up just copy an empty file called “ssh” (without any file extension) to your micro SD card.
If you got problems creating a file without file extension on windows check out https://www.techwalla.com/articles/how-to-create-a-file-without-an-extension-with-notepad.

2b. Headless installation on Raspberry pi zero W

Headless installation on Raspberry pi zero W is basically the same as on other Raspberries except for an additional file for WiFi configuration.
Two files have to be copied to your SD card:

  • Empty file called “ssh” (without any file extension)
  • file called “wpa_supplicant.conf” with the following code. Adjust country code, ssid and psk to your needs.

country=DE

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev

update_config=1

network={

ssid=“SSID_NAME”

scan_ssid=1

psk=“PASSWORD”

key_mgmt=WPA-PSK

}

Alright, now we can get started! Safely eject your micro SD card from your PC and put it into your pi. Power it on and wait about 1 minute for it to start. You can access it via SSH-Software by connecting to the following:

Host: raspberrypi
User: pi
Password: raspberry

All went fine? Okay! Then we can finally get our hands on setting up our miner!

Installation on a Raspberry

If you skipped the previous step make sure you are connected to your raspberry via ssh and changed in your pi home directory. In short: if you see

[email protected]:~ $

on your screen you are fine :wink:

3. Installing newest version of Golang

By default Raspian comes with an old version of go — way too old to use it with NKN. So we first need to install the newest version. Copy and paste the following lines to your terminal:

Look up the latest version of go and store the download path in “url”:

url=`curl https://golang.org/dl/ | grep armv6l | sort --version-sort | tail -1 | grep -o -E "https://dl.google.com/go/go[0-9]+\.[0-9]+((\.[0-9]+)?).linux-armv6l.tar.gz"`

Download the file:

wget ${url}

Extract go environment to /usr/local :

sudo tar -C /usr/local -xvf `echo ${url} | cut -d '/' -f5`

Finally set your environment variables correctly: Please enter line by line with pressing RETURN.

cat >> ~/.bashrc << 'EOF'
export GOPATH=$HOME/go
export PATH=/usr/local/go/bin:$PATH:$GOPATH/bin
EOF

Good job! Now we only need to re-source our bash with:

source ~/.bashrc

and everything should be done! Confirm that by typing

go version

Now your pi should be on a go version of 1.11 or up . Therefore we could start downloading an building the NKN executables.

4. Download and build your NKN node

At the time of writing NKN has to be built in a pre-defined folder structure. Let’s create that and finally jump into it:

mkdir -p ~/go/src/github.com/nknorg && cd ~/go/src/github.com/nknorg

From now on we’re following the official guide to build NKN under https://github.com/nknorg/nkn, but though git is not installed on Raspian Stretch Lite we have to install it:

sudo apt-get install git

Now we can clone the NKN repository:

git clone https://github.com/nknorg/nkn.git

Change directory to new folder:

cd nkn

Install glide package management:

make glide

Download dependencies for building

make vendor

Build the source code with make

make

Easy, isn’t it? Now you’ve built the main two executables nknc and nknd and are ready for some mining work!

5. Testnet config and wallet creation

Before starting your node you have to rename the right configuration file and create a wallet.

The configuration of a NKN Node is stored in the config.json file. Fortunately NKN already has a testnet configuration file for you which has only to be copied. Just type

cp config.testnet.json config.json

and you’re done!

If you want to create a new wallet for NKN type:

./nknc wallet -c

choose a password and store all of your data on a safe place.

If you already got a wallet on another machine just copy the wallet.dat with a sftp-client (same credentials as ssh) to /home/pi/go/src/github.com/nknorg/nkn .

6. Test mining

Wow, you did it! Everything is set up and you can start mining! Start your node with

./nknd -p YOUR_WALLET_PASSWORD

and test if your node successfully connects to the network. If that’s not the case you probably forgot to forward ports 30000 to 30003 in your router. This is very specific to your model so make sure you check out https://portforward.com/ to help you get started.

If your node has connected properly you can terminate it by pressing CTRL+C because there is one last thing to do: Configure your NKM to be completely Plug’n’Play — autostart on system load and restart on crash.

7. Plug’n’Play NKM

Switch back into your home directory with

cd ~

Then open a new file called “nkn.service” in nano

nano nkn.service

copy and paste this content. Don’t forget to replace your wallet password :

[Unit]Description=nkn[Service]User=piWorkingDirectory=/home/pi/go/src/github.com/nknorg/nkn

ExecStart=/home/pi/go/src/github.com/nknorg/nkn/nknd -p YOUR_WALLET_PASSWORD

Restart=alwaysRestartSec=3

LimitNOFILE=500000

[Install]WantedBy=default.target

That creates a service file that automatically creates a process that starts a node and restarts it 3 seconds after it exited unexpectedly.

Copy this file into /etc/systemd/system as root:

sudo cp nkn.service /etc/systemd/system/nkn.service

And now the great final : make the service start on boot.

sudo systemctl enable nkn.service

reboot your device and you’re done!

sudo reboot

8. (NEW) Auto updating NKM

Since NKN is in early development stage it is necessary to update your node regularly. For that you have to repeat some steps under section 4 again — named stopping your currently running node, updating your nkn-files with git pull and then running make again.

Wouldn’t it be nice to make the node updating itself regularly so that we don’t have to take care of it?

As always we start from our home directory. So if you don’t see

[email protected]:~ $

flashing at your screen change to your home directory by typing

cd ~

First of all we have to create a script that updates the NKN files. So let’s create it by directly jumping into our text editor (cool kids could use vi, I prefer nano)

nano nkn-updater

copy and paste the following contents to it:

HOME=/home/pi
GOPATH=$HOME/go
PATH=/usr/local/go/bin:$PATH:$GOPATH/bin
cd $HOME/go/src/github.com/nknorg/nkn

git fetch &>/dev/null

LOCAL=$(git rev-parse HEAD)
UPSTREAM=$(git rev-parse @{u})

if [ $LOCAL != $UPSTREAM ]
then
systemctl stop nkn.service;
git merge;
make deepclean;
make glide;
make vendor;
make;
chown -R pi:pi $HOME/go;
systemctl restart nkn.service;
fi

Then exit the file by pressing CTRL+X and Y. To make this file executable we quickly run

chmod +x nkn-updater

Last thing to do is to tell Debian to run our script daily at 12 AM. For this we have to edit our crontab list. Start it by running

sudo crontab -e

accept nano as your default editor by pressing RETURN and now you will get to the crontab file. Then append the following line to the end of the file:

00 12 * * * /home/pi/nkn-updater >/dev/null 2>&1

Again exit and save your file by pressing CTRL+X and Y. And you’re done — no more configuration needed!

Now you can lean back and enjoy your new automated miner — A true New Kind of Miner!

If there are any questions don’t hesitate to ask me about it. I will update and expand this tutorial regularly. You can reach me on the NKN discord channel (https://discord.gg/c7mTynX#ChrisT) as well on twitter (@ChristianoBusch) or the NKN telegram channel (#ChrisT).

Home: https://nkn.org/

Email: [email protected]

Telegram: https://t.me/nknorg

Twitter: https://twitter.com/NKN_ORG

Medium: https://medium.com/nknetwork

Linkedin: https://www.linkedin.com/company/nknetwork/

Github: https://github.com/nknorg

Reddit:https://www.reddit.com/r/nknblockchain

Discord: https://discord.gg/c7mTynX

NKN Mining FAQ