Class: Bitcoin::BlockHeader

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

Overview

Block Header

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#bitsObject

Returns the value of attribute bits.



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

def bits
  @bits
end

#merkle_rootObject

Returns the value of attribute merkle_root.



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

def merkle_root
  @merkle_root
end

#nonceObject

Returns the value of attribute nonce.



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

def nonce
  @nonce
end

#prev_hashObject

Returns the value of attribute prev_hash.



7
8
9
# File 'lib/bitcoin/block_header.rb', line 7

def prev_hash
  @prev_hash
end

#timeObject

unix timestamp



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

def time
  @time
end

#versionObject

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_idObject

block hash(big endian)



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

def block_id
  hash.rhex
end

#difficulty_targetObject

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

#hashObject

block hash(little endian)



40
41
42
# File 'lib/bitcoin/block_header.rb', line 40

def hash
  calc_hash
end

#to_payloadObject



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

Returns:

  • (Boolean)


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

def valid?
  valid_pow? && valid_timestamp?
end

#valid_pow?Boolean

evaluate valid proof of work.

Returns:

  • (Boolean)


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

Returns:

  • (Boolean)


61
62
63
# File 'lib/bitcoin/block_header.rb', line 61

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

#workInteger

compute chain work of this block.

Returns:



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