進階
區塊鏈 RPC
Bitcoin Core 區塊鏈查詢相關 RPC 命令,包括區塊、交易和 UTXO 查詢。
15 分鐘
鏈狀態查詢
getblockchaininfo
獲取區塊鏈的整體狀態信息:
bitcoin-cli getblockchaininfo
# 返回範例
{
"chain": "main",
"blocks": 825000,
"headers": 825000,
"bestblockhash": "00000000000000000002...",
"difficulty": 72006146478567.1,
"time": 1704067200,
"mediantime": 1704063600,
"verificationprogress": 0.9999998,
"initialblockdownload": false,
"chainwork": "00000000000000000000...",
"size_on_disk": 590000000000,
"pruned": false,
"warnings": ""
} 其他狀態命令
# 獲取當前區塊高度 bitcoin-cli getblockcount # 獲取最佳區塊雜湊 bitcoin-cli getbestblockhash # 獲取難度 bitcoin-cli getdifficulty # 獲取鏈上統計 bitcoin-cli getchaintxstats 144 # 最近 144 區塊
區塊查詢
getblock
# 獲取區塊(簡要信息) bitcoin-cli getblock "<blockhash>" 1 # 獲取區塊(含交易詳情) bitcoin-cli getblock "<blockhash>" 2 # 獲取區塊(原始 hex) bitcoin-cli getblock "<blockhash>" 0 # 按高度獲取 bitcoin-cli getblockhash 825000 bitcoin-cli getblock $(bitcoin-cli getblockhash 825000) 1
區塊返回結構
{
"hash": "00000000000000000002...",
"confirmations": 100,
"height": 825000,
"version": 536870912,
"versionHex": "20000000",
"merkleroot": "abc123...",
"time": 1704067200,
"mediantime": 1704063600,
"nonce": 123456789,
"bits": "1703a30c",
"difficulty": 72006146478567.1,
"chainwork": "00000000...",
"nTx": 3500,
"previousblockhash": "00000000...",
"nextblockhash": "00000000...",
"strippedsize": 999999,
"size": 1500000,
"weight": 3999996,
"tx": ["txid1", "txid2", ...] // verbosity=1
} getblockheader
# 只獲取區塊頭(更快) bitcoin-cli getblockheader "<blockhash>" # 獲取原始區塊頭 bitcoin-cli getblockheader "<blockhash>" false
交易查詢
getrawtransaction
注意:
需要啟用 txindex=1 才能查詢任意交易,否則只能查詢 mempool 和錢包相關的交易。
# 獲取原始交易(hex) bitcoin-cli getrawtransaction "<txid>" # 獲取解碼後的交易 bitcoin-cli getrawtransaction "<txid>" true # 指定區塊(不需要 txindex) bitcoin-cli getrawtransaction "<txid>" true "<blockhash>"
decoderawtransaction
# 解碼原始交易
bitcoin-cli decoderawtransaction "<hex>"
# 返回結構
{
"txid": "...",
"hash": "...", # wtxid (含 witness)
"version": 2,
"size": 250,
"vsize": 166, # virtual size
"weight": 661,
"locktime": 0,
"vin": [...], # 輸入
"vout": [...] # 輸出
} UTXO 查詢
gettxout
# 查詢特定 UTXO
bitcoin-cli gettxout "<txid>" <vout>
# 包含未確認交易
bitcoin-cli gettxout "<txid>" <vout> true
# 返回範例
{
"bestblock": "00000000...",
"confirmations": 100,
"value": 0.01234567,
"scriptPubKey": {
"asm": "0 abc123...",
"desc": "addr(bc1q...)#checksum",
"hex": "0014abc123...",
"address": "bc1q...",
"type": "witness_v0_keyhash"
},
"coinbase": false
} gettxoutsetinfo
# 獲取 UTXO 集合統計(需要時間)
bitcoin-cli gettxoutsetinfo
# 返回範例
{
"height": 825000,
"bestblock": "00000000...",
"txouts": 85000000,
"bogosize": 6500000000,
"muhash": "...",
"total_amount": 19500000.00000000,
"transactions": 50000000,
"disk_size": 8500000000
} scantxoutset
# 掃描地址的 UTXO(不需要錢包) bitcoin-cli scantxoutset "start" '["addr(bc1q...)"]' # 掃描公鑰 bitcoin-cli scantxoutset "start" '["combo(pubkey)"]' # 掃描描述符 bitcoin-cli scantxoutset "start" '["wpkh([fingerprint/84h/0h/0h]xpub.../0/*)"]' # 中止掃描 bitcoin-cli scantxoutset "abort"
腳本與驗證
# 解碼腳本 bitcoin-cli decodescript "<hex>" # 驗證交易簽名(不廣播) bitcoin-cli testmempoolaccept '["<tx_hex>"]' # 獲取區塊過濾器(BIP-157) bitcoin-cli getblockfilter "<blockhash>"
常用命令速查
| 命令 | 說明 |
|---|---|
| getblockchaininfo | 鏈狀態概覽 |
| getblockcount | 當前高度 |
| getblockhash | 高度 → 雜湊 |
| getblock | 獲取區塊 |
| getrawtransaction | 獲取交易 |
| gettxout | 查詢 UTXO |
| scantxoutset | 掃描 UTXO |
配置提示:
在 bitcoin.conf 中添加 txindex=1
可以索引所有交易,但會增加約 30GB 存儲需求。
已複製連結