Link

Bitcoin

Let’s get your Bitcoin full node operational.


Table of contents

  1. Bitcoin Core
  2. Installation
    1. Prepare data directory
    2. Configuration
  3. Running bitcoind
    1. Autostart on boot
    2. Verification of bitcoind operations
    3. Explore bitcoin-cli
  4. Reduce ‘dbcache’ after full sync
  5. Bitcoin Core upgrade

Bitcoin Core

The base of a sovereign Bitcoin node is a fully validating Bitcoin client. We are using Bitcoin Core, the reference implementation, but not the only option available. This application will download the whole blockchain from other peers and validate every single transaction that ever happened. After validation, the client can check all future transactions whether they are valid or not.

The validated blocks are also the base layer for other applications, like Electrs (to use with hardware wallets) or LND (the Lightning Network client).

Be already warned that the downloading and validation of all transactions since 2009, more than 600’000 blocks with a size of over 250 GB, is not an easy task. It’s great that the Raspberry Pi 4 can do it, even if it takes a few days, as this was simply not possible with the Raspberry Pi 3.


Installation

We download the latest Bitcoin Core binaries (the application) and compare the file with the signed checksum. This is a precaution to make sure that this is an official release and not a malicious version trying to steal our money.

  • Login as “admin” and change to the tmp directory, which is cleared on reboot.

    cd /tmp
    
  • Get the latest download links at bitcoincore.org/en/download (ARM Linux 32 bit), they change with each update. Then run the following commands (with adjusted filenames) and check the output where indicated:

    # download Bitcoin Core binary
    $ wget https://bitcoincore.org/bin/bitcoin-core-0.19.0.1/bitcoin-0.19.0.1-arm-linux-gnueabihf.tar.gz
    $ wget https://bitcoincore.org/bin/bitcoin-core-0.19.0.1/SHA256SUMS.asc
    $ wget https://bitcoin.org/laanwj-releases.asc
    
    # check that the reference checksum matches the real checksum
    # (ignore the "lines are improperly formatted" warning)
    $ sha256sum --check SHA256SUMS.asc --ignore-missing
    > bitcoin-0.19.0.1-arm-linux-gnueabihf.tar.gz: OK
    
    # import the public key of Wladimir van der Laan, verify the signed  checksum file
    # and check the fingerprint again in case of malicious keys
    $ gpg --import ./laanwj-releases.asc
    $ gpg --refresh-keys
    $ gpg --verify SHA256SUMS.asc
    > gpg: Good signature from "Wladimir J. van der Laan ..."
    > Primary key fingerprint: 01EA 5486 DE18 A882 D4C2 6845 90C8 019E 36C2 E964
    
  • Extract the Bitcoin Core binaries, install them and check the version.

    $ tar -xvf bitcoin-0.19.0.1-arm-linux-gnueabihf.tar.gz
    $ sudo install -m 0755 -o root -g root -t /usr/local/bin bitcoin-0.19.0.1/bin/*
    $ bitcoind --version
    > Bitcoin Core version v0.19.0.1
    

Prepare data directory

We use the Bitcoin daemon, called bitcoind, that runs in the background without user interface. It stores all data in a the directory /home/bitcoin/.bitcoin. Instead of creating a real directory, we create a link that points to a directory on the external hard disk.

  • Change to user “bitcoin” and add a symbolic link that points to the external drive.

    $ sudo su - bitcoin
    $ ln -s /mnt/ext/bitcoin /home/bitcoin/.bitcoin
    
  • Navigate to the home directory and check the symbolic link (the target must not be red). The content of this directory will actually be on the external drive.

    $ ls -la
    

Configuration

Now, the configuration file for bitcoind needs to be created. Still as user “bitcoin”, open it with Nano and paste the configuration below. Save and exit.

$ nano /mnt/ext/bitcoin/bitcoin.conf
# RaspiBolt: bitcoind configuration
# /mnt/ext/bitcoin/bitcoin.conf

# Bitcoin daemon
server=1

# Network
listen=1
listenonion=1
proxy=127.0.0.1:9050
bind=127.0.0.1

# Connections
rpcuser=raspibolt
rpcpassword=PASSWORD_[B]
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28333

# Raspberry Pi optimizations
dbcache=2000
maxconnections=40
maxuploadtarget=5000

🚨 Change the rpcpassword to your secure password [B].

🔍 more: configuration options in Bitcoin Wiki


Running bitcoind

Still logged in as user “bitcoin”, let’s start “bitcoind” manually. Monitor the log file a few minutes to see if it works fine (it may stop at “dnsseed thread exit”, that’s ok). Exit the logfile monitoring with Ctrl-C, check the blockchain info and, if there are no errors, stop “bitcoind” again.

$ bitcoind

Note: the following screencast skips longer waiting times, the initial start is longer in real life.

Autostart on boot

The system needs to run the bitcoin daemon automatically in the background, even when nobody is logged in. We use “systemd“, a daemon that controls the startup process using configuration files.

  • Exit the “bitcoin” user session back to user “admin”

    $ exit
    
  • Create the configuration file in the Nano text editor and copy the following paragraph. Save and exit.

    $ sudo nano /etc/systemd/system/bitcoind.service
    
    # RaspiBolt: systemd unit for bitcoind
    # /etc/systemd/system/bitcoind.service
    
    [Unit]
    Description=Bitcoin daemon
    After=network.target
    
    [Service]
    
    # Service execution
    ###################
    
    ExecStart=/usr/local/bin/bitcoind -daemon \
                                      -pid=/run/bitcoind/bitcoind.pid \
                                      -conf=/mnt/ext/bitcoin/bitcoin.conf \
                                      -datadir=/mnt/ext/bitcoin
    
    
    # Process management
    ####################
    
    Type=forking
    PIDFile=/run/bitcoind/bitcoind.pid
    Restart=on-failure
    TimeoutSec=300
    RestartSec=30
    
    
    # Directory creation and permissions
    ####################################
    
    # Run as bitcoin:bitcoin
    User=bitcoin
    Group=bitcoin
    
    # /run/bitcoind
    RuntimeDirectory=bitcoind
    RuntimeDirectoryMode=0710
    
    
    # Hardening measures
    ####################
    
    # Provide a private /tmp and /var/tmp.
    PrivateTmp=true
    
    # Mount /usr, /boot/ and /etc read-only for the process.
    ProtectSystem=full
    
    # Deny access to /home, /root and /run/user
    ProtectHome=true
    
    # Disallow the process and all of its children to gain
    # new privileges through execve().
    NoNewPrivileges=true
    
    # Use a new /dev namespace only populated with API pseudo devices
    # such as /dev/null, /dev/zero and /dev/random.
    PrivateDevices=true
    
    # Deny the creation of writable and executable memory mappings.
    MemoryDenyWriteExecute=true
    
    [Install]
    WantedBy=multi-user.target
    
  • Enable the service

    $ sudo systemctl enable bitcoind.service
    
  • Link the Bitcoin data directory in the user “admin” home. As a member or the group “bitcoin”, admin has read-only access to certain files.

    $ ln -s /mnt/ext/bitcoin/ /home/admin/.bitcoin
    
  • Restart the Raspberry Pi

    $ sudo reboot
    

Verification of bitcoind operations

After rebooting, the bitcoind should start and begin to sync and validate the Bitcoin blockchain.

  • Wait a bit, reconnect via SSH and login with the user “admin”.

  • Check the status of the bitcoin daemon that was started by systemd (exit with Ctrl-C)

    $ systemctl status bitcoind.service
    
  • See bitcoind in action by monitoring its log file (exit with Ctrl-C)

    $ sudo tail -f /mnt/ext/bitcoin/debug.log
    
  • Use the Bitcoin Core client bitcoin-cli to get information about the current blockchain

    $ bitcoin-cli getblockchaininfo
    
  • Please note:

    • When “bitcoind” is still starting, you may get an error message like “verifying blocks”. That’s normal, just give it a few minutes.
    • Among other infos, the “verificationprogress” is shown. Once this value reaches almost 1 (0.999…), the blockchain is up-to-date and fully validated.

🚨 Please let Bitcoin Core sync fully before proceeding.

This can take up to a week when using a Raspberry Pi 4, depending mostly on your external drive (SSD good, HDD bad; USB3 good, USB2 bad).

Explore bitcoin-cli

If everything is running smoothly, this is the perfect time to familiarize yourself with Bitcoin, the technical aspects of Bitcoin Core and play around with bitcoin-cli until the blockchain is up-to-date.


Reduce ‘dbcache’ after full sync

Once Bitcoin Core is fully synced, we can reduce the size of the database cache. A bigger cache speeds up the initial block download, now we want to reduce memory consumption to allow LND and Electrs to run in parallel.

  • As user “admin”, comment the following line out (add a # at the beginning) in the Bitcoin settings file. Bitcoin Core will then just use the default of 300 MB instead of 2 GB. Save and exit.

    $ sudo nano /mnt/ext/bitcoin/bitcoin.conf
    
    #dbcache=2000
    
  • Restart Bitcoin Core for the settings to take effect.

    $ sudo systemctl restart bitcoind
    

Bitcoin Core upgrade

If you want to upgrade to a new release of Bitcoin Core in the future, check out the FAQ section: How to upgrade Bitcoin Core


Next: Lightning »