進階
網路 RPC
Bitcoin Core 網路和 Mempool 相關 RPC 命令,包括節點管理和手續費估算。
12 分鐘
網路狀態
getnetworkinfo
bitcoin-cli getnetworkinfo
# 返回範例
{
"version": 260000,
"subversion": "/Satoshi:26.0.0/",
"protocolversion": 70016,
"localservices": "0000000000000c09",
"localservicesnames": ["NETWORK", "WITNESS", "NETWORK_LIMITED", "P2P_V2"],
"localrelay": true,
"timeoffset": 0,
"networkactive": true,
"connections": 125,
"connections_in": 115,
"connections_out": 10,
"networks": [...],
"relayfee": 0.00001000,
"incrementalfee": 0.00001000,
"localaddresses": [...],
"warnings": ""
} 連線計數
# 獲取連線數量 bitcoin-cli getconnectioncount # 檢查網路是否啟用 bitcoin-cli getnetworkinfo | jq '.networkactive'
節點管理
getpeerinfo
# 列出所有連接的節點
bitcoin-cli getpeerinfo
# 每個節點的資訊包括
{
"id": 1,
"addr": "192.168.1.100:8333",
"addrbind": "192.168.1.1:45678",
"network": "ipv4",
"services": "0000000000000c09",
"servicesnames": ["NETWORK", "WITNESS", ...],
"relaytxes": true,
"lastsend": 1704067200,
"lastrecv": 1704067199,
"bytessent": 123456789,
"bytesrecv": 987654321,
"conntime": 1704000000,
"pingtime": 0.05,
"version": 70016,
"subver": "/Satoshi:26.0.0/",
"inbound": false,
"connection_type": "outbound-full-relay",
"transport_protocol_type": "v2",
"startingheight": 825000,
"synced_headers": 825100,
"synced_blocks": 825100
} 節點連線控制
# 手動添加節點 bitcoin-cli addnode "192.168.1.100:8333" "add" bitcoin-cli addnode "192.168.1.100:8333" "remove" bitcoin-cli addnode "192.168.1.100:8333" "onetry" # 列出手動添加的節點 bitcoin-cli getaddednodeinfo # 斷開特定節點 bitcoin-cli disconnectnode "192.168.1.100:8333" # 禁止節點 bitcoin-cli setban "192.168.1.100" "add" 86400 # 禁止 24 小時 bitcoin-cli listbanned bitcoin-cli clearbanned
網路開關
# 停用/啟用網路 bitcoin-cli setnetworkactive false bitcoin-cli setnetworkactive true
Mempool 管理
getmempoolinfo
bitcoin-cli getmempoolinfo
# 返回範例
{
"loaded": true,
"size": 5000, # 交易數量
"bytes": 2500000, # 總大小
"usage": 15000000, # 記憶體使用
"total_fee": 0.12345678, # 總手續費
"maxmempool": 300000000, # 最大容量
"mempoolminfee": 0.00001000, # 最低進入費率
"minrelaytxfee": 0.00001000, # 最低中繼費率
"incrementalrelayfee": 0.00001000,
"unbroadcastcount": 0,
"fullrbf": true
} 查詢 Mempool
# 列出所有 mempool 交易 ID bitcoin-cli getrawmempool # 包含詳細資訊 bitcoin-cli getrawmempool true # 按祖先/後代排序 bitcoin-cli getrawmempool false "sequence" # 查詢特定交易 bitcoin-cli getmempoolentry "<txid>" # 查詢交易的祖先 bitcoin-cli getmempoolancestors "<txid>" # 查詢交易的後代 bitcoin-cli getmempooldescendants "<txid>"
廣播與驗證
# 廣播原始交易
bitcoin-cli sendrawtransaction "<hex>"
# 測試交易是否會被接受(不廣播)
bitcoin-cli testmempoolaccept '["<hex>"]'
# 返回範例
[{
"txid": "...",
"wtxid": "...",
"allowed": true,
"vsize": 141,
"fees": {
"base": 0.00001410
}
}] 手續費估算
estimatesmartfee
# 估算 N 區塊內確認的費率
bitcoin-cli estimatesmartfee 1 # 下一區塊
bitcoin-cli estimatesmartfee 6 # 約 1 小時
bitcoin-cli estimatesmartfee 144 # 約 1 天
# 返回範例
{
"feerate": 0.00012345, # BTC/kvB
"blocks": 6
}
# 指定估算模式
bitcoin-cli estimatesmartfee 6 "economical"
bitcoin-cli estimatesmartfee 6 "conservative" economical 模式
較低的費率估算,適合不急著確認的交易。
conservative 模式
較高的費率估算,確保快速確認。
常用命令速查
| 命令 | 說明 |
|---|---|
| getnetworkinfo | 網路狀態概覽 |
| getpeerinfo | 連接的節點列表 |
| getmempoolinfo | Mempool 狀態 |
| getrawmempool | Mempool 交易列表 |
| estimatesmartfee | 手續費估算 |
| sendrawtransaction | 廣播交易 |
相關文檔: 了解底層的 P2P 協議 可以幫助更好地理解網路行為。
已複製連結