mac搭建比特币开发环境
0 曾尝试源码自行编译,无奈各种error,官网有已经编译好的;
1 下载后解压即可,bin文件下面有我们想要的;
2 启动测试网络,-daemon表示后台运行,想看实时的信息就不需要加这个,信息打印了默认data的路径;
rpc的接口文档
不一定都是最新的,如果有问题,命令行执行时会有相关提示;
2.1 配置文件
默认会在当前目录下查找配置bitcoin.conf,自行创建主要配置项如下,具体含义可以查询帮助文档;
regtest=1
rpcbind=127.0.0.1
rpcuser=test
rpcpassword=test
server=1
daemon=1
txindex=1
然后启动
./bitcoind
Default data directory /Users/zhouyc/Library/Application Support/Bitcoin
2019-04-16T02:39:27Z Using data directory /Users/zhouyc/Library/Application Support/Bitcoin/regtest
2019-04-16T02:39:27Z Using config file /Users/zhouyc/Library/Application Support/Bitcoin/bitcoin.conf
启动命令行进行挖矿,100个确认之后才能使用,官网有说明
./bitcoin-cli -regtest generate 101
3 查看余额
./bitcoin-cli -regtest getbalance
50.00000000
4 默认的账户是哪个?
是""
5 查看账户listaccounts
这个接口已经废弃,有提示显示如下:
listaccounts is deprecated and will be removed in V0.18. To use this command, start bitcoind with -deprecatedrpc=accounts.
bitcoin-cli -regtest listaccounts
{
"": 50.00000000
}
bitcoin-cli -regtest getaccountaddress ""
#regtest网络的地址以2开头?
2MsTShjE9vT4G36FDDkmimFnzcKhNb8rQAC
6 产生一个新地址
regtest类型的地址,跟mainnet的不一样,如果需要模拟mainnet的,需要修改源码,自行编译,具体哪一块我还没研究 :)
./bitcoin-cli -regtest getnewaddress "test"
2N2qPWWUtxnevHf9Db56CYjnZG5zwotUeLx
7 给指定账户产出btc
The generatetoaddress RPC mines blocks immediately to a specified address.
挖2个block
./bitcoin-cli -regtest generatetoaddress 2 "2N2qPWWUtxnevHf9Db56CYjnZG5zwotUeLx" 500
[
"326941b8ce74e9d98223155cff58b2842612c1ff893e2f38b47f4959067a46ea",
"188641b5dd72512482af2ed9aa625dbb46d4564afb5fef777697ee8e79f27f1a"
]
查看指定账户余额,1表示至少1个确认即可,true?
bitcoin-cli -regtest getbalance "test" 1 true
100.00000000
8 脚本挖矿
因为regtest模式不会自动挖矿,需要自己执行命令,可以尝试通过脚本执行
$ while true; do bitcoin-cli -regtest generate 1; sleep 300; done
9 发送比特币
bitcoin-cli -regtest sendtoaddress 2N2qPWWUtxnevHf9Db56CYjnZG5zwotUeLx 1 "sendtoaddress example" "Nemo From Example.com"
827fd8abb52185522bb6937adbda60c22228712816187a39b3f8230ec396ac4f
查看交易信息,确认数0,需要手动产生一些新的区块
bitcoin-cli -regtest gettransaction 827fd8abb52185522bb6937adbda60c22228712816187a39b3f8230ec396ac4f
{
"amount": 0.00000000,
"fee": -0.00003740,
"confirmations": 0,
"trusted": true,
"txid": "827fd8abb52185522bb6937adbda60c22228712816187a39b3f8230ec396ac4f",
"walletconflicts": [
],
"time": 1555386788,
"timereceived": 1555386788,
"bip125-replaceable": "no",
"comment": "sendtoaddress example",
"to": "Nemo From Example.com",
"details": [
{
"account": "",
"address": "2N2qPWWUtxnevHf9Db56CYjnZG5zwotUeLx",
"category": "send",
"amount": -1.00000000,
"label": "test",
"vout": 1,
"fee": -0.00003740,
"abandoned": false
},
{
"account": "test",
"address": "2N2qPWWUtxnevHf9Db56CYjnZG5zwotUeLx",
"category": "receive",
"amount": 1.00000000,
"label": "test",
"vout": 1
}
],
"hex": "02000000011a52a4d6dea85ed1b6f845a81f1e1f5b3178e3db62aea521e85332220ebb3f42000000004847304402206e1fe7c307f402aadece58a040b7d614e9ed292f4cd24c854d06f35ab628a1c3022015cd049bb0b659c02e758403e584d5c679fdc8cab2492d0525e602edd8fff93101feffffff02640210240100000017a914dee8cf707b6af33f63b9366d7b12f0f3981abc3a8700e1f5050000000017a914692f38fb8d8b28502f0240d4ea2470f9d7f8bafb87cb000000"
}
10 如何通过http的方式发起请求?
使用帮助命令,会给出用例
Examples:
bitcoin-cli getblockchaininfo
curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblockchaininfo", "params": [] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/
11 默认端口
通过查询进程与端口号
ps -ef|grep bitcoind
501 1854 93800 0 5:08PM ttys011 0:00.26 ./bitcoind
lsof -p 1854|grep LISTEN
bitcoind 1854 zhouyc 10u IPv6 0x10f9fba835ac81ff 0t0 TCP localhost:18443 (LISTEN)
bitcoind 1854 zhouyc 11u IPv4 0x10f9fba811bbb7ff 0t0 TCP localhost:18443 (LISTEN)
bitcoind 1854 zhouyc 31u IPv6 0x10f9fba835ac53ff 0t0 TCP *:18444 (LISTEN)
bitcoind 1854 zhouyc 32u IPv4 0x10f9fba8102087ff 0t0 TCP *:18444 (LISTEN)
curl --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getblockchaininfo", "params": [] }' -H 'content-type: text/plain;' http://test:test@127.0.0.1:18443/
{
"result": {
"chain": "regtest",
"blocks": 204,
"headers": 204,
"bestblockhash": "1469510f5eb444e33bd64506a322dea6f83a877c765121445a0c99f874aa4190",
"difficulty": 4.656542373906925e-10,
"mediantime": 1555385898,
"verificationprogress": 1,
"initialblockdownload": false,
"chainwork": "000000000000000000000000000000000000000000000000000000000000019a",
"size_on_disk": 64202,
"pruned": false,
"softforks": [{
"id": "bip34",
"version": 2,
"reject": {
"status": false
}
}, {
"id": "bip66",
"version": 3,
"reject": {
"status": false
}
}, {
"id": "bip65",
"version": 4,
"reject": {
"status": false
}
}],
"bip9_softforks": {
"csv": {
"status": "started",
"bit": 0,
"startTime": 0,
"timeout": 9223372036854775807,
"since": 144,
"statistics": {
"period": 144,
"threshold": 108,
"elapsed": 61,
"count": 61,
"possible": true
}
},
"segwit": {
"status": "active",
"startTime": -1,
"timeout": 9223372036854775807,
"since": 0
}
},
"warnings": ""
},
"error": null,
"id": "curltest"
}