Class: Bitcoin::Message::CmpctBlock

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

Overview

Constant Summary collapse

COMMAND =
'cmpctblock'

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_and_short_ids) ⇒ CmpctBlock

Returns a new instance of CmpctBlock.



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

def initialize(header_and_short_ids)
  @header_and_short_ids = header_and_short_ids
end

Instance Attribute Details

#header_and_short_idsObject

Returns the value of attribute header_and_short_ids.



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

def header_and_short_ids
  @header_and_short_ids
end

Class Method Details

.from_block(block, version, nonce = SecureRandom.hex(8).to_i(16)) ⇒ Bitcoin::Message::CmpctBlock

generate CmpctBlock from Block data.

Parameters:

  • block (Bitcoin::Block)

    the block to generate CmpctBlock.

  • version (Integer)

    Compact Block version specified by sendcmpct message.

  • nonce (Integer) (defaults to: SecureRandom.hex(8).to_i(16))

Returns:



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

def self.from_block(block, version, nonce = SecureRandom.hex(8).to_i(16))
  raise 'Unsupported version.' unless [1, 2].include?(version)
  h = HeaderAndShortIDs.new(block.header, nonce)
  block.transactions[1..-1].each do |tx|
    h.short_ids << h.short_id(version == 1 ? tx.txid : tx.wtxid)
  end
  h.prefilled_txn << PrefilledTx.new(0, block.transactions.first)
  self.new(h)
end

.parse_from_payload(payload) ⇒ Object



31
32
33
# File 'lib/bitcoin/message/cmpct_block.rb', line 31

def self.parse_from_payload(payload)
  self.new(HeaderAndShortIDs.parse_from_payload(payload))
end

Instance Method Details

#to_payloadObject



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

def to_payload
  header_and_short_ids.to_payload
end