bitcoin-搭建测试网络

bitcoin 0.16.2
docker run -it --name bitcoinNode1 -v /Users/zhengjun/project/docker/ubuntu16.04:/home/data --net=bridge 938 /bin/bash

Regtest Mode(regression test mode)

创建私有的链,类似于 testnet 但是可以自己选择创建新块。可以完全控制整个网络。
注意不同节点的 DATA 目录要分开,不能使用同一个目录。

./bitcoind -regtest -printtoconsole -rpcuser=user -rpcpassword=password -txindex=1 -addrindex=1 -datadir=/home/data/bin_bitcoin_0.16.2/regtestData

查看端口,默认监听 18444 端口

netstat -pant

使用 rpc 创建 101 个区块,只有在 regtest mode 才支持该命令,因为使用默认规则,所以产生速度很快,regtest mode 只有前 150 块奖励 50 bitcoin
,只有经过 100 个确认才可以消费奖励。所以我们创建 101 块,才能访问第一块的 coinbase transaction 交易

## Bitcoin Core 0.10.1 and earlier
./bitcoin-cli -regtest -rpcuser=user -rpcpassword=password setgenerate true 101
## Bitcoin Core master (as of commit 48265f3)
./bitcoin-cli -regtest -printtoconsole -rpcuser=user -rpcpassword=password generate 101
./bitcoin-cli -regtest -printtoconsole -rpcuser=user -rpcpassword=password getbalance
50.00000000

添加节点

docker run -it --name bitcoinNode2 -v /Users/zhengjun/project/docker/ubuntu16.04:/home/data --net=bridge 938 /bin/bash
// 下面是用于退出后,登录容器的命令
docker exec -e COLUMNS="`tput cols`" -e LINES="`tput lines`" -it d5 bash

查看 1 节点的 ip,启动 2 节点,增加参数 addnode=172.17.0.2

./bitcoind -regtest -printtoconsole -rpcuser=user -rpcpassword=password -txindex=1 -addrindex=1 -addnode=172.17.0.2  -datadir=/home/data/bin_bitcoin_0.16.2/regtestDataNode2
// 查看信息
./bitcoin-cli -regtest -printtoconsole -rpcuser=user -rpcpassword=password getblockchaininfo
// 获取地址
./bitcoin-cli -regtest -printtoconsole -rpcuser=user -rpcpassword=password getnewaddress
2N7nvzMfpAHR9675TPdWGJG1wDVh2783Uug
// 从节点 1 发送比特币
./bitcoin-cli -regtest -printtoconsole -rpcuser=user -rpcpassword=password sendtoaddress 2N7nvzMfpAHR9675TPdWGJG1wDVh2783Uug 20
252fd4d3af8839c0349b52653aee654722e680d1759f550302e474e291cf2695
// 在节点 1 产生 100 个新区块来确认交易
./bitcoin-cli -regtest -printtoconsole -rpcuser=user -rpcpassword=password generate 100
// 这时查看节点 2 应该看到余额 20 列出钱包的 UTXO
./bitcoin-cli -regtest -printtoconsole -rpcuser=user -rpcpassword=password listunspent