Class: Bitcoin::BlockHeader

Inherits:
Object
  • Object
show all
Includes:
HexConverter
Defined in:
lib/bitcoin/block_header.rb

Overview

Block Header

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HexConverter

#to_hex

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

#bitsObject

Returns the value of attribute bits.



12
13
14
# File 'lib/bitcoin/block_header.rb', line 12

def bits
  @bits
end

#merkle_rootObject

Returns the value of attribute merkle_root.



10
11
12
# File 'lib/bitcoin/block_header.rb', line 10

def merkle_root
  @merkle_root
end

#nonceObject

Returns the value of attribute nonce.



13
14
15
# File 'lib/bitcoin/block_header.rb', line 13

def nonce
  @nonce
end

#prev_hashObject

Returns the value of attribute prev_hash.



9
10
11
# File 'lib/bitcoin/block_header.rb', line 9

def prev_hash
  @prev_hash
end

#timeObject

unix timestamp



11
12
13
# File 'lib/bitcoin/block_header.rb', line 11

def time
  @time
end

#versionObject

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.

Parameters:

  • payload (String|StringIO)

    block header paylaod

Returns:



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_hashObject



49
50
51
# File 'lib/bitcoin/block_header.rb', line 49

def block_hash
  calc_hash
end

#block_idObject

block hash(big endian)



54
55
56
# File 'lib/bitcoin/block_header.rb', line 54

def block_id
  block_hash.rhex
end

#difficulty_targetObject

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

#hashObject



45
46
47
# File 'lib/bitcoin/block_header.rb', line 45

def hash
  calc_hash.to_i(16)
end

#to_payloadObject



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

Returns:

  • (Boolean)


59
60
61
# File 'lib/bitcoin/block_header.rb', line 59

def valid?
  valid_pow? && valid_timestamp?
end

#valid_pow?Boolean

evaluate valid proof of work.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


70
71
72
# File 'lib/bitcoin/block_header.rb', line 70

def valid_timestamp?
  time <= Time.now.to_i + Bitcoin::MAX_FUTURE_BLOCK_TIME
end

#workInteger

compute chain work of this block.

Returns:



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