Class: Bitcoin::Store::ChainEntry
- Inherits:
-
Object
- Object
- Bitcoin::Store::ChainEntry
- Includes:
- HexConverter
- Defined in:
- lib/bitcoin/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) ⇒ Bitcoin::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.
13 14 15 16 |
# File 'lib/bitcoin/store/chain_entry.rb', line 13 def initialize(header, height) @header = header @height = height end |
Instance Attribute Details
#header ⇒ Object (readonly)
Returns the value of attribute header.
8 9 10 |
# File 'lib/bitcoin/store/chain_entry.rb', line 8 def header @header end |
#height ⇒ Object (readonly)
Returns the value of attribute height.
9 10 11 |
# File 'lib/bitcoin/store/chain_entry.rb', line 9 def height @height end |
Class Method Details
.parse_from_payload(payload) ⇒ Object
43 44 45 46 47 48 |
# 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 |
Instance Method Details
#block_hash ⇒ Object
block hash
28 29 30 |
# File 'lib/bitcoin/store/chain_entry.rb', line 28 def block_hash header.block_hash end |
#build_next_block(next_block) ⇒ Bitcoin::Store::ChainEntry
build next block StoredBlock
instance.
53 54 55 |
# File 'lib/bitcoin/store/chain_entry.rb', line 53 def build_next_block(next_block) ChainEntry.new(next_block, height + 1) end |
#genesis? ⇒ Boolean
whether genesis block
38 39 40 |
# File 'lib/bitcoin/store/chain_entry.rb', line 38 def genesis? Bitcoin.chain_params.genesis_block.header == header end |
#hash ⇒ Object
23 24 25 |
# File 'lib/bitcoin/store/chain_entry.rb', line 23 def hash header.hash end |
#key ⇒ Object
get database key
19 20 21 |
# File 'lib/bitcoin/store/chain_entry.rb', line 19 def key Bitcoin::Store::KEY_PREFIX[:entry] + header.block_hash end |
#prev_hash ⇒ Object
previous block hash
33 34 35 |
# File 'lib/bitcoin/store/chain_entry.rb', line 33 def prev_hash header.prev_hash end |
#to_payload ⇒ Object
generate payload
58 59 60 61 62 |
# 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 |