Run Your Own Node for Bitcoin Testnet (For Blockchain Developers)
Standard
In this post, I show you how to set up your own Bitcoin Core for Testnet, and learn how to work with your own wallet and address for Bitcoin beginners (developers).
Mục lục bài viết
Prerequisite Settings
Here I’ll run Bitcoin Core on Ubuntu 18.04 in Microsoft Azure.
Bitcoin Core needs a large disk to store blocks. Then I expanded disks to around 1 TB by running the following commands in Azure Cloud Shell (Bash).
# deallocate VM
# (replace "myvm01" with your VM name and "BITCOIN-TESTNET-rg" with your resource group name)
az vm deallocate -n myvm01 -g BITCOIN-TESTNET-rg
# see your disk name ("name" attribute)
# (here we assume the name is "myvm01_OsDisk_1_f5cf4517593d468eb05597f5a4594072")
az disk list -g BITCOIN-TESTNET-rg
# update disk size
# (replace "--name" attribute's value in below with your copied name)
az disk update --resource-group BITCOIN-TESTNET-rg --name myvm01_OsDisk_1_f5cf4517593d468eb05597f5a4594072 --size-gb 1000
# start your VM
az vm start -n myvm01 -g BITCOIN-TESTNET-rg
Set up and Run Bitcoin Core
First you should downlaod the latest Bitcoin Core binary from here and extract the tar file as follows. (Here I’ve downloaded Bitcoin Core 0.20.1.)
wget https://bitcoincore.org/bin/bitcoin-core-0.20.1/bitcoin-0.20.1-x86_64-linux-gnu.tar.gz
tar xvzf bitcoin-0.20.1-x86_64-linux-gnu.tar.gz
cd bitcoin-0.20.1
Now you can soon start your own Bitcoin testnet’s node as follows.
For the first time, it will synchronize and verify all blocks, and synchronize transaction index (txindex) in mempool. Then please be patient to wait till it’s ready. (It has taken several hours in my case.)
./bin/bitcoind -testnet \
-rpcuser=myrpc01 -rpcpassword=password01 \
-fallbackfee=0.0002 \
-txindex=1 -server
See here about fallback fee settings in testnet.
By running above command, Bitcoin Core will run using the default port 18332. All files (block files, wallet database, etc) will be located in the default datadir ~/.bitcoin/testnet3
.
ls ~/.bitcoin/testnet3
Now, Work With Your Own Wallet
You can easily work with your node using Bitcoin CLI commands in your node.
For instance, run the following CLI command to see whether it’s correctly running.
./bin/bitcoin-cli -testnet -getinfo \
-rpcuser=myrpc01 -rpcpassword=password01
When you have installed and started your node above, there’ll be no address and balance in your wallet at first.
Now let’s create a new address in your wallet by invoking RPC command “getnewaddress
” through CLI.
./bin/bitcoin-cli -testnet \
-rpcuser=myrpc01 \
-rpcpassword=password01 \
getnewaddress
tb1qwk2evu...
For submitting some transaction in your application, you need a private key for address. In order to retrieve this key, you can dump a key using dumpprivkey
command as follows. (Use this key in your blockchain application.)
./bin/bitcoin-cli -testnet \
-rpcuser=myrpc01 \
-rpcpassword=password01 \
dumpprivkey tb1qwk2evu...
cRAkycGAHeXCy2d9ezWK...
You can list the generated address in your wallet by invoking listreceivedbyaddress
command as follows.
./bin/bitcoin-cli -testnet \
-rpcuser=myrpc01 \
-rpcpassword=password01 \
listreceivedbyaddress 1 true
[
{
"address": "tb1qwk2evu...",
"amount": 0.00000000,
"confirmations": 0,
"label": "",
"txids": [
]
}
]
As you can see above (see “amount” attribute), there’s no balance in your generated address and then you cannot issue any transaction. (The transaction fee is needed for running any transaction.)
If you want to deposit some amount of BTCs in testnet, you can use the bitcoin testnet faucet on the Web, such as, here. (Search for other bitcoin testnet faucet sites on the Web.)
Unlike Bitcoin mainnet, there’s no need to pay real money for getting BTCs (satoshis) in Bitcoin testnet.
The resource in testnet is limited and then you can get a little amount of BTCs at once with these faucet site. When you need large amount of charge, consider to run a standalone (sandbox) Bitcoin network, such as, bitcoin-regnet (regtest) or bitcoin-testnet-box.
Soon you can see the unconfirmed balance by running getwalletinfo
command as follows. (See unconfirmed_balance
in below.)
Several minutes later (till it’s mined), this transaction might be confirmed and the amount will be charged in your wallet (address).
./bin/bitcoin-cli -testnet \
-rpcuser=myrpc01 -rpcpassword=password01 \
getwalletinfo
{
"walletname": "",
"walletversion": 169900,
"balance": 0.00000000,
"unconfirmed_balance": 0.10000000,
"immature_balance": 0.00000000,
"txcount": 1,
"keypoololdest": 1598404106,
"keypoolsize": 999,
"hdseedid": "3640a39df478c905f88e0c9d6a66e7c5001d0c4a",
"keypoolsize_hd_internal": 1000,
"paytxfee": 0.00000000,
"private_keys_enabled": true,
"avoid_reuse": false,
"scanning": false
}
Now let’s issue some transaction.
You can issue any raw transaction using sendrawtransaction
command, but here I simply issue a payment transaction sending BTCs by running sendtoaddress
command as follows.
The transaction fee is required for issuing transaction, then you cannot send all coins you have. (You should leave several BTCs for paying transaction fee.)
You wallet is not encrypted by default. If you have encrypted your wallet for safety, please run
walletpassphrase
command to unlock your wallet before running the followingsendtoaddress
.
# send 0.7 BTC to other address
./bin/bitcoin-cli -testnet \
-rpcuser=myrpc01 \
-rpcpassword=password01 \
sendtoaddress tb1qz92ard... 0.7
You can also view transactions using online Bitcoin explorer (see here).
I note that RPC commands will differ (deprecated or modified) from each versions.
For details about RPC commands in each version, see the reference document as follows. (Change the version number in the following Uri.)
RPC command’s reference (Version 0.20.0)
https://bitcoincore.org/en/doc/0.20.0/rpc/
(To see the command’s reference, click the command’s group in the right navigation.)
Share this:
Like this:
Like
Loading…