Class: Bitcoin::BlockHeader
- Inherits:
-
Object
- Object
- Bitcoin::BlockHeader
- Includes:
- HexConverter
- Defined in:
- lib/bitcoin/block_header.rb
Overview
Block Header
Instance Attribute Summary collapse
-
#bits ⇒ Object
Returns the value of attribute bits.
-
#merkle_root ⇒ Object
Returns the value of attribute merkle_root.
-
#nonce ⇒ Object
Returns the value of attribute nonce.
-
#prev_hash ⇒ Object
Returns the value of attribute prev_hash.
-
#time ⇒ Object
unix timestamp.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
-
.parse_from_payload(payload) ⇒ Bitcoin::BlockHeader
Parse block header from payload.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #block_hash ⇒ Object
-
#block_id ⇒ Object
block hash(big endian).
-
#difficulty_target ⇒ Object
compute difficulty target from bits.
- #hash ⇒ Object
-
#initialize(version, prev_hash, merkle_root, time, bits, nonce) ⇒ BlockHeader
constructor
A new instance of BlockHeader.
- #to_payload ⇒ Object
-
#valid? ⇒ Boolean
evaluate block header.
-
#valid_pow? ⇒ Boolean
evaluate valid proof of work.
-
#valid_timestamp? ⇒ Boolean
evaluate valid timestamp.
-
#work ⇒ Integer
compute chain work of this block.
Methods included from HexConverter
Constructor Details
#initialize(version, prev_hash, merkle_root, time, bits, nonce) ⇒ BlockHeader
Returns a new instance of BlockHeader.
15 16 17 18 19 20 21 22 |
# File 'lib/bitcoin/block_header.rb', line 15 def initialize(version, prev_hash, merkle_root, time, bits, nonce) @version = version @prev_hash = prev_hash @merkle_root = merkle_root @time = time @bits = bits @nonce = nonce end |
Instance Attribute Details
#bits ⇒ Object
Returns the value of attribute bits.
12 13 14 |
# File 'lib/bitcoin/block_header.rb', line 12 def bits @bits end |
#merkle_root ⇒ Object
Returns the value of attribute merkle_root.
10 11 12 |
# File 'lib/bitcoin/block_header.rb', line 10 def merkle_root @merkle_root end |
#nonce ⇒ Object
Returns the value of attribute nonce.
13 14 15 |
# File 'lib/bitcoin/block_header.rb', line 13 def nonce @nonce end |
#prev_hash ⇒ Object
Returns the value of attribute prev_hash.
9 10 11 |
# File 'lib/bitcoin/block_header.rb', line 9 def prev_hash @prev_hash end |
#time ⇒ Object
unix timestamp
11 12 13 |
# File 'lib/bitcoin/block_header.rb', line 11 def time @time end |
#version ⇒ Object
Returns the value of attribute version.
8 9 10 |
# File 'lib/bitcoin/block_header.rb', line 8 def version @version end |
Class Method Details
.parse_from_payload(payload) ⇒ Bitcoin::BlockHeader
Parse block header from payload.
27 28 29 30 31 |
# File 'lib/bitcoin/block_header.rb', line 27 def self.parse_from_payload(payload) buf = payload.is_a?(String) ? StringIO.new(payload) : payload version, prev_hash, merkle_root, time, bits, nonce = buf.read(80).unpack('Va32a32VVV') new(version, prev_hash.bth, merkle_root.bth, time, bits, nonce) end |
Instance Method Details
#==(other) ⇒ Object
82 83 84 |
# File 'lib/bitcoin/block_header.rb', line 82 def ==(other) other && other.to_payload == to_payload end |
#block_hash ⇒ Object
49 50 51 |
# File 'lib/bitcoin/block_header.rb', line 49 def block_hash calc_hash end |
#block_id ⇒ Object
block hash(big endian)
54 55 56 |
# File 'lib/bitcoin/block_header.rb', line 54 def block_id block_hash.rhex end |
#difficulty_target ⇒ Object
compute difficulty target from bits.
38 39 40 41 42 43 |
# File 'lib/bitcoin/block_header.rb', line 38 def difficulty_target exponent = ((bits >> 24) & 0xff) mantissa = bits & 0x7fffff mantissa *= -1 if (bits & 0x800000) > 0 (mantissa * 2 ** (8 * (exponent - 3))) end |
#hash ⇒ Object
45 46 47 |
# File 'lib/bitcoin/block_header.rb', line 45 def hash calc_hash.to_i(16) end |
#to_payload ⇒ Object
33 34 35 |
# File 'lib/bitcoin/block_header.rb', line 33 def to_payload [version, prev_hash.htb, merkle_root.htb, time, bits, nonce].pack('Va32a32VVV') end |
#valid? ⇒ Boolean
evaluate block header
59 60 61 |
# File 'lib/bitcoin/block_header.rb', line 59 def valid? valid_pow? && end |
#valid_pow? ⇒ Boolean
evaluate valid proof of work.
64 65 66 |
# File 'lib/bitcoin/block_header.rb', line 64 def valid_pow? block_id.hex < difficulty_target end |
#valid_timestamp? ⇒ Boolean
evaluate valid timestamp. en.bitcoin.it/wiki/Block_timestamp
70 71 72 |
# File 'lib/bitcoin/block_header.rb', line 70 def time <= Time.now.to_i + Bitcoin::MAX_FUTURE_BLOCK_TIME end |
#work ⇒ Integer
compute chain work of this block.
76 77 78 79 80 |
# File 'lib/bitcoin/block_header.rb', line 76 def work target = difficulty_target return 0 if target < 1 115792089237316195423570985008687907853269984665640564039457584007913129639936.div(target + 1) # 115792089237316195423570985008687907853269984665640564039457584007913129639936 is 2**256 end |