Sunday, November 1, 2015

Gridcoin Headless Wallet on Ubuntu (VPS)

I got into Gridcoin [1] at about the start of 2015 if I remember correctly, a cryptocurrency based on BOINC [2]. Users are no longer doing "useless" hashing work, but rather contribute to space, medical or other useful science topics by donating computing power. Now, with the help of Gridcoin, contributors can get a "payment" for their work on white-listed projects.

Since the Gridcoin Project uses both Proof of Work (technically "Proof of Research", aka PoR) and Proof of Stake schemes at the same time, it is beneficial to have a node running to stake coins and earn interest on them.  

Summary
  • How to install gridcoinresearchd (compiling & .deb)
  • Automatically unlock wallet for staking
  • Wallet upgrade (automatic & manual)

Getting a VPS Server

I bought a server with the features listed below for about 20 €/year (don't remember exactly as I bought a cheaper package first and then upgraded) from time4VPS (affiliate link). I can say I am satisfied with their service. They have great support, astonishing prices, upgrading servers work seamlessly and really easy setup. Perfect uptime so far and low ping within the EU, even my small mumble server has a ping around 80 ms.
  • 2.4 GHz CPU core
  • 1024 MB RAM (+ 512 MB of fixed SWAP)
  • 40 GB HDD
  • 400 Mbit/s port connection (dedicated)
  • 1 TB monthly traffic

Installing Gridcoin-Research

This will be a guide on how to install gridcoinresearchd, that is the gridcoin-research daemon. This is a headless version of the client, that is there will be no GUI for configuration or inspection. However, this is not even needed if you just want to run a client and know that it works. Three ways of installing I have covered here are,
  1. Pull from git and compile
  2. Use pre-compiled .deb packages for Ubuntu
  3. Add PPA to your system (simplest)

Compile from git repository

Installing the client is well-described on the Github page and on the official site of the project [3] [1] and following it step-by-step will result in a perfect installation. I copied here the important commands relevant for installing it on an Ubuntu VPS.
  1. sudo apt-get install ntp git build-essential libssl-dev libdb-dev libdb++-dev libboost-all-dev libqrencode-dev
  2. git clone https://github.com/gridcoin/Gridcoin-Research
  3. cd ~/Gridcoin-Research/src
  4. chmod 755 leveldb/build_detect_platform 
  5. make -f makefile.unix USE_UPNP=- 
  6. strip gridcoinresearchd
    sudo install -m 755 gridcoinresearchd /usr/bin/gridcoinresearchd
After this the daemon can be run from the terminal with the command,
gridcoinreserachd

Pre-compiled packages

Turns out that the compiler keeps getting out of RAM during the process and fails. So instead of compiling it on my own, I have decided (for now) to download pre-compiled .deb packages from launchpad. The latest compiled package can be downloaded from : https://code.launchpad.net/~caraka/+recipe/gridcoin-daily.

Select the one that matches your distribution and architecture and grab the link. Then on your server e.g.,
wget  https://code.launchpad.net/~gridcoin/+archive/ubuntu/gridcoin-daily/+build/8190699/+files/gridcoinresearchd_3.5.2.1-r166%7Eubuntu14.04.1_amd64.deb

This will save into gridcoinresearchd_3.5.2.1-r166~ubuntu14.04.1_amd64.deb. Now I had a problem here with the "~" in the name, so what I usually do is replace it with a "-" to avoid issues when installing directly with dpkg.
mv gridcoinresearchd_3.5.2.1-r166~ubuntu14.04.1_amd64.deb gridcoinresearchd_3.5.2.1-r166-ubuntu14.04.1_amd64.deb
And lastly,
sudo dpkg -i gridcoinresearchd_3.5.2.1-r166-ubuntu14.04.1_amd64.deb
After this the daemon can be run from the terminal with the command,
gridcoinreserachd

Adding PPA to your system

Just recently I found that I can also install the Gridcoin daemon via PPA. From https://launchpad.net/~gridcoin/+archive/ubuntu/gridcoin-daily. This is probably less fancy than compiling from the source, but definitely faster.
Code to add Gridcoin PPA to your system
Simply,
sudo add-apt-repository ppa:gridcoin/gridcoin-daily
sudo apt-get update
sudo apt-get install gridcoinresearchd 

After this the daemon can be run from the terminal with the command,
gridcoinreserachd
And the next time you run
sudo apt-get update
sudo apt-get upgrade
to update your system, gridcoinresearchd will also be automatically upgraded.

Note: If you got a VPS from Time4VPS you first have to run sudo apt-get install software-properties-common python-software-properties before you can add-apt-repository.

Unlocking the Wallet for Staking

In order to stake coins - receive interest on the coins in your wallet - the wallet must be unlocked [9]. Of course, this only applies if your wallet is encrypted, which it should be (!) if you are storing it on a remote server! For obvious reasons.

The problem is, in order to unlock it, you have to type the password in your ssh session. This is secure. Unless someone gets access to your VPS server when he/she can check the terminal history (history) and your password is out. The solution is to use the autounlock feature in the config and provide an encrypted password. This will automatically unlock your wallet upon start.

Go to your desktop wallet and in the debug console do the following
execute encrypt YourwalletPassword
and add a last line to your gridcoinresearch.conf file
autounlock=looooongmumbojumboencryptedpassphrase

So you are essentially using an encrypted password that your client can decrypt and prove that it is indeed the correct password and so unlocks your wallet for staking. Pretty cool, huh? So even if someone has access to your VPS, they won't have your wallet passphrase.


Note: The password hashing algorithm takes a salt from the computer's CPU and hard drive. This means that the hash has to be generated on the machine where the wallet will run and as such the hash will be unique to that machine. (The same passphrase will result in different hashes on different computers!)

Config file

I have stripped down my config file and currently use only a minimal version. To edit your gridcoinresearch.conf file,
nano ~/.GridcoinResearch/gridcoinresearch.conf
The following lines need to be present,
email=YourEmailAddressHere
rpcuser=whatever_you_want_you_dont_have_to_remember_it    rpcpassword=whatever_you_want_you_dont_have_to_remember_it
addnode=node.gridcoin.us
addnode=typh00n.net
addnode=gridcoin.asia
autounlock=looooongmumbojumboencryptedpassphrase
maxconnections=45
server=1
daemon=1
After this you should be able to run gridcoinresearchd and get some connections soon. Maybe not right away, but check back in 10-30-60 minutes and see if it works.

Note: The rpcuser and rpcpassword fields are necessary for the headless (gridcoin-researchd) client, you will get an error if these are not present. The "addnode" entries should be purely optional, peers should be found even without these entries.

Tip: "gridcoinresearchd" is a system command now and can be called quickly by starting to type it "grid..." and pressing [TAB]. Also, type gridcoinresearchd help to get a list of available commands with the headless client.

Keeping Gridcoin Updated

Upgrading for self-compiled client

I have written a scrip for myself to upgrade the client, based on the Github Linux guide [3] again. The script upgrades the client if it is installed in the "default" location (that is where I have installed it) in ~/Gridcoin-Reserach/. The script can then be added to the aliases file and simply run with a command. I personally like having a ~/MyScripts directory on my systems where I have a collection of scripts doing a range of things. I call this one ~/MyScripts/grcupgrade.sh and has the following in it.
#!/bin/bash
#Script to Upgrade Gridcoin-Research client
###Config###
#Install directory
Dir=/home/YourUsernameHere/Gridcoin-Research/

###Config END###

#Defining some colours for message output
red='\033[0;31m'
NC='\033[0m'

#Stop gridcoinresearch if running
gridcoinresearchd stop
#Go to the install Directory
cd $Dir
#Git update
echo "${red}Pulling Changes from Git Repository...${NC}"
sudo git fetch --all
sudo git reset --hard origin/master
echo "${red}Git Repository Updated!${NC}"
cd $Dir/src
chmod 755 leveldb/build_detect_platform
#Makefile
echo "${red}Making makefile.unix! This may take a while!${NC}"
make -f makefile.unix USE_UPNP=-
echo "${red}Makefile ready.${NC}"
strip gridcoinresearchd
sudo install -m 755 gridcoinresearchd /usr/bin/gridcoinresearchd
echo "${red}Upgrade finished. You can start Gridcoin-Research now\nby running 'gridcoinresearchd' ${NC}"
#Uncomment the following line to automatically run gridcoinresearchd upon finishing the client update
#gridcoinresearchd

After copying this into the file remember to make the file executable with,
sudo chmod +X ~/MyScripts/grcupgrade.sh
Then we can add an alias to call this script for the simple command grcupgrade. This file doesn't exist by default, so it will be likely empty if you haven't added commands to it before.
sudo nano ~/.bash_aliases
And add the following line to the file,
alias grcupgrade='sudo sh ~/MyScripts/grcupgrade.sh'
Save with Ctrl+O and exit with, Ctrl+X. Then run the following command (or reboot if you wish) to apply changes made to the aliases file.
source ~/.bashrc
After this the upgrade script can be run by simply issuing the 
grcupgrade
command. If you want gridcoinresearchd to start at the end of the client upgrade then you can uncomment the last line in the bash script. A further step is that you can run a cronjob to run this script, say once every week to make sure that your client gets upgraded.

Upgrading when installed via PPA

If you have added the gridcoin-daily PPA to your system, then as mentioned earlier, your gridcoin client will be upgraded the next time you do
sudo apt-get update
sudo apt-get upgrade

Troubleshooting

Resource usage

From personal experience I can say that running on 512 MB of system RAM is not going to work. I had multiple wallet crashes due to "out of memory" error. Therefore I would recommend a minimum of 1 GB for simply running the client (not compiling!). I think CPU power for running a simple wallet is not that much important, it mainly influences the time it takes to make the wallet from the git source. Even a Raspberry Pi's CPU can handle it.

If one wants to compile the client from the git repository as mentioned above, I would suggest having at least 2 GB of system RAM. Although, I cannot confirm if this would truly be enough. I personally am pulling the deb packages and installing it with dpkg for the moment.

Compiler error

Even though it should be simple to compile, I ran into a problem. The compiler ran for a while and then gave back a weird-looking error:
g++: internal compiler error: Killed (program cc1plus)
Please submit a full bug report,
with preprocessed source if appropriate.
See <file:///usr/share/doc/gcc-4.6/README.Bugs> for instructions.
 Turns out this happens when it runs out of RAM. Command dmesg returned
[241411.374594] Out of memory in UB 8727: OOM killed process 11475 (cc1plus) score 0 vm:734444kB, rss:493656kB, swap:207248kB
I started investigating the use of a larger swap file [8], however for obvious reasons, the server restricts this (otherwise anyone could get e.g. 4 GB extra RAM for free). So I tried shutting down unnecessary services, apache, my mumble server and I even tried increasing swappiness. This was also set to 1, and cannot be changed. Again, because of server configuration and the virtualization.

Further help

gridcoinresearchd help
And of course, feel free to leave a comment if you find this useful or you got stuck somewhere.

Extras

Some extras briefly mentioned that are actually required, but beyond the scope of the article.

Automatic updates (cronjob)

Setting up a cronjob for the updater script is a good idea. Here is an example on how to set it to run once a week. Cron takes the following syntax,

Minute Hour Day_of_Month Month Day_of_Week Command
47 6 * * Sun grcupgrade
For example the above code would run the upgrade script every Sunday at 06:47.

Encrypt your wallet.dat

If you have a local Gridcoin wallet on your machine and would like to use the same on a VPS, you should encrypt it. This can be done through the GUI in Settings/Encrypt Wallet. Make sure to use a sufficiently strong password.

Copy an existing wallet.dat file to the server

If you have encrypted your wallet.dat file as mentioned above, it should be safe to copy it to a remote VPS server. This can be done securely with scp,
scp ~/GridcoinResearch/wallet.dat user@vps.server:/home/user/GridcoinResearch/
user: your username on the VPS server
vps.server: IP address or domain name of your VPS server

References