Class: Bitcoin::Message::Inventory

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

Overview

inventory class. inventory is a part of message. bitcoin.org/en/developer-reference#term-inventory

Constant Summary collapse

SEGWIT_FLAG =
1 << 30
MSG_TX =
1
MSG_BLOCK =
2
MSG_FILTERED_BLOCK =
3
MSG_CMPCT_BLOCK =
4
MSG_WITNESS_TX =
SEGWIT_FLAG | MSG_TX
MSG_WITNESS_BLOCK =
SEGWIT_FLAG | MSG_BLOCK
MSG_FILTERED_WITNESS_BLOCK =
SEGWIT_FLAG | MSG_FILTERED_BLOCK

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(identifier, hash) ⇒ Inventory

Returns a new instance of Inventory.

Raises:



20
21
22
23
24
# File 'lib/bitcoin/message/inventory.rb', line 20

def initialize(identifier, hash)
  raise Error, "invalid type identifier specified. identifier = #{identifier}" unless valid_identifier?(identifier)
  @identifier = identifier
  @hash = hash
end

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



18
19
20
# File 'lib/bitcoin/message/inventory.rb', line 18

def hash
  @hash
end

#identifierObject

Returns the value of attribute identifier.



17
18
19
# File 'lib/bitcoin/message/inventory.rb', line 17

def identifier
  @identifier
end

Class Method Details

.parse_from_payload(payload) ⇒ Object

parse inventory payload

Raises:



27
28
29
30
31
32
# File 'lib/bitcoin/message/inventory.rb', line 27

def self.parse_from_payload(payload)
  raise Error, 'invalid inventory size.' if payload.bytesize != 36
  identifier = payload[0..4].unpack1('V')
  hash = payload[4..-1].bth # internal byte order
  new(identifier, hash)
end

Instance Method Details

#block?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/bitcoin/message/inventory.rb', line 38

def block?
  [MSG_BLOCK, MSG_WITNESS_BLOCK, MSG_FILTERED_WITNESS_BLOCK].include?(identifier)
end

#to_payloadObject



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

def to_payload
  [identifier].pack('V') << hash.htb
end