Ever so often a question comes by where a forum member requests some test bitcoins to test out some functionality. Lots of times they are told they could probably test whatever they want to test using regtest instead of the test network. But it might not be clear what the regtest environment is and how to use it. So I decided to write this detailed guide to get you started from scratch.
About this guideThis guide is a write-up on how I set things up to use regtest. I'm not claiming this is the only/best way to do thing. Use it as as starting point to play around as much as you want!
Used setupThis guide was written using the following setup:
- Bitcoin Core v0.20.1
- Python 3.8.2
- bitcoinrpc library v1.0
The Basics: What is regnet?In short regnet is a bitcoin blockchain you entirely build yourself from scratch but still adhers to all the rules of the Bitcoin network. The difficulty of the network is fixed at the lowest difficulty so you can easily mine as much blocks as you need, giving you as much bitcoins to use as you desire as a result.
You should however realize the regtest is your own private blockchain, so for instance you can't lookup transactions in your favorite blockexplorer like you can do with mainnet/testnet. In turn this also means you can mess up as much as you like and can even start all over from scratch without a problem. Regtest is a great way of fiddling around without worrying you messing things up, it really is an excellent test environment for most cases.
So if your interested if it's something you could try/use continue reading and decide yourself.
That sound promising: how to start?All you really need is the bitcoin-core client software. The good news is if you are already running a full-node you can easily run a regtest instance alongside without it effecting your mainnet node.
You can specify settings to be used for regtest-mode only if you include a "[regtest]" section to your bitcoin.conf configuration file. I'm using Linux to write this guide so mine is in ~/.bitcoin. So either add the following lines to your bitcoin.conf file or create a new file with this content if you are starting from scratch:
[regtest]
txindex=1
server=1
rpcport=18444
rpcuser=bitcoin
rpcpassword=talk
connect=127.0.0.1:18444
Some more info on the values:
- [regtest] statesa block with values only applicable to the regtest environment.
- txindex=1 is not necessary but I recommend it since you will be able to get info on transactions not in your wallet if needed;
- server=1 is set so we can communicate using the bitcoin-cli commandline tool;
- rpcport=18444 this is the port used for regtest communication;
- rpcuser=bitcoin this is the username used for accessing, change to whatever value you see fit;
- rpcpassword=talk this is the password used for accessing, change to whatever value you see fit;
- connect=127.0.0.1:18444 to ensure only local connections are allowed.
Let's get ready to rumble!Well that wasn't to hard was it? Now fire things up:
At this time I think it might be good to get some feedback of bitcoind displayed. If you want you could also start bitcoind as a daemon like this:
bitcoind -daemon -regtest
This guide assumes you are comfortable using the command line interface, if you like a bit more visual aid you can instead use bitcoin-qt instead:
Then go to the console window to enter commands:
When this guide uses a command like "bitcoin-cli -regtest blockcount" you could simply just enter the last part ("blockount") in the bitcoin-qt console window to get the same result.
Time to mine!Ok, it's time to mine some new coins since the regtest chain is currently empty. In order to do this the "generatetoaddress" command can be used taking two parameters:
bitcoin-cli -regtest generatetoaddress
Where
is an integer value of the number of blocks we want to mine and is the address the mining reward should go to.
As there is currently not an address we can use one can be created using "getnewaddress":
bitcoin-cli -regtest getnewaddress
Which will give a bech32 regtest address starting with "bcrt". If needed you can also specify to generate a legacy/p2sh-segwit address:
bitcoin-cli -regtest getnewaddress "" legacy
Or:
bitcoin-cli -regtest getnewaddress "" p2sh-segwit
Note: the "" refers to an empty label for the address. If you would use a label just put the name there, something like "Mining address".
So that brings me to addresses in regtest. Addresses on regtest are in the same format as on testnet. So if you already created addresses for testnet you can use them on regtest as well. But as always there is an exception: bech-32 addresses on testnet have a hrp (human readable part) of "tb" while on regtest the hrp is "bcrt". Just so you know.
So now that we have a regtest-address we can mine the very first block:
bitcoin-cli -regtest generatetoaddress 1 bcrt1q4gfcga7jfjmm02zpvrh4ttc5k7lmnq2re52z2y
Note: the bcrt1-address is the output I got from the getnewaddress command before.
As a result the block-hash the regtest genesis block gets returned:
[
"0092228179de46db1fd49b59ac3e02ad6ca5ef93ac710b2fbc17831950caf821"
]
I messed up, now what?
Great! That's what regtest is all about. You created an unspendable output by making a faulty transaction? Mined a lot of coins to an address you don't have the private key to (anymore)? On testnet (and god forbid:mainnet) that would cause a problem but on regtest we can just start all over again. To do so be sure to stop running bitcoind/bitcoin-qt first. Then go to your .bitcoin directory and remove the entire regtest directory (and underlying subdirectories) to completely start over from scratch. You should be aware the regtest wallet.dat file is also in the ./bitcoin/regtest/wallets/ directory so you will delete that as well if you just nuke the entire regtest directory.
Ending of part one
This concludes the first part where we managed to setup the regtest environment, generate a new address and mine the very first block of our own private blockchain. Now there's not a lot we can do yet with our regtest environment yet so in part 2 we will dive into a few python programs to help us out.