比特幣BTC私有鏈搭建

下載並安裝可執行程序

1、下載 bitcoind/bitcoin-cli :BTC 節點服務端/客戶端 程序

創建本地目錄結構如下

新建目錄

[devops@wicc-coin bitcoin-regtest]$ pwd
/home/devops/coins/bitcoin-regtest

新建相關文件和子目錄

[devops@wicc-coin bitcoin-regtest]$ tree
.
├── bitcoind-cli.sh  //客戶端查詢腳本
├── conf
│   └── bitcoin.conf	//節點配置文件
└── run-bitcoin.sh   //節點啓動腳本
└── data    //存放數據的目錄,包含debug.log

文件和腳本內容如下

[devops@wicc-coin bitcoin-regtest]$ cat ./bitcoind-cli.sh
#!/bin/bash
#

if [ $# -eq 0 ];then
	echo "Error, Usage: $(basename $0) method"
	exit 2
fi

bitcoin-cli -rpcport=18443 -rpcuser=wayki -rpcpassword=admin@123  $@
[devops@wicc-coin bitcoin-regtest]$ cat conf/bitcoin.conf
regtest=1
server=1
rpcuser=wayki
rpcpassword=admin@123
rpcallowip=0.0.0.0/0
txindex=1
gen=1

[devops@wicc-coin bitcoin-regtest]$ cat run-bitcoin.sh
#!/bin/bash

basedir=/home/devops/coins/bitcoin-regtest
bitcoind -daemon -conf=$basedir/conf/bitcoin.conf -txindex -rpcbind=0.0.0.0 -datadir=$basedir/data

[devops@wicc-coin bitcoin-regtest]$

配置文件說明

todo

啓動節點

運行腳本啓動
[devops@wicc-coin bitcoin-regtest]$ ./run-bitcoin.sh
Bitcoin server starting
查看log
[devops@wicc-coin regtest]$ tail -f debug.log
2019-12-27T03:49:39Z init message: Starting network threads...
2019-12-27T03:49:39Z addcon thread start
2019-12-27T03:49:39Z dnsseed thread start
2019-12-27T03:49:39Z Loading addresses from DNS seeds (could take a while)
2019-12-27T03:49:39Z 0 addresses found from DNS seeds
2019-12-27T03:49:39Z dnsseed thread exit
2019-12-27T03:49:39Z init message: Done loading
2019-12-27T03:49:39Z net thread start
2019-12-27T03:49:39Z msghand thread start
2019-12-27T03:49:39Z opencon thread start
查看進程和端口是否正常

結果同預想一直,默認私有鏈regtest的RPC端口號爲18443

[devops@wicc-coin bitcoin-regtest]$ ps -ef |grep  bitcoind
devops   14744     1  0 12月19 ?      00:59:31 bitcoind -daemon -conf=/home/devops/coins/bitcoin/conf/bitcoin.conf -txindex -rpcbind=0.0.0.0 -datadir=/home/devops/coins/bitcoin/data   //測試網
devops   30043     1  0 11:49 ?        00:00:03 bitcoind -daemon -conf=/home/devops/coins/bitcoin-regtest/conf/bitcoin.conf -txindex -rpcbind=0.0.0.0 -datadir=/home/devops/coins/bitcoin-regtest/data
devops   30137 29792  0 12:14 pts/1    00:00:00 grep --color=auto bitcoind  //私有鏈
[devops@wicc-coin bitcoin-regtest]$
 [devops@wicc-coin bitcoin-regtest]$ ss -tnl -p |grep 30043  //查看私有鏈進程監聽的端口號
LISTEN     0      128          *:18443                    *:*                   users:(("bitcoind",pid=30043,fd=9))  //RPC端口
LISTEN     0      128          *:18444                    *:*                   users:(("bitcoind",pid=30043,fd=29)) //P2P端口
LISTEN     0      128         :::18444                   :::*                   users:(("bitcoind",pid=30043,fd=27))
[devops@wicc-coin bitcoin-regtest]$
使用bitcoin-cli查詢節點信息

help 查看所有支持的命令

[devops@wicc-coin bitcoin-regtest]$ ./bitcoind-cli.sh help
== Blockchain ==
getbestblockhash
getblock "blockhash" ( verbosity )
getblockchaininfo
getblockcount
getblockhash height
getblockheader "blockhash" ( verbose )
getblockstats hash_or_height ( stats )
getchaintips
getchaintxstats ( nblocks "blockhash" )
getdifficulty
getmempoolancestors "txid" ( verbose )
getmempooldescendants "txid" ( verbose )
getmempoolentry "txid"
getmempoolinfo
getrawmempool ( verbose )
gettxout "txid" n ( include_mempool )
gettxoutproof ["txid",...] ( "blockhash" )
gettxoutsetinfo
preciousblock "blockhash"
pruneblockchain height
savemempool
scantxoutset "action" [scanobjects,...]
verifychain ( checklevel nblocks )
verifytxoutproof "proof"

== Control ==
getmemoryinfo ( "mode" )
getrpcinfo
help ( "command" )
logging ( ["include_category",...] ["exclude_category",...] )
stop
uptime

== Generating ==
generate nblocks ( maxtries )
generatetoaddress nblocks "address" ( maxtries )

== Mining ==
getblocktemplate "template_request"
getmininginfo
getnetworkhashps ( nblocks height )
prioritisetransaction "txid" ( dummy ) fee_delta
submitblock "hexdata" ( "dummy" )
submitheader "hexdata"

== Network ==
addnode "node" "command"
clearbanned
disconnectnode ( "address" nodeid )
getaddednodeinfo ( "node" )
getconnectioncount
getnettotals
getnetworkinfo
getnodeaddresses ( count )
getpeerinfo
listbanned
ping
setban "subnet" "command" ( bantime absolute )
setnetworkactive state

== Rawtransactions ==
analyzepsbt "psbt"
combinepsbt ["psbt",...]
combinerawtransaction ["hexstring",...]
converttopsbt "hexstring" ( permitsigdata iswitness )
createpsbt [{"txid":"hex","vout":n,"sequence":n},...] [{"address":amount},{"data":"hex"},...] ( locktime replaceable )
createrawtransaction [{"txid":"hex","vout":n,"sequence":n},...] [{"address":amount},{"data":"hex"},...] ( locktime replaceable )
decodepsbt "psbt"
decoderawtransaction "hexstring" ( iswitness )
decodescript "hexstring"
finalizepsbt "psbt" ( extract )
fundrawtransaction "hexstring" ( options iswitness )
getrawtransaction "txid" ( verbose "blockhash" )
joinpsbts ["psbt",...]
sendrawtransaction "hexstring" ( allowhighfees )
signrawtransactionwithkey "hexstring" ["privatekey",...] ( [{"txid":"hex","vout":n,"scriptPubKey":"hex","redeemScript":"hex","witnessScript":"hex","amount":amount},...] "sighashtype" )
testmempoolaccept ["rawtx",...] ( allowhighfees )
utxoupdatepsbt "psbt"

== Util ==
createmultisig nrequired ["key",...] ( "address_type" )
deriveaddresses "descriptor" ( range )
estimatesmartfee conf_target ( "estimate_mode" )
getdescriptorinfo "descriptor"
signmessagewithprivkey "privkey" "message"
validateaddress "address"
verifymessage "address" "signature" "message"

== Wallet ==
abandontransaction "txid"
abortrescan
addmultisigaddress nrequired ["key",...] ( "label" "address_type" )
backupwallet "destination"
bumpfee "txid" ( options )
createwallet "wallet_name" ( disable_private_keys blank )
dumpprivkey "address"
dumpwallet "filename"
encryptwallet "passphrase"
getaddressesbylabel "label"
getaddressinfo "address"
getbalance ( "dummy" minconf include_watchonly )
getnewaddress ( "label" "address_type" )
getrawchangeaddress ( "address_type" )
getreceivedbyaddress "address" ( minconf )
getreceivedbylabel "label" ( minconf )
gettransaction "txid" ( include_watchonly )
getunconfirmedbalance
getwalletinfo
importaddress "address" ( "label" rescan p2sh )
importmulti "requests" ( "options" )
importprivkey "privkey" ( "label" rescan )
importprunedfunds "rawtransaction" "txoutproof"
importpubkey "pubkey" ( "label" rescan )
importwallet "filename"
keypoolrefill ( newsize )
listaddressgroupings
listlabels ( "purpose" )
listlockunspent
listreceivedbyaddress ( minconf include_empty include_watchonly "address_filter" )
listreceivedbylabel ( minconf include_empty include_watchonly )
listsinceblock ( "blockhash" target_confirmations include_watchonly include_removed )
listtransactions ( "label" count skip include_watchonly )
listunspent ( minconf maxconf ["address",...] include_unsafe query_options )
listwalletdir
listwallets
loadwallet "filename"
lockunspent unlock ( [{"txid":"hex","vout":n},...] )
removeprunedfunds "txid"
rescanblockchain ( start_height stop_height )
sendmany "" {"address":amount} ( minconf "comment" ["address",...] replaceable conf_target "estimate_mode" )
sendtoaddress "address" amount ( "comment" "comment_to" subtractfeefromamount replaceable conf_target "estimate_mode" )
sethdseed ( newkeypool "seed" )
setlabel "address" "label"
settxfee amount
signmessage "address" "message"
signrawtransactionwithwallet "hexstring" ( [{"txid":"hex","vout":n,"scriptPubKey":"hex","redeemScript":"hex","witnessScript":"hex","amount":amount},...] "sighashtype" )
unloadwallet ( "wallet_name" )
walletcreatefundedpsbt [{"txid":"hex","vout":n,"sequence":n},...] [{"address":amount},{"data":"hex"},...] ( locktime options bip32derivs )
walletlock
walletpassphrase "passphrase" timeout
walletpassphrasechange "oldpassphrase" "newpassphrase"
walletprocesspsbt "psbt" ( sign "sighashtype" bip32derivs )

== Zmq ==
getzmqnotifications

getblockchaininfo查看私有鏈運行狀態等信息,目前區塊高度爲0,即沒有開啓挖礦

[devops@wicc-coin bitcoin-regtest]$ ./bitcoind-cli.sh getblockchaininfo
{
  "chain": "regtest",
  "blocks": 0,
  "headers": 0,
  "bestblockhash": "0f9188f13cb7b2c71f2a335e3a4fc328bf5beb436012afca590b1a11466e2206",
  "difficulty": 4.656542373906925e-10,
  "mediantime": 1296688602,
  "verificationprogress": 1,
  "initialblockdownload": true,
  "chainwork": "0000000000000000000000000000000000000000000000000000000000000002",
  "size_on_disk": 293,
  "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": "defined",
      "startTime": 0,
      "timeout": 9223372036854775807,
      "since": 0
    },
    "segwit": {
      "status": "active",
      "startTime": -1,
      "timeout": 9223372036854775807,
      "since": 0
    }
  },
  "warnings": ""
}
[devops@wicc-coin bitcoin-regtest]$

開啓挖礦

1、在配置文件中添加 gen=1 表示節點可以挖礦
[devops@wicc-coin bitcoin-regtest]$ cat conf/bitcoin.conf
regtest=1
server=1
rpcuser=wayki
rpcpassword=admin@123
rpcallowip=0.0.0.0/0
txindex=1
gen=1

[devops@wicc-coin bitcoin-regtest]$
2、由於BTC的共識機制是POW,需要指定礦工,即挖礦的錢給誰,需要新建地址

由於BTC節點 包含 錢包 + 節點,新建的地址私鑰默認保存在錢包中

[devops@wicc-coin bitcoin-regtest]$ ./bitcoind-cli.sh getnewaddress //新建地址
2NFKmphFReyaZCn24HE3znqXC2RkfZiowcp
3、挖礦

以下命令表示挖100個塊,並將挖礦產出的錢指定給第2步中的地址

[devops@wicc-coin bitcoin-regtest]$ ./bitcoind-cli.sh generatetoaddress 100 2NFKmphFReyaZCn24HE3znqXC2RkfZiowcp
[
  "5a8756f8cbcfe91289c995c9ecdcfe029219d14273c4cee2ed7835b4794c7f18",
  "5c73906f91604c78362d742fc92c310c94a4ccc1a83a370091ee2b70c0d3942c",
  "5b96db2026b69b0ac0c09a38d083b67f6fdaa7da56485cbcc3ab1dcf99666c05",
  "07df1bb874addb45dc4bb5e68fe781c03d9da35c94ca149ee8e548306cb555a4",
  "02562d8ac0611453f4b84e85524a66fe3e9f3f4391593f25cb29c1a3e713d37a",
  ...
   "59a78c24ed2a9223d5bd965b3c0c027257bd46404ce2cfff89680c5ee199b1e9",
  "49475e2a2c039295aa2354c2d708b813cb66dd2d1a83b9867fc9cd29ff7fd94e",
  "3f3beddffe9bd7e2d253cc33e360070e125cfa1ee96518c7b25438b6dd067452"
]
4、查詢錢包餘額

如下查詢錢包已經有餘額

[devops@wicc-coin bitcoin-regtest]$ ./bitcoind-cli.sh getwalletinfo
{
  "walletname": "",
  "walletversion": 169900,
  "balance": 14949.99998350,
  "unconfirmed_balance": 0.00000000,
  "immature_balance": 0.00000000,
  "txcount": 10100,
  "keypoololdest": 1577418579,
  "keypoolsize": 999,
  "keypoolsize_hd_internal": 1000,
  "paytxfee": 0.00000000,
  "hdseedid": "2ca1a4d90692fca1af51c907b0aa4e55ce4150db",
  "private_keys_enabled": true
}
[devops@wicc-coin bitcoin-regtest]$
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章