小坚的技术博客

Ubuntu比特币测试环境搭建

本文作者:陈进坚
博客地址:https://jian1098.github.io
CSDN博客:https://blog.csdn.net/c_jian
简书:https://www.jianshu.com/u/8ba9ac5706b6
联系方式:jian1098@qq.com

安装bitcoin核心

1
2
3
4
root@ubuntu:~# sudo apt-get install software-properties-common
root@ubuntu:~# sudo apt-add-repository ppa:bitcoin/bitcoin
root@ubuntu:~# sudo apt-get update
root@ubuntu:~# sudo apt-get install bitcoind

配置bitcoin参数

1
2
3
root@ubuntu:~$ mkdir .bitcoin	#创建目录
root@ubuntu:~$ cd .bitcoin/
root@ubuntu:~$ vi bitcoin.conf

将下面信息全部复制,并修改rpcuser(RPC用户名),rpcpassword(RPC用户密码),rpcallowip(允许访问的ip地址)然后保存

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# Generated by https://jlopp.github.io/bitcoin-core-config-generator/

# This config should be placed in following path:
# ~/.bitcoin/bitcoin.conf

# [rpc]
# Accept command line and JSON-RPC commands.
server=1

# Username for JSON-RPC connections
rpcuser=bitcoinrpc

# Password for JSON-RPC connections
rpcpassword=bitcoinrpc

# Listen for JSON-RPC connections on this port
rpcport=18332

# Allow JSON-RPC connections from specified source. Valid for <ip> are a single IP (e.g. 1.2.3.4),
# a network/netmask (e.g. 1.2.3.4/255.255.255.0) or a network/CIDR (e.g. 1.2.3.4/24). This option
# can be specified multiple times.
rpcallowip=192.168.1.178
rpcallowip=192.168.1.179

# Run this node on the Bitcoin Test Network.
testnet=1

启动bitcoin程序

1
2
root@ubuntu:~/.bitcoin$ bitcoind -daemon
Bitcoin server starting

查看是否启动成功,需要输入上一步配置的密码,用户名bitcoinrpc改为你配置的用户名:

1
2
root@ubuntu:~$ curl --user bitcoinrpc --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getnetworkinfo", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:18332/
Enter host password for user 'bitcoinrpc':

能返回下面的信息表示启动成功

1
{"result":{"version":170000,"subversion":"/Satoshi:0.17.0/","protocolversion":70015,"localservices":"000000000000040d","localrelay":true,"timeoffset":0,"networkactive":true,"connections":5,"networks":[{"name":"ipv4","limited":false,"reachable":true,"proxy":"","proxy_randomize_credentials":false},{"name":"ipv6","limited":false,"reachable":true,"proxy":"","proxy_randomize_credentials":false},{"name":"onion","limited":true,"reachable":false,"proxy":"","proxy_randomize_credentials":false}],"relayfee":0.00001000,"incrementalfee":0.00001000,"localaddresses":[],"warnings":""},"error":null,"id":"curltest"}

停止bitcoin程序

1
2
root@ubuntu:~/.bitcoin/testnet3$ bitcoin-cli stop
Bitcoin server stopping

检查区块同步信息

检查同步日志(刷屏):

1
2
3
4
5
6
7
8
9
10
11
12
13
root@ubuntu:~/.bitcoin$ cd testnet3/
root@ubuntu:~/.bitcoin/testnet3$ tail -f debug.log
2018-11-21T02:40:28Z net thread start
2018-11-21T02:40:31Z New outbound peer connected: version: 70015, blocks=1444366, peer=1
2018-11-21T02:40:31Z New outbound peer connected: version: 70015, blocks=1444366, peer=0
2018-11-21T02:40:32Z 108 addresses found from DNS seeds
2018-11-21T02:40:32Z dnsseed thread exit
2018-11-21T02:40:36Z New outbound peer connected: version: 70015, blocks=1444366, peer=2
2018-11-21T02:40:37Z New outbound peer connected: version: 70015, blocks=1444366, peer=5
2018-11-21T02:40:37Z New outbound peer connected: version: 70015, blocks=1444366, peer=3
2018-11-21T02:40:41Z New outbound peer connected: version: 70015, blocks=1444366, peer=4
2018-11-21T02:40:48Z New outbound peer connected: version: 70015, blocks=1414448, peer=6
.......

查询当前同步到的区块数:

1
root@ubuntu:~$ curl --user bitcoinrpc --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblockcount", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:18332/

查询测试网络中的总区块:https://live.blockcypher.com/btc-testnet/

需要注意的是只有区块同步到最新才可以查到钱包余额,从头开始同步大概需要一天时间。

创建钱包地址

1
2
3
root@ubuntu:~$ curl --user bitcoinrpc --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getnewaddress", "params": ["test"] }' -H 'content-type: text/plain;' http://127.0.0.1:18332/
Enter host password for user 'bitcoinrpc':
{"result":"2MyUBCnC2FvwX6ihegTVCKn1iKc5JeyEbKr","error":null,"id":"curltest"}

返回结果中的 “2MyUBCnC2FvwX6ihegTVCKn1iKc5JeyEbKr” 即为新创建的比特币钱包,以2开头表示是测试链的钱包地址,公链上的钱包地址一般是3开头

查询余额

1
omni@ubuntu:~$ curl --user bitcoinrpc --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getbalance", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:18332/

获取测试币

在下面两个网站中可以获得少量的测试币,用来测试足够了:

https://coinfaucet.eu/en/btc-testnet/

http://bitcoinfaucet.uo1.net/send.php

你可以在比特币区块链浏览器查到余额:https://live.blockcypher.com/btc-testnet/address/2MtW8kA68TMrBLZb373WttjHbxfuRNdvHh7/

-------------本文结束感谢您的阅读-------------
🐶 您的支持将鼓励我继续创作 🐶