Class: Bitcoin::BlockHeader
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
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#block_id ⇒ Object
block hash(big endian).
-
#difficulty_target ⇒ Object
compute difficulty target from bits.
-
#hash ⇒ Object
block hash(little endian).
-
#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.
Constructor Details
#initialize(version, prev_hash, merkle_root, time, bits, nonce) ⇒ BlockHeader
Returns a new instance of BlockHeader.
13 14 15 16 17 18 19 20 |
# File 'lib/bitcoin/block_header.rb', line 13 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.
10 11 12 |
# File 'lib/bitcoin/block_header.rb', line 10 def bits @bits end |
#merkle_root ⇒ Object
Returns the value of attribute merkle_root.
8 9 10 |
# File 'lib/bitcoin/block_header.rb', line 8 def merkle_root @merkle_root end |
#nonce ⇒ Object
Returns the value of attribute nonce.
11 12 13 |
# File 'lib/bitcoin/block_header.rb', line 11 def nonce @nonce end |
#prev_hash ⇒ Object
Returns the value of attribute prev_hash.
7 8 9 |
# File 'lib/bitcoin/block_header.rb', line 7 def prev_hash @prev_hash end |
#time ⇒ Object
unix timestamp
9 10 11 |
# File 'lib/bitcoin/block_header.rb', line 9 def time @time end |
#version ⇒ Object
Returns the value of attribute version.
6 7 8 |
# File 'lib/bitcoin/block_header.rb', line 6 def version @version end |
Class Method Details
.parse_from_payload(payload) ⇒ Object
22 23 24 25 |
# File 'lib/bitcoin/block_header.rb', line 22 def self.parse_from_payload(payload) version, prev_hash, merkle_root, time, bits, nonce = payload.unpack('Va32a32VVV') new(version, prev_hash.bth, merkle_root.bth, time, bits, nonce) end |
Instance Method Details
#==(other) ⇒ Object
73 74 75 |
# File 'lib/bitcoin/block_header.rb', line 73 def ==(other) other && other.to_payload == to_payload end |
#block_id ⇒ Object
block hash(big endian)
45 46 47 |
# File 'lib/bitcoin/block_header.rb', line 45 def block_id hash.rhex end |
#difficulty_target ⇒ Object
compute difficulty target from bits.
32 33 34 35 36 37 |
# File 'lib/bitcoin/block_header.rb', line 32 def difficulty_target exponent = ((bits >> 24) & 0xff) mantissa = bits & 0x7fffff mantissa *= -1 if (bits & 0x800000) > 0 (mantissa * 2 ** (8 * (exponent - 3))) end |
#hash ⇒ Object
block hash(little endian)
40 41 42 |
# File 'lib/bitcoin/block_header.rb', line 40 def hash calc_hash end |
#to_payload ⇒ Object
27 28 29 |
# File 'lib/bitcoin/block_header.rb', line 27 def to_payload [version, prev_hash.htb, merkle_root.htb, time, bits, nonce].pack('Va32a32VVV') end |
#valid? ⇒ Boolean
evaluate block header
50 51 52 |
# File 'lib/bitcoin/block_header.rb', line 50 def valid? valid_pow? && end |
#valid_pow? ⇒ Boolean
evaluate valid proof of work.
55 56 57 |
# File 'lib/bitcoin/block_header.rb', line 55 def valid_pow? block_id.hex < difficulty_target end |
#valid_timestamp? ⇒ Boolean
evaluate valid timestamp. en.bitcoin.it/wiki/Block_timestamp
61 62 63 |
# File 'lib/bitcoin/block_header.rb', line 61 def time <= Time.now.to_i + Bitcoin::MAX_FUTURE_BLOCK_TIME end |
#work ⇒ Integer
compute chain work of this block.
67 68 69 70 71 |
# File 'lib/bitcoin/block_header.rb', line 67 def work target = difficulty_target return 0 if target < 1 (2**256) / (target + 1) end |