Class: Tapyrus::Store::ChainEntry
- Inherits:
-
Object
- Object
- Tapyrus::Store::ChainEntry
- Includes:
- HexConverter
- Defined in:
- lib/tapyrus/store/chain_entry.rb
Overview
wrap a block header object with extra data.
Instance Attribute Summary collapse
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#height ⇒ Object
readonly
Returns the value of attribute height.
Class Method Summary collapse
Instance Method Summary collapse
-
#block_hash ⇒ Object
block hash.
-
#build_next_block(next_block) ⇒ Tapyrus::Store::ChainEntry
build next block
StoredBlock
instance. -
#genesis? ⇒ Boolean
whether genesis block.
- #hash ⇒ Object
-
#initialize(header, height) ⇒ ChainEntry
constructor
A new instance of ChainEntry.
-
#key ⇒ Object
get database key.
-
#prev_hash ⇒ Object
previous block hash.
-
#to_payload ⇒ Object
generate payload.
Methods included from HexConverter
Constructor Details
#initialize(header, height) ⇒ ChainEntry
Returns a new instance of ChainEntry.
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
#header ⇒ Object (readonly)
Returns the value of attribute header.
7 8 9 |
# File 'lib/tapyrus/store/chain_entry.rb', line 7 def header @header end |
#height ⇒ Object (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
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_hash ⇒ Object
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.
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
37 38 39 |
# File 'lib/tapyrus/store/chain_entry.rb', line 37 def genesis? height == 0 end |
#hash ⇒ Object
22 23 24 |
# File 'lib/tapyrus/store/chain_entry.rb', line 22 def hash header.hash end |
#key ⇒ Object
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_hash ⇒ Object
previous block hash
32 33 34 |
# File 'lib/tapyrus/store/chain_entry.rb', line 32 def prev_hash header.prev_hash end |
#to_payload ⇒ Object
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 |