If you need some testnet bitcoins, you can easily get them by solo mining. Some other guides I've found in the web are outdated, so I made this one to work with the latest software/hardware.
You'll need:
- Latest version of Bitcoin Core(v23.0 at the time of writing)
- Special build of cgminer that allows solo mining, available from https://github.com/cmmodtools/cgminer with a small update from this guide.
- Ideally an ASIC miner. You can use any miner that works with cgminer, even the USB miner from GekkoScience, the Compac F:
To make things simple, for this guide I am going to assume you have two computers, one running a Compac F miner and another one running Bitcoin core. It shouldn't matter if you're running both on the same computer or if you have a different miner.
Step 1: Install and run Bitcoin CoreWe're going to use the folder
~/bitcoin_testnet to save everything so that it's easy to remove once you're done and to keep things simple. You can of course use a different path if you want.
Inside ~/bitcoin_testnet create a folder called data:
mkdir -p ~/bitcoin_testnet/data
Download the
Bitcoin Core for your platform into ~/bitcoin_testnet/ and extract it there.
Now let's prepare the configuration file. Create a text file named
bitcoin_testnet.conf in ~/bitcoin_testnet/ and put these content in it:
testnet=1
txindex=1
server=1
[test]
rpcport=5000
rpcallowip=YOUR_MINER_IP
rpcuser=YOUR_RPC_USER
rpcpassword=YOUR_RPC_PASSWORD
rpcbind=0.0.0.0
The thing that you need to understand here is that you're setting up the Bitcoin node to run in testnet, and you're defining an RPC port(5000 in this case, can be anything), user and password, and whitelisting a specific IP to connect to your node(YOUR_MINER_IP). You'll need these details and your Bitcoin node IP later when connecting from cgminer.
You can now start running your Bitcoin node by doing the following(make sure to change user to your actual user:
/home/user/bitcoin_testnet/bitcoin-23.0/bin/bitcoind -conf=/home/user/bitcoin_testnet/bitcoin_testnet.conf -datadir=/home/user/bitcoin_testnet/data
If you want, you can keep this running in the background with
screen:
screen -dm -S bitcoin_testnet /home/user/bitcoin_testnet/bitcoin-23.0/bin/bitcoind -conf=/home/user/bitcoin_testnet/bitcoin_testnet.conf -datadir=/home/user/bitcoin_testnet/data
You can then see what's happening with:
screen -r bitcoin_testnet
To detach the screen, simply press
Ctrl-A and then
Ctrl-D. You'll be back in the console, and the command will continue running in the background.
Step 2: Install and run cgminerWe're going to use an updated repo of cgminer, this one has the latest changes from kano so that we can use the Compac F, plus some fixes that allow solo mining. Of course you might want to change the options to match your environment or miner.
cd ~/bitcoin_testnet/
git clone https://github.com/cmmodtools/cgminer
cd cgminer
./autogen.sh
CFLAGS="-O2 -Wall -march=native -fcommon" ./configure --enable-gecko
make
Now, in theory we should be ready, but we need one extra change. Open the file
cgminer.c with your favorite text editor and change this line:
if (opt_btc_address[0] != '1') {
To this:
if (opt_btc_address[0] != '1' && opt_btc_address[0] != 'm' && opt_btc_address[0] != 'n' ) {
Basically we need to also consider the first symbol of the testnet address, which is either n or m. OK, now run make again:
And now cgminer is compiled and ready. Let's create the configuration file:
Create a text file named
~/bitcoin_testnet/cgminer_testnet.conf with these contents:
{
"pools" : [
{
"url" : "http://YOUR_BITCOIN_CORE_IP:5000",
"user" : "YOUR_RPC_USER",
"pass" : "YOUR_RPC_PASSWORD"
}
],
"btc-address": "YOUR_TESTNET_BTC_ADDRESS",
"gekko-compacf-freq" : "400",
"gekko-compacf-detect" : true,
"gekko-start-freq" : "300",
"gekko-mine2" : true,
"gekko-tune2" : "60",
"suggest-diff" : "442",
"failover-only" : true,
}
If you don't have a testnet btc address yet, note that Bitcoin Core now doesn't generate a wallet by default any more so you will need to create one. You can read about it in
createwallet. Then you should be able to
getnewaddress. Make sure to use the legacy format as that's what cgminer is expecting. You address should start with n or m.
Alternatively, you can simply use the GUI. To open the GUI, first stop the bitcoind process (Ctrl-C) and then run bitcoin-qt instead. There you'll be able to easily create wallets and generate addresses by clicking around.
And now you can simply run cgminer like this:
sudo /home/user/bitcoin_testnet/cgminer/cgminer -c /home/user/bitcoin_testnet/mine_testnet.conf
You can of course also run it in the background with screen, or run this at startup, etc. It will depend on your particular setup. If you're on a pi for example, you could create a bash shell executable (~/start_mining.sh) with that code and then simply add this to
/etc/rc.local:
su - pi -c "screen -dm -S cgminer ~/start_mining.sh"
That's just one example, you can choose to run it however you prefer.
There it is, it should now say:
Solo mining to valid address: YOUR_TESTNET_BTC_ADDRESS
If you leave it overnight you should hit a few blocks with the Compac F USB miner.
Happy testnet solo mining!