Class: Tapyrus::Store::ChainEntry

Inherits:
Object
  • Object
show all
Includes:
HexConverter
Defined in:
lib/tapyrus/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:



12
13
14
15
# File 'lib/tapyrus/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/tapyrus/store/chain_entry.rb', line 7

def header
  @header
end

#heightObject (readonly)

Returns the value of attribute height.



8
9
10
# File 'lib/tapyrus/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.



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

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

Instance Method Details

#block_hashObject

block hash



27
28
29
# File 'lib/tapyrus/store/chain_entry.rb', line 27

def block_hash
  header.block_hash
end

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

build next block StoredBlock instance.

Parameters:

Returns:



52
53
54
# File 'lib/tapyrus/store/chain_entry.rb', line 52

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

#genesis?Boolean

whether genesis block

Returns:

  • (Boolean)


37
38
39
# File 'lib/tapyrus/store/chain_entry.rb', line 37

def genesis?
  height == 0
end

#hashObject



22
23
24
# File 'lib/tapyrus/store/chain_entry.rb', line 22

def hash
  header.hash
end

#keyObject

get database key



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

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

#prev_hashObject

previous block hash



32
33
34
# File 'lib/tapyrus/store/chain_entry.rb', line 32

def prev_hash
  header.prev_hash
end

#to_payloadObject

generate payload



57
58
59
60
61
# File 'lib/tapyrus/store/chain_entry.rb', line 57

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