Class: Bitcoin::Store::ChainEntry

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

Overview

wrap a block header object with extra data.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HexConverter

#to_hex

Constructor Details

#initialize(header, height) ⇒ ChainEntry

Returns a new instance of ChainEntry.

Parameters:



13
14
15
16
# File 'lib/bitcoin/store/chain_entry.rb', line 13

def initialize(header, height)
  @header = header
  @height = height
end

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



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

def header
  @header
end

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

Class Method Details

.parse_from_payload(payload) ⇒ Object

Parameters:

  • payload (String)

    a payload with binary format.



43
44
45
46
47
48
# File 'lib/bitcoin/store/chain_entry.rb', line 43

def self.parse_from_payload(payload)
  buf = StringIO.new(payload)
  len = Bitcoin.unpack_var_int_from_io(buf)
  height = buf.read(len).reverse.bth.to_i(16)
  new(Bitcoin::BlockHeader.parse_from_payload(buf.read(80)), height)
end

Instance Method Details

#block_hashObject

block hash



28
29
30
# File 'lib/bitcoin/store/chain_entry.rb', line 28

def block_hash
  header.block_hash
end

#build_next_block(next_block) ⇒ Bitcoin::Store::ChainEntry

build next block StoredBlock instance.

Parameters:

Returns:



53
54
55
# File 'lib/bitcoin/store/chain_entry.rb', line 53

def build_next_block(next_block)
  ChainEntry.new(next_block, height + 1)
end

#genesis?Boolean

whether genesis block

Returns:

  • (Boolean)


38
39
40
# File 'lib/bitcoin/store/chain_entry.rb', line 38

def genesis?
  Bitcoin.chain_params.genesis_block.header == header
end

#hashObject



23
24
25
# File 'lib/bitcoin/store/chain_entry.rb', line 23

def hash
  header.hash
end

#keyObject

get database key



19
20
21
# File 'lib/bitcoin/store/chain_entry.rb', line 19

def key
  Bitcoin::Store::KEY_PREFIX[:entry] + header.block_hash
end

#prev_hashObject

previous block hash



33
34
35
# File 'lib/bitcoin/store/chain_entry.rb', line 33

def prev_hash
  header.prev_hash
end

#to_payloadObject

generate payload



58
59
60
61
62
# File 'lib/bitcoin/store/chain_entry.rb', line 58

def to_payload
  height_value = height.to_even_length_hex
  height_value = height_value.htb.reverse
  Bitcoin.pack_var_int(height_value.bytesize) + height_value + header.to_payload
end