Proj Bitcoin 2: Adding a Second Node to your Private Regtest Blockchain

What you need:

Before you Start

Do this project first:

Proj Bitcoin 1: Setting up a Private Regtest Blockchain

Purpose

To join your own private blockchain network, so you can stat using networking commands.

The purpose of this project is merely to learn how blockchain technology works, not to get rich quick.

Checking your First Node

You should have an Ubuntu server running the Bitcoin daemon. This machine is the "first node".

On your First Node, execute this command:

netstat -pant
The daemon should be listening on port 18444, as shown below.

On your First Node, execute this command:

ifconfig
Find your IP address and make a note of it. On my system, it was 172.16.1.135, as shown below.

On your First Node, execute this command:

bitcoin-cli -regtest getbalance
Your balance should be 50 bitcoins, as shown below.

Preparing Ubuntu

To prepare your new Ubuntu node, execute these commands:
sudo apt-get update
sudo apt-get install git build-essential -y
sudo apt-get install autoconf libboost-all-dev libssl-dev libprotobuf-dev protobuf-compiler libqt4-dev libqrencode-dev libtool -y
sudo apt-get install libdb5.1++-dev libevent-dev -y 

Installing Berkeley Database

Execute these commands:
wget http://download.oracle.com/berkeley-db/db-4.8.30.NC.tar.gz
sha256sum db-4.8.30.NC.tar.gz 
The result should be 12edc0df75bf9abd7f82f821795bcee50f42cb2e5f76a6a281b85732798364ef. Execute these commands:
tar -xvf db-4.8.30.NC.tar.gz
cd db-4.8.30.NC/build_unix
mkdir -p build
BDB_PREFIX=$(pwd)/build
../dist/configure --disable-shared --enable-cxx --with-pic --prefix=$BDB_PREFIX
make install
cd ../..

Compiling Bitcoin Core from Source

Execute these commands to clone the Bitcoin repository, and to see the versions available.
git clone https://github.com/bitcoin/bitcoin.git
cd bitcoin
git checkout v0.12.1rc2
./autogen.sh
./configure --with-incompatible-bdb
make
sudo make install

Creating a Configuration File

On your new node, execute these commands:
cd
mkdir .bitcoin
nano .bitcoin/bitcoin.conf
Paste in these lines, replacing the password with something unique, and the IP address with the IP address of your first node.
rpcuser=bitcoinrpc
rpcpassword=wqfgewgewi
addnode=172.16.1.135
as shown below.

Press Ctrl+X, Y, Enter to save the file.

Starting the Bitcoin Daemon

On your new node, execute this command to start the Bitcoin daemon:
bitcoind -regtest -daemon
Execute this command to see network connections:
netstat -pant
The daemon for our private "regtest" network is listening on port 18444, and also connected to the first node on its port 18444, as shown below.

Examining the Blockchain

On your new node, execute this command to see information about the blockchain:
bitcoin-cli -regtest getblockchaininfo
The blockchain contains over 100 blocks, as shown below. These are the blocks mined by the first node.

Getting an Address

Execute this command to get an address:
bitcoin-cli -regtest getnewaddress
The address appears, as shown below.

Sending Bitcoins from the First Node

On your First Node, execute these commands, to unlock your wallet, and then send 20 bitcoins to your new node.

You will need to replace the address with the address of your second node.

bitcoin-cli -regtest walletpassphrase P@ssw0rd 360
bitcoin-cli -regtest sendtoaddress mhpfrvvhQMgd7NmZUGudXc8iW6Dwd8dWKs 20
The response shows the Transaction ID, as shown below.

Viewing your Balance on the First Node

On your First Node, execute this command to view your balance:
bitcoin-cli -regtest getbalance
Your balance is now approximately 30, as shown below.

Why isn't your balance 50? Because the block you mined isn't "confirmed" yet. On the real Bitcoin blockchain, a block is confirmed when there have been 6 blocks after it mined, but on our testnet, confirmation takes 100 additional blocks.

Viewing your Balance on the New Node

On your New Node, execute this command to view your balance:
bitcoin-cli -regtest getbalance
Your balance is still zero, as shown below.

Why isn't your balance 20? Because the block you mined isn't "confirmed" yet. On the real Bitcoin blockchain, a block is confirmed when there have been 6 blocks after it mined, but on our testnet, confirmation takes 100 additional blocks.

Mining 100 More Blocks on your First Node

On your First Node, execute this command to mine 100 more blocks:
bitcoin-cli -regtest generate 100
You see 100 block hashes, as shown below.

Viewing your Balance on the New Node

On your New Node, execute this command to view your balance:
bitcoin-cli -regtest getbalance
Your balance is now 20 bitcoins, as shown below.