Sha256: c5b835215de832fdc25340abc78b0bf4f1eb8c2efc3e71087eb779cef99b4045

Contents?: true

Size: 1.25 KB

Versions: 32

Compression:

Stored size: 1.25 KB

Contents

module Bitcoin
  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 Bitcoin::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 = 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

      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

    end

  end
end

Version data entries

32 entries across 32 versions & 1 rubygems

Version Path
bitcoinrb-1.7.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-1.6.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-1.5.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-1.4.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-1.3.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-1.2.1 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-1.2.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-1.1.1 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-1.1.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-1.0.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-0.9.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-0.8.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-0.7.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-0.6.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-0.5.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-0.4.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-0.3.2 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-0.3.1 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-0.3.0 lib/bitcoin/message/block_transaction_request.rb
bitcoinrb-0.2.9 lib/bitcoin/message/block_transaction_request.rb