Class: Bitcoin::Message::Block

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

Overview

Constant Summary collapse

COMMAND =
'block'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

from_pkt, #to_pkt

Methods included from Util

#byte_to_bit, #calc_checksum, #decode_base58_address, #double_sha256, #encode_base58_address, #hash160, #hkdf_sha256, #hmac_sha256, #pack_boolean, #pack_var_int, #pack_var_string, #padding_zero, #sha256, #tagged_hash, #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 = [], use_segwit = false) ⇒ Block

Returns a new instance of Block.



14
15
16
17
18
# File 'lib/bitcoin/message/block.rb', line 14

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

Instance Attribute Details

#headerObject

Returns the value of attribute header.



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

def header
  @header
end

#transactionsObject

Returns the value of attribute transactions.



9
10
11
# File 'lib/bitcoin/message/block.rb', line 9

def transactions
  @transactions
end

#use_segwitObject

Returns the value of attribute use_segwit.



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

def use_segwit
  @use_segwit
end

Class Method Details

.parse_from_payload(payload) ⇒ Object



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

def self.parse_from_payload(payload)
  buf = StringIO.new(payload)
  header = Bitcoin::BlockHeader.parse_from_payload(buf.read(80))
  transactions = []
  unless buf.eof?
    tx_count = Bitcoin.unpack_var_int_from_io(buf)
    tx_count.times do
      transactions << Bitcoin::Tx.parse_from_payload(buf)
    end
  end
  new(header, transactions)
end

Instance Method Details

#to_blockObject

generate Bitcoin::Block object.



39
40
41
# File 'lib/bitcoin/message/block.rb', line 39

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

#to_payloadObject



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

def to_payload
  header.to_payload << Bitcoin.pack_var_int(transactions.size) <<
    transactions.map{|t|use_segwit ? t.to_payload : t.serialize_old_format}.join
end