Class: Tapyrus::Message::Inventory

Inherits:
Object
  • Object
show all
Defined in:
lib/tapyrus/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.



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

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

Instance Attribute Details

#hashObject

Returns the value of attribute hash.



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

def hash
  @hash
end

#identifierObject

Returns the value of attribute identifier.



16
17
18
# File 'lib/tapyrus/message/inventory.rb', line 16

def identifier
  @identifier
end

Class Method Details

.parse_from_payload(payload) ⇒ Object

parse inventory payload

Raises:



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

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

Instance Method Details

#block?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/tapyrus/message/inventory.rb', line 39

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

#to_payloadObject



35
36
37
# File 'lib/tapyrus/message/inventory.rb', line 35

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