class Tapyrus::Store::ChainEntry

wrap a block header object with extra data.

Attributes

header[R]
height[R]

Public Class Methods

new(header, height) click to toggle source

@param [Tapyrus::BlockHeader] header a block header. @param [Integer] height a block height.

# File lib/tapyrus/store/chain_entry.rb, line 12
def initialize(header, height)
  @header = header
  @height = height
end
parse_from_payload(payload) click to toggle source

@param [String] payload a payload with binary format.

# 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

Public Instance Methods

block_hash() click to toggle source

block hash

# File lib/tapyrus/store/chain_entry.rb, line 27
def block_hash
  header.block_hash
end
build_next_block(next_block) click to toggle source

build next block StoredBlock instance. @param [Tapyrus::BlockHeader] next_block a next block candidate header. @return [Tapyrus::Store::ChainEntry] a next stored block (not saved).

# File lib/tapyrus/store/chain_entry.rb, line 52
def build_next_block(next_block)
  ChainEntry.new(next_block, height + 1)
end
genesis?() click to toggle source

whether genesis block

# File lib/tapyrus/store/chain_entry.rb, line 37
def genesis?
  height == 0
end
hash() click to toggle source
# File lib/tapyrus/store/chain_entry.rb, line 22
def hash
  header.hash
end
key() click to toggle source

get database key

# File lib/tapyrus/store/chain_entry.rb, line 18
def key
  Tapyrus::Store::KEY_PREFIX[:entry] + header.block_hash
end
prev_hash() click to toggle source

previous block hash

# File lib/tapyrus/store/chain_entry.rb, line 32
def prev_hash
  header.prev_hash
end
to_payload() click to toggle source

generate payload

# 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