Is there a sandbox in Bitcoin for testing, so that I can create sample bitcoins without mining and do transactions for testing purposes?
3 Answers
Testnet is really heavy (hundreds of gigabytes), there is a lighter alternative to that now: signet
. There is also a local-only network called regtest
, which I put in its own section. Finally, I'll explain txindex
, which is a useful option to have your own mini block explorer.
Signet
To use it, first start bitcoind with the option:
bitcoind -signet
It will need to sync, but it will be a lot faster than testnet since the chain started recently and as of writing only has about 27000 blocks:
% bitcoin-cli -signet getblockchaininfo
{
"chain": "signet",
"blocks": 26029
You can use bitcoin-cli -signet getnewaddress
to get a new address, which you can paste into the faucet: https://signet.bc-2.jp/ (if it complains that you have no wallet, see next section). Note how signet
addresses also start with tb
(which is called the "human readable prefix", hrp), just like testnet addresses.
% bitcoin-cli -signet listunspent
[
{
"txid": "9484eff65e5c33f601a8d6446859ffbc6ba59ffd53dca564d66a7c09e040c26a",
"address": "tb1q7x3nnyskqmgrj9hp95fgfg75fz50x4khay3s0t",
"amount": 10.00000000,
"confirmations": 3,
If you don't see anything right away, wait a bit. Once the transaction arrives, you'll see the confirmation count rising at a regular interval. Signet has much more regular blocks than testnet.
Regtest
The regtest is not really a network, since it isn't meant to be used publicly. The chain starts with the genesis, and proof-of-work is effectively disabled, so you can just mine your own blocks. This is useful if you need to test reorgs and stuff like that, which you won't be able to do with signet.
% bitcoind -regtest
Let it run in the a separate terminal window.
Now you should have an empty chain:
% bitcoin-cli -regtest getblockchaininfo
{
"chain": "regtest",
"blocks": 0,
Now, lets see if you have a wallet:
% bitcoin-cli -regtest getwalletinfo
error code: -18
error message:
No wallet is loaded.
If you get an error like this, create a wallet:
% bitcoin-cli -regtest createwallet testwallet
{
"name": "testwallet",
"warning": ""
}
Now, you can create a new address:
% bitcoin-cli -regtest getnewaddress
bcrt1q0qx0d7prvrd64s3e3trq39fet7jwdecstw5tzu
Note how the HRP mentioned above is now "bcrt", "bitcoin regtest".
Mining some coins
That's right, CPU mining is alive and well ;) In fact, regtest only has blocks when you generate some, yourself. That's nice for testing, you have time to figure out.
You can mine directly to the wallet (so-called coinbase outputs, you may have heard of a company named after these). Here I am using the shell to insert an address generated using the command from above (should work in PowerShell on Windows too):
% bitcoin-cli -regtest generatetoaddress 10 $(bitcoin-cli -regtest getnewaddress)
[
"2e25176b5122a3d16eca11355b2807ebf99e8ef3cf01e32634417baf47cb9a4b",
"3e8136a62d513925a075700e92789d70da67d5b3dd986e936d65b12a3ba53c72",
"55ca10e3932a9087259a3826148aa660e09b69b63fe84d6a81bc4e57de052060",
"4864e894662693ae7e32cb0d2a8bc3ad94c4f5e67e109873555bc5f7b5c166bc",
"0de8a4bfb5fa5a38fba39dcf9fe360e3382e6fa56c226553c30736525bea659e",
"69e3a04724793b90de126358a99f297b821cac83b9be12f3d569effd73729199",
"3eb2a83540ce04ef75f5258359ee1695fa0b2e6cbc04a078f85e3182cd500004",
"35e39307638bca2db359ee8fa6930fbbf20e5d465882c50e05c05e2510ca2e28",
"12f2b855729929aa091a7cffccb66886a6c406c7855f8851c25ab162b17455f1",
"2414f3ddfa767aa1916f296066d35ae58c828b49e84542c57509e7161201b746"
]
We mined some bitcoin, but you have to remember that there is "coin maturity", which means that coins can not be used immediately after mining, not even on regtest.
So after mining these 10 blocks, you'll see that the wallet still reports no unspent outputs:
% bitcoin-cli -regtest listunspent
[
]
To mature the coins, mine a hundred blocks more (I'll omit the output, it's just the command above with 100
instead of 10
), and you'll see the coins mined in the first command:
% bitcoin-cli -regtest listunspent
{
"txid": "6fe92df06b95a20db44f989471483802a9aebc56e93057dca9fe0546445d35ff",
"vout": 0,
"address": "bcrt1qlu9a5687f3m4ct3r5ndlwy8juppgghysy2s67f",
"label": "",
"scriptPubKey": "0014ff0bda68fe4c775c2e23a4dbf710f2e042845c90",
"amount": 50.00000000,
"confirmations": 110,
"spendable": true,
"solvable": true,
"desc": "wpkh([a45ff478/0'/0'/2']02ebc0275e56ecca4a80d6a6ff7d7ab884c972cff9db9a91b9bb3681741624d41a)#rgeusc6u",
There will be a ten tx's like this, because the ten blocks that we initially mined have all matured. They have confirmation counts from 101 to 110.
Processing bitcoin output with jq
Note how you can use standard JSON tools to extract information from the bitcoin-cli
output.
For example, we can verify the number of unspent wallet outputs. In the example from the previous section, we mined 110 blocks, that each have one coinbase output for the Core wallet. But only the first 10 have matured:
% bitcoin-cli -regtest listunspent | jq length
10
Starting over, nuke the chain
You'll possibly want to reset your testing environment before and/or after running a test. With regtest, you can remove the whole blockchain. So shut down the bitcoind
process that we started above. You can simply wipe the regtest directory, but be very careful that you don't wipe the wrong one (actually, you shouldn't even have real (mainnet) coins in the filesystem that you run your tests from).
% rm -rf ~/.bitcoin/regtest/
This isn't a true factory reset, since if you have any configuration in ~/.bitcoin/bitcoin.conf
, it would still be loaded. So if something is still working unexpectedly, check your config.
Deleting a signet blockchain makes less sense, since you'd just download the exact same chain after relaunching (unless something is horribly broken). So to ensure a clean signet testing environment, maybe just wipe everything except blocks
, e.g. chainstate
, wallets
and indexes
.
Txindex, your own block explorer
Bitcoin Core has a neat feature that lets you look up information on an arbitrary transaction. But you enable must it first. To do that, write txindex=1
in ~/.bitcoin/bitcoin.conf
, possibly in a regtest
or signet
section, though I just put it at the top-level since my mainnet coins are elsewhere, and it is useful for all testing.
Now, with txindex enabled, you can retrieve information on, lets say, the first transaction of block 1 (right after genesis):
bitcoin-cli -signet decoderawtransaction $(
bitcoin-cli -signet getrawtransaction $(
bitcoin-cli -signet getblock $(
bitcoin-cli -signet getblockhash 1
) | jq -r '.tx[0]'
)
)
You get, for example, the output script these coins were locked with:
...
"vout": [
{
"value": 50.00000000,
"n": 0,
"scriptPubKey": {
"asm": "0 9243f727dd5343293eb83174324019ec16c2630f",
The "getrawtransaction" step requires txindex.
This is similar to information that you'll see in a block explorer. In this example, I used signet (which has a public block explorer), but for regtest you obviously won't have a block explorer available, so the txindex could come in handy.
-
2Thank you for rising to my challenge and going above and beyond!– Murch ♦Commented Feb 22, 2021 at 19:29
first install the software(bitcoin-qt) by following the steps:
sudo apt-add-repository ppa:luke-jr/bitcoincore
sudo apt-get update
sudo apt-get install bitcoin-qt
run:
bitcoin-qt -testnet
since testnet
is huge, as @Janus Troelsen suggest, there is a lighter version(signet
):
bitcoin-qt -signet
click on the Receive
button in the screenshot to get the address
go to link http://tpfaucet.appspot.com/ and give the address
to get testnet bitcoins
.
-
1i installed it successfully. may be you need to run
sudo apt update
– suhailvsCommented Feb 23, 2021 at 1:11
You can use testnet, it is a separate chain: https://en.bitcoin.it/wiki/Testnet
-
Can i create
bitcoins
withoutmining
?. if not then how to minebitcoin
ontestnet
– suhailvsCommented Nov 30, 2013 at 7:37 -
There are bitcoin "faucets" for testnet bitcoins. tpfaucet.appspot.com Commented Nov 30, 2013 at 8:39
-
If you want to run
bitcoin
/bitcoind
with testnet, just use-testnet
flag. Commented Nov 30, 2013 at 8:54 -
@KarelBílek thank you very much for your help. but still i have
0.00
btc balance– suhailvsCommented Nov 30, 2013 at 9:16 -