比特币系列 - BTC同步公链,测试链与私链

(1)连接到公链

前台运行

bitcoind
	#如果报错,应该是环境变量的关系
	bitcoind: error while loading shared libraries: libboost_system.so.1.64.0: cannot open shared object file: No such file or directory

	#配置环境变量
	vim ~/.bashrc
	#添加以下内容:
	LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib:/usr/local/lib:/usr/local/lib64
	export LD_LIBRARY_PATH
	export PYTHONPATH=.
	#保存退出后执行
	source ~/.bashrc

后台运行

启动并同步区块
bitcoind -daemon
启动 不同步区块
bitcoind -daemon -maxconnections=0

##### 公链跑这句 ############
bitcoind -daemon -deprecatedrpc=estimatefee

#能使用挖矿命令 (已被弃用)
bitcoind -daemon -deprecatedrpc=setgenerate

#启用账户的命令
bitcoind -daemon -deprecatedrpc=accounts
#不同步区块
bitcoind -daemon -maxconnections=0 -deprecatedrpc=accounts

常用的命令行命令

#增加同步节点
bitcoin-cli addnode "119.23.63.114:8333" onetry
#查看同步信息
bitcoin-cli -getinfo
#查看account与余额
bitcoin-cli listaccounts
#查看交易信息
bitcoin-cli listtransactions
#列出整个钱包的所有地址 
bitcoin-cli getaddressesbyaccount ""
#列出整个钱包的所有地址和余额
bitcoin-cli listaddressgroupings
#显示钱包当前管理的所有地址的余额总和 
bitcoin-cli getbalance
bitcoin-cli getbalance 地址 
#停止运行
bitcoin-cli stop

(2)连接到测试网络

前台运行

bitcoind -testnet
bitcoind -testnet -gen    //-gen参数代表开始挖矿

##### 测试链跑这句 ############
开始运行 (后台运行)
bitcoind -testnet -daemon -deprecatedrpc=accounts
不同步区块
bitcoind -testnet -daemon -maxconnections=0 -deprecatedrpc=accounts
#############################

#停止运行
bitcoin-cli -testnet stop

#生成区块
bitcoin-cli -regtest generate 101
#生成钱包地址
bitcoin-cli -regtest getnewaddress
#生成钱包地址 指定账户
bitcoin-cli -regtest getnewaddress "platform"
#查询地址的私钥
bitcoin-cli -regtest dumpprivkey 2MvSvdJBGSZVfdMGYXFpnDofv3ADQBeXPq4
#转账 (单位是btc)
bitcoin-cli -regtest sendtoaddress [ADDRESS] [AMOUNT]
bitcoin-cli -regtest sendtoaddress 2MxNL6tPRKNCEWqrC1z5Sw9WWNVqA39nDc8 2
#查看交易信息(受钱包管理的地址的交易)
bitcoin-cli -regtest listtransactions
bitcoin-cli -regtest gettransaction txid
bitcoin-cli decoderawtransaction hex

常用钱包命令

#**生成钱包地址**
bitcoin-cli -testnet getnewaddress
#**加密钱包**
bitcoin-cli -testnet encryptwallet "123456"
#**解锁钱包**   最后一个参数是解锁时间
bitcoin-cli -testnet walletpassphrase "123456" 600
#**查询地址的私钥**
bitcoin-cli -testnet dumpprivkey 2MvSvdJBGSZVfdMGYXFpnDofv3ADQBeXPq4
#**导入相应地址到钱包**
bitcoin-cli -testnet importaddress mfcwZvnSE44MuHoNPZcrwvaGSsGnTerKa2
#**导入相应地址的私钥到钱包**
bitcoin-cli -testnet importprivkey cVYGW6TbAVDpP183yPJkGPwAYgoNugnq6ueywcNAY2uRGdyi4X38
#导出钱包
bitcoin-cli -testnet backupwallet mywallet
#导入钱包
bitcoin-cli -testnet importwallet  mywallet

(3)私链

后台运行

bitcoind -regtest -daemon -deprecatedrpc=accounts  -datadir=/root/blockchain/btc
bitcoind -regtest -daemon -deprecatedrpc=accounts  -datadir=/root/blockchain/btc -rpcuser=admin1 -rpcpassword=123 -rpcallowip=0.0.0.0/0

#设置了rpc端口 账号 密码的话,使用bitcoin-cli时也得相应设置 -rpcport=19001
bitcoin-cli -regtest -rpcuser=admin1 -rpcpassword=123 -getinfo
bitcoin-cli -regtest -rpcuser=admin1 -rpcpassword=123 -datadir=/root/blockchain/btc getnewaddress "platform"
bitcoin-cli -regtest -rpcuser=admin1 -rpcpassword=123 stop

#如果指定了路径的话,使用bitcoin-cli时可以不指定rcp密码等
bitcoin-cli -datadir=/root/blockchain/btc -getinfo
bitcoin-cli -datadir=/root/blockchain/btc getnewaddress "platform"
bitcoin-cli -datadir=/root/blockchain/btc stop

#停止运行
bitcoin-cli -regtest stop

(4)通过 curl 直接调 rpc接口 (RESTAPI)

curl --user admin1 --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "getaddressesbyaccount", "params": ["tabby"] }' -H 'content-type: text/plain;' http://192.168.131.135:18444/

(5)使用make命令

cd blockchain/btc/regtest/
#在目录下放个Makefile文件,里面配置好对make命令的响应, 然后就可以
make start
make getinfo
make cli command=getbalance			//command=后面可以输入命令
make stop

#注意帐户余额变动,与网络上的实际硬币无关
#当您使用sendfrom汇款时,指定的账户被扣除。但创建的实际链上交易始终使用钱包控制的任意个硬币。再次说明:链上硬币(交易产出)和账户余额之间没有关系

状态相关

#网络概览(包括网络类型 区块高度)
make cli command=getblockchaininfo
#钱包状态
make cli command=getwalletinfo
#查看当前节点的网络状况
make cli command=getnetworkinfo
#基本信息
make getinfo

区块相关

#生成区块
make cli command="generate 10"

#查区块号对应的hash
make cli command="getblockhash 0"
#查区块信息
make cli command="getblock [blockhash]"
#查区块详细信息
make cli command="getblock [blockhash] 2"

账户地址

#生成钱包地址 默认账户
make cli command=getnewaddress
#生成钱包地址 指定账户
make cli command="getnewaddress platform"

#列出默认账户下的所有地址 
make cli command="getaddressesbyaccount ''"
#列出指定账户下的所有地址 
make cli command="getaddressesbyaccount platform"
make cli command="getaddressesbyaccount user"

#***查询一个地址的相关信息,可用于验证一个地址是否有效 ***
make cli command="validateaddress 2NAvfLb7p4BmLAUKiVGTyfCFAAFJ2h88CNg"
make cli command="validateaddress 2NEVRE7j9ZzLmFZREnf3d9QvTq9Y8hP9UqM"
#返回指定地址相关联的帐户
make cli command="getaccount 2NAvfLb7p4BmLAUKiVGTyfCFAAFJ2h88CNg"
#将地址关联到指定帐户
make cli command="setaccount 2NAvfLb7p4BmLAUKiVGTyfCFAAFJ2h88CNg user"

#返回指定账户当前的币收款地址
make cli command="getaccountaddress 'platform'"
#返回指定账户上收到至少[minconf]个确认的收款交易总金额(不包括付款)
make cli command="getreceivedbyaccount 'user' 1"
#返回指定地址上收到至少[minconf]个确认的收款交易总金额
make cli command="getreceivedbyaddress '2NAvfLb7p4BmLAUKiVGTyfCFAAFJ2h88CNg' 1"

账户余额

#钱包的总余额
make cli command="getbalance"
#钱包内指定账户的总余额
make cli command="getbalance platform"
make cli command="getbalance user"

#返回钱包上的所有地址信息(地址,余额,所属账户)
make cli command=listaddressgroupings
#列出账户的收款信息
make cli command=listreceivedbyaccount
#**列出地址的收款信息**
make cli command=listreceivedbyaddress

#**列出账户与余额**
make cli command=listaccounts
#列出全部未花费的交易输出
make cli command=listunspent
#列出至少 X 个确认的未花费交易输出,并指定地址过滤器。
make cli command="listunspent 0 9999999 '[\"2NAvfLb7p4BmLAUKiVGTyfCFAAFJ2h88CNg\"]'"

交易相关

#查看交易信息(受钱包管理的地址的交易)
make cli command=listtransactions
#查看单笔交易信息
make cli command="gettransaction [txid]"
make cli command="gettransaction 8d44aa1523d1b44519fbfff3f4f0b9373877ad4151d4588673b35518e90a0547"
#查看单笔原始数据
make cli command="getrawtransaction [txid]"
#解码交易的原始数据
make cli command="decoderawtransaction [hex]"

#转账 (单位是btc)
make cli command="sendtoaddress [ADDRESS] [AMOUNT]"
make cli command="sendtoaddress 2NEVRE7j9ZzLmFZREnf3d9QvTq9Y8hP9UqM 1000"
make cli command="sendtoaddress 2N1XpzLA1ZtUpy5WRfiRJRnhgTJXFmejPCs 1000"

#从指定账户<fromaccount>向指定地址<tobitcoinaddress> 发送指定金额<amount>的BTC,确保帐户拥有得到[minconf]个确认的有效余额
make cli command="sendfrom <fromaccount> <tobitcoinaddress> <amount> [minconf=1] [comment] [comment-to] "
make cli command="sendfrom platform 2NAvfLb7p4BmLAUKiVGTyfCFAAFJ2h88CNg 5"

#估算交易手续费
make cli command="estimatesmartfee 6"

#钱包内账户间转账
make cli command="move <fromaccount> <toaccount> <amount> [minconf=1] [comment]"
make cli command="move user platform 10"
 #move 转账 ★★★★ 账户 <fromaccount> <toaccount> <amount> [minconf=1] [comment] 把你钱包中一个账户<fromaccount>上的指定金额<amount> 转移到另一个账户<toaccount>上。 Move from one account in your wallet to another N

钱包相关

make cli command="encryptwallet 123456"
#**解锁钱包**   最后一个参数是解锁时间
make cli command="walletpassphrase 123456 600000"
#**锁定钱包**
make cli command="walletlock"
#**修改钱包密码 原 新**
make cli command="walletpassphrasechange 123456 123123"
#**重新填满密钥池**
make cli command="keypoolrefill"

#**查询地址的私钥**
make cli command="dumpprivkey 2NEVRE7j9ZzLmFZREnf3d9QvTq9Y8hP9UqM"
#**导入地址到钱包默认账户**
make cli command="importaddress mfcwZvnSE44MuHoNPZcrwvaGSsGnTerKa2"
#**导入地址到钱包指定账户** 不重新扫描块 适用于新生成的地址
make cli command="importaddress 2NEVRE7j9ZzLmFZREnf3d9QvTq9Y8hP9UqM platform false"		
#**导入地址到钱包指定账户** 重新扫描块 适用于有余额的地址
make cli command="importaddress 2NEVRE7j9ZzLmFZREnf3d9QvTq9Y8hP9UqM platform true"		
#******导入无余额的私钥到钱包*******
make cli command="importprivkey cSKLMu6HVX9TyJPgqJSCu4SWe7aLcpdmYgo76cgQgAV4NwfkgFJq '' false"
#******导入有余额的私钥到钱包*******
make cli command="importprivkey cSKLMu6HVX9TyJPgqJSCu4SWe7aLcpdmYgo76cgQgAV4NwfkgFJq platform true"
#导出钱包
make cli command="backupwallet mywallet"
#导入钱包
make cli command="importwallet  mywallet"

mining开关(setgenerate该命令貌似已被弃用)

#setgenerate 设定是否采矿 ★★★★ 采矿 <generate> [genproclimit] 根据<generate>参数的值(true/false)决定客户端是否采矿。[genproclimit]参数指定采矿最大线程数,-1代表无限制。 <generate> is true or false to turn generation on or off.Generation is limited to [genproclimit] processors, -1 is unlimited. N

#开
make cli command="setgenerate true 1"
#关
make cli command="setgenerate false"

关闭bitcoin

make -C  /data/blockchain/btc/fast  stop

往期精彩回顾:
区块链知识系列
密码学系列
零知识证明系列
共识系列
公链调研系列
比特币系列
以太坊系列
EOS系列
Filecoin系列
联盟链系列
Fabric系列
智能合约系列
Token系列

猜你喜欢

转载自blog.csdn.net/wcc19840827/article/details/120611867
今日推荐