Class: Bitcoin::Message::MerkleBlock

Inherits:
Base show all
Defined in:
lib/bitcoin/message/merkle_block.rb

Overview

Constant Summary collapse

COMMAND =
'merkleblock'

Constants included from Util

Util::DIGEST_NAME_SHA256

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#to_pkt

Methods included from Util

#byte_to_bit, #calc_checksum, #decode_base58_address, #double_sha256, #encode_base58_address, #hash160, #hmac_sha256, #pack_boolean, #pack_var_int, #pack_var_string, #sha256, #unpack_boolean, #unpack_var_int, #unpack_var_int_from_io, #unpack_var_string

Constructor Details

#initializeMerkleBlock

Returns a new instance of MerkleBlock.



15
16
17
# File 'lib/bitcoin/message/merkle_block.rb', line 15

def initialize
  @hashes = []
end

Instance Attribute Details

#flagsObject

Returns the value of attribute flags.



13
14
15
# File 'lib/bitcoin/message/merkle_block.rb', line 13

def flags
  @flags
end

#hashesObject

Returns the value of attribute hashes.



12
13
14
# File 'lib/bitcoin/message/merkle_block.rb', line 12

def hashes
  @hashes
end

#headerObject

Returns the value of attribute header.



10
11
12
# File 'lib/bitcoin/message/merkle_block.rb', line 10

def header
  @header
end

#tx_countObject

Returns the value of attribute tx_count.



11
12
13
# File 'lib/bitcoin/message/merkle_block.rb', line 11

def tx_count
  @tx_count
end

Class Method Details

.parse_from_payload(payload) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bitcoin/message/merkle_block.rb', line 19

def self.parse_from_payload(payload)
  m = new
  buf = StringIO.new(payload)
  m.header = Bitcoin::BlockHeader.parse_from_payload(buf.read(80))
  m.tx_count = buf.read(4).unpack('V').first
  hash_count = Bitcoin.unpack_var_int_from_io(buf)
  hash_count.times do
    m.hashes << buf.read(32).bth
  end
  flag_count = Bitcoin.unpack_var_int_from_io(buf)
  # A sequence of bits packed eight in a byte with the least significant bit first.
  m.flags = buf.read(flag_count).bth
  m
end

Instance Method Details

#to_payloadObject



34
35
36
37
# File 'lib/bitcoin/message/merkle_block.rb', line 34

def to_payload
  header.to_payload << [tx_count].pack('V') << Bitcoin.pack_var_int(hashes.size) <<
      hashes.map(&:htb).join << Bitcoin.pack_var_int(flags.htb.bytesize) << flags.htb
end