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_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.
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
76 77 78 |
# File 'lib/bitcoin/block_header.rb', line 76 def ==(other) other && other.to_payload == to_payload end |
#block_hash ⇒ Object
43 44 45 |
# File 'lib/bitcoin/block_header.rb', line 43 def block_hash calc_hash end |
#block_id ⇒ Object
block hash(big endian)
48 49 50 |
# File 'lib/bitcoin/block_header.rb', line 48 def block_id block_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
39 40 41 |
# File 'lib/bitcoin/block_header.rb', line 39 def hash calc_hash.to_i(16) 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
53 54 55 |
# File 'lib/bitcoin/block_header.rb', line 53 def valid? valid_pow? && end |
#valid_pow? ⇒ Boolean
evaluate valid proof of work.
58 59 60 |
# File 'lib/bitcoin/block_header.rb', line 58 def valid_pow? block_id.hex < difficulty_target end |
#valid_timestamp? ⇒ Boolean
evaluate valid timestamp. en.bitcoin.it/wiki/Block_timestamp
64 65 66 |
# File 'lib/bitcoin/block_header.rb', line 64 def time <= Time.now.to_i + Bitcoin::MAX_FUTURE_BLOCK_TIME end |
#work ⇒ Integer
compute chain work of this block.
70 71 72 73 74 |
# File 'lib/bitcoin/block_header.rb', line 70 def work target = difficulty_target return 0 if target < 1 115792089237316195423570985008687907853269984665640564039457584007913129639936.div(target + 1) # 115792089237316195423570985008687907853269984665640564039457584007913129639936 is 2**256 end |