7

I am having trouble using regtest mode on Linux. Using pre-compiled version 0.9.2.1 for linux and starting regtest mode as instructed on bitcoin.org:

<bitcoin-ver>-linux/bin/64>> ./bitcoind -regtest -daemon

The server starts but doing a getinfo returns 'connections' : 0. Tried experimenting with various rpc* settings in the .conf file. What exactly do I need to set in order to use regtest mode? Do I need to add real nodes via addnode for regtest? It doesn't seem like I should need to. I was able to get a few connections by adding several real nodes one time but after a few minutes 'connections' dropped back to 0. Let me know if any additional info would help.

8

You need to run more than one node on your machine (you can do that if you give each node a different -port and -rpcport). And you need to tell them how to find each other (using -connect=127.0.0.1:portnumber).

There are examples written in bash and python in the Bitcoin Core source tree: https://github.com/bitcoin/bitcoin/tree/master/test/functional

  • 1
    Thanks I will look at the examples. Is regtest appropriate for developing mining systems or should I use testnet for that? – Joe M Jun 23 '14 at 20:32
10

Although I believe Fred Tingey's answer is complete, it has the following attributes:

  • it depends on a config file.
  • it provides examples for bitcoin-qt on the windows platform, and the OP was asking about bitcoind on the linux platform.

Thus, I wanted to share the following concise bash script which does what the OP wants. As others have partially noted the key to getting this to work is:

  • specify a unique port (if listening) and rpcport (if using rpc) for each node
  • specify a unique data directory for each node
  • if using -connect to restrict to own nodes, remember to use -listen to accept connections from own nodes.

That said, here's my start-regtest-network.sh script:

#!/bin/bash
bitcoind -server -listen -port=17591 -rpcuser=bitcoinrpc -rpcpassword=P$SECONDS -rpcport=16591 -datadir=$HOME/regtest/A/ -connect=localhost:17592 -regtest -pid=$HOME/regtest/A/.pid -daemon -debug
bitcoind -server -listen -port=17592 -rpcuser=bitcoinrpc -rpcpassword=P$SECONDS -rpcport=16592 -datadir=$HOME/regtest/B/ -connect=localhost:17593 -regtest -pid=$HOME/regtest/B/.pid -daemon -debug
bitcoind -server -listen -port=17593 -rpcuser=bitcoinrpc -rpcpassword=P$SECONDS -rpcport=16593 -datadir=$HOME/regtest/C/ -connect=localhost:17594 -regtest -pid=$HOME/regtest/C/.pid -daemon -debug
bitcoind -server -listen -port=17594 -rpcuser=bitcoinrpc -rpcpassword=P$SECONDS -rpcport=16594 -datadir=$HOME/regtest/D/ -connect=localhost:17595 -regtest -pid=$HOME/regtest/D/.pid -daemon -debug
bitcoind -server -listen -port=17595 -rpcuser=bitcoinrpc -rpcpassword=P$SECONDS -rpcport=16595 -datadir=$HOME/regtest/E/ -connect=localhost:17591 -regtest -pid=$HOME/regtest/E/.pid -daemon -debug

Additionally, here's my stop-regtest-network.sh:

#!/bin/bash
sudo killall --regex bitcoin.*

I realize my stop script is sloppy, and will kill all bitcoin processes.. if you're so inclined you could modify this to pull the individual pids out of each data directory.

Lastly, You will probably need to precreate some data directors for bitcoind if you don't edit the script at all.

mkdir $HOME/regtest/A/
mkdir $HOME/regtest/B/
mkdir $HOME/regtest/C/
mkdir $HOME/regtest/D/
mkdir $HOME/regtest/E/

You should have regtest bitcoind nodes connecting round-robin on ports 17591, 17592, 17593, 17594 and 17595. Likewise, you should be able to connect to rpc ports 16591, 16592, 16593, 16594 and 16595. If you need to know the RPC password you can check ps aux, or set an explicit rpcpassword in the script.

HTH

  • Nice. I'm getting an error ../.pid directory or file does not exist. is pid mandatory ? – Emeka Mbah Sep 12 '17 at 19:51
8

In order to run multiple nodes in regtest mode on a single machine you will need to sandbox each node.

In this example I have three nodes, they are named Alice, Bob and Cory. Since Bitcoin is a Peer/Mesh network, my goal is to connect each nodes so that changes made to Cory are ultimately visible to Bob (without necessarily requiring a direct connection between the two.)

Step 1: Create folders for each node.

These folders will contain our data and configs for each node. The names are not important, they can be renamed later, so you can name them "Alice", "Bob" and "Cory" for now. Remember where you create them, you will need their paths for Step 3 below.

Step 2: In each folder, create a bitcoin.conf file

In the config files Alice will be configured to connect to Cory, and Bob will be configured to connect to Alice. In addition to configuring outbound connections, this is also where we specify regtest=1 and server=1 settings.

bitcoin.conf for Alice

regtest=1
server=1
 # Alice connects to Cory
connect=localhost:18333

bitcoin.conf for Bob

regtest=1
server=1
 # Bob connects to Alice
connect=localhost:18444

bitcoin.conf for Cory

regtest=1
server=1
 # Cory connects to nobody

Step 3: Start each node.

Note each node must have a different port number since only one node can bind to a port number at any time.

  • Alice on port 18444
  • Bob on port 18555
  • Cory on port 18333

You can choose your own port numbers of course, just be careful not to use a port number that is already in use by something else.

The command-line options will tell bitcoin what port number to use, where to store data, and where to find its config file:

Alice:

"C:\Program Files\Bitcoin\bitcoin-qt.exe" -conf="C:\Alice\bitcoin.conf" -datadir="C:\Alice" -port=18444

Bob:

"C:\Program Files\Bitcoin\bitcoin-qt.exe" -conf="C:\Bob\bitcoin.conf" -datadir="C:\Bob" -port=18555

Cory:

"C:\Program Files\Bitcoin\bitcoin-qt.exe" -conf="C:\Cory\bitcoin.conf" -datadir="C:\Cory" -port=18333

This will launch three instances of the Bitcoin Client, and the folders for each node will begin to initialize with regtest data.

Step 4: Testing

You can use the Help->Debug Window->Console option to interactively execute commands on any of the nodes. For example, you can mine 101 blocks with the following bitcoin console command:

setgenerate true 101

You should (after a few seconds) then have been awarded coins.

These mining activities result in coinbase transactions - and only the last one of 50 BTC is available to spend. Now you can send some BTC to one of the other nodes. After a bit more mining such a transaction will be confirmed.

You can also use the console commands getpeerinfo and getinfo to check the network is set up correctly. One of them (Cory with port=18333) should have 'sync=true' in its peerinfo output, and all nodes should have the same "block height" regardless of which node is used for mining.

2

I would like to make an addition to above answers.

Instead of -connect=ip the correct option to use is -addnode=ip. -connect lets a node connect only to one node and whereas '-addnode' attempts at connecting to the specified list of nodes(ip) among other nodes.

  • i vaguely recall reading that --addnode will also allow discovery and use of nodes from peers? (whereas --connect will restrict to only the specified node.) if this is true it might be worth updating your answer, it would be good for readers to know – Shaun Wilson Apr 17 '16 at 17:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.