【发布时间】:2018-09-05 12:56:29
【问题描述】:
我在 Ubuntu 16.04 中运行 bitcoind 服务器。同样使用bitcoin-ruby连接bitcoind的RPC的方法:
require 'bitcoin'
require 'net/http'
require 'json'
RPCUSER = "**"
RPCPASSWORD = "**"
HOST = "localhost"
PORT= 8332
def bitcoinRPC(method,param)
http = Net::HTTP.new(HOST,PORT)
request = Net::HTTP::Post.new('/')
request.basic_auth(RPCUSER,RPCPASSWORD)
request.content_type = 'application/json'
request.body = {method:method,params:param,id:'jsonrpc'}.to_json
JSON.parse(http.request(request).body)["result"]
结束
以下 RPC 命令显示块号 514641 的解析数据:
bhash= 514641
bid= bitcoinRPC('getblockhash',[bhash])
bid="0000000000000000003b34a5f6cb571435b71449c38e54bf2cbafb7ca3800501"
blk= bitcoinRPC("getblock",[bid])
而blk变量里面的key如下:
blk.keys
["hash", "confirmations", "strippedsize", "size", "weight", "height",
"version", "versionHex", "merkleroot", "tx", "time", "mediantime", "nonce",
"bits", "difficulty", "chainwork", "previousblockhash", "nextblockhash"]
我想从块号 514641 中解析 "hash" 、 "tx" 、 "time" 、 "difficulty" 的键值,使用 ruby 编程计算回块号 1 并将输出解析为文本文件制表符分隔的以下格式:
hash tx time difficulty
000... 12X.... 2344556 5455345
-- 13X... -- 5678899
-- 14X... -- 6454545
在这里,“hash”和“time”对于同一个块将是相同的值。我是红宝石编程的新手。任何指南都将受到高度赞赏。
提前致谢。
【问题讨论】:
标签: ruby bitcoin json-rpc bitcoind