Class: Tapyrus::Message::Block

Inherits:
Base
  • Object
show all
Defined in:
lib/tapyrus/message/block.rb

Overview

Constant Summary collapse

COMMAND =
"block"

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, #padding_zero, #sha256, #unpack_boolean, #unpack_var_int, #unpack_var_int_from_io, #unpack_var_string, #valid_address?

Methods included from HexConverter

#to_hex

Constructor Details

#initialize(header, transactions = []) ⇒ Block

Returns a new instance of Block.



12
13
14
15
16
# File 'lib/tapyrus/message/block.rb', line 12

def initialize(header, transactions = [])
  @header = header
  @transactions = transactions
  @use_segwit = false
end

Instance Attribute Details

#headerObject

Returns the value of attribute header.



6
7
8
# File 'lib/tapyrus/message/block.rb', line 6

def header
  @header
end

#transactionsObject

Returns the value of attribute transactions.



7
8
9
# File 'lib/tapyrus/message/block.rb', line 7

def transactions
  @transactions
end

#use_segwitObject

Returns the value of attribute use_segwit.



8
9
10
# File 'lib/tapyrus/message/block.rb', line 8

def use_segwit
  @use_segwit
end

Class Method Details

.parse_from_payload(payload) ⇒ Object



18
19
20
21
22
23
24
25
26
27
# File 'lib/tapyrus/message/block.rb', line 18

def self.parse_from_payload(payload)
  buf = StringIO.new(payload)
  header = Tapyrus::BlockHeader.parse_from_payload(buf)
  transactions = []
  unless buf.eof?
    tx_count = Tapyrus.unpack_var_int_from_io(buf)
    tx_count.times { transactions << Tapyrus::Tx.parse_from_payload(buf) }
  end
  new(header, transactions)
end

Instance Method Details

#to_blockObject

generate Tapyrus::Block object.



34
35
36
# File 'lib/tapyrus/message/block.rb', line 34

def to_block
  Tapyrus::Block.new(header, transactions)
end

#to_payloadObject



29
30
31
# File 'lib/tapyrus/message/block.rb', line 29

def to_payload
  header.to_payload << Tapyrus.pack_var_int(transactions.size) << transactions.map(&:to_payload).join
end