Browsing all articles from March, 2018

Raspberry Pi - Palm of your Hand

The Lightning Network is Bitcoin’s biggest step in achieving global performance and scalability. To truly achieve scale in the billions of transactions per day, you need what’s called “off-chain” transactions, and the Lightning Protocol lays out exactly how it’s done.

This guide walks you through the steps in running a full Bitcoin Lightning Network node on a Raspberry Pi. When you’re finished, you’ll have a full-featured, decentralized international bank in the palm of your hand contributing to world commerce! How cool is that?

 

 

There’s 4 steps to getting this done:

1. Setting up your Raspberry Pi
2. Compiling and configuring a Bitcoin full node, syncing the blockchain, and adding your node to the Internet
3. Compiling and configuring a Lightning Node, and broadcasting its existence to the Internet
4. Funding your wallet

Plow through these steps, and you’ll soon be up, running and doing your part in making Bitcoin and the Lightning Network bigger and stronger!

1. Raspberry Pi Setup

Raspbery Pi SetupFirst, you need a Raspberry Pi and an External USB drive with at least 250GB (as of the time of this post) capacity. I found an old, slow 250GB USB 2.0 drive lying around. It doesn’t have to be super fast – it’s more about storage than speed.

Install Raspian and enable SSH. Follow this guide for detailed instructions.

Once you’re up and running, it’s always a good idea to update to latest packages:

pi@raspberrypi:~$ sudo apt-get update
pi@raspberrypi:~$ sudo apt-get upgrade
pi@raspberrypi:~$ sudo apt-get dist-upgrade
pi@raspberrypi:~$ sudo apt-get autoremove

Next, you need to mount your USB drive. The MicroSD card should be used for the operating system and programs only, not for the large and growing Bitcoin blockchain. I recommend formatting the drive as NTFS, but you can also use native Linux EXT4. One of the main reasons you may want to use NTFS, is that if you want to download the blockchain faster, you could download it via a Windows full node, and then use the drive for the Raspberry Pi. For details on other options, here’s a detailed guide. For this example below, it assumes you’ve formatted the drive as NTFS. Plug your drive into your Raspberry Pi. Now you’ll install NFTS on Raspberry Pi and mount it.

pi@raspberrypi:~$ sudo apt-get install ntfs-3g

Confirm that the drive is recognized:

pi@raspberrypi:~$ sudo fdisk -l

At the bottom of the partion list, you should see something like this:

Device Start End Sectors Size Type
/dev/sda1 2048 625072127 625070080 298.1G Microsoft basic data

‘/dev/sda1’ is your mount point. Create a mount point and mount the disk:

pi@raspberrypi:~$ mkdir data
pi@raspberrypi:~$ sudo mount /dev/sda1 /home/pi/data

To ensure it’s mounted each time you reboot, edit /etc/fstab:

pi@raspberrypi:~$ sudo vi /etc/fstab

and add this line:

/dev/sda1 /home/pi/data ntfs-3g rw,default 0 0

Now you will be able to see the contents of the drive:

pi@raspberrypi:~$ ls data
drwxrwxrwx 1 root root 0 Mar 18 22:44 $RECYCLE.BIN
drwxrwxrwx 1 root root 0 Mar 18 20:47 System Volume Information

Great! Now our Raspberry Pi is setup, we’ve got the latest-and-greatest updates, and enough disk space to host a full node. On to Step 2.

2. Compile, configure, sync, and broadcast a full Bitcoin node

First, install the pre-requisites needed to compile and run Bitcoin.

pi@raspberrypi:~$ sudo apt-get install git build-essential autoconf libssl-dev libboost-dev libboost-chrono-dev libboost-filesystem-dev libboost-program-options-dev libboost-system-dev libboost-test-dev libboost-thread-dev libtool libzmq3-dev libevent-dev libtool libssl-dev libboost-all-dev libminiupnpc-dev qt4-dev-tools libprotobuf-dev protobuf-compiler libqrencode-dev db4.8-util -y

There’s more… Download, compile, and install Berkeley DB:

pi@raspberrypi:~$ wget http://download.oracle.com/berkeley-db/db-4.8.30.zip
pi@raspberrypi:~$ unzip db-4.8.30.zip
pi@raspberrypi:~$ cd db-4.8.30
pi@raspberrypi:~/db-4.8.30$ cd build_unix
pi@raspberrypi:~db-4.8.30/build_unix$ ../dist/configure --prefix=/usr/local --enable-cxx
pi@raspberrypi:~db-4.8.30/build_unix$ make
pi@raspberrypi:~db-4.8.30/build_unix$ sudo make install
pi@raspberrypi:~db-4.8.30/build_unix$ cd ~

Let’s update all packages again just to ensure we have latest and greatest.

pi@raspberrypi:~$ sudo apt-get update
pi@raspberrypi:~$ sudo apt-get upgrade
pi@raspberrypi:~$ sudo apt-get dist-upgrade
pi@raspberrypi:~$ sudo apt-get autoremove

OK, let’s download Bitcoin and build it. As of this post, Bitcoin is on version 0.16. Change the branch name (-b) below to the latest version.

pi@raspberrypi:~$ git clone -b 0.16 https://github.com/bitcoin/bitcoin.git
pi@raspberrypi:~$ cd bitcoin/
pi@raspberrypi:~bitcoin$ ./autogen.sh
pi@raspberrypi:~bitcoin$ ./configure
pi@raspberrypi:~bitcoin$ make
pi@raspberrypi:~bitcoin$ sudo make install

Be prepared to wait. This may take at least 2 hours!

Now that you’ve got Bitcoin downloaded, compiled, and installed, the next step is to RUN it. First, cd into your USB drive and let’s make a bitcoin.conf file:

pi@raspberrypi:~$ cd data
pi@raspberrypi:~/data$ mkdir BitcoinData
pi@raspberrypi:~/data$ cd BitcoinData
pi@raspberrypi:~/data/BitcoinData$ vi bitcoin.conf

Add these lines (change the rpc password, this is just an example):

server=1
daemon=1
txindex=1
rpcuser=bitcoinrpc
rpcpassword=ysSDnfRcSTCmI0syEwrM67brd8QnPc12pkKyFLtU
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28332

Add a symbolic link for ~/.bitcoin. This makes it easy to run bitcoin-cli commands without having to specify a specific -datadir each time.

pi@raspberrypi:~/data/BitcoinData$ cd ~
pi@raspberrypi:~$ ln -s /home/pi/data/BitcoinData/ ~/.bitcoin

Start the Bitcoind daemon to run the node:

pi@raspberrypi~$ bitcoind -daemon

Nice! We’re off and running. Let’s check the progress of the node as it downloads the blockchain. It’s gonna take a while. Depending on your bandwidth, it could take a few days – if not more. Eat. Sleep. Code. Repeat. Wait this out…

Patience, you must have

While you’re waiting, you can check progress by looking at the ‘progress’ output. Once it hits 1.0, you’re synced!:

pi@raspberrypi~$ tail -n 1 ~/.bitcoin/debug.log
pi@raspberrypi~$ bitcoin-cli getblockchaininfo

Allow the pi user to run tasks at startup:

pi@raspberrypi~$ sudo vi /etc/cron.allow

Add this line:

pi

Create a bash file to start the Bitcoin Daemon on reboot:

pi@raspberrypi~$ vi bitcoin-start.sh

With this:

#!/bin/bash
sleep 10
/home/pi/bitcoin/src/bitcoind -daemon

Add it as a cron job on boot:

pi@raspberrypi~$ chmod +x bitcoin-start.sh
pi@raspberrypi~$ crontab -u pi -e

Add this line to the bottom:

@reboot /home/pi/bitcoin-start.sh

So now when you reboot, bitcoind will startup.

Once the blockchain is synced – WHEW – that’s a lot of steps, but hey, if it was easy everyone would be doing it.  We’re still in the early phases here.

It’s a good idea to now make your node available to the outside world. This helps improve the security of the Bitcoin network, and you’ll be rewarded with good mojo and karma to spare. Take the time to open your heart / port to the outside world.  See how analogous technology and physics is to philosophy? 🙂

Be sure to open and forward both port 8333 (Bitcoin) and port 9735 (Lightning) on your router.

Onward to lightning.

3. Lightning strikes now

The lnd Lightning implementation was developed using Golang. Learn about why Go is great!

pi@raspberrypi~$ wget https://dl.google.com/go/go1.10.linux-armv6l.tar.gz
pi@raspberrypi~$ sudo tar -C /usr/local -xzf go1.10.linux-armv6l.tar.gz

Setup go properly by editing .bashrc:

pi@raspberrypi~$ mkdir gocode
pi@raspberrypi~$ vi .bashrc

Add these lines:

export GOPATH=~/gocode
export PATH=$PATH:$GOPATH/bin

Run your .bashrc

pi@raspberrypi~$ source .bashrc

Get one more LN dependency:

pi@raspberrypi~$ go get -u github.com/golang/dep/cmd/dep

Now we’ll install LND – the reference client, the “Lightning Daemon“:

pi@raspberrypi~$ git clone https://github.com/lightningnetwork/lnd $GOPATH/src/github.com/lightningnetwork/lnd
pi@raspberrypi~$ cd $GOPATH/src/github.com/lightningnetwork/lnd
pi@raspberrypi:~/gocode/src/github.com/lightningnetwork/lnd$ dep ensure
pi@raspberrypi:~/gocode/src/github.com/lightningnetwork/lnd$ go install . ./cmd/...

Again, always good to run “latest-and-greatest”, so when you want to update LND, run these commands:

pi@raspberrypi~$ cd $GOPATH/src/github.com/lightningnetwork/lnd
pi@raspberrypi:~/gocode/src/github.com/lightningnetwork/lnd$ git pull && dep ensure
pi@raspberrypi:~/gocode/src/github.com/lightningnetwork/lnd$ go install . ./cmd/...

Test your LND installation:

pi@raspberrypi:~/gocode/src/github.com/lightningnetwork/lnd$ go install; go test -v -p 1 $(go list ./... | grep -v '/vendor/')

Similar to how we setup bitcoin.conf, let’s setup lnd.conf. Replace X.X.X.X below with your public IP address. This will broadcast that you have a Lightning Node to other nodes, and auto-connect to available channels:

pi@raspberrypi:~$ cd data
pi@raspberrypi:~/data$ mkdir LightningData
pi@raspberrypi:~/data$ cd LightningData
pi@raspberrypi:~/data/LightningData$ vi lnd.conf

Add these lines:

[Application Options]
debuglevel=debug
debughtlc=true
maxpendingchannels=10
noencryptwallet=1
externalip=X.X.X.X

[Bitcoin]
bitcoin.active=1
bitcoin.mainnet=1
bitcoin.node=bitcoind

[Autopilot]
autopilot.active=1
autopilot.maxchannels=10
autopilot.allocation=1.0

Add a symbolic link for ~/.lnd:

pi@raspberrypi:~/data/LightningData$ cd ~
pi@raspberrypi:~$ ln -s /home/pi/data/LightningData/ ~/.lnd

Create a bash file to start it up:

pi@raspberrypi~:~$ vi lightning-start.sh

With these lines:

#!/bin/bash
sleep 20
/home/pi/gocode/bin/lnd

OK, the Lightning Node software is installed and configured. Now it’s time to set it up to run at boot time:

pi@raspberrypi:~$ chmod +x lightning-start.sh
pi@raspberrypi:~$ crontab -u pi -e

Add this line to the bottom, under the bitcoin-start.sh line:

@reboot /home/pi/lightning-start.sh

You can reboot your Raspberry Pi now, and both bitcoind and lnd will start up. For now, let’s just start it manually:

pi@raspberrypi:~$ ./lightning-start.sh &

4. Bolt it together

At this point, both Bitcoin and Lightning are running and serving the world! There’s an incredible amount of detail for both the Bitcoin and Lightning protocols and commands, too much to cover in this post.  That said, here’s a few to get started:

Create a new wallet:

pi@raspberrypi:~$ lncli create

Get a new receiving address:

pi@raspberrypi:~$ lncli newaddress p2wkh

Send a small amount of Bitcoin to your Lightning Node. As of this post, LND is still beta! Be careful, and just send a small amount of BTC.

Run these to see some diagnostic info:

pi@raspberrypi:~$ lncli getinfo
pi@raspberrypi:~$ lncli listpeers
pi@raspberrypi:~$ lncli listchannels
pi@raspberrypi:~$ lncli listpayments

Looking back, that was quite a few steps, right? I know that this can be simplified using Docker, and I may do this at some point. This is my predecessor guide to packing this up as Docker container that as a future iteration.

You made it! There’s obviously a lot to Lightning, and this is just the beginning. These are the very early days. Soon, Lightning will live on your phone and desktop machines and you won’t even realize you’re using it. It will be as seamless as mobile location services are today. For now, plowing through these steps helps you understand how it all works, and you can sleep well knowing that your little $35 computer is helping grow and secure the network for Bitcoin – the world’s future currency.

Now that you’re up and running with a full Bitcoin Lightning Network node, what happens to it if there’s a problem? I’ll tackle maintenance, monitoring, and health in my next post!

Brett Morrison – Official Site

The official web site of Brett Morrison, Self-Made Technology Entrepreneur.

Links

Latest Tweets

Archives