bitcoin is money

My History with Bitcoin

Unfortunately for me, my story with Bitcoin didn’t start in 2011. While I’d heard about it back in 2012, I’d started a new job at Wotif.com and was deep in building and deploying two new data centres, networks, server farms and more. My time was consumed with my day job, and I had limited time to learn about a future global monetary asset.

In 2017, a friend I worked with suggested we get involved in crypto mining to pass some time while we waited for our employer to merge with another ASX business. The merger process was long and drawn out, so I had free to explore new concepts and ideas. 

The mining was reasonably profitable at the time; we both made some $Eth which I can say I still HODL to this day. While crypto mining was a great intro and learning experience, the actual value was it opened my mind to many new concepts and ideas. 

Back then, I didn’t understand what Bitcoin was… It’s more than technology. It’s a multi-faceted system with a brilliant and nuanced design, which attempts to fix the root of many of the problems we face today around “money“.

The first time I ever made a $BTC transaction, I was blown away! How is it possible to transfer a bearer instrument directly between smartphones without permission? How is this possible to transfer money without intermediaries like banks or other custodians; the possibilities are endless.

Bitcoin Recap

For the those unfamiliar with Bitcoin, here’s a quick recap.

Bitcoin is a digital currency that uses decentralised technology for secure payments and storage of money (BTC). It operates independently of a central bank or government. Transactions are recorded on a public digital ledger called a blockchain. Bitcoin was created in 2009 by an anonymous individual or group using the pseudonym Satoshi Nakamoto as what many consider as a response the Global Financial Crisis (GFC). The GFC highlighted the massive problems and short comings with the financial system which in my opinion continue to fester today.  

btc created to replace the banks

The first block in the Bitcoin blockchain is known as the genesis block (check the image above). It was mined by, Satoshi, on January 3, 2009. The block references a newspaper headline from the same day, proofing the block’s timestamp. The genesis block has a reward of 50 bitcoins, the only block in the blockchain with this reward. The block also contains a special message in the coinbase parameter, which reads, “The Times 03/Jan/2009 Chancellor on the brink of second bailout for banks”. This message has been interpreted as a criticism of the financial system and a call for a decentralised alternative.

There are many books written on the history of Bitcoin, so I won’t dive any deeper. Whoever created the system clearly understood many of the worlds problems and thought deeply about the networks design. 

Why run a node?

A Bitcoin node is a computer that connects to the Bitcoin network and helps to validate transactions and blocks on the blockchain. When you run your own node, you are essentially helping to support the network by participating in the process of validating transactions.

Many Bitcoiners are familiar with and embrace taking ownership of their own money. A node helps to support the decentralisation of the network. It arguably supports the network’s security, but that is hotly debated across communities. 

I decided to spin up a Bitcoin node because I wanted to deploy a Lighting Network Node, but more on that in another post.   

Node Setup

Before starting, you need somewhere to run your Bitcoin node. I use an older Intel NUC, it’s been more than sufficient hardware-wise. There are numerous options out there, a quick google will find all sorts of options. 

Ubuntu Configuration

Install Bitcoin Core – with SNAP

The first and probably the easiest option is installing Bitcoin using SNAP on Ubuntu 22. 

sudo apt-get install snapd
sudo snap install bitcoind
bitcoind --version

Traditional Option

If you’re using an older version of Ubuntu you may prefer to use the more traditional approach.

Open a terminal on your Ubuntu machine and run the following commands in order:

sudo apt-get update
sudo apt-get install build-essential libtool autotools-dev automake pkg-config libssl-dev libevent-dev bsdmainutils python3 libboost-system-dev libboost-filesystem-dev libboost-chrono-dev libboost-test-dev libboost-thread-dev
wget https://bitcoin.org/bin/bitcoin-core-22.0/bitcoin-22.0-x86_64-linux-gnu.tar.gz
tar -xzvf bitcoin-22.0-x86_64-linux-gnu.tar.gz
cd bitcoin-22.0-x86_64-linux-gnu
./configure
make
sudo make install
bitcoind --version

(Note that this is the version 0.22 of Bitcoin, if you want to install a different version please check the bitcoin website for the link and version number)

Configure Bitcoind

Now we need to create a Bitcoin configuration file if it doesn’t already exist. Create the following file in your home directory, in the .bitcoin folder.

touch ~/.bitcoin/bitcoin.conf

Now let’s open the bitcoind configuration file with your favourite text editor. Add the following text and change the “someuser” and “somepassword” to your desired user and password.

server=1
txindex=1
daemon=1
maxconnections=20
rpcconnect=127.0.0.1
rpcport=8332
rpcuser=someuser
rpcpassword=somepassword
dbcache=1024
rpcallowip=0.0.0.0/0
rpcthreads=4
rpctimeout=300
minrelaytxfee=0.00000000
incrementalrelayfee=0.00000010
zmqpubrawblock=tcp://127.0.0.1:28332
zmqpubrawtx=tcp://127.0.0.1:28333

For a full breakdown of options available in the bitcoin configuration file, check out https://jlopp.github.io/bitcoin-core-config-generator/

Your Public IP Address

It’s preferable for Bitcoin to start with a known public ip address so you can access it from anywhere on the interwebs. There are many ways to achieve this, but if you’re like me and receive a dynamic IP address from your service provider, you need a way to learn the address and set within bitcoind as it loads.

To solve this problem, I created an Ubuntu service that writes the public address to a file. Bitcoin on startup, is configured with the --external_ip= connection option which is learnt from the file. I’m sure there are many ways to solve this problem and if you have an alternative, please post it in the comments below.

Note: some highly recommend using the TOR network to hide the public IP Address of your node. I’ll work on updating this article in a future post to cover this optional config.

IP Address Python Script

The public_ip.service (see below) executes this getpublicip.py script, writing the current IP Address to a file in the same directory.

I deployed the script to /opt/public_ip_service/ but you could run it from anywhere. Remember, if you change the location, you’ll need to update each of the below.

getpublicip.py

import requests

def get_public_ip():
    response = requests.get('https://api.ipify.org')
    return response.text

public_ip = get_public_ip()
print(public_ip)

with open('public_ip.log', 'w') as f:
    f.write("PUBLIC_IP=" + public_ip)
The above python script when executed creates a file called public_ip.log in the same directory which outputs the following.
PUBLIC_IP=<youraddress>

Configure the public_ip.service

In Ubuntu, system services are typically stored in the /etc/systemd/system/ directory. These files, which have the file extension .service, are used by the systemd init system to manage and start services at boot time.

To create the new public_ip.service, you can create a new file in the /etc/systemd/system/ directory with a unique name and the public_ip.service file extension. The file should contain the necessary information for systemd to manage the service, such as the location of the service’s executable file and any command-line options that should be passed to it.

In addition, the public_ip.service is configured to run every 5 minutes with what’s known as a .timer. This is also saved in the /etc/systemd/system/ directory.

public_ip.service

[Unit]
Description=Get Public IP Address

[Service]
WorkingDirectory=/opt/public_ip_service
ExecStart=/usr/bin/python3 /opt/public_ip_service/getpublicip.py
User=root
Group=root

[Install]
WantedBy=multi-user.target

public_ip.timer

[Unit]
Description=Get Public IP Address

[Timer]
OnCalendar=*:0/5
Persistent=true

[Install]
WantedBy=timers.target

Start the public_ip.timer

To start and enable the timer at boot we run the following commands:

sudo systemctl daemon-reload
sudo systemctl enable example.timer
sudo systemctl start public_ip.timer

This kicks off our timer, running in 5 minutes intervals.

To check the status of the timer or to troubleshoot potential issues, you can use the following commands:

sudo systemctl status public_ip.timer
● public_ip.timer - Get Public IP Address
     Loaded: loaded (/etc/systemd/system/public_ip.timer; enabled; vendor preset: enabled)
     Active: active (waiting) since Thu 2022-12-29 13:14:58 AEST; 2 weeks 2 days ago
    Trigger: Sun 2023-01-15 09:00:00 AEST; 1min 27s left
   Triggers: ● public_ip.service

systemctl list-timers
NEXT                         LEFT          LAST                         PASSED       UNIT                           ACTIVATES
Sat 2023-01-14 17:00:00 AEST 46s left      Sat 2023-01-14 16:55:00 AEST 4min 13s ago public_ip.timer                public_ip.service
Sat 2023-01-14 17:30:12 AEST 30min left    Sat 2023-01-14 16:33:03 AEST 26min ago    anacron.timer                  anacron.service
Sat 2023-01-14 18:56:21 AEST 1h 57min left Sat 2023-01-14 11:59:06 AEST 5h 0min ago  ua-timer.timer                 ua-timer.service

Bitcoin Service Setup

We’ve now got Bitcoin installed and have the public_ip.service ready so we can move on!

Much like the public_ip.service, we need to create a bitcoind service to match to ensure Bitcoin runs at Ubuntu boot time.

Here is the service definition; remember it relies on the public_ip.service for the --external_ip configuration option.

bitcoind.service

[Unit]
Description=Bitcoin daemon
Requires=public_ip.service
After=network.target
After=public_ip.service

[Service]
# get var PUBLIC_IP from file
EnvironmentFile=/opt/public_ip_service/public_ip.log

User=youruser
Group=yourpassword
Type=forking
PIDFile=/home/scott/.bitcoin/bitcoind.pid
ExecStart=/usr/bin/bitcoind -conf=/home/scott/.bitcoin/bitcoin.conf -pid=/home/scott/.bitcoin/bitcoind.pid -externalip=${PUBLIC_IP}
KillMode=control-group
Restart=always
TimeoutSec=120
RestartSec=30

[Install]
WantedBy=multi-user.target

The public_ip is loaded from /opt/public_ip_service/public_ip.log. If you installed the service somewhere else, remember to change it here.

 

Starting the Bitcoin Service

Now we need to start bitcoind at machine boot, ensuring it kicks off after the public_ip.service.

sudo systemctl start bitcoind.service
sudo systemctl enable bitcoind.service
We now have a working bitcoind. It will now take time to sync with the bitcoin network. This sync time depends on your machine, network access speed and geography.

The best way to check is to watch the bitcoind log files found in the location specified in the config file above. Here’s my machine as an example.

$ tail -f ./.bitcoin/debug.log
2023-01-14T07:26:15Z New outbound peer connected: version: 70016, blocks=771861, peer=35132 (outbound-full-relay)
2023-01-14T07:27:10Z New outbound peer connected: version: 70016, blocks=771861, peer=35133 (outbound-full-relay)
2023-01-14T07:35:03Z New outbound peer connected: version: 70016, blocks=771861, peer=35134 (block-relay-only)
2023-01-14T07:36:11Z UpdateTip: new best=0000000000000000000522238537475b1c48b871d8ed58015be92a648b6eaa3a height=771862 version=0x20002000 log2_work=93.946790 tx=796071592 date='2023-01-14T07:36:03Z' progress=1.000000 cache=34.9MiB(168703txo)
2023-01-14T07:44:58Z UpdateTip: new best=000000000000000000025fcdee4b92614fd290a8de91ba7dff4f863b6fef85d0 height=771863 version=0x211c6000 log2_work=93.946801 tx=796074288 date='2023-01-14T07:44:00Z' progress=1.000000 cache=36.1MiB(178466txo)
2023-01-14T07:52:32Z UpdateTip: new best=000000000000000000065f3261de1b99e58cf2e7ee47e441a0c12ddebf851600 height=771864 version=0x20400000 log2_work=93.946812 tx=796077123 date='2023-01-14T07:52:27Z' progress=1.000000 cache=37.0MiB(186299txo)
2023-01-14T07:54:27Z UpdateTip: new best=0000000000000000000088ba2292ecd09cf01884420783b3336a90d031d168c3 height=771865 version=0x20002000 log2_work=93.946823 tx=796078149 date='2023-01-14T07:53:54Z' progress=1.000000 cache=37.1MiB(186958txo)

Now that just about does it! Good luck with your own node, and in the coming weeks I’ll post my Lightning Network Configuration and more. 

Scott