Class: Bitcoin::Store::ChainEntry

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(header, height) ⇒ ChainEntry

Returns a new instance of ChainEntry.

Parameters:



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

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

Instance Attribute Details

#headerObject (readonly)

Returns the value of attribute header.



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

def header
  @header
end

#heightObject (readonly)

Returns the value of attribute height.



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

def height
  @height
end

Class Method Details

.parse_from_payload(payload) ⇒ Object

Parameters:

  • payload (String)

    a payload with binary format.



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

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

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

build next block StoredBlock instance.

Parameters:

Returns:



48
49
50
# File 'lib/bitcoin/store/chain_entry.rb', line 48

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

#genesis?Boolean

whether genesis block

Returns:

  • (Boolean)


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

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

#hashObject

block hash



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

def hash
  header.hash
end

#keyObject

get database key



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

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

#prev_hashObject

previous block hash



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

def prev_hash
  header.prev_hash
end

#to_payloadObject

generate payload



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

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