Sha256: 5e3a0336b5174c2008456a757eb40a668480c14311b0c053744d8172b47e0ae4

Contents?: true

Size: 1.12 KB

Versions: 14

Compression:

Stored size: 1.12 KB

Contents

module Bitcoin
  module Message

    # merckleblock message
    # https://bitcoin.org/en/developer-reference#merkleblock
    class MerkleBlock < Base

      COMMAND = 'merkleblock'

      attr_accessor :header
      attr_accessor :tx_count
      attr_accessor :hashes
      attr_accessor :flags

      def initialize
        @hashes = []
      end

      def self.parse_from_payload(payload)
        m = new
        buf = StringIO.new(payload)
        m.header = Bitcoin::BlockHeader.parse_from_payload(buf.read(80))
        m.tx_count = buf.read(4).unpack1('V')
        hash_count = Bitcoin.unpack_var_int_from_io(buf)
        hash_count.times do
          m.hashes << buf.read(32).bth
        end
        flag_count = Bitcoin.unpack_var_int_from_io(buf)
        # A sequence of bits packed eight in a byte with the least significant bit first.
        m.flags = buf.read(flag_count).bth
        m
      end

      def to_payload
        header.to_payload << [tx_count].pack('V') << Bitcoin.pack_var_int(hashes.size) <<
            hashes.map(&:htb).join << Bitcoin.pack_var_int(flags.htb.bytesize) << flags.htb
      end

    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
bitcoinrb-1.7.0 lib/bitcoin/message/merkle_block.rb
bitcoinrb-1.6.0 lib/bitcoin/message/merkle_block.rb
bitcoinrb-1.5.0 lib/bitcoin/message/merkle_block.rb
bitcoinrb-1.4.0 lib/bitcoin/message/merkle_block.rb
bitcoinrb-1.3.0 lib/bitcoin/message/merkle_block.rb
bitcoinrb-1.2.1 lib/bitcoin/message/merkle_block.rb
bitcoinrb-1.2.0 lib/bitcoin/message/merkle_block.rb
bitcoinrb-1.1.1 lib/bitcoin/message/merkle_block.rb
bitcoinrb-1.1.0 lib/bitcoin/message/merkle_block.rb
bitcoinrb-1.0.0 lib/bitcoin/message/merkle_block.rb
bitcoinrb-0.9.0 lib/bitcoin/message/merkle_block.rb
bitcoinrb-0.8.0 lib/bitcoin/message/merkle_block.rb
bitcoinrb-0.7.0 lib/bitcoin/message/merkle_block.rb
bitcoinrb-0.6.0 lib/bitcoin/message/merkle_block.rb