转自 https://blog.csdn.net/yzpbright/article/details/81004202

比特币

一、安装 Docker

二、安装和运行比特币测试网络(bitcoin-testnet)

1.下载比特币测试网络(bitcoin-testnet)的Docker镜像

docker pull freewil/bitcoin-testnet-box

2.运行Docker镜像

docker run -t -i -p 19001:19001 -p 19011:19011 freewil/bitcoin-testnet-box

输出如下类似:

@ubuntu:~$ sudo docker run -t -i -p 19001:19001 -p 19011:19011 freewil/bitcoin-testnet-box
tester@647e5bee8412:~/bitcoin-testnet-box$

3.启动测试网络

进入Docker运行环境后,输入下面的命令来启动两个比特币节点,从而组成比特币测试网络:

make start

输出如下类似:

tester@647e5bee8412:~/bitcoin-testnet-box$ make start
bitcoind -datadir=1 -daemon
Bitcoin server starting
bitcoind -datadir=2 -daemon
Bitcoin server starting

启动成功后,将在本机模拟运行两个比特币测试钱包节点,组成一个私有范围的比特币测试网络。

输入下面的命令可以查看测试网络节点状态信息:

make getinfo

输出如下类似:

bitcoin-cli -datadir=1 getinfo //第一个钱包节点的信息
{
​ “version”: 120100,
​ “protocolversion”: 70012,
​ “walletversion”: 60000,
​ “balance”: 0.00000000,
​ “blocks”: 0,
​ “timeoffset”: 0,
​ “connections”: 1,
​ “proxy”: “”,
​ “difficulty”: 4.656542373906925e-10,
​ “testnet”: false,
​ “keypoololdest”: 1467253951,
​ “keypoolsize”: 101,
​ “paytxfee”: 0.00000000,
​ “relayfee”: 0.00001000,
​ “errors”: “”
}
bitcoin-cli -datadir=2 getinfo //第二个钱包节点的信息
{
​ “version”: 120100,
​ “protocolversion”: 70012,
​ “walletversion”: 60000,
​ “balance”: 0.00000000,
​ “blocks”: 0,
​ “timeoffset”: 0,
​ “connections”: 1,
​ “proxy”: “”,
​ “difficulty”: 4.656542373906925e-10,
​ “testnet”: false,
​ “keypoololdest”: 1467253951,
​ “keypoolsize”: 101,
​ “paytxfee”: 0.00000000,
​ “relayfee”: 0.00001000,
​ “errors”: “”
}

我们可以顺便查看下两个节点的bitcoin.conf文件是如何配置的
节点1的配置文件,路径:/home/tester/bitcoin-testnet-box/1/bitcoin.conf

# testnet-box functionality
regtest=1
dnsseed=0
upnp=0
# listen on different ports than default testnet
port=19000
rpcport=19001
# always run a server, even with bitcoin-qt
server=1
# enable SSL for RPC server
#rpcssl=1
rpcallowip=0.0.0.0/0
rpcuser=admin1
rpcpassword=123

节点2的配置文件,路径:/home/tester/bitcoin-testnet-box/2/bitcoin.conf

内容类似节点1。

再顺便查看下节点1的对等节点的信息:

tester@0cc1d63a966c:~/bitcoin-testnet-box$ bitcoin-cli -datadir=1  getpeerinfo

[
{
​ "id": 0,
​ "addr": "127.0.0.1:52766",
​ "addrbind": "127.0.0.1:19000",
​ "services": "000000000000040d",
​ "relaytxes": true,
​ "lastsend": 1531383259,
​ "lastrecv": 1531383259,
​ "bytessent": 429,
​ "bytesrecv": 453,
​ "conntime": 1531383259,
​ "timeoffset": 0,
​ "pingtime": 0.002959,
​ "minping": 0.002959,
​ "version": 70015,
​ "subver": "/Satoshi:0.16.0/",
​ "inbound": true,
​ "addnode": false,
​ "startingheight": 0,
​ "banscore": 0,
​ "synced_headers": -1,
​ "synced_blocks": -1,
​ "inflight": [
​ ],
​ "whitelisted": false,
​ "bytessent_per_msg": {
​ "feefilter": 32,
​ "getheaders": 93,
​ "ping": 32,
​ "pong": 32,
​ "sendcmpct": 66,
​ "sendheaders": 24,
​ "verack": 24,
​ "version": 126
​ },
​ "bytesrecv_per_msg": {
​ "feefilter": 32,
​ "getaddr": 24,
​ "getheaders": 93,
​ "ping": 32,
​ "pong": 32,
​ "sendcmpct": 66,
​ "sendheaders": 24,
​ "verack": 24,
​ "version": 126
​ }
}
]

再顺便查看下节点2的对等节点的信息:

tester@0cc1d63a966c:~/bitcoin-testnet-box$ bitcoin-cli -datadir=2  getpeerinfo

输出与节点1类型

4. 初始化和区块链数据

使用getnewaddress命令分别为两个钱包生成一个地址(或者用命令 make address1 也可以):

tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ bitcoin-cli -datadir=1 getnewaddress
2NAUVNvRVKn2QT2yoKYo1LXBBHES1DiiAWp
tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ bitcoin-cli -datadir=2 getnewaddress
2N5czXHSEFronnYvMHUhSinQW8jjp7UjRtu

这里生成的KEY,根据具体情况会发生变化

查看地址对应的私钥:

tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ bitcoin-cli -datadir=1  dumpprivkey  2NAUVNvRVKn2QT2yoKYo1LXBBHES1DiiAWp
cP5cEwanYgwJTUA6RG43DQ9p3ErpmqdPrJK7kX48zaMKkEfoL8s3
tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ bitcoin-cli -datadir=2  dumpprivkey  2N5czXHSEFronnYvMHUhSinQW8jjp7UjRtu
cQ8UyP5CoJ2SbHXdJagxxHSjYnxd69kaCaSU7uMPq6iRxpEhpuYP

注意:在正式的比特币网络环境下,平均是10分钟左右才能产生一个新的区块。但在这里的测试网络(testnet)特殊设定的环境下,区块是通过简单的命令控制就可以即时和批量产生的,方便程序开发测试。

生成一个区块:

tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ make generate

输出类似如下:

bitcoin-cli -datadir=1 generate 1
[
"5db89a9818792f8665b6540d32c3834856cb6a07ed34aecb03128e96cbe3658a"
]

继续生成9个区块:

tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ make generate BLOCKS=9  

bitcoin-cli -datadir=1 generate 9
[
"61bc358db1ddb82972b830a00cb1dd27a7e6f3f5b409d667ff90af7a72caeb75",
"0eedb9ecb498df0e638013343ca97189ca768c54592ed5155a2ccbedf13f5309",
"7f74386d70b295102c5e44e373232648f7c07e00918c0b6de1ad033c3c121b38",
"7ca80b7bf1209d18e56b5c399992171c074c566e3109376b09877401b86a0b1d",
"61f0b2f3f4d2d4d199d7cbbed9bba721e65de3e30f474042c0eb4ce4cfc1ad54",
"2f97d16c46e5ccc46b795b9ee72f58720c494ae0f48d98d70a5b5a9e8b5be2ba",
"26edd9fd85b3e75ba7e731596ac065afbc29f68d8a6dd16f928d1e898ae18fb6",
"684fa2b10a662bffdd7e4e148e3a73c430cf7bbc14d0ed320200e008dd1ea625",
"2b62353790c59d2e643c815d56952264052dd99876a61e0d46e81accd7661262"
]

此时查看钱包信息:

tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ make getwalletinfo

输出类似如下:

bitcoin-cli -datadir=1 getwalletinfo
{
"walletname": "wallet.dat",
"walletversion": 159900,
"balance": 0.00000000,
"unconfirmed_balance": 0.00000000,
"immature_balance": 500.00000000,
"txcount": 10,
"keypoololdest": 1531366844,
"keypoolsize": 999,
"keypoolsize_hd_internal": 1000,
"paytxfee": 0.00000000,
"hdmasterkeyid": "0ac63ad922d529b4ff4be71a10f1751c3978b5f5"
}
bitcoin-cli -datadir=2 getwalletinfo
{
"walletname": "wallet.dat",
"walletversion": 159900,
"balance": 0.00000000,
"unconfirmed_balance": 0.00000000,
"immature_balance": 0.00000000,
"txcount": 0,
"keypoololdest": 1531366844,
"keypoolsize": 999,
"keypoolsize_hd_internal": 1000,
"paytxfee": 0.00000000,
"hdmasterkeyid": "78f6a0f992aae42ff3504477f150a65e86c1f287"
}

结果显示钱包1的balance(余额)里根本没有任何比特币,但immature_balance(未成熟的余额)里有相应的500个比特币,这是为什么呢?

结果显示,钱包1和钱包2都有20个区块了,没有什么问题,那么为什么钱包1的balance里没有比特币呢?

继续创建200个区块:

tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ make generate BLOCKS=200

再查询钱包余额:

tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ make getwalletinfo

bitcoin-cli -datadir=1 getwalletinfo
{
"walletname": "wallet.dat",
"walletversion": 159900,
"balance": 6000.00000000,
"unconfirmed_balance": 0.00000000,
"immature_balance": 3225.00000000,
"txcount": 220,
"keypoololdest": 1531366844,
"keypoolsize": 999,
"keypoolsize_hd_internal": 1000,
"paytxfee": 0.00000000,
"hdmasterkeyid": "0ac63ad922d529b4ff4be71a10f1751c3978b5f5"
}
bitcoin-cli -datadir=2 getwalletinfo
{
"walletname": "wallet.dat",
"walletversion": 159900,
"balance": 0.00000000,
"unconfirmed_balance": 0.00000000,
"immature_balance": 0.00000000,
"txcount": 0,
"keypoololdest": 1531366844,
"keypoolsize": 999,
"keypoolsize_hd_internal": 1000,
"paytxfee": 0.00000000,
"hdmasterkeyid": "78f6a0f992aae42ff3504477f150a65e86c1f287"
}

结果显示钱包1的balance(余额)里6000个比特币了。

到这里,可以初步得出结论是之前生成的20个区块还太”新鲜”,不够”老”,需要后面生成更多的区块来完成确认。

(补充:后来查询资料知道,挖矿所得的coinbase交易中的奖励必须要在100个区块深度之后才能花费。)

给钱包2转账,这里转10个比特币:

tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ make sendfrom1 ADDRESS=2N5czXHSEFronnYvMHUhSinQW8jjp7UjRtu  AMOUNT=10
bitcoin-cli -datadir=1  sendtoaddress 2N5czXHSEFronnYvMHUhSinQW8jjp7UjRtu 10
967d37c71c1f768ff75e8ee675cf0dc5899a953bb4954b3b0f8ad3f25fc2aa52

这时,查询钱包余额,

tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ make getwalletinfo

结果显示钱包2的10个比特币还未确认。

好办,继续生成10个区块,使得交易得到确认:

tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ make generate BLOCKS=10

这时,再查看钱包余额:

tester@0f4ec9f7a91a:~/bitcoin-testnet-box$ make getwalletinfo

从结果可以看到钱包2的10个比特币到账了,说明交易得到了确认。

posted on 2018-11-28 14:49  陈峰  阅读(3532)  评论(2编辑  收藏  举报