========= 比特币节点搭建 ========
搭建软件,
Win10、Ubuntu20系统
数据同步,磁盘容量,同步时间
本地服务器:RPC节点信息
BTC测试节点 172.17.3.31 端口 8332 用户名 root 密码 12345678
BTC正式网节点 172.17.3.30 端口 8332 用户名 root 密码 12345678
Ssh连接测试节点
IP: 172.17.3.31,
Port: 10220
登录用户名: cszy,
密码: cszy.
使用XShell5无法登录该节点,需要升级到新版本
使用PuTTy可以连接成功。
在服务器上,安装目录: /data/bitcoin
/data目录是一个500G的硬盘,已用30G。
==========客户端软件===========
Bitcoin Core,官网下载下载 - 比特币
bitcoin-0.21.1-win64-setup-unsigned.exe
安装后在开始菜单中自动添加了两个图标:分别是主网、测试网客户端。实际上是同一个程序启动命令的参数不同。
第一次启动客户端,自动连接主网或测试网,同步区块数据,选择保留近期数据,大约需要占用2GB磁盘空间。
完成同步的主网客户端:
1使用客户端命令行
在BTC客户端,窗口—控制台,输入help,显示所有的命令:
例如:直接输入 getblockchaininfo,查看执行结果。
2 win10下的cmd命令行
进入客户端软件安装目录:C:\Program Files\Bitcoin\daemon>,进入cmd窗口,执行命令:
bitcoin-cli.exe -rpcconnect=172.17.3.31 -rpcuser=root -rpcpassword=12345678 getblockchaininfo
该命令是连接本地部署的测试BTC节点,执行getblockchaininfo命令。
3 Ubuntu20系统下的命令行
通过putty连接到测试节点,
4 通过curl执行命令
首先在BTC客户端的控制台查询命令用法:
输入:help getblockchaininfo
结果:下面的蓝色内容就是curl的用法,拷贝出来。
4.1 putty终端执行curl
打开putty终端工具,连接到测试节点上,输入命令:注意黄色内容是节点的IP端口和RPC访问用户名,执行后输入密码12345678,成功得到结果。
curl --user root --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockchaininfo", "params": []}' -H 'content-type: text/plain;' http://172.17.3.31:8332/
4.2 在Win10命令行下执行curl
上述在ubuntu执行的curl命令不能在win10上执行,原因是单引号不能识别。修改如下:
curl --user root --data-binary “{\"jsonrpc\": \"1.0\", \"id\": \"curltest\", \"method\": \"getblockchaininfo\", \"params\": []}” -H “content-type: text/plain;” http://172.17.3.31:8332/
执行成功:
============ BTC浏览器====================
Blockchain Explorer - Search the Blockchain | BTC | ETH | BCH 只有主网API
- Bitcoin: https://blockstream.info/api/
- Bitcoin Testnet: https://blockstream.info/testnet/api/
主网浏览器:https://blockstream.info/
测试网浏览器: Bitcoin Testnet Explorer - Blockstream.info
https://github.com/Blockstream/esplora/blob/master/API.md
成功访问:
https://blockstream.info/testnet/api/tx/869dce348dc3a04b43ebd16b2400c034d5d045934b07687e4799c9b176d665a3
==============获得交易详情======================
1 通过区块浏览器的API
测试网浏览器: https://blockstream.info/testnet/api/
curl https://blockstream.info/testnet/api/tx/6988293eac7bb299f74d4dfb508293eac7bb299f74d4dfb50a647cd466b1b4139565382a815c2722e48ab3d
2 通过BTC客户端的控制台
命令:getrawtransaction , 参数列表:
1. txid (string, required) The transaction id
2. verbose (boolean, optional, default=false) If false, return a string, otherwise return a json object 数据是否解析成json对象。
3. blockhash (string, optional) The block in which to look for the transaction
交易所在的区块hash。
实际结果:
getrawtransaction "635f60a5451b67ef5289a670b4026184e66377987963fe3b55957b4a4f89d746" true "000000000000003862170557a54d259b7ee85f69cd1bbc23e290e20b52ac42e2"
3 通过putty终端命令行
执行:
curl --user root:12345678 --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getrawtransaction", "params": ["b35553558686fbce9b6621edf0e2ea1a493cb295afe23b33a005fa0d23c00754", true, "00000000000000349ff7a603522e937072702a36e1907751f6ea98fbb04243a5"]}' -H 'content-type: text/plain;' http://172.17.3.31:8332/
结果:
对于交易数据的解析研究
目前发现有两类交易, 一类是vin中有prevout字段,一类是vin中没有prevout字段。
1 vin中有prevout字段
实例:主网上的交易,通过浏览器API获得交易数据:
https://blockstream.info/api/tx/b7c471b21915a28bc2dba1ed336d581ec9f35bdfca540bf3034870fcd2c23fb0
返回结果进行json格式解析:
Prevout中
"scriptpubkey_address": "16FZM8USXrjrAgkFG5C9gY1mMjNWnsZJvg", 是用户地址
"value": 28247 是转出金额
2 vin中没有prevout字段
实例:从自己搭建的测试节点访问交易数据
curl --user root:12345678 --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getrawtransaction", "params": ["e5b97a14e35ca08c454b30e617e4393a1c3d756795da700653947bc42fc2ba72", true, "00000000da4e5474bbe4cbc7ec188a38012b8d743c9c0d85bb00cfd180b7eb16"]}' -H 'content-type: text/plain;' http://172.17.3.31:8332/
现在看来,从自己的节点获得的交易数据没有preout字段,而从浏览器获得的有prevout字段。这说明浏览器api接口服务是进行了数据加工,才多出了prevout字段,原始交易中并没有这个。
在来一个交易例子:
Vin中没有用户地址和金额,只能依据txid:vout再次查询交易,从vout中获得这个用户地址和金额。
================= ==========================
读取区块数据
1 BTC客户端控制台
命令:
getblock 00000000da4e5474bbe4cbc7ec188a38012b8d743c9c0d85bb00cfd180b7eb16
2 通过putty终端命令行
命令:
curl --user root:12345678 --data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblock", "params": ["00000000da4e5474bbe4cbc7ec188a38012b8d743c9c0d85bb00cfd180b7eb16"]}' -H 'content-type: text/plain;' http://172.17.3.31:8332/
3 从浏览器API获取
命令:
返回区块头信息。解析如下
命令:
返回区块中所有的交易ID。
命令:
上述命令返回区块中的前25笔交易数据。[start_index]必须是25的倍数,0、25、50……。