Class: Bitcoin::Message::BlockTransactionRequest

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

Overview

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(block_hash, indexes) ⇒ BlockTransactionRequest

Returns a new instance of BlockTransactionRequest.



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

def initialize(block_hash, indexes)
  @block_hash = block_hash
  @indexes = indexes
end

Instance Attribute Details

#block_hashObject

When matching with Bitcoin::BlockHeader#hash It is necessary to reverse the byte order.



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

def block_hash
  @block_hash
end

#indexesObject

Returns the value of attribute indexes.



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

def indexes
  @indexes
end

Class Method Details

.parse_from_payload(payload) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bitcoin/message/block_transaction_request.rb', line 16

def self.parse_from_payload(payload)
  buf = StringIO.new(payload)
  block_hash = buf.read(32).bth
  index_len = Bitcoin.unpack_var_int_from_io(buf)
  indexes = index_len.times.map{Bitcoin.unpack_var_int_from_io(buf)}
  # index data differentially encoded
  offset = 0
  index_len.times do |i|
    index = indexes[i]
    index += offset
    indexes[i] = index
    offset = index + 1
  end
  self.new(block_hash, indexes)
end

Instance Method Details

#to_payloadObject



32
33
34
35
36
37
38
39
40
# File 'lib/bitcoin/message/block_transaction_request.rb', line 32

def to_payload
  p = block_hash.htb << Bitcoin.pack_var_int(indexes.size)
  indexes.size.times do |i|
    index = indexes[i]
    index -= indexes[i-1] + 1 if i > 0
    p << Bitcoin.pack_var_int(index)
  end
  p
end