Sha256: 7c860221bcd53753bbd1e3115369a9aac528740b35e5a9d7e8a687a22a2c6375

Contents?: true

Size: 1.25 KB

Versions: 11

Compression:

Stored size: 1.25 KB

Contents

module Tapyrus
  module Message
    # BIP-152 Compact Block's data format.
    # https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki#BlockTransactionsRequest
    class BlockTransactionRequest
      attr_accessor :block_hash # When matching with Tapyrus::BlockHeader#hash It is necessary to reverse the byte order.
      attr_accessor :indexes

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

      def self.parse_from_payload(payload)
        buf = StringIO.new(payload)
        block_hash = buf.read(32).bth
        index_len = Tapyrus.unpack_var_int_from_io(buf)
        indexes = index_len.times.map { Tapyrus.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

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

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
tapyrus-0.3.5 lib/tapyrus/message/block_transaction_request.rb
tapyrus-0.3.4 lib/tapyrus/message/block_transaction_request.rb
tapyrus-0.3.3 lib/tapyrus/message/block_transaction_request.rb
tapyrus-0.3.2 lib/tapyrus/message/block_transaction_request.rb
tapyrus-0.3.1 lib/tapyrus/message/block_transaction_request.rb
tapyrus-0.3.0 lib/tapyrus/message/block_transaction_request.rb
tapyrus-0.2.13 lib/tapyrus/message/block_transaction_request.rb
tapyrus-0.2.12 lib/tapyrus/message/block_transaction_request.rb
tapyrus-0.2.10 lib/tapyrus/message/block_transaction_request.rb
tapyrus-0.2.9 lib/tapyrus/message/block_transaction_request.rb
tapyrus-0.2.8 lib/tapyrus/message/block_transaction_request.rb