Sha256: 48817cbdc22952ad1b598973b48586fb19dbb133c3dc5c6948a35a67e0a79e60

Contents?: true

Size: 1.04 KB

Versions: 10

Compression:

Stored size: 1.04 KB

Contents

module Tapyrus
  module Message
    # block message
    # https://bitcoin.org/en/developer-reference#block
    class Block < Base
      attr_accessor :header
      attr_accessor :transactions
      attr_accessor :use_segwit

      COMMAND = 'block'

      def initialize(header, transactions = [])
        @header = header
        @transactions = transactions
        @use_segwit = false
      end

      def self.parse_from_payload(payload)
        buf = StringIO.new(payload)
        header = Tapyrus::BlockHeader.parse_from_payload(buf)
        transactions = []
        unless buf.eof?
          tx_count = Tapyrus.unpack_var_int_from_io(buf)
          tx_count.times { transactions << Tapyrus::Tx.parse_from_payload(buf) }
        end
        new(header, transactions)
      end

      def to_payload
        header.to_payload << Tapyrus.pack_var_int(transactions.size) << transactions.map(&:to_payload).join
      end

      # generate Tapyrus::Block object.
      def to_block
        Tapyrus::Block.new(header, transactions)
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

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