Bitcoin
Build Instructions
- link: https://github.com/bitcoin/bitcoin
- linux build
# there is a document about linux build instruction (./doc/build-unix.md)
$ cmake -B build
$ cd ./build
$ make
# if you need to install binaries
$ make install
Mining Instructions
Wallet setup for mining
To start mining on your regtest node, you need to have a wallet set up to receive mining rewards.
# If you use bitcoin as test node mode, You can modify the ".bitcoin/bitcoin.conf" file like below
# testnet=1
# Wallet commands
$ bitcoin-cli createwallet "mywallet"
$ bitcoin-cli listwallets
$ bitcoin-cli getwalletinfo
# Load your wallet when your wallet is already created
$ bitcoin-cli loadwallet "mywallet"
# Start mining to a new address of your wallet
$ YOUR_ADDRESS=$(bitcoin-cli getnewaddress)
$ echo $YOUR_ADDRESS >> ~/.bitcoin/mining_address.txt
# Or load an latest address from file (last line)
$ YOUR_ADDRESS=$(tail -n 1 ~/.bitcoin/mining_address.txt)
# It is only for regtest or testnet mode (It is a feature for testing purpose)
# Fist agument is number of blocks to mine
# Second argument is your bitcoin address to receive mining rewards
$ bitcoin-cli generatetoaddress 1 $YOUR_ADDRESS
# Mine 1..100 blocks to your address -> mininum 100 blocks are needed to spend mining rewards
$ bitcoin-cli generatetoaddress 100 $YOUR_ADDRESS
# Check your "hdkeypath" field if "m/84h/1h/0h/0/7" --> it is bech32 address and the wallet index is 7
$ bitcoin-cli getaddressinfo $YOUR_ADDRESS
# Check private key of the address you are mining to (be careful with private keys!)
# Look at the 7th entry (index 7) for the private key information
$ bitcoin-cli listdescriptors true | jq
# See mining rewards in your wallet
# total balance should increase after mining
$ bitcoin-cli getbalance
# each mined block reward
$ bitcoin-cli listreceivedbyaddress 1 true
Mining (Full Node)
# There is a document about mining instruction (./doc/mining.md)
$ bitcoind
Mining (RegTest)
# RegTest mode is for local testing purpose
# You can set up your own private bitcoin network
bitcoind -regtest
# or
CONF_FILE=~/.bitcoin/bitcoin.conf
cat > $CONF_FILE << 'EOF'
regtest=1
[regtest]
server=1
rpcuser=root
rpcpassword=root
rpcallowip=127.0.0.1
rpcbind=127.0.0.1
rpcport=8332
fallbackfee=0.00001
mintxfee=0.00001
txindex=1
EOF
bitcoind
Mining (TestNet)
# Setting up a full node is needed too large storage space (over 500GB)
# But testnet is having small storage space (around 50GB)
# If you want to set up test net node, run the following command
# It is enough to test bitcoin commands
$ bitcoind -testnet
# or
$ echo "testnet=1" >> ~/.bitcoin/bitcoin.conf
$ bitcoind
Bitcoin Directory Structure
witnessjo ~/.bitcoin $ ls -al
total 27544
drwx------ 5 witnessjo witnessjo 4096 Nov 3 22:59 .
drwx------ 45 witnessjo witnessjo 4096 Nov 3 23:11 ..
-rw------- 1 witnessjo witnessjo 221 Nov 3 22:59 banlist.json
-rw------- 1 witnessjo witnessjo 6 Nov 3 22:59 bitcoind.pid
drwx------ 3 witnessjo witnessjo 4096 Nov 3 23:04 blocks
drwx------ 2 witnessjo witnessjo 4096 Nov 3 22:59 chainstate
-rw------- 1 witnessjo witnessjo 75 Nov 3 22:59 .cookie
-rw------- 1 witnessjo witnessjo 28154187 Nov 3 23:11 debug.log
-rw------- 1 witnessjo witnessjo 0 Nov 3 22:59 .lock
-rw------- 1 witnessjo witnessjo 4210 Nov 3 22:59 peers.dat
-rw------- 1 witnessjo witnessjo 193 Nov 3 22:59 settings.json
drwx------ 3 witnessjo witnessjo 4096 Nov 3 23:05 wallets
- banlist.json: list of banned peers
- bitcoind.pid: process id file
- blocks/: block files
- chainstate/: UTXO set database
- .cookie: authentication cookie for RPC
- debug.log: log file
- peers.dat: known peers
- settings.json: user settings
- wallets/: wallet data
How to see the chainstate files (chainstate directory)
- The chainstate directory contains LevelDB files representing the UTXO set.
- You can use the `leveldb` tools to inspect the contents, but it’s generally not human-readable.
- For practical purposes, use Bitcoin Core’s RPC commands to query UTXO information.
# latest UTXO information can be queried using $ TXID=$(bitcoin-cli getblock $(bitcoin-cli getbestblockhash) 2 | jq -r '.tx[0].txid') $ bitcoin-cli gettxout $TXID 0 { "bestblock": "0000000000000000111424bacbcd919fc9f237f05e10127d3c8d00faeb4dc0e5", "confirmations": 315, "value": 25.01631000, "scriptPubKey": { "asm": "OP_DUP OP_HASH160 ed306ae5bc7cf7c3b7f0fe956149a7bd73d25ff2 OP_EQUALVERIFY OP_CHECKSIG", "desc": "addr(1Nd99aNgYWpKkqcqSMgWtdtVDadewAS5F7)#26ytyu96", "hex": "76a914ed306ae5bc7cf7c3b7f0fe956149a7bd73d25ff288ac", "address": "1Nd99aNgYWpKkqcqSMgWtdtVDadewAS5F7", "type": "pubkeyhash" }, "coinbase": true } ## description # bestblock: hash of the block containing this UTXO # confirmations: number of confirmations # value: amount of bitcoin in this UTXO # scriptPubKey: details about the script that locks this UTXO # coinbase: indicates if this UTXO is from a coinbase transaction (conbase transactions are special transactions that create new bitcoins)
How to see the block files (blocks directory)
- The blocks directory contains blk*.dat files which store the raw block data.
- You can use Bitcoin Core’s RPC commands to retrieve and decode block data.
How to start bitcoin mining
- To start mining on your full node, you need to have a wallet set up to receive mining rewards.
- Use the following command to start mining:
Wallet Instructions
Create a new wallet
- You can create a new wallet using the following command:
$ bitcoin-cli createwallet "mywallet"
List wallets
- To list all loaded wallets, use:
$ bitcoin-cli listwallets
Get wallet info
- To get information about the currently loaded wallet, use:
$ bitcoin-cli getwalletinfo
Load previously created wallet
- To load a previously created wallet, use:
$ bitcoin-cli loadwallet "mywallet"
Get more detailed information about wallet
-
To see generated addresses in your wallet, use :
# this show only addresses having transactions bitcoin-cli listaddressgroupings # this show all addresses in your wallet bitcoin-cli listreceivedbyaddress 0 true -
To generate a new address in your wallet, use:
bitcoin-cli getnewaddress
Mining to a specific address and checking balance
- To mine to a specific address instantly (!!! only for regtest or testnet mode), use:
# Set your mining address YOUR_ADDRESS=$(bitcoin-cli getnewaddress) echo $YOUR_ADDRESS >> ~/.bitcoin/mining_address.txt bitcoin-cli generatetoaddress 1 $YOUR_ADDRESS # Start mining 100 blocks to your address bitcoin-cli generatetoaddress 100 $YOUR_ADDRESS
Send bitcoin from your address to another address
-
To send bitcoin from your address to another address, use: #+begin_src bash
YOUR_ADDRESS=$(tail -n 1 ~/.bitcoin/mining_address.txt)
bitcoin-cli listunspent 1 9999999 ‘["’"$YOUR_ADDRESS"’"]’ bitcoin-cli listunspent | jq “.[] | select(.address==\"$YOUR_ADDRESS\”)"
RECEIVER_ADDRESS=$(bitcoin-cli getnewaddress) bitcoin-cli sendtoaddress $RECEIVER_ADDRESS 10.0
bitcoin-cli getbalance bitcoin-cli listreceivedbyaddress 1 true
HD Key Management
# Dump public key of your mining address
bitcoin-cli -regtest -rpcwallet=mywallet gethdkeys '{"private": false}' >> ~/.bitcoin/mining_public_keys.txt
Network Monitoring (Block Explorer)
btc-rpc-explorer
An Official blockchain explorer
# Before running btc-rpc-explorer, make sure your bitcoind is running with RPC server enabled.
# You need to set up config file (~/.bitcoin/bitcoin.conf) like below
#####################
# server=1
# rpcuser=root
# rpcpassword=root
# rpcallowip=127.0.0.1
# rpcbind=127.0.0.1
## for regtest network
# regtest=1 (for using regtest network as default)
#
# [regtest]
# rpcuser=root
# rpcpassword=root
# rpcallowip=127.0.0.1
# rpcbind=127.0.0.1
#####################
# And restart your bitcoind process
pkill bitcoind
bitcoind --daemon
# Or for regtest network
bitcoind -regtest -rpcport=8332 -port=8333
# Then run btc-rpc-explorer
# Default bitcoind rpc port is 8332
git clone https://github.com/janoside/btc-rpc-explorer
docker build -t btc-rpc-explorer ./btc-rpc-explorer
docker run -it --network host \
-e BTCEXP_HOST=0.0.0.0 \
-e BTCEXP_PORT=3002 \
-e BTCEXP_BITCOIND_HOST=127.0.0.1 \
-e BTCEXP_BITCOIND_PORT=8332 \
-e BTCEXP_BITCOIND_USER=root \
-e BTCEXP_BITCOIND_PASS=root \
-e BTCEXP_BASIC_AUTH_PASSWORD="" \
btc-rpc-explorer