欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

比特币BTC私有链搭建

程序员文章站 2022-07-14 16:00:36
...

下载并安装可执行程序

1、下载 bitcoind/bitcoin-cli :BTC 节点服务端/客户端 程序

创建本地目录结构如下

新建目录

[[email protected] bitcoin-regtest]$ pwd
/home/devops/coins/bitcoin-regtest

新建相关文件和子目录

[[email protected] bitcoin-regtest]$ tree
.
├── bitcoind-cli.sh  //客户端查询脚本
├── conf
│   └── bitcoin.conf	//节点配置文件
└── run-bitcoin.sh   //节点启动脚本
└── data    //存放数据的目录,包含debug.log

文件和脚本内容如下

[[email protected] 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 [email protected]  [email protected]
[[email protected] bitcoin-regtest]$ cat conf/bitcoin.conf
regtest=1
server=1
rpcuser=wayki
[email protected]
rpcallowip=0.0.0.0/0
txindex=1
gen=1

[[email protected] 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

[[email protected] bitcoin-regtest]$

配置文件说明

todo

启动节点

运行脚本启动
[[email protected] bitcoin-regtest]$ ./run-bitcoin.sh
Bitcoin server starting
查看log
[[email protected] 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

[[email protected] 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  //私有链
[[email protected] bitcoin-regtest]$
 [[email protected] 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))
[[email protected] bitcoin-regtest]$
使用bitcoin-cli查询节点信息

help 查看所有支持的命令

[[email protected] 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,即没有开启挖矿

[[email protected] 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": ""
}
[[email protected] bitcoin-regtest]$

开启挖矿

1、在配置文件中添加 gen=1 表示节点可以挖矿
[[email protected] bitcoin-regtest]$ cat conf/bitcoin.conf
regtest=1
server=1
rpcuser=wayki
[email protected]
rpcallowip=0.0.0.0/0
txindex=1
gen=1

[[email protected] bitcoin-regtest]$
2、由于BTC的共识机制是POW,需要指定矿工,即挖矿的钱给谁,需要新建地址

由于BTC节点 包含 钱包 + 节点,新建的地址私钥默认保存在钱包中

[[email protected] bitcoin-regtest]$ ./bitcoind-cli.sh getnewaddress //新建地址
2NFKmphFReyaZCn24HE3znqXC2RkfZiowcp
3、挖矿

以下命令表示挖100个块,并将挖矿产出的钱指定给第2步中的地址

[[email protected] bitcoin-regtest]$ ./bitcoind-cli.sh generatetoaddress 100 2NFKmphFReyaZCn24HE3znqXC2RkfZiowcp
[
  "5a8756f8cbcfe91289c995c9ecdcfe029219d14273c4cee2ed7835b4794c7f18",
  "5c73906f91604c78362d742fc92c310c94a4ccc1a83a370091ee2b70c0d3942c",
  "5b96db2026b69b0ac0c09a38d083b67f6fdaa7da56485cbcc3ab1dcf99666c05",
  "07df1bb874addb45dc4bb5e68fe781c03d9da35c94ca149ee8e548306cb555a4",
  "02562d8ac0611453f4b84e85524a66fe3e9f3f4391593f25cb29c1a3e713d37a",
  ...
   "59a78c24ed2a9223d5bd965b3c0c027257bd46404ce2cfff89680c5ee199b1e9",
  "49475e2a2c039295aa2354c2d708b813cb66dd2d1a83b9867fc9cd29ff7fd94e",
  "3f3beddffe9bd7e2d253cc33e360070e125cfa1ee96518c7b25438b6dd067452"
]
4、查询钱包余额

如下查询钱包已经有余额

[[email protected] 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
}
[[email protected] bitcoin-regtest]$
相关标签: Blockchain