Class: Avalon::Block

Inherits:
Object
  • Object
show all
Extended by:
Extractable
Defined in:
lib/avalon/block.rb

Overview

Block contains details about block found by the pool

Constant Summary collapse

FIELDS =

Field formats: name => [width, original name, type/conversion]

{
  :height => [6, "height", :i],  # not in miner status string...
  :time => [19, "time", ->(t){ my_time(t, :absolute_date)}],
  :received => [8, "received_time", ->(t){ my_time(t)}],
  :reward => [6, "fee", ->(x){ (25 + x/100_000_000.0).round(3)}],
  :txns => [4, "n_tx", :i],
  :size => [3, "size", ->(x){ x/1024 }],
  :relayed_by => [14, "relayed_by", :s],
  :chain => [5, "main_chain", ->(x){ x ? 'main' : 'orphan' }],
  :conf => [4, "confirmations", :i],
  :hash => [56, "hash", ->(x){ x.sub(/^0*/,'')}],
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Extractable

extract_data_from, my_time, print_headers

Constructor Details

#initialize(hash) ⇒ Block

Returns a new instance of Block.



71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/avalon/block.rb', line 71

def initialize hash
  case hash
  when Hash
    @data = hash
  when String
    @data = {:hash => hash}
    bitcoind_update
    blockchain_update
  else
    raise ArgumentError, "Wrong argument to Block.new: #{hash}"
  end
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/avalon/block.rb', line 8

def data
  @data
end

Class Method Details



24
25
26
# File 'lib/avalon/block.rb', line 24

def self.print_headers
  puts FIELDS.map {|name, (width,_,_ )| name.to_s.ljust(width)}.join(' ')
end

Instance Method Details

#bitcoind_updateObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/avalon/block.rb', line 28

def bitcoind_update
  # {"hash" : "0000000000000029714fcc1f7bcd43cd13286b665f759eb018cfc539841623a4",
  #    "previousblockhash" : "0000000000000004d388fd4e7bd6aa1c3f3eeae0ceadd7a0bc51ee1fee0be910",
  #    "merkleroot" : "844f841c3fc276023a561503fc21d064bd898d6c9530d34a864efe70f67bfde8",
  #    "version" : 2,
  #    "size" : 249181,
  #    "height" : 238605,
  #    "time" : 1369877473,
  #    "bits" : "1a016164",
  #    "tx" : [..]
  #    ---------
  #    "nonce" : 3370559418,
  #    "confirmations" : 0,
  #    "difficulty" : 12153411.70977583}
  bitcoind_info = Bitcoind.getblock @data[:hash], "| grep -v -E '^        .*,'"
  @data.merge! self.class.extract_data_from( bitcoind_info ) if bitcoind_info
end

#blockchain_updateObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/avalon/block.rb', line 46

def blockchain_update
  #{"hash"=>"00000000000000783be7e82df4d8a71bf1fd8073d2bbd60f2b8638e4d042d32c",
  #  "prev_block"=> "00000000000000b277cace9f2556fb8e0545038e83d20846cc4e3a3f61d0f2f2",
  #  "mrkl_root"=> "0a9a292c93ad732e55eeb25633d3e7580894faea908876f2d17589e127e904cd",
  #  "ver"=>2,
  #  "size"=>232091,
  #  "height"=>238557,
  #  "time"=>1369850827,
  #  "bits"=>436298084,
  #  ---------
  #  "fee"=>41058500,
  #  "nonce"=>825834732,
  #  "n_tx"=>504,
  #  "block_index"=>386930,
  #  "main_chain"=>true,
  #  "received_time"=>1369850888,
  #  "relayed_by"=>"37.251.86.21"}
  blockchain_info = Blockchain.rawblock @data[:hash].rjust(64, '0')
  @data.merge! self.class.extract_data_from( blockchain_info ) if blockchain_info
end

#pending?Boolean

Returns:

  • (Boolean)


67
68
69
# File 'lib/avalon/block.rb', line 67

def pending?
  @data[:reward].nil? && @data[:received].nil?
end

#to_sObject



84
85
86
# File 'lib/avalon/block.rb', line 84

def to_s
  FIELDS.map {|key, (width, _, _ )| @data[key].to_s.ljust(width)}.join(" ")
end