Learning-Bitcoin-from-the-C.../15_1_Building_the_Regtest.md
2017-12-12 12:21:56 -08:00

1.9 KiB
Raw Blame History

15.1: Building the Regtest

NOTE: This is a draft in progress, so that I can get some feedback from early reviewers. It is not yet ready for learning.

This document explains how to build a Regtest (Regression Test) by hand to be able to develop new applications without the need to interact with other peers and blocks. Bitcoin Cores regression test mode (regtest mode) lets you instantly create a brand-new private block chain with the same basic rules as testnet—but one major difference: you choose when to create new blocks, so you have complete control over the environment.

Starting Bitcoind in Regtest Mode

To start your bitcoind (Bitcoin Daemon) in Regtest mode and create a private Blockchain, you have to use the following command:

$ bitcoind -regtest -daemon

Generating blocks

You can generate/mine new blocks using the RPC method generate. This method is only available in the Regtest mode, using the following command:

$ bitcoin-cli -regtest generate 101

This command will generate 101 blocks using a special RPC which is only available in regtest mode. This takes less than a second on a generic PC. Because this is a new block chain using Bitcoins default rules, the first blocks pay a block reward of 50 bitcoins. Unlike mainnet, in regtest mode only the first 150 blocks pay a reward of 50 bitcoins. However, a block must have 100 confirmations before that reward can be spent, so we generate 101 blocks to get access to the coinbase transaction from block #1.

Verifying balance

After mining blocks and getting the reward for the first one, you can verify the balance on your wallet:

$ bitcoin-cli -regtest getbalance

Now you can use this balance for any kind of interaction with the private Blockchain, such as sending Bitcoin transactions according to Chapter 4 in this guide.