Introduction

This past week I’ve learned a lot about bitcoin and Bitcoin (the currency and protocol respectively). I’ve mainly been playing with bitcoin-cli – a commandline client for interacting with the Bitcoin network. I admit, I’ve mainly bought coins off of big exchanges, but have no experience actually using the currency. Instead of playing with real money, I’ll walk through how to setup a few fake nodes and confirm connectivity. In a future post I’ll show you how to perform transactions from node-to-node.

Requirements

The only software you will need for this project is Bitcoin Core.

The commands in this guide work with Unix-based operating systems (Linux, Mac, etc). They should mostly work for Windows, but some of the operating-system commands will not work with Windows (for example Windows uses md instead of Unix mkdir).

Setup

First, lets create some directories for this test. We will create a bitcoin directory and inside that directory we will create three nodes: alice, bob, and network.

mkdir bitcoin mkdir bitcoin/alice mkdir bitcoin/bob mkdir bitcoin/network

Since transacting on the real bitcoin network (mainnet) costs money, we can use a fake and local network (regtest) for our testing purposes.

There are two ways to get bitcoind running in regtest. One way is to add -regtest after every bitcoin-cli command. The more convenient way is to add regtest=1 to your bitcoin.conf configuration file. In addition to -regtest there are other arguments we can use to facilitate the connections. These can all be specified in the configuration file.

Let’s change directory to our bitcoin directory.

cd bitcoin

Next we will create configuration files for Alice, Bob, and Network.

Alice Setup

Use your favorite text editor to create and edit Alice’s bitcoin.conf file.

regtest=1 port=8330 rpcport=8331 connect=127.0.0.1:8335

Let’s review this config file.

regtest: regtest=1 means you are using bitcoind and bitcoin-cli in regtest mode.

port: This is a port bitcoind will use. Listen for connections on (default: 8333 or testnet: 18333)

rpcport: This is a port bitcoind will use. Listen for JSON-RPC connections on (default: 8332 or testnet: 18332)

connect: Connect only to the specified node. This means Alice will be connecting to the Network node on port 8335.

Bob Setup

Use your favorite text editor to create and edit Bob’s bitcoin.conf file.

regtest=1 port=8332 rpcport=8333 connect=127.0.0.1:8335

This config file is extremely similar to Alice’s, except we have to run Bob’s bitcoind on a different port from Alice’s.

Network Setup

Use your favorite text editor to create and edit the network’s bitcoin.conf file.

regtest=1 rpcuser=rpc rpcpassword=rpc port=8335 rpcport=8336

We don’t need a ‘connect’ line like the other config files because this will be the node everyone connects to.

Why have a separate network node?

But why even have this node if two users are going to be transacting with each other?

To complete a transaction the transaction must be confirmed by a miner. Every time a new block is generated (AKA ‘mined’) the node that generated the block gets rewarded 50 bitcoins (this is the default reward for regtest and is different from mainnet’s reward). When we do a transaction between Alice and Bob we will make the Network node generate the block. This makes transactions less confusing. Basically, Bob can send Alice 1 bitcoin. Alice’s balance will be +1 bitcoin and Bob will lose 1 bitcoin. Network will be rewarded 50 bitcoins for doing the work.

Starting The Nodes

Let’s fire up the Bitcoin nodes we created!

First lets start the network node, since everything else connects to it.

bitcoind -datadir=network -daemon >Bitcoin server starting

Then we can start Alice and Bob in any order we prefer.

bitcoind -datadir=alice -daemon >Bitcoin server starting

bitcoind -datadir=bob -daemon >Bitcoin server starting

If you get any errors starting these nodes you are likely either in the wrong directory (you didn’t cd bitcoin ) or you have a misconfigured config file for the respective node.

We can confirm the nodes are connected by getting the amount of connected nodes on the network node.

bitcoin-cli --datadir=network getconnectioncount >2

The command should return 2 for Alice and Bob’s connections.

Conclusion

I hope this post helped you out. Please let me know if you run into any issues or have any questions.

In my next post, we’ll discuss transactions between the different Bitcoin Core nodes.