Categories
bitcoin

Running mainnet and testnet on the same bitcoin node

In this post I will tell you how to run a mainnet and testnet bitcoin node simulataneously on the same device.

Bitcoin Conf Sections

It boils down to splitting out the bitcoin.conf into sections:

[main]
mempoolsize=300
maxconnections=20
maxuploadtarget=144
txindex=1

[test]
mempoolsize=100
maxconnections=20
maxuploadtarget=144
txindex=1

Then adding an additional systemd (or any other service manager you like like supervisord) startup script for bitcoind -testnet -daemon.

You don’t need to set the rpcport or other options as they are different anyway:

  • The default Bitcoin network protocol listen port is 18333 on the testnet (8333 for mainnet)
  • The default RPC connection port is 18332 for testnet (8332 for mainnet)

Setup Systemd scripts

In /etc/systemd/system/bitcoind_test.service:

[Unit]
Description=bitcoin
After=network.target

[Service]
Type=simple
User=ubuntu
Group=ubuntu

Environment=BITCOIN_PID=/home/btc/testnet/bitcoin.pid
Environment=BITCOIN_HOME=/home/btc/testnet/.bitcoin

ExecStart=/usr/local/bin/bitcoind -testnet
ExecStop=/bin/kill -15 $MAINPID

[Install]
WantedBy=multi-user.target

In /etc/systemd/system/bitcoind.service:

[Unit]
Description=bitcoin
After=network.target

[Service]
Type=simple
User=ubuntu
Group=ubuntu

Environment=BITCOIN_PID=/home/btc/bitcoin.pid
Environment=BITCOIN_HOME=~/home/btc/.bitcoin

ExecStart=/usr/local/bin/bitcoind
ExecStop=/bin/kill -15 $MAINPID

[Install]
WantedBy=multi-user.target

Enable the services:

sudo systemctl enable bitcoind_test
sudo systemctl enable bitcoind

Start the services:

sudo systemctl start bitcoind_test
sudo systemctl start bitcoind

Identifying the Network to use in Commands

Now the problem is that when running bitcoin-cli commands it will by default use mainnet, so you must specify testnet.

Example calling mainnet:

bitcoin-cli getblockcount

Example calling testnet:

bitcoin-cli -testnet getblockcount

Sources