class Bitcoin::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 [Bitcoin::BlockHeader] header a block header. @param [Integer] height a block height.

# File lib/bitcoin/store/chain_entry.rb, line 13
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/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

Public Instance Methods

block_hash() click to toggle source

block hash

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

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

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

whether genesis block

# File lib/bitcoin/store/chain_entry.rb, line 38
def genesis?
  Bitcoin.chain_params.genesis_block.header == header
end
hash() click to toggle source
# File lib/bitcoin/store/chain_entry.rb, line 23
def hash
  header.hash
end
key() click to toggle source

get database key

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

previous block hash

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

generate payload

# 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