简体   繁体   English

如何使用bitcoin-ruby接口将制式的比特币JSON-RPC的输出导出到制表符分隔格式的文本文件中

[英]How to export the output of bitcoind JSON-RPC calls using bitcoin-ruby interface to a text file in tab-delimited format

I am running the bitcoind server in Ubuntu 16.04. 我正在Ubuntu 16.04中运行比特币服务器。 Also using a method of connecting to bitcoind's RPC using bitcoin-ruby: 还使用一种通过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"]

end 结束

The following RPC commands shows the parsed data of block number 514641 : 以下RPC命令显示了已解析的块号514641的数据:

bhash= 514641
bid= bitcoinRPC('getblockhash',[bhash])
bid="0000000000000000003b34a5f6cb571435b71449c38e54bf2cbafb7ca3800501"
blk= bitcoinRPC("getblock",[bid])

And the keys inside the blk variables are as follows: blk变量中的键如下:

blk.keys
["hash", "confirmations", "strippedsize", "size", "weight", "height", 
"version", "versionHex", "merkleroot", "tx", "time", "mediantime", "nonce", 
"bits", "difficulty", "chainwork", "previousblockhash", "nextblockhash"]

I want to parse the key values of "hash" , "tx", "time", "difficulty" from inside the block number 514641 calculating back to block number 1 using ruby programming and parse the output to a text file in tab-delimited following format: 我想使用ruby编程从块号514641内计算“哈希”,“ tx”,“时间”,“难度”的键值并计算回块号1,然后将输出解析为制表符分隔的文本文件格式如下:

hash     tx      time     difficulty
000...  12X....  2344556   5455345
 --     13X...      --     5678899
 --     14X...      --     6454545

Here, the "hash" and "time" will be same values for the same block. 在这里,“哈希”和“时间”将是同一块的相同值。 I am new to ruby programming. 我是红宝石编程的新手。 Any guideline will be highly appreciated. 任何指导方针将不胜感激。

Thanks in advance. 提前致谢。

I assume your blk object is just a ruby hash at this point so you should be able to just do: 我认为此时您的blk对象只是一个红宝石哈希,因此您应该能够做到:

keys = %w[hash tx time difficulty]  # array of strings (keys you want)
data = keys.map{|key| blk[key]} # array of data from the keys

require 'csv'
CSV.open("myfile.csv", "w") do |csv|
  csv << keys # keys will be header row of csv
  data.each{|d| csv << d} # loop over data and push into new row of csv
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM