Bitcoin for Developers

Bitcoin Nodes



Now that we’ve covered the history behind Bitcoin and have a theoretical understanding of how blockchains function, let’s see it in action.

Thus far when we have said “node” we meant “full node”. As mentioned, full nodes keeps a complete copy of the Bitcoin blockchain and participates in network consensus by checking and forwarding valid transactions. They download every block and transaction and check them against Bitcoin’s consensus rules.

THINGS A "FULL NODE" DOES


In these ways, a full node contributes to network “consensus”.

Lightweight or thin clients are nodes that use Simplified Payment Verification. SPV is a method for verifying that particular transactions were included in a block without downloading the entire blockchain. This allows you to run software that interacts with the bitcoin network on low resource machines, such as running a wallet on a smartphone. However, using SPV instead of full nodes means the loss of some privacy and security.

Before we can set up a node there are two more things to consider. Do we want to run on mainnet, testnet or regtest? And which Bitcoin implementation do we want to use?

Mainnet

Mainnet is the live Bitcoin network. To fully participate in the Bitcoin ecosystem you’ll want to be running a mainnet node. However, when starting out and testing new applications mainnet is likely not the best option as it comes with the risk of losing coins which have real value. To run a mainnet full node you will need to download the entire ~200GB blockchain.

Testnet

Testnet is a public bitcoin network that is used by developers around the world to test Bitcoin applications. You can find and use Bitcoin testnet coins and exchange them with others on the testnet. These coins very intentionally have no exchange value; they are used simply for testing. To run a testnet node you will need to download the testnet blockchain which is significantly smaller than mainnet at only ~20GB.

Regtest

Regtest allows you to set up your own local blockchain. When you start a node in regtest mode there is no blockchain data to download. You need to create all the blockchain data yourself!

Once you know what network you want to run on, you’ll need to choose a client or implementation. Bitcoin core (formerly Bitcoin-Qt), is the reference implementation and is still the most commonly used bitcoin implementation. It is written in C++. However, there are now a number of other options such as Bitcore witch is written in Node.js or btcd which is written in GoLang.

Here we will walk through instructions for both Bitcoin’s testnet and running in regtest mode using the reference implementation Bitcoin Core, or more specifically the bitcoind daemon.

The below instructions are for Ubuntu machines. Mac OS and Windows instructions can be found here.

UBUNTU INSTRUCTIONS


>_

Install the Bitcoin Daemon

hannah@ubuntu: ~/ $ apt-add-repository ppa:bitcoin/bitcoin
hannah@ubuntu: ~/ $ apt-get update
hannah@ubuntu: ~/ $ apt-get install bitcoind

BITCOIND RESOURCES


Once you have the software installed, we’ll need to configure it.

TESTNET INSTRUCTIONS


>_

Install the Bitcoin Daemon

hannah@ubuntu: ~/ $ apt-add-repository ppa:bitcoin/bitcoin
hannah@ubuntu: ~/ $ apt-get update
hannah@ubuntu: ~/ $ apt-get install bitcoind
>_

Start bitcoind

hannah@ubuntu: ~/ $ bitcoind -testnet -printtoconsole -daemon -server
>_

View the debug log in a separate console window

hannah@ubuntu: ~/ $ tail -f .bitcoin/testnet/debug.log
>_

Create a config file in .bitcoin

hannah@ubuntu: ~/ $ touch bitcoin.conf

THE BITCOIN.CONF FILE


Bitcoin.conf Configured for RPC

1 # Set up RPC
2 server=1
3 rpcuser=test
4 rpcpassword=test
5 rpchost=127.0.0.1
6 rpcport=6163
7 # Run on the testnet
8 testnet=1
9 # Specify a directory for testnet data
10 #datadir=/home/bitcoin/.bitcoin/testnet

REGTEST MODE INSTRUCTIONS


Learn more about regtest.
>_

Start bitcoind in regtest mode

hannah@ubuntu: ~/ $ bitcoind -regtest -printtoconsole -daemon -server
>_

View the debug log in a separate console window

hannah@ubuntu: ~/ $ tail -f .bitcoin/regtest/debug.log
>_

Create a config file in .bitcoin/regtest

hannah@ubuntu: ~/ $ touch bitcoin.conf

THE BITCOIN.CONF FILE


Bitcoin.conf Configured for RPC

1 # Set up RPC
2 server=1
3 rpcuser=test
4 rpcpassword=test
5 rpchost=127.0.0.1
6 rpcport=6163
7 # Run on a regtest network
8 regtest=1
9 # Connect only to the specified node
10 connect=[your local IP]

TRY OUT SOME COMMANDS


>_

Kill bitcoind and restart

hannah@ubuntu: ~/ $ pkill -9 -f bitcoind
hannah@ubuntu: ~/ $ bitcoind [-regtest or -testnet] -printtoconsole -daemon -server
>_

Use bitcoin-cli to view your connection to peers

hannah@ubuntu: ~/ $ bitcoin-cli [-regtest or -testnet] -getinfo
>_

Generate a new address to receive coins

hannah@ubuntu: ~/ $ bitcoin-cli [-regtest or -testnet] getnewaddress
>_

Check your balance

hannah@ubuntu: ~/ $ bitcoin-cli [-regtest or -testnet] getbalance

Now that we’ve had a chance to experience bitcoin on the command line, let’s take a step back in our next lesson and be sure we understand in more depth what a bitcoin is.