Sha256: 3fcd010cdee20ca85360cc22f27ae5335da1b9bc5143925c752174bf179fa752

Contents?: true

Size: 971 Bytes

Versions: 6

Compression:

Stored size: 971 Bytes

Contents

module Tapyrus
  class Block
    include Tapyrus::Util

    attr_accessor :header
    attr_accessor :transactions

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

    def self.parse_from_payload(payload)
      Tapyrus::Message::Block.parse_from_payload(payload).to_block
    end

    def hash
      header.hash
    end

    def block_hash
      header.block_hash
    end

    # check the merkle root in the block header matches merkle root calculated from tx list.
    def valid_merkle_root?
      calculate_merkle_root == header.merkle_root
    end

    # calculate merkle root from tx list.
    def calculate_merkle_root
      Tapyrus::MerkleTree.build_from_leaf(transactions.map(&:tx_hash)).merkle_root
    end

    # return this block height. block height is included in coinbase.
    # @return [Integer] block height.
    def height
      transactions.first.in.first.out_point.index
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
tapyrus-0.2.7 lib/tapyrus/block.rb
tapyrus-0.2.6 lib/tapyrus/block.rb
tapyrus-0.2.5 lib/tapyrus/block.rb
tapyrus-0.2.4 lib/tapyrus/block.rb
tapyrus-0.2.3 lib/tapyrus/block.rb
tapyrus-0.2.2 lib/tapyrus/block.rb