bitcoin全节点部署及bitcoind bitcoin-cli命令使用解释

bitcoin全节点部署及bitcoind bitcoin-cli命令使用解释

 
 

服务器配置:

操作系统:   ubuntu 16.04
CPU:        4U
内存:     16G     #一般来说4G就够,但如果要查历史记录,需要加载完整的交易索引表-tindex,这导致需要8G+的内存
硬盘:     500G    #比特币从08年开始运行,已经积累了大量数据,区块链内容大小达210G。
带宽:     10MB+

安装部署:

下载地址:https://bitcoincore.org/en/download/

root@wallet-test1:/data# tar xf bitcoin-0.17.1-x86_64-linux-gnu.tar.gz 
root@wallet-test1:/data# ln -s bitcoin-0.17.1 bitcoin           #用软链接的方式在升级程序时,可不用重新设置环境变量
root@wallet-test1:/data# mkdir -p coin/btccoin/                 #数据存储目录
root@wallet-test1:/data# vim /etc/profile.d/bitcoin.sh          #添加环境变量
export PATH=/data/bitcoin/bin:$PATH
root@wallet-test1:/data# source /etc/profile.d/bitcoin.sh       #加载环境变量

配置文件:

-conf=<file>                #指定配置文件,默认~/.bitcoin/bitcoin.conf
-port=<port>                #监听端口,默认8332或testnet 18332
-bind=<addr>                #监听地址,使用[host]:port标识ipv6地址
-datadir=<dir>              #数据保存目录,建议500G以上
-daemon                     #运行为守护进程
-dbcache=<n>                #设置数据缓存大小,单位百万字节,默认450
-debuglogfile=<file>        #指定debug级别日志保存的位置。
-includeconf=<file>         #指定额外配置文件路径。只能用于配置文件,不能用于命令行。
-loadblock=<file>           #启动时从外部的名为blk000??.dat加载数据块
-reindex                    #从磁盘上的blk*.dat文件重建链状态和块索引
-reindex-chainstate         #从当前索引的块重建链状态
-version                    #查看当前程序版本并退出
-txindex                    #默认情况下,比特币核心只创建与使用者钱包相关的交易索引,如果需要维护全部交易的索引,需要设置该选项。
-maxmempool=<n>             #将事务内存保持在<n> mb以下(default: 300)
-maxorphantx=<n>            #在内存中最多保存<n>不可连接事务(default: 100)
-mempoolexpiry=<n>          #在内存池中保存事务的时间不要超过<n>小时(default:336)
-server                     #接受命令行和JSON-RPC命令
-rpcuser=<user>             #JSON-RPC连接时使用的用户名
-rpcpassword=<pw>           #JSON-RPC连接时使用的密码
-rpcport=<port>             #JSON-RPC连接监听的端口,默认8332或testnet 18332
-rpcallowip=<ip>            #允许JSON-RPC连接从外部连接。可以是一个单独IP(e.g. 1.2.3.4)、网络地址/掩码(e.g.1.2.3.4/255.255.255.0)或网络地址/掩码长度(e.g. 1.2.3.4/24)。该选项可指定多次。
-rpcbind=<addr>[:port]      #JSON-RPC连接的服务监听地址和端口。不指定-rpcallowip参数时,该参数无效。端口参数可选,若指定,则覆盖-rpcport选项。使用[host]:port标识ipv6地址。地址格式与-rpcallowip相同。

钱包选项

-addresstype                #使用什么类型的地址("legacy", "p2sh-segwit", or "bech32",default: "p2sh-segwit")
-disablewallet              #不加载钱包并禁用钱包RPC调用
-discardfee=<amt>           #费率(以BTC/kB表示)
-fallbackfee=<amt>          #收费比率(以BTC/kB为单位),当收费估计数据不足时,会使用该比率(默认值:0.0002)
-keypool=<n>                #将密钥池大小设置为<n>
-mintxfee=<amt>             #如果费用(以BTC/kB为单位)小于此值,则认为创建事务的费用为零(默认值为0.00001)。
-paytxfee=<amt>             #向您发送的交易中添加的费用(以BTC/kB为单位)(默认值:0.00)
-rescan                     #重新扫描在启动时丢失的钱包事务的块链
-salvagewallet              #尝试在启动时从损坏的钱包中恢复私钥
-wallet=<path>              #指定钱包数据库路径,可以指定多次加载多个钱包。如果路径不是绝对的,则相对于<walletdir>;如果路径不存在,则创建路径(作为包含wallet.dat文件和日志文件的目录)。为了向后兼容,它还将接受<walletdir>中现有数据文件的名称。)
-walletdir=<dir>            #指定保存钱包的目录(默认:<datadir>/wallets,如该目录不存在,则使用<datadir>)
-walletnotify=<cmd>         #当钱包事务发生更改时执行的命令

服务管理:

运行

bitcoind -conf=/data/bitcoin/bitcoin.conf -daemon

停止

bitcoin-cli stop

其它常用命令:

bitcoin-cli getnetworkinfo              #查看网络状态

bitcoin-cli getpeerinfo                 #查看网络节点

bitcoin-cli getblockchaininfo           #查看区块链信息:如同步进度

bitcoin-cli help                        #查看所有命令

bitcoin-cli命令使用:

命令格式:

bitcoin-cli [options] <command> [params]
bitcoin-cli [options] -named <command> [name=value]...
bitcoin-cli [options] help                      #获取命令帮助
bitcoin-cli [options] help <command>            #获取command的命令帮助

选项:

-conf=<file>                #指定配置文件路径。相对路径将以datadir位置作为前缀。默认文件名:bitcoin.conf
-datadir=<dir>              #指定数据存储位置
-getinfo                    #从远程服务器获取一般信息。与服务器端RPC调用不同,-getinfo的结果是多个非原子请求的结果。结果中的一些条目可能表示来自不同状态的结果(例如,钱包余额可能来自报告的链状态的不同块)
-named                      #传递指定的参数而不是位置参数
-rpcclienttimeout=<n>       #HTTP请求超时(以秒为单位),0表示没有超时。默认900
-rpcconnect=<ip>            #向指定IP的节点发送命令,默认127.0.0.1
-rpccookiefile=<loc>        #认证cookie的路径,相对路径以datadir地址为前缀。默认:data dir
-rpcpassword=<pw>           #JSON-RPC连接的密码
-rpcport=<port>             #JSON-RPC连接的端口
-rpcuser=<user>             #JSON-RPC连接时使用的用户名
-rpcwait                    #等待RPC服务器启动
-rpcwallet=<walletname>     #向非默认RPC服务器发起RPC连接。需要精确匹配传递给bitcoin-cli的参数
-stdin                      #从标准输入读取额外的参数,每行一个,直到EOF/Ctrl-D结束(推荐用于敏感信息,如密码)。当与-stdinrpcpass结合使用时,使用标准输入的第一行作为RPC密码。
-stdinrpcpass               #读取标准输入第一行作为RPC密码。当与-stdin结合使用时,标准输入第一行作为RPC密码。

bitcoind-cli常用命令

  1. bitcoind运行为testnet,并指定相关参数(可在运行时指定,也可写在配置文件中,运行时指定):
root@wallet-test1:/data/bitcoin# bitcoind -testnet -datadir=/data/coin/btccoin -rpcbind=0.0.0.0 -rpcallowip=36.152.8.188 -rpcport=30011 -rpcuser=test -rpcpassword=test -txindex -daemon -server
Bitcoin server starting
  1. 关闭bitcoind(运行命令后,需等待一段时间):
root@wallet-test1:/data/bitcoin# bitcoin-cli -rpcconnect=127.0.0.1 -rpcport=30011 -rpcuser=test -rpcpassword=test stop
Bitcoin server stopping
  1. 生成一个新地址
getnewaddress ( "label" "address_type" )
返回用于接收付款的新比特币地址。
语法:
    1. "label" :字符串,可选。要链接到的地址的标签名称,若未指定,默认为空。label可以不存在
    2. "address_type" :字符串,可选。若使用本选项,可用选项为"legacy", "p2sh-segwit", 和"bech32"。默认值为bitcoind运行时-address_type指定的值。
运行结果:
    "address"       #字符串,新的bitcoin地址

示例

root@wallet-test1:/data/bitcoin# bitcoin-cli -rpcconnect=127.0.0.1 -rpcport=30011 -rpcuser=test -rpcpassword=test  getnewaddress "lxk"
2N2gX4iwgmKEUiZuPcyJ9iCesN1WcPz8bVy
  1. 导入地址进钱包

importaddress "address" ( "label" rescan p2sh )
添加一个可视的脚本(十六进制)或地址到钱包,但并不能用来支付。需要一个新的钱包备份

语法:

  1. script:字符串,必须项。十六进制脚本或地址
  2. label:字符串,可选项,默认为空。
  3. rescan:布尔值,可选项,默认为true。为交易重新扫描钱包
  4. p2sh:布尔值,可选项,默认false。添加脚本的p2sh版本

注意1:
如果rescan值为true,操作可能会花费一段时间。在这期间,其他rpc请求可能报告导入的地址存在,但相关事务丢失,导致暂时不正确的/伪造的余额和未使用的输出,直到rescan完成。
如果你有完整的公钥,可以使用importpubkey,而不用本命令。

注意2:
如果您以十六进制的形式导入一个非标准的原始脚本,发送给它的输出将被视为更改,并且不会显示在许多rpc中。

示例:

~]#  bitcoin-cli importaddress "myscript"       #导入一个地址并重新扫描

~]#  bitcoin-cli importaddress "myscript" "testing" false           #添加一个标签,不重新扫描

~]# curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "importaddress", "params": ["myscript", "testing", false] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/      #JSON格式请求
  1. 显示地址及余额信息

调用按地址分组显示余额信息。该调用需要节点启用钱包功能。

参数:无

命令返回值:

[
  [
    [
      "bitcoinaddress",     (字符串)比特币地址
      amount,                 (数字)以 BTC 为单位的金额
      "account"             (字符串,可选,已过时)账户
    ],
    [
      "bitcoinaddress",     (字符串)比特币地址
      amount,                 (数字)以 BTC 为单位的金额
      "account"             (字符串,可选,已过时)账户
    ]
  ]
]

示例:

[root@wallet-test bin]# ./bitcoin-cli -rpcconnect=127.0.0.1 -rpcport=30011 -rpcuser=test -rpcpassword=test listaddressgroupings
[
  [
    [
      "mmDT8XrYautmFJ2Z7upZCMWeTmDt5a9Kza",
      0.00000000,
      ""
    ]
  ]
]
  1. 用密码解锁钱包

walletpassphrase "passphrase" timeout

在设置的timeout时间内,将钱包解密密钥存储在内存中。
在执行与私钥相关的事务前,这是必须操作。

参数:

  1. passphrase:字符串,必须项。钱包密码。
  2. timeout:数值,必须项。保存解密密钥的时间(以秒为单位)。限制在最多1073741824(2^30)秒。任何大于1073741824秒的值将被设置为1073741824秒。

注意:
在钱包已经解锁时发出walletpassphrase命令将设置一个新的解锁时间并覆盖旧解锁时间。

示例:

~]# bitcoin-cli walletpassphrase "my pass phrase" 60        #解锁钱包60秒
~]# bitcoin-cli walletlock          #再次锁定钱包
~]# curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "walletpassphrase", "params": ["my pass phrase", 60] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/           #JSON格式请求
  1. 导出私钥

dumpprivkey "address"

显示与“address”对应的私钥。importprivkey可以使用这个输出。

注意:
使用本命令前,必须先解锁钱包。

参数:

  1. "address":字符串,必须项。比特币的私钥地址。

输出结果:

  1. "key":字符串,私钥。

示例:

~]# bitcoin-cli dumpprivkey "myaddress"         #导出地址为myaddress钱包的私钥
~]# bitcoin-cli importprivkey "mykey"           #导入值为mykey的私钥
~]# curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "dumpprivkey", "params": ["myaddress"] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/         #JSON格式请求
  1. 导入私钥

importprivkey "privkey" ( "label" ) ( rescan )

将私钥(dumpprivkey返回)添加到钱包中。需要一个新的钱包备份。
提示:使用importmulti导入多个私钥。

参数:

  1. "privkey":字符串,必须项。私钥(dumpprivkey的输出)
  2. "label":字符串,可选项,默认为空。可选标签。
  3. rescan:布尔值,可选项,默认为true。重新扫描钱包的交易。

注意:
如果rescan为真,则此调用可能需要一个多小时才能完成,在此期间,其他rpc调用可能报告导入的密钥存在,但相关事务仍然缺失,导致临时错误/虚假余额和未使用的输出,直到rescan完成。

示例:

~]# bitcoin-cli dumpprivkey "myaddress"     #导出地址为myaddress钱包的私钥
~]# bitcoin-cli importprivkey "mykey"           #导入值为mykey的私钥并重新扫描该钱包的交易
~]# bitcoin-cli importprivkey "mykey" "testing" false       #导入值为mykey的私钥,标签为testing,不扫描。
~]# bitcoin-cli importprivkey "mykey" "" false      #导入值为mykey的私钥,标签为空,不扫描。
~]# curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "importprivkey", "params": ["mykey", "testing", false] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/
            #JSON格式请求
  1. 签名

signrawtransaction "hexstring" ( [{"txid":"id","vout":n,"scriptPubKey":"hex","redeemScript":"hex"},...] ["privatekey1",...] sighashtype )

调用使用钱包中的私钥对指定的序列化交易字符串进行签名。

参数:

  1. "hexstring":字符串,必须项。事务十六进制字符串
  2. "prevtxs":字符串,可选项。以前相关事务输出的json数组。
    [ (json array of json objects, or 'null' if none provided)
    {
    "txid":"id", (字符串,必须项) 事务ID
    "vout":n, (数字,必须项) 输出数量
    "scriptPubKey": "hex", (字符串,必须项) script key
    "redeemScript": "hex", (字符串,必须项,值为P2SH或P2WSH) redeem script
    "amount": value (numeric, required) The amount spent
    }
    ,...
    ]
  3. "privkeys":字符串,可选。一个由base58编码的专用密钥组成的json数组,用于签名
    [ (json array of strings, or 'null' if none provided)
    "privatekey" (字符串) 基于base58编码的私钥
    ,...
    ]
  4. "sighashtype":字符串,可选,默认为ALL。散列HASH的类型必定为"ALL"、"NONE"、"SINGLE"、"ALL|ANYONECANPAY"、"NONE|ANYONECANPAY"、"SINGLE|ANYONECANPAY"中的一种。

输出:

{
  "hex" : "value",           (string) The hex-encoded raw transaction with signature(s)
  "complete" : true|false,   (boolean) If the transaction has a complete set of signatures
  "errors" : [                 (json array of objects) Script verification errors (if there are any)
    {
      "txid" : "hash",           (string) The hash of the referenced, previous transaction
      "vout" : n,                (numeric) The index of the output to spent and used as input
      "scriptSig" : "hex",       (string) The hex-encoded signature script
      "sequence" : n,            (numeric) Script sequence number
      "error" : "text"           (string) Verification or signing error related to the input
    }
    ,...
  ]
}

示例:

~]# bitcoin-cli signrawtransaction "myhex"
~]# curl --user myusername --data-binary '{"jsonrpc": "1.0", "id":"curltest", "method": "signrawtransaction", "params": ["myhex"] }' -H 'content-type: text/plain;' http://127.0.0.1:8332/
发布了161 篇原创文章 · 获赞 69 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/baidu_38432732/article/details/104658944